forked from ClearFoundry/ClearScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathV8ScriptEngine.InitScript.js
More file actions
144 lines (116 loc) · 4.07 KB
/
V8ScriptEngine.InitScript.js
File metadata and controls
144 lines (116 loc) · 4.07 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
// 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 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),
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);
}),
isPromise: bind(value => value instanceof savedPromise),
isHostObject: bind(isHostObject),
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;
}),
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
});
})(this) });