-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathV8ScriptEngine.InitScript.cs
More file actions
221 lines (174 loc) · 7.9 KB
/
V8ScriptEngine.InitScript.cs
File metadata and controls
221 lines (174 loc) · 7.9 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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
namespace Microsoft.ClearScript.V8
{
public sealed partial class V8ScriptEngine
{
private const string initScript = @"
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
Object.defineProperty(this, 'EngineInternal', { value: (globalObject => {
const bind = value => value.bind();
function construct() {
return new this(...arguments);
}
const isHostObjectKey = globalObject.isHostObjectKey;
delete globalObject.isHostObjectKey;
const isHostObject = value => !!value && (value[isHostObjectKey] === true);
const moduleResultKey = globalObject.moduleResultKey;
delete globalObject.moduleResultKey;
const getPromiseState = globalObject.getPromiseState;
delete globalObject.getPromiseState;
const getPromiseResult = globalObject.getPromiseResult;
delete globalObject.getPromiseResult;
const savedPromise = Promise;
const savedJSON = JSON;
const checkpointSymbol = Symbol();
const toJson = globalObject.toJson;
delete globalObject.toJson;
return Object.freeze({
commandHolder: {},
getCommandResult: bind(value => {
if ((value === null) || (value === undefined)) {
return value;
}
if (typeof(value.hasOwnProperty) !== 'function') {
if (value[Symbol.toStringTag] === 'Module') {
return '[module]';
}
return '[external]';
}
if (value[isHostObjectKey] === true) {
return value;
}
if (typeof(value.toString) !== 'function') {
return '[' + typeof(value) + ']';
}
return value.toString();
}),
strictEquals: bind((left, right) => left === right),
isPromise: bind(value => value instanceof savedPromise),
isHostObject: bind(isHostObject),
invokeConstructor: bind((constructor, args) => {
if (typeof(constructor) !== 'function') {
throw new Error('Function expected');
}
return construct.apply(constructor, Array.from(args));
}),
invokeMethod: bind((target, method, args) => {
if (typeof(method) !== 'function') {
throw new Error('Function expected');
}
return method.apply(target, Array.from(args));
}),
createPromise: bind(function () {
return new savedPromise(...arguments);
}),
createSettledPromiseWithResult: bind(getResult => {
try {
return savedPromise.resolve(getResult());
}
catch (exception) {
return savedPromise.reject(exception);
}
}),
createSettledPromise: bind(wait => {
try {
wait();
return savedPromise.resolve();
}
catch (exception) {
return savedPromise.reject(exception);
}
}),
completePromiseWithResult: bind((getResult, resolve, reject) => {
try {
resolve(getResult());
}
catch (exception) {
reject(exception);
}
return undefined;
}),
completePromise: bind((wait, resolve, reject) => {
try {
wait();
resolve();
}
catch (exception) {
reject(exception);
}
return undefined;
}),
getPromiseState: bind(getPromiseState),
getPromiseResult: bind(getPromiseResult),
initializeTask: bind((promise, isPending, isRejected, onResolved, onRejected) => {
if (isPending) {
promise.then(onResolved, onRejected);
}
else if (isRejected) {
onRejected(getPromiseResult(promise));
}
else {
onResolved(getPromiseResult(promise));
}
return undefined;
}),
createArray: bind(() => []),
throwValue: bind(value => {
throw value;
}),
getStackTrace: bind(() => {
try {
throw new Error('[stack trace]');
}
catch (exception) {
return exception.stack;
}
}),
toIterator: bind(function* (enumerator) {
try {
while (enumerator.ScriptableMoveNext()) {
yield enumerator.ScriptableCurrent;
}
}
finally {
enumerator.ScriptableDispose();
}
}),
toAsyncIterator: bind(async function* (asyncEnumerator) {
try {
while (await asyncEnumerator.ScriptableMoveNextAsync()) {
yield asyncEnumerator.ScriptableCurrent;
}
}
finally {
await asyncEnumerator.ScriptableDisposeAsync();
}
}),
getIterator: bind(obj => obj?.[Symbol.iterator]?.()),
getAsyncIterator: bind(obj => obj?.[Symbol.asyncIterator]?.()),
checkpoint: bind(() => {
const value = globalObject[checkpointSymbol];
if (value) {
throw value;
}
}),
toJson: bind((key, value) => toJson ? savedJSON.parse(toJson(key, value)) : value),
parseJson: bind(json => savedJSON.parse(json)),
asyncGenerator: (async function* () {})().constructor,
getModuleResult: bind(async (result, metaHolder) => {
await result;
return metaHolder[0]?.[moduleResultKey];
}),
dispose: bind(obj => {
using x = obj;
}),
disposeAsync: bind(async obj => {
await using x = obj;
})
});
})(this) });
";
}
}