-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
112 lines (95 loc) · 3.43 KB
/
Copy pathMainWindow.xaml.cs
File metadata and controls
112 lines (95 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using Microsoft.ClearScript;
using Microsoft.ClearScript.JavaScript;
using Microsoft.ClearScript.V8;
using System.Diagnostics;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Linq;
namespace TestClearScript
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private V8ScriptEngine? engine;
private ScriptController scriptController;
public MainWindow()
{
InitializeComponent();
DataContext = scriptController = new ScriptController();
SetupScriptEngine();
StartScriptEngine();
}
private void SetupScriptEngine()
{
engine = new V8ScriptEngine(
V8ScriptEngineFlags.EnableDebugging |
V8ScriptEngineFlags.EnableRemoteDebugging |
V8ScriptEngineFlags.EnableDynamicModuleImports |
V8ScriptEngineFlags.EnableTaskPromiseConversion |
V8ScriptEngineFlags.AddPerformanceObject |
V8ScriptEngineFlags.SetTimerResolution,
9224 /* the debug port */
);
engine.DocumentSettings.AccessFlags = DocumentAccessFlags.EnableAllLoading | DocumentAccessFlags.AllowCategoryMismatch;
// this way in the script we can invoke testCL.WriteLog(...)
engine.AddHostObject("testCL", HostItemFlags.GlobalMembers, scriptController);
engine.Script.Global = engine.Script;
}
private void StopScriptEngine()
{
if (engine != null)
{
engine.Interrupt();
engine.CollectGarbage(true);
engine.Dispose();
engine = null;
}
}
private void StartScriptEngine()
{
// clear cache
DocumentLoader.Default.DiscardCachedDocuments();
// clear the log
scriptController.ClearLog();
// stop engine in case it's running
if (engine != null)
StopScriptEngine();
SetupScriptEngine();
// dump the script on file, and execute
System.IO.File.WriteAllText("TestClearScript.js", scriptCode.Text);
engine?.ExecuteDocument("TestClearScript.js", ModuleCategory.CommonJS);
}
private void OnStart(object sender, RoutedEventArgs e)
{
StartScriptEngine();
}
/* Stop ClearScript V8 engine */
private void OnStop(object sender, RoutedEventArgs e)
{
StopScriptEngine();
}
/* invoke the method DoSomething with three arguments */
private void OnDoSomething(object sender, RoutedEventArgs e)
{
try
{
engine?.Invoke("DoSomething", 1, 2, 3);
}
catch (Exception ex)
{
Debug.WriteLine("Error in DoSomething " + ex.Message);
}
}
}
}