V8引擎的样例。来源于ClearScript的github上的例子。

 

以下例子足以满足大部分需求。

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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using System;
using Microsoft.ClearScript;
using Microsoft.ClearScript.JavaScript;
 
using Microsoft.ClearScript.V8;
 
 
// create a script engine
 
using (var engine = new V8ScriptEngine())
 
{
 
    // expose a host type
 
    engine.AddHostType("Console", typeof(Console));
 
    engine.Execute("Console.WriteLine('{0} is an interesting number.', Math.PI)");
 
  
 
    // expose a host object
 
    engine.AddHostObject("random", new Random());
 
    engine.Execute("Console.WriteLine(random.NextDouble())");
 
  
 
    // expose entire assemblies
 
    engine.AddHostObject("lib", new HostTypeCollection("mscorlib", "System.Core"));
 
    engine.Execute("Console.WriteLine(lib.System.DateTime.Now)");
 
  
 
    // create a host object from script
 
    engine.Execute(@"
 
        birthday = new lib.System.DateTime(2007, 5, 22);
 
        Console.WriteLine(birthday.ToLongDateString());
 
    ");
 
  
 
    // use a generic class from script
 
    engine.Execute(@"
 
        Dictionary = lib.System.Collections.Generic.Dictionary;
 
        dict = new Dictionary(lib.System.String, lib.System.Int32);
 
        dict.Add('foo', 123);
 
    ");
 
  
 
    // call a host method with an output parameter
 
    engine.AddHostObject("host", new HostFunctions());
 
    engine.Execute(@"
 
        intVar = host.newVar(lib.System.Int32);
 
        found = dict.TryGetValue('foo', intVar.out);
 
        Console.WriteLine('{0} {1}', found, intVar);
 
    ");
 
  
 
    // create and populate a host array
 
    engine.Execute(@"
 
        numbers = host.newArr(lib.System.Int32, 20);
 
        for (var i = 0; i < numbers.Length; i++) { numbers[i] = i; }
 
        Console.WriteLine(lib.System.String.Join(', ', numbers));
 
    ");
 
  
 
    // create a script delegate
 
    engine.Execute(@"
 
        Filter = lib.System.Func(lib.System.Int32, lib.System.Boolean);
 
        oddFilter = new Filter(function(value) {
 
            return (value & 1) ? true : false;
 
        });
 
    ");
 
  
 
    // use LINQ from script
 
    engine.Execute(@"
 
        oddNumbers = numbers.Where(oddFilter);
 
        Console.WriteLine(lib.System.String.Join(', ', oddNumbers));
 
    ");
 
  
 
    // use a dynamic host object
 
    engine.Execute(@"
 
        expando = new lib.System.Dynamic.ExpandoObject();
 
        expando.foo = 123;
 
        expando.bar = 'qux';
 
        delete expando.foo;
 
    ");
 
  
 
    // call a script function
 
    engine.Execute("function print(x) { Console.WriteLine(x); }");
 
    engine.Script.print(DateTime.Now.DayOfWeek);
 
  
 
    // examine a script object
 
    engine.Execute("person = { name: 'Fred', age: 5 }");
 
    Console.WriteLine(engine.Script.person.name);
 
  
 
    // read a JavaScript typed array
 
    engine.Execute("values = new Int32Array([1, 2, 3, 4, 5])");
 
    var values = (ITypedArray<int>)engine.Script.values;
 
    Console.WriteLine(string.Join(", ", values.ToArray()));
 
}