@builder.io/sdk-solid 0.4.5 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/package.json +1 -1
- package/src/blocks/BaseText.jsx +1 -1
- package/src/blocks/button/button.jsx +5 -3
- package/src/blocks/button/component-info.js +16 -22
- package/src/blocks/columns/columns.jsx +12 -21
- package/src/blocks/columns/component-info.js +203 -226
- package/src/blocks/custom-code/component-info.js +19 -25
- package/src/blocks/embed/component-info.js +31 -37
- package/src/blocks/embed/helpers.js +3 -9
- package/src/blocks/form/component-info.js +174 -212
- package/src/blocks/form/form.jsx +1 -268
- package/src/blocks/fragment/component-info.js +1 -3
- package/src/blocks/helpers.js +27 -0
- package/src/blocks/image/component-info.js +105 -133
- package/src/blocks/image/image.helpers.js +3 -5
- package/src/blocks/img/component-info.js +8 -12
- package/src/blocks/img/img.jsx +2 -0
- package/src/blocks/input/component-info.js +29 -57
- package/src/blocks/input/input.jsx +2 -0
- package/src/blocks/raw-text/component-info.js +7 -11
- package/src/blocks/raw-text/raw-text.jsx +2 -2
- package/src/blocks/section/component-info.js +24 -31
- package/src/blocks/section/section.jsx +3 -0
- package/src/blocks/select/component-info.js +34 -48
- package/src/blocks/select/select.jsx +2 -0
- package/src/blocks/submit-button/component-info.js +6 -10
- package/src/blocks/submit-button/submit-button.jsx +3 -1
- package/src/blocks/symbol/component-info.js +30 -37
- package/src/blocks/symbol/symbol.helpers.js +60 -0
- package/src/blocks/symbol/symbol.jsx +31 -68
- package/src/blocks/text/component-info.js +10 -13
- package/src/blocks/text/text.jsx +1 -1
- package/src/blocks/textarea/component-info.js +22 -30
- package/src/blocks/textarea/textarea.jsx +3 -0
- package/src/blocks/video/component-info.js +74 -96
- package/src/blocks/video/video.jsx +1 -0
- package/src/components/{render-block/render-block.helpers.js → block/block.helpers.js} +26 -44
- package/src/components/{render-block/render-block.jsx → block/block.jsx} +65 -80
- package/src/components/{render-block → block/components}/block-styles.jsx +16 -16
- package/src/components/block/components/block-wrapper.jsx +50 -0
- package/src/components/block/components/component-ref/component-ref.helpers.js +41 -0
- package/src/components/block/components/component-ref/component-ref.jsx +58 -0
- package/src/components/block/components/interactive-element.jsx +30 -0
- package/src/components/block/components/repeated-block.jsx +20 -0
- package/src/components/blocks/blocks-wrapper.jsx +66 -0
- package/src/components/blocks/blocks.jsx +57 -0
- package/src/components/{render-content/render-content.jsx → content/components/enable-editor.jsx} +86 -175
- package/src/components/{render-content/components/render-styles.helpers.js → content/components/styles.helpers.js} +6 -7
- package/src/components/{render-content/components/render-styles.jsx → content/components/styles.jsx} +4 -4
- package/src/components/{render-content/render-content.helpers.js → content/content.helpers.js} +15 -15
- package/src/components/content/content.jsx +136 -0
- package/src/components/content/index.js +2 -0
- package/src/components/content/wrap-component-ref.js +2 -0
- package/src/components/{render-content-variants/render-content-variants.jsx → content-variants/content-variants.jsx} +19 -22
- package/src/components/{render-content-variants → content-variants}/helpers.js +28 -40
- package/src/constants/builder-registered-components.js +34 -25
- package/src/constants/device-sizes.js +6 -6
- package/src/constants/sdk-version.js +1 -1
- package/src/context/builder.context.js +1 -1
- package/src/context/components.context.js +5 -0
- package/src/functions/apply-patch-with-mutation.js +66 -0
- package/src/functions/camel-to-kebab-case.js +2 -4
- package/src/functions/evaluate/acorn.js +1595 -0
- package/src/functions/{evaluate.js → evaluate/evaluate.js} +36 -10
- package/src/functions/evaluate/index.js +2 -0
- package/src/functions/evaluate/interpreter.js +2801 -0
- package/src/functions/evaluate/non-node-runtime.js +92 -0
- package/src/functions/evaluate/types.js +0 -0
- package/src/functions/event-handler-name.js +2 -4
- package/src/functions/extract-text-styles.js +4 -12
- package/src/functions/fast-clone.js +2 -4
- package/src/functions/get-block-actions-handler.js +3 -5
- package/src/functions/get-block-actions.js +15 -4
- package/src/functions/get-block-component-options.js +11 -12
- package/src/functions/get-block-properties.js +29 -19
- package/src/functions/get-builder-search-params/index.js +5 -10
- package/src/functions/get-content/generate-content-url.js +17 -19
- package/src/functions/get-content/index.js +43 -29
- package/src/functions/get-fetch.js +1 -3
- package/src/functions/get-global-this.js +1 -3
- package/src/functions/get-processed-block.js +12 -13
- package/src/functions/get-react-native-block-styles.js +11 -12
- package/src/functions/if-target.js +1 -3
- package/src/functions/is-browser.js +1 -3
- package/src/functions/is-editing.js +1 -3
- package/src/functions/is-iframe.js +1 -3
- package/src/functions/is-non-node-server.js +9 -0
- package/src/functions/is-previewing.js +1 -3
- package/src/functions/on-change.js +1 -4
- package/src/functions/register-component.js +34 -42
- package/src/functions/register.js +1 -3
- package/src/functions/sanitize-react-native-block-styles.js +22 -17
- package/src/functions/set-editor-settings.js +1 -3
- package/src/functions/set.js +1 -3
- package/src/functions/track/helpers.js +3 -5
- package/src/functions/track/index.js +45 -43
- package/src/functions/track/interaction.js +11 -7
- package/src/functions/transform-block-properties.js +1 -3
- package/src/functions/transform-block.js +1 -3
- package/src/helpers/ab-tests.js +45 -28
- package/src/helpers/canTrack.js +3 -5
- package/src/helpers/cookie.js +15 -24
- package/src/helpers/css.js +3 -7
- package/src/helpers/flatten.js +15 -18
- package/src/helpers/localStorage.js +1 -4
- package/src/helpers/logger.js +1 -3
- package/src/helpers/nullable.js +2 -4
- package/src/helpers/preview-lru-cache/get.js +8 -0
- package/src/helpers/preview-lru-cache/helpers.js +10 -0
- package/src/helpers/preview-lru-cache/init.js +10 -0
- package/src/helpers/preview-lru-cache/set.js +35 -0
- package/src/helpers/preview-lru-cache/types.js +0 -0
- package/src/helpers/sessionId.js +14 -11
- package/src/helpers/time.js +1 -3
- package/src/helpers/url.js +2 -4
- package/src/helpers/uuid.js +4 -6
- package/src/helpers/visitorId.js +8 -7
- package/src/index-helpers/blocks-exports.js +3 -14
- package/src/index-helpers/top-of-file.js +1 -3
- package/src/index.js +2 -17
- package/src/scripts/init-editing.js +62 -48
- package/src/types/api-version.js +1 -3
- package/src/types/builder-props.js +0 -0
- package/src/blocks/util.js +0 -8
- package/src/components/render-block/render-component.jsx +0 -40
- package/src/components/render-block/render-repeated-block.jsx +0 -16
- package/src/components/render-blocks.jsx +0 -100
- package/src/components/render-content/builder-editing.jsx +0 -5
- package/src/components/render-content/index.js +0 -4
- package/src/components/render-content/wrap-component-ref.js +0 -4
- package/src/functions/evaluate.test.js +0 -17
- package/src/functions/get-builder-search-params/fn.test.js +0 -13
- package/src/functions/get-content/generate-content-url.test.js +0 -97
- package/src/functions/get-processed-block.test.js +0 -34
- package/src/functions/on-change.test.js +0 -19
- package/src/functions/set.test.js +0 -16
- package/src/helpers/url.test.js +0 -21
- /package/src/components/{render-block → block}/types.js +0 -0
- /package/src/components/{render-content/render-content.types.js → content/content.types.js} +0 -0
- /package/src/components/{render-content-variants/render-content-variants.types.js → content-variants/content-variants.types.js} +0 -0
|
@@ -0,0 +1,2801 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2013 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
var Interpreter = function (code, opt_initFunc) {
|
|
7
|
+
if (typeof code === "string") {
|
|
8
|
+
code = this.parse_(code, "code");
|
|
9
|
+
}
|
|
10
|
+
var nodeConstructor = code.constructor;
|
|
11
|
+
this.newNode = function () {
|
|
12
|
+
return new nodeConstructor({
|
|
13
|
+
options: {}
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
var ast = this.newNode();
|
|
17
|
+
for (var prop in code) {
|
|
18
|
+
ast[prop] = prop === "body" ? code[prop].slice() : code[prop];
|
|
19
|
+
}
|
|
20
|
+
this.ast = ast;
|
|
21
|
+
this.initFunc_ = opt_initFunc;
|
|
22
|
+
this.paused_ = false;
|
|
23
|
+
this.polyfills_ = [];
|
|
24
|
+
this.functionCounter_ = 0;
|
|
25
|
+
this.stepFunctions_ = Object.create(null);
|
|
26
|
+
var stepMatch = /^step([A-Z]\w*)$/;
|
|
27
|
+
var m;
|
|
28
|
+
for (var methodName in this) {
|
|
29
|
+
if (typeof this[methodName] === "function" && (m = methodName.match(stepMatch))) {
|
|
30
|
+
this.stepFunctions_[m[1]] = this[methodName].bind(this);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
this.globalScope = this.createScope(this.ast, null);
|
|
34
|
+
this.globalObject = this.globalScope.object;
|
|
35
|
+
this.ast = this.parse_(this.polyfills_.join("\n"), "polyfills");
|
|
36
|
+
this.polyfills_ = void 0;
|
|
37
|
+
Interpreter.stripLocations_(this.ast, void 0, void 0);
|
|
38
|
+
var state = new Interpreter.State(this.ast, this.globalScope);
|
|
39
|
+
state.done = false;
|
|
40
|
+
this.stateStack = [state];
|
|
41
|
+
this.run();
|
|
42
|
+
this.value = void 0;
|
|
43
|
+
this.ast = ast;
|
|
44
|
+
state = new Interpreter.State(this.ast, this.globalScope);
|
|
45
|
+
state.done = false;
|
|
46
|
+
this.stateStack.length = 0;
|
|
47
|
+
this.stateStack[0] = state;
|
|
48
|
+
};
|
|
49
|
+
Interpreter.Completion = {
|
|
50
|
+
NORMAL: 0,
|
|
51
|
+
BREAK: 1,
|
|
52
|
+
CONTINUE: 2,
|
|
53
|
+
RETURN: 3,
|
|
54
|
+
THROW: 4
|
|
55
|
+
};
|
|
56
|
+
Interpreter.PARSE_OPTIONS = {
|
|
57
|
+
locations: true,
|
|
58
|
+
ecmaVersion: 5
|
|
59
|
+
};
|
|
60
|
+
Interpreter.READONLY_DESCRIPTOR = {
|
|
61
|
+
configurable: true,
|
|
62
|
+
enumerable: true,
|
|
63
|
+
writable: false
|
|
64
|
+
};
|
|
65
|
+
Interpreter.NONENUMERABLE_DESCRIPTOR = {
|
|
66
|
+
configurable: true,
|
|
67
|
+
enumerable: false,
|
|
68
|
+
writable: true
|
|
69
|
+
};
|
|
70
|
+
Interpreter.READONLY_NONENUMERABLE_DESCRIPTOR = {
|
|
71
|
+
configurable: true,
|
|
72
|
+
enumerable: false,
|
|
73
|
+
writable: false
|
|
74
|
+
};
|
|
75
|
+
Interpreter.NONCONFIGURABLE_READONLY_NONENUMERABLE_DESCRIPTOR = {
|
|
76
|
+
configurable: false,
|
|
77
|
+
enumerable: false,
|
|
78
|
+
writable: false
|
|
79
|
+
};
|
|
80
|
+
Interpreter.VARIABLE_DESCRIPTOR = {
|
|
81
|
+
configurable: false,
|
|
82
|
+
enumerable: true,
|
|
83
|
+
writable: true
|
|
84
|
+
};
|
|
85
|
+
Interpreter.STEP_ERROR = {
|
|
86
|
+
STEP_ERROR: true
|
|
87
|
+
};
|
|
88
|
+
Interpreter.SCOPE_REFERENCE = {
|
|
89
|
+
SCOPE_REFERENCE: true
|
|
90
|
+
};
|
|
91
|
+
Interpreter.VALUE_IN_DESCRIPTOR = {
|
|
92
|
+
VALUE_IN_DESCRIPTOR: true
|
|
93
|
+
};
|
|
94
|
+
Interpreter.REGEXP_TIMEOUT = {
|
|
95
|
+
REGEXP_TIMEOUT: true
|
|
96
|
+
};
|
|
97
|
+
Interpreter.toStringCycles_ = [];
|
|
98
|
+
Interpreter.vm = null;
|
|
99
|
+
Interpreter.currentInterpreter_ = null;
|
|
100
|
+
Interpreter.nativeGlobal = typeof globalThis === "undefined" ? void 0 : globalThis;
|
|
101
|
+
Interpreter.WORKER_CODE = ["onmessage = function(e) {", "var result;", "var data = e.data;", "switch (data[0]) {", "case 'split':", "result = data[1].split(data[2], data[3]);", "break;", "case 'match':", "result = data[1].match(data[2]);", "break;", "case 'search':", "result = data[1].search(data[2]);", "break;", "case 'replace':", "result = data[1].replace(data[2], data[3]);", "break;", "case 'exec':", "var regexp = data[1];", "regexp.lastIndex = data[2];", "result = [regexp.exec(data[3]), data[1].lastIndex];", "break;", "default:", "throw Error('Unknown RegExp operation: ' + data[0]);", "}", "postMessage(result);", "close();", "};"];
|
|
102
|
+
Interpreter.legalArrayLength = function (x) {
|
|
103
|
+
var n = x >>> 0;
|
|
104
|
+
return n === Number(x) ? n : NaN;
|
|
105
|
+
};
|
|
106
|
+
Interpreter.legalArrayIndex = function (x) {
|
|
107
|
+
var n = x >>> 0;
|
|
108
|
+
return String(n) === String(x) && n !== 4294967295 ? n : NaN;
|
|
109
|
+
};
|
|
110
|
+
Interpreter.stripLocations_ = function (node, start, end) {
|
|
111
|
+
if (start) {
|
|
112
|
+
node.start = start;
|
|
113
|
+
} else {
|
|
114
|
+
delete node.start;
|
|
115
|
+
}
|
|
116
|
+
if (end) {
|
|
117
|
+
node.end = end;
|
|
118
|
+
} else {
|
|
119
|
+
delete node.end;
|
|
120
|
+
}
|
|
121
|
+
for (var name in node) {
|
|
122
|
+
if (name !== "loc" && node.hasOwnProperty(name)) {
|
|
123
|
+
var prop = node[name];
|
|
124
|
+
if (prop && typeof prop === "object") {
|
|
125
|
+
Interpreter.stripLocations_(prop, start, end);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
Interpreter.prototype["REGEXP_MODE"] = 2;
|
|
131
|
+
Interpreter.prototype["REGEXP_THREAD_TIMEOUT"] = 1e3;
|
|
132
|
+
Interpreter.prototype["POLYFILL_TIMEOUT"] = 1e3;
|
|
133
|
+
Interpreter.prototype.getterStep_ = false;
|
|
134
|
+
Interpreter.prototype.setterStep_ = false;
|
|
135
|
+
Interpreter.prototype.appendCodeNumber_ = 0;
|
|
136
|
+
Interpreter.prototype.parse_ = function (code, sourceFile) {
|
|
137
|
+
var options = {};
|
|
138
|
+
for (var name in Interpreter.PARSE_OPTIONS) {
|
|
139
|
+
options[name] = Interpreter.PARSE_OPTIONS[name];
|
|
140
|
+
}
|
|
141
|
+
options.sourceFile = sourceFile;
|
|
142
|
+
return Interpreter.nativeGlobal.acornParse(code, options);
|
|
143
|
+
};
|
|
144
|
+
Interpreter.prototype.appendCode = function (code) {
|
|
145
|
+
var state = this.stateStack[0];
|
|
146
|
+
if (!state || state.node.type !== "Program") {
|
|
147
|
+
throw Error("Expecting original AST to start with a Program node");
|
|
148
|
+
}
|
|
149
|
+
if (typeof code === "string") {
|
|
150
|
+
code = this.parse_(code, "appendCode" + this.appendCodeNumber_++);
|
|
151
|
+
}
|
|
152
|
+
if (!code || code.type !== "Program") {
|
|
153
|
+
throw Error("Expecting new AST to start with a Program node");
|
|
154
|
+
}
|
|
155
|
+
this.populateScope_(code, state.scope);
|
|
156
|
+
Array.prototype.push.apply(state.node.body, code.body);
|
|
157
|
+
state.node.body.variableCache_ = null;
|
|
158
|
+
state.done = false;
|
|
159
|
+
};
|
|
160
|
+
Interpreter.prototype.step = function () {
|
|
161
|
+
var stack = this.stateStack;
|
|
162
|
+
var endTime;
|
|
163
|
+
do {
|
|
164
|
+
var state = stack[stack.length - 1];
|
|
165
|
+
if (!state) {
|
|
166
|
+
return false;
|
|
167
|
+
}
|
|
168
|
+
var node = state.node,
|
|
169
|
+
type = node.type;
|
|
170
|
+
if (type === "Program" && state.done) {
|
|
171
|
+
return false;
|
|
172
|
+
} else if (this.paused_) {
|
|
173
|
+
return true;
|
|
174
|
+
}
|
|
175
|
+
var oldInterpreterValue = Interpreter.currentInterpreter_;
|
|
176
|
+
Interpreter.currentInterpreter_ = this;
|
|
177
|
+
try {
|
|
178
|
+
var nextState = this.stepFunctions_[type](stack, state, node);
|
|
179
|
+
} catch (e) {
|
|
180
|
+
if (e !== Interpreter.STEP_ERROR) {
|
|
181
|
+
if (this.value !== e) {
|
|
182
|
+
this.value = void 0;
|
|
183
|
+
}
|
|
184
|
+
throw e;
|
|
185
|
+
}
|
|
186
|
+
} finally {
|
|
187
|
+
Interpreter.currentInterpreter_ = oldInterpreterValue;
|
|
188
|
+
}
|
|
189
|
+
if (nextState) {
|
|
190
|
+
stack.push(nextState);
|
|
191
|
+
}
|
|
192
|
+
if (this.getterStep_) {
|
|
193
|
+
this.value = void 0;
|
|
194
|
+
throw Error("Getter not supported in this context");
|
|
195
|
+
}
|
|
196
|
+
if (this.setterStep_) {
|
|
197
|
+
this.value = void 0;
|
|
198
|
+
throw Error("Setter not supported in this context");
|
|
199
|
+
}
|
|
200
|
+
if (!endTime && !node.end) {
|
|
201
|
+
endTime = Date.now() + this["POLYFILL_TIMEOUT"];
|
|
202
|
+
}
|
|
203
|
+
} while (!node.end && endTime > Date.now());
|
|
204
|
+
return true;
|
|
205
|
+
};
|
|
206
|
+
Interpreter.prototype.run = function () {
|
|
207
|
+
while (!this.paused_ && this.step()) {}
|
|
208
|
+
return this.paused_;
|
|
209
|
+
};
|
|
210
|
+
Interpreter.prototype.initGlobal = function (globalObject) {
|
|
211
|
+
this.setProperty(globalObject, "NaN", NaN, Interpreter.NONCONFIGURABLE_READONLY_NONENUMERABLE_DESCRIPTOR);
|
|
212
|
+
this.setProperty(globalObject, "Infinity", Infinity, Interpreter.NONCONFIGURABLE_READONLY_NONENUMERABLE_DESCRIPTOR);
|
|
213
|
+
this.setProperty(globalObject, "undefined", void 0, Interpreter.NONCONFIGURABLE_READONLY_NONENUMERABLE_DESCRIPTOR);
|
|
214
|
+
this.setProperty(globalObject, "window", globalObject, Interpreter.READONLY_DESCRIPTOR);
|
|
215
|
+
this.setProperty(globalObject, "this", globalObject, Interpreter.NONCONFIGURABLE_READONLY_NONENUMERABLE_DESCRIPTOR);
|
|
216
|
+
this.setProperty(globalObject, "self", globalObject);
|
|
217
|
+
this.OBJECT_PROTO = new Interpreter.Object(null);
|
|
218
|
+
this.FUNCTION_PROTO = new Interpreter.Object(this.OBJECT_PROTO);
|
|
219
|
+
this.initFunction(globalObject);
|
|
220
|
+
this.initObject(globalObject);
|
|
221
|
+
globalObject.proto = this.OBJECT_PROTO;
|
|
222
|
+
this.setProperty(globalObject, "constructor", this.OBJECT, Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
223
|
+
this.initArray(globalObject);
|
|
224
|
+
this.initString(globalObject);
|
|
225
|
+
this.initBoolean(globalObject);
|
|
226
|
+
this.initNumber(globalObject);
|
|
227
|
+
this.initDate(globalObject);
|
|
228
|
+
this.initRegExp(globalObject);
|
|
229
|
+
this.initError(globalObject);
|
|
230
|
+
this.initMath(globalObject);
|
|
231
|
+
this.initJSON(globalObject);
|
|
232
|
+
var thisInterpreter = this;
|
|
233
|
+
var func = this.createNativeFunction(function (_x) {
|
|
234
|
+
throw EvalError("Can't happen");
|
|
235
|
+
}, false);
|
|
236
|
+
func.eval = true;
|
|
237
|
+
this.setProperty(globalObject, "eval", func, Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
238
|
+
this.setProperty(globalObject, "parseInt", this.createNativeFunction(parseInt, false), Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
239
|
+
this.setProperty(globalObject, "parseFloat", this.createNativeFunction(parseFloat, false), Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
240
|
+
this.setProperty(globalObject, "isNaN", this.createNativeFunction(isNaN, false), Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
241
|
+
this.setProperty(globalObject, "isFinite", this.createNativeFunction(isFinite, false), Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
242
|
+
var strFunctions = [[escape, "escape"], [unescape, "unescape"], [decodeURI, "decodeURI"], [decodeURIComponent, "decodeURIComponent"], [encodeURI, "encodeURI"], [encodeURIComponent, "encodeURIComponent"]];
|
|
243
|
+
for (var i = 0; i < strFunctions.length; i++) {
|
|
244
|
+
var wrapper = function (nativeFunc) {
|
|
245
|
+
return function (str) {
|
|
246
|
+
try {
|
|
247
|
+
return nativeFunc(str);
|
|
248
|
+
} catch (e) {
|
|
249
|
+
thisInterpreter.throwException(thisInterpreter.URI_ERROR, e.message);
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
}(strFunctions[i][0]);
|
|
253
|
+
this.setProperty(globalObject, strFunctions[i][1], this.createNativeFunction(wrapper, false), Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
254
|
+
}
|
|
255
|
+
this["OBJECT"] = this.OBJECT;
|
|
256
|
+
this["OBJECT_PROTO"] = this.OBJECT_PROTO;
|
|
257
|
+
this["FUNCTION"] = this.FUNCTION;
|
|
258
|
+
this["FUNCTION_PROTO"] = this.FUNCTION_PROTO;
|
|
259
|
+
this["ARRAY"] = this.ARRAY;
|
|
260
|
+
this["ARRAY_PROTO"] = this.ARRAY_PROTO;
|
|
261
|
+
this["REGEXP"] = this.REGEXP;
|
|
262
|
+
this["REGEXP_PROTO"] = this.REGEXP_PROTO;
|
|
263
|
+
this["DATE"] = this.DATE;
|
|
264
|
+
this["DATE_PROTO"] = this.DATE_PROTO;
|
|
265
|
+
if (this.initFunc_) {
|
|
266
|
+
this.initFunc_(this, globalObject);
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
Interpreter.prototype.functionCodeNumber_ = 0;
|
|
270
|
+
Interpreter.prototype.initFunction = function (globalObject) {
|
|
271
|
+
var thisInterpreter = this;
|
|
272
|
+
var wrapper;
|
|
273
|
+
var identifierRegexp = /^[A-Za-z_$][\w$]*$/;
|
|
274
|
+
wrapper = function Function2(var_args) {
|
|
275
|
+
if (arguments.length) {
|
|
276
|
+
var code = String(arguments[arguments.length - 1]);
|
|
277
|
+
} else {
|
|
278
|
+
var code = "";
|
|
279
|
+
}
|
|
280
|
+
var argsStr = Array.prototype.slice.call(arguments, 0, -1).join(",").trim();
|
|
281
|
+
if (argsStr) {
|
|
282
|
+
var args = argsStr.split(/\s*,\s*/);
|
|
283
|
+
for (var i = 0; i < args.length; i++) {
|
|
284
|
+
var name = args[i];
|
|
285
|
+
if (!identifierRegexp.test(name)) {
|
|
286
|
+
thisInterpreter.throwException(thisInterpreter.SYNTAX_ERROR, "Invalid function argument: " + name);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
argsStr = args.join(", ");
|
|
290
|
+
}
|
|
291
|
+
try {
|
|
292
|
+
var ast = thisInterpreter.parse_("(function(" + argsStr + ") {" + code + "})", "function" + thisInterpreter.functionCodeNumber_++);
|
|
293
|
+
} catch (e) {
|
|
294
|
+
thisInterpreter.throwException(thisInterpreter.SYNTAX_ERROR, "Invalid code: " + e.message);
|
|
295
|
+
}
|
|
296
|
+
if (ast.body.length !== 1) {
|
|
297
|
+
thisInterpreter.throwException(thisInterpreter.SYNTAX_ERROR, "Invalid code in function body");
|
|
298
|
+
}
|
|
299
|
+
var node = ast.body[0].expression;
|
|
300
|
+
return thisInterpreter.createFunction(node, thisInterpreter.globalScope, "anonymous");
|
|
301
|
+
};
|
|
302
|
+
this.FUNCTION = this.createNativeFunction(wrapper, true);
|
|
303
|
+
this.setProperty(globalObject, "Function", this.FUNCTION, Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
304
|
+
this.setProperty(this.FUNCTION, "prototype", this.FUNCTION_PROTO, Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
305
|
+
this.setProperty(this.FUNCTION_PROTO, "constructor", this.FUNCTION, Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
306
|
+
this.FUNCTION_PROTO.nativeFunc = function () {};
|
|
307
|
+
this.FUNCTION_PROTO.nativeFunc.id = this.functionCounter_++;
|
|
308
|
+
this.FUNCTION_PROTO.illegalConstructor = true;
|
|
309
|
+
this.setProperty(this.FUNCTION_PROTO, "length", 0, Interpreter.READONLY_NONENUMERABLE_DESCRIPTOR);
|
|
310
|
+
this.FUNCTION_PROTO.class = "Function";
|
|
311
|
+
wrapper = function apply(thisArg, args) {
|
|
312
|
+
var state = thisInterpreter.stateStack[thisInterpreter.stateStack.length - 1];
|
|
313
|
+
state.func_ = this;
|
|
314
|
+
state.funcThis_ = thisArg;
|
|
315
|
+
state.arguments_ = [];
|
|
316
|
+
if (args !== null && args !== void 0) {
|
|
317
|
+
if (args instanceof Interpreter.Object) {
|
|
318
|
+
state.arguments_ = thisInterpreter.arrayPseudoToNative(args);
|
|
319
|
+
} else {
|
|
320
|
+
thisInterpreter.throwException(thisInterpreter.TYPE_ERROR, "CreateListFromArrayLike called on non-object");
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
state.doneExec_ = false;
|
|
324
|
+
};
|
|
325
|
+
this.setNativeFunctionPrototype(this.FUNCTION, "apply", wrapper);
|
|
326
|
+
wrapper = function call(thisArg) {
|
|
327
|
+
var state = thisInterpreter.stateStack[thisInterpreter.stateStack.length - 1];
|
|
328
|
+
state.func_ = this;
|
|
329
|
+
state.funcThis_ = thisArg;
|
|
330
|
+
state.arguments_ = [];
|
|
331
|
+
for (var i = 1; i < arguments.length; i++) {
|
|
332
|
+
state.arguments_.push(arguments[i]);
|
|
333
|
+
}
|
|
334
|
+
state.doneExec_ = false;
|
|
335
|
+
};
|
|
336
|
+
this.setNativeFunctionPrototype(this.FUNCTION, "call", wrapper);
|
|
337
|
+
this.polyfills_.push("Object.defineProperty(Function.prototype, 'bind',", "{configurable: true, writable: true, value:", "function bind(oThis) {", "if (typeof this !== 'function') {", "throw TypeError('What is trying to be bound is not callable');", "}", "var aArgs = Array.prototype.slice.call(arguments, 1),", "fToBind = this,", "fNOP = function() {},", "fBound = function() {", "return fToBind.apply(this instanceof fNOP", "? this", ": oThis,", "aArgs.concat(Array.prototype.slice.call(arguments)));", "};", "if (this.prototype) {", "fNOP.prototype = this.prototype;", "}", "fBound.prototype = new fNOP();", "return fBound;", "}", "});", "");
|
|
338
|
+
wrapper = function toString() {
|
|
339
|
+
return String(this);
|
|
340
|
+
};
|
|
341
|
+
this.setNativeFunctionPrototype(this.FUNCTION, "toString", wrapper);
|
|
342
|
+
this.setProperty(this.FUNCTION, "toString", this.createNativeFunction(wrapper, false), Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
343
|
+
wrapper = function valueOf() {
|
|
344
|
+
return this.valueOf();
|
|
345
|
+
};
|
|
346
|
+
this.setNativeFunctionPrototype(this.FUNCTION, "valueOf", wrapper);
|
|
347
|
+
this.setProperty(this.FUNCTION, "valueOf", this.createNativeFunction(wrapper, false), Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
348
|
+
};
|
|
349
|
+
Interpreter.prototype.initObject = function (globalObject) {
|
|
350
|
+
var thisInterpreter = this;
|
|
351
|
+
var wrapper;
|
|
352
|
+
wrapper = function Object2(value) {
|
|
353
|
+
if (value === void 0 || value === null) {
|
|
354
|
+
if (thisInterpreter.calledWithNew()) {
|
|
355
|
+
return this;
|
|
356
|
+
} else {
|
|
357
|
+
return thisInterpreter.createObjectProto(thisInterpreter.OBJECT_PROTO);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
if (!(value instanceof Interpreter.Object)) {
|
|
361
|
+
var box = thisInterpreter.createObjectProto(thisInterpreter.getPrototype(value));
|
|
362
|
+
box.data = value;
|
|
363
|
+
return box;
|
|
364
|
+
}
|
|
365
|
+
return value;
|
|
366
|
+
};
|
|
367
|
+
this.OBJECT = this.createNativeFunction(wrapper, true);
|
|
368
|
+
this.setProperty(this.OBJECT, "prototype", this.OBJECT_PROTO, Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
369
|
+
this.setProperty(this.OBJECT_PROTO, "constructor", this.OBJECT, Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
370
|
+
this.setProperty(globalObject, "Object", this.OBJECT, Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
371
|
+
var throwIfNullUndefined = function (value) {
|
|
372
|
+
if (value === void 0 || value === null) {
|
|
373
|
+
thisInterpreter.throwException(thisInterpreter.TYPE_ERROR, "Cannot convert '" + value + "' to object");
|
|
374
|
+
}
|
|
375
|
+
};
|
|
376
|
+
wrapper = function getOwnPropertyNames(obj) {
|
|
377
|
+
throwIfNullUndefined(obj);
|
|
378
|
+
var props = obj instanceof Interpreter.Object ? obj.properties : obj;
|
|
379
|
+
return thisInterpreter.arrayNativeToPseudo(Object.getOwnPropertyNames(props));
|
|
380
|
+
};
|
|
381
|
+
this.setProperty(this.OBJECT, "getOwnPropertyNames", this.createNativeFunction(wrapper, false), Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
382
|
+
wrapper = function keys(obj) {
|
|
383
|
+
throwIfNullUndefined(obj);
|
|
384
|
+
if (obj instanceof Interpreter.Object) {
|
|
385
|
+
obj = obj.properties;
|
|
386
|
+
}
|
|
387
|
+
return thisInterpreter.arrayNativeToPseudo(Object.keys(obj));
|
|
388
|
+
};
|
|
389
|
+
this.setProperty(this.OBJECT, "keys", this.createNativeFunction(wrapper, false), Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
390
|
+
wrapper = function create_(proto) {
|
|
391
|
+
if (proto === null) {
|
|
392
|
+
return thisInterpreter.createObjectProto(null);
|
|
393
|
+
}
|
|
394
|
+
if (!(proto instanceof Interpreter.Object)) {
|
|
395
|
+
thisInterpreter.throwException(thisInterpreter.TYPE_ERROR, "Object prototype may only be an Object or null, not " + proto);
|
|
396
|
+
}
|
|
397
|
+
return thisInterpreter.createObjectProto(proto);
|
|
398
|
+
};
|
|
399
|
+
this.setProperty(this.OBJECT, "create", this.createNativeFunction(wrapper, false), Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
400
|
+
this.polyfills_.push("(function() {", "var create_ = Object.create;", "Object.create = function create(proto, props) {", "var obj = create_(proto);", "props && Object.defineProperties(obj, props);", "return obj;", "};", "})();", "");
|
|
401
|
+
wrapper = function defineProperty(obj, prop, descriptor) {
|
|
402
|
+
prop = String(prop);
|
|
403
|
+
if (!(obj instanceof Interpreter.Object)) {
|
|
404
|
+
thisInterpreter.throwException(thisInterpreter.TYPE_ERROR, "Object.defineProperty called on non-object: " + obj);
|
|
405
|
+
}
|
|
406
|
+
if (!(descriptor instanceof Interpreter.Object)) {
|
|
407
|
+
thisInterpreter.throwException(thisInterpreter.TYPE_ERROR, "Property description must be an object");
|
|
408
|
+
}
|
|
409
|
+
if (obj.preventExtensions && !(prop in obj.properties)) {
|
|
410
|
+
thisInterpreter.throwException(thisInterpreter.TYPE_ERROR, "Can't define property '" + prop + "', object is not extensible");
|
|
411
|
+
}
|
|
412
|
+
thisInterpreter.setProperty(obj, prop, Interpreter.VALUE_IN_DESCRIPTOR, descriptor.properties);
|
|
413
|
+
return obj;
|
|
414
|
+
};
|
|
415
|
+
this.setProperty(this.OBJECT, "defineProperty", this.createNativeFunction(wrapper, false), Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
416
|
+
this.polyfills_.push("(function() {", "var defineProperty_ = Object.defineProperty;", "Object.defineProperty = function defineProperty(obj, prop, d1) {", "var d2 = {};", "if ('configurable' in d1) d2.configurable = d1.configurable;", "if ('enumerable' in d1) d2.enumerable = d1.enumerable;", "if ('writable' in d1) d2.writable = d1.writable;", "if ('value' in d1) d2.value = d1.value;", "if ('get' in d1) d2.get = d1.get;", "if ('set' in d1) d2.set = d1.set;", "return defineProperty_(obj, prop, d2);", "};", "})();", "Object.defineProperty(Object, 'defineProperties',", "{configurable: true, writable: true, value:", "function defineProperties(obj, props) {", "var keys = Object.keys(props);", "for (var i = 0; i < keys.length; i++) {", "Object.defineProperty(obj, keys[i], props[keys[i]]);", "}", "return obj;", "}", "});", "");
|
|
417
|
+
wrapper = function getOwnPropertyDescriptor(obj, prop) {
|
|
418
|
+
if (!(obj instanceof Interpreter.Object)) {
|
|
419
|
+
thisInterpreter.throwException(thisInterpreter.TYPE_ERROR, "Object.getOwnPropertyDescriptor called on non-object: " + obj);
|
|
420
|
+
}
|
|
421
|
+
prop = String(prop);
|
|
422
|
+
if (!(prop in obj.properties)) {
|
|
423
|
+
return void 0;
|
|
424
|
+
}
|
|
425
|
+
var descriptor = Object.getOwnPropertyDescriptor(obj.properties, prop);
|
|
426
|
+
var getter = obj.getter[prop];
|
|
427
|
+
var setter = obj.setter[prop];
|
|
428
|
+
var pseudoDescriptor = thisInterpreter.createObjectProto(thisInterpreter.OBJECT_PROTO);
|
|
429
|
+
if (getter || setter) {
|
|
430
|
+
thisInterpreter.setProperty(pseudoDescriptor, "get", getter);
|
|
431
|
+
thisInterpreter.setProperty(pseudoDescriptor, "set", setter);
|
|
432
|
+
} else {
|
|
433
|
+
thisInterpreter.setProperty(pseudoDescriptor, "value", descriptor["value"]);
|
|
434
|
+
thisInterpreter.setProperty(pseudoDescriptor, "writable", descriptor["writable"]);
|
|
435
|
+
}
|
|
436
|
+
thisInterpreter.setProperty(pseudoDescriptor, "configurable", descriptor["configurable"]);
|
|
437
|
+
thisInterpreter.setProperty(pseudoDescriptor, "enumerable", descriptor["enumerable"]);
|
|
438
|
+
return pseudoDescriptor;
|
|
439
|
+
};
|
|
440
|
+
this.setProperty(this.OBJECT, "getOwnPropertyDescriptor", this.createNativeFunction(wrapper, false), Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
441
|
+
wrapper = function getPrototypeOf(obj) {
|
|
442
|
+
throwIfNullUndefined(obj);
|
|
443
|
+
return thisInterpreter.getPrototype(obj);
|
|
444
|
+
};
|
|
445
|
+
this.setProperty(this.OBJECT, "getPrototypeOf", this.createNativeFunction(wrapper, false), Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
446
|
+
wrapper = function isExtensible(obj) {
|
|
447
|
+
return Boolean(obj) && !obj.preventExtensions;
|
|
448
|
+
};
|
|
449
|
+
this.setProperty(this.OBJECT, "isExtensible", this.createNativeFunction(wrapper, false), Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
450
|
+
wrapper = function preventExtensions(obj) {
|
|
451
|
+
if (obj instanceof Interpreter.Object) {
|
|
452
|
+
obj.preventExtensions = true;
|
|
453
|
+
}
|
|
454
|
+
return obj;
|
|
455
|
+
};
|
|
456
|
+
this.setProperty(this.OBJECT, "preventExtensions", this.createNativeFunction(wrapper, false), Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
457
|
+
this.setNativeFunctionPrototype(this.OBJECT, "toString", Interpreter.Object.prototype.toString);
|
|
458
|
+
this.setNativeFunctionPrototype(this.OBJECT, "toLocaleString", Interpreter.Object.prototype.toString);
|
|
459
|
+
this.setNativeFunctionPrototype(this.OBJECT, "valueOf", Interpreter.Object.prototype.valueOf);
|
|
460
|
+
wrapper = function hasOwnProperty(prop) {
|
|
461
|
+
throwIfNullUndefined(this);
|
|
462
|
+
if (this instanceof Interpreter.Object) {
|
|
463
|
+
return String(prop) in this.properties;
|
|
464
|
+
}
|
|
465
|
+
return this.hasOwnProperty(prop);
|
|
466
|
+
};
|
|
467
|
+
this.setNativeFunctionPrototype(this.OBJECT, "hasOwnProperty", wrapper);
|
|
468
|
+
wrapper = function propertyIsEnumerable(prop) {
|
|
469
|
+
throwIfNullUndefined(this);
|
|
470
|
+
if (this instanceof Interpreter.Object) {
|
|
471
|
+
return Object.prototype.propertyIsEnumerable.call(this.properties, prop);
|
|
472
|
+
}
|
|
473
|
+
return this.propertyIsEnumerable(prop);
|
|
474
|
+
};
|
|
475
|
+
this.setNativeFunctionPrototype(this.OBJECT, "propertyIsEnumerable", wrapper);
|
|
476
|
+
wrapper = function isPrototypeOf(obj) {
|
|
477
|
+
while (true) {
|
|
478
|
+
obj = thisInterpreter.getPrototype(obj);
|
|
479
|
+
if (!obj) {
|
|
480
|
+
return false;
|
|
481
|
+
}
|
|
482
|
+
if (obj === this) {
|
|
483
|
+
return true;
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
};
|
|
487
|
+
this.setNativeFunctionPrototype(this.OBJECT, "isPrototypeOf", wrapper);
|
|
488
|
+
};
|
|
489
|
+
Interpreter.prototype.initArray = function (globalObject) {
|
|
490
|
+
var thisInterpreter = this;
|
|
491
|
+
var wrapper;
|
|
492
|
+
wrapper = function Array2(var_args) {
|
|
493
|
+
if (thisInterpreter.calledWithNew()) {
|
|
494
|
+
var newArray = this;
|
|
495
|
+
} else {
|
|
496
|
+
var newArray = thisInterpreter.createArray();
|
|
497
|
+
}
|
|
498
|
+
var first = arguments[0];
|
|
499
|
+
if (arguments.length === 1 && typeof first === "number") {
|
|
500
|
+
if (isNaN(Interpreter.legalArrayLength(first))) {
|
|
501
|
+
thisInterpreter.throwException(thisInterpreter.RANGE_ERROR, "Invalid array length: " + first);
|
|
502
|
+
}
|
|
503
|
+
newArray.properties.length = first;
|
|
504
|
+
} else {
|
|
505
|
+
for (var i = 0; i < arguments.length; i++) {
|
|
506
|
+
newArray.properties[i] = arguments[i];
|
|
507
|
+
}
|
|
508
|
+
newArray.properties.length = i;
|
|
509
|
+
}
|
|
510
|
+
return newArray;
|
|
511
|
+
};
|
|
512
|
+
this.ARRAY = this.createNativeFunction(wrapper, true);
|
|
513
|
+
this.ARRAY_PROTO = this.ARRAY.properties["prototype"];
|
|
514
|
+
this.setProperty(globalObject, "Array", this.ARRAY, Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
515
|
+
wrapper = function isArray(obj) {
|
|
516
|
+
return obj && obj.class === "Array";
|
|
517
|
+
};
|
|
518
|
+
this.setProperty(this.ARRAY, "isArray", this.createNativeFunction(wrapper, false), Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
519
|
+
this.setProperty(this.ARRAY_PROTO, "length", 0, {
|
|
520
|
+
configurable: false,
|
|
521
|
+
enumerable: false,
|
|
522
|
+
writable: true
|
|
523
|
+
});
|
|
524
|
+
this.ARRAY_PROTO.class = "Array";
|
|
525
|
+
this.polyfills_.push("(function() {", "function createArrayMethod_(f) {", "Object.defineProperty(Array.prototype, f.name,", "{configurable: true, writable: true, value: f});", "}", "createArrayMethod_(", "function pop() {", "if (!this) throw TypeError();", "var o = Object(this), len = o.length >>> 0;", "if (!len || len < 0) {", "o.length = 0;", "return undefined;", "}", "len--;", "var x = o[len];", "delete o[len];", "o.length = len;", "return x;", "}", ");", "createArrayMethod_(", "function push(var_args) {", "if (!this) throw TypeError();", "var o = Object(this), len = o.length >>> 0;", "for (var i = 0; i < arguments.length; i++) {", "o[len] = arguments[i];", "len++;", "}", "o.length = len;", "return len;", "}", ");", "createArrayMethod_(", "function shift() {", "if (!this) throw TypeError();", "var o = Object(this), len = o.length >>> 0;", "if (!len || len < 0) {", "o.length = 0;", "return undefined;", "}", "var value = o[0];", "for (var i = 0; i < len - 1; i++) {", "if ((i + 1) in o) {", "o[i] = o[i + 1];", "} else {", "delete o[i];", "}", "}", "delete o[i];", "o.length = len - 1;", "return value;", "}", ");", "createArrayMethod_(", "function unshift(var_args) {", "if (!this) throw TypeError();", "var o = Object(this), len = o.length >>> 0;", "if (!len || len < 0) {", "len = 0;", "}", "for (var i = len - 1; i >= 0; i--) {", "if (i in o) {", "o[i + arguments.length] = o[i];", "} else {", "delete o[i + arguments.length];", "}", "}", "for (var i = 0; i < arguments.length; i++) {", "o[i] = arguments[i];", "}", "return (o.length = len + arguments.length);", "}", ");", "createArrayMethod_(", "function reverse() {", "if (!this) throw TypeError();", "var o = Object(this), len = o.length >>> 0;", "if (!len || len < 2) {", "return o;", "}", "for (var i = 0; i < len / 2 - 0.5; i++) {", "var x = o[i];", "var hasX = i in o;", "if ((len - i - 1) in o) {", "o[i] = o[len - i - 1];", "} else {", "delete o[i];", "}", "if (hasX) {", "o[len - i - 1] = x;", "} else {", "delete o[len - i - 1];", "}", "}", "return o;", "}", ");", "createArrayMethod_(", "function indexOf(searchElement, fromIndex) {", "if (!this) throw TypeError();", "var o = Object(this), len = o.length >>> 0;", "var n = fromIndex | 0;", "if (!len || n >= len) {", "return -1;", "}", "var i = Math.max(n >= 0 ? n : len - Math.abs(n), 0);", "while (i < len) {", "if (i in o && o[i] === searchElement) {", "return i;", "}", "i++;", "}", "return -1;", "}", ");", "createArrayMethod_(", "function lastIndexOf(searchElement, fromIndex) {", "if (!this) throw TypeError();", "var o = Object(this), len = o.length >>> 0;", "if (!len) {", "return -1;", "}", "var n = len - 1;", "if (arguments.length > 1) {", "n = fromIndex | 0;", "if (n) {", "n = (n > 0 || -1) * Math.floor(Math.abs(n));", "}", "}", "var i = n >= 0 ? Math.min(n, len - 1) : len - Math.abs(n);", "while (i >= 0) {", "if (i in o && o[i] === searchElement) {", "return i;", "}", "i--;", "}", "return -1;", "}", ");", "createArrayMethod_(", "function slice(start, end) {", "if (!this) throw TypeError();", "var o = Object(this), len = o.length >>> 0;", "start |= 0;", "start = (start >= 0) ? start : Math.max(0, len + start);", "if (typeof end !== 'undefined') {", "if (end !== Infinity) {", "end |= 0;", "}", "if (end < 0) {", "end = len + end;", "} else {", "end = Math.min(end, len);", "}", "} else {", "end = len;", "}", "var size = end - start;", "var cloned = new Array(size);", "for (var i = 0; i < size; i++) {", "if ((start + i) in o) {", "cloned[i] = o[start + i];", "}", "}", "return cloned;", "}", ");", "createArrayMethod_(", "function splice(start, deleteCount, var_args) {", "if (!this) throw TypeError();", "var o = Object(this), len = o.length >>> 0;", "start |= 0;", "if (start < 0) {", "start = Math.max(len + start, 0);", "} else {", "start = Math.min(start, len);", "}", "if (arguments.length < 1) {", "deleteCount = len - start;", "} else {", "deleteCount |= 0;", "deleteCount = Math.max(0, Math.min(deleteCount, len - start));", "}", "var removed = [];", "for (var i = start; i < start + deleteCount; i++) {", "if (i in o) {", "removed.push(o[i]);", "} else {", "removed.length++;", "}", "if ((i + deleteCount) in o) {", "o[i] = o[i + deleteCount];", "} else {", "delete o[i];", "}", "}", "for (var i = start + deleteCount; i < len - deleteCount; i++) {", "if ((i + deleteCount) in o) {", "o[i] = o[i + deleteCount];", "} else {", "delete o[i];", "}", "}", "for (var i = len - deleteCount; i < len; i++) {", "delete o[i];", "}", "len -= deleteCount;", "var arl = arguments.length - 2;", "for (var i = len - 1; i >= start; i--) {", "if (i in o) {", "o[i + arl] = o[i];", "} else {", "delete o[i + arl];", "}", "}", "len += arl;", "for (var i = 2; i < arguments.length; i++) {", "o[start + i - 2] = arguments[i];", "}", "o.length = len;", "return removed;", "}", ");", "createArrayMethod_(", "function concat(var_args) {", "if (!this) throw TypeError();", "var o = Object(this);", "var cloned = [];", "for (var i = -1; i < arguments.length; i++) {", "var value = (i === -1) ? o : arguments[i];", "if (Array.isArray(value)) {", "for (var j = 0, l = value.length; j < l; j++) {", "if (j in value) {", "cloned.push(value[j]);", "} else {", "cloned.length++;", "}", "}", "} else {", "cloned.push(value);", "}", "}", "return cloned;", "}", ");", "createArrayMethod_(", "function join(opt_separator) {", "if (!this) throw TypeError();", "var o = Object(this), len = o.length >>> 0;", "var sep = typeof opt_separator === 'undefined' ?", "',' : ('' + opt_separator);", "var str = '';", "for (var i = 0; i < len; i++) {", "if (i && sep) {", "str += sep;", "}", "str += (o[i] === null || o[i] === undefined) ? '' : o[i];", "}", "return str;", "}", ");", "createArrayMethod_(", "function every(callbackfn, thisArg) {", "if (!this || typeof callbackfn !== 'function') throw TypeError();", "var t, k = 0;", "var o = Object(this), len = o.length >>> 0;", "if (arguments.length > 1) t = thisArg;", "while (k < len) {", "if (k in o && !callbackfn.call(t, o[k], k, o)) return false;", "k++;", "}", "return true;", "}", ");", "createArrayMethod_(", "function filter(fun, var_args) {", "if (this === void 0 || this === null || typeof fun !== 'function') throw TypeError();", "var o = Object(this), len = o.length >>> 0;", "var res = [];", "var thisArg = arguments.length >= 2 ? arguments[1] : void 0;", "for (var i = 0; i < len; i++) {", "if (i in o) {", "var val = o[i];", "if (fun.call(thisArg, val, i, o)) res.push(val);", "}", "}", "return res;", "}", ");", "createArrayMethod_(", "function forEach(callback, thisArg) {", "if (!this || typeof callback !== 'function') throw TypeError();", "var t, k = 0;", "var o = Object(this), len = o.length >>> 0;", "if (arguments.length > 1) t = thisArg;", "while (k < len) {", "if (k in o) callback.call(t, o[k], k, o);", "k++;", "}", "}", ");", "createArrayMethod_(", "function map(callback, thisArg) {", "if (!this || typeof callback !== 'function') throw TypeError();", "var t, k = 0;", "var o = Object(this), len = o.length >>> 0;", "if (arguments.length > 1) t = thisArg;", "var a = new Array(len);", "while (k < len) {", "if (k in o) a[k] = callback.call(t, o[k], k, o);", "k++;", "}", "return a;", "}", ");", "createArrayMethod_(", "function reduce(callback /*, initialValue*/) {", "if (!this || typeof callback !== 'function') throw TypeError();", "var o = Object(this), len = o.length >>> 0;", "var k = 0, value;", "if (arguments.length === 2) {", "value = arguments[1];", "} else {", "while (k < len && !(k in o)) k++;", "if (k >= len) {", "throw TypeError('Reduce of empty array with no initial value');", "}", "value = o[k++];", "}", "for (; k < len; k++) {", "if (k in o) value = callback(value, o[k], k, o);", "}", "return value;", "}", ");", "createArrayMethod_(", "function reduceRight(callback /*, initialValue*/) {", "if (null === this || 'undefined' === typeof this || 'function' !== typeof callback) throw TypeError();", "var o = Object(this), len = o.length >>> 0;", "var k = len - 1, value;", "if (arguments.length >= 2) {", "value = arguments[1];", "} else {", "while (k >= 0 && !(k in o)) k--;", "if (k < 0) {", "throw TypeError('Reduce of empty array with no initial value');", "}", "value = o[k--];", "}", "for (; k >= 0; k--) {", "if (k in o) value = callback(value, o[k], k, o);", "}", "return value;", "}", ");", "createArrayMethod_(", "function some(fun/*, thisArg*/) {", "if (!this || typeof fun !== 'function') throw TypeError();", "var o = Object(this), len = o.length >>> 0;", "var thisArg = arguments.length >= 2 ? arguments[1] : void 0;", "for (var i = 0; i < len; i++) {", "if (i in o && fun.call(thisArg, o[i], i, o)) {", "return true;", "}", "}", "return false;", "}", ");", "createArrayMethod_(", "function sort(opt_comp) {", "if (!this) throw TypeError();", "if (typeof opt_comp !== 'function') {", "opt_comp = undefined;", "}", "for (var i = 0; i < this.length; i++) {", "var changes = 0;", "for (var j = 0; j < this.length - i - 1; j++) {", "if (opt_comp ? (opt_comp(this[j], this[j + 1]) > 0) :", "(String(this[j]) > String(this[j + 1]))) {", "var swap = this[j];", "var hasSwap = j in this;", "if ((j + 1) in this) {", "this[j] = this[j + 1];", "} else {", "delete this[j];", "}", "if (hasSwap) {", "this[j + 1] = swap;", "} else {", "delete this[j + 1];", "}", "changes++;", "}", "}", "if (!changes) break;", "}", "return this;", "}", ");", "createArrayMethod_(", "function toLocaleString() {", "if (!this) throw TypeError();", "var o = Object(this), len = o.length >>> 0;", "var out = [];", "for (var i = 0; i < len; i++) {", "out[i] = (o[i] === null || o[i] === undefined) ? '' : o[i].toLocaleString();", "}", "return out.join(',');", "}", ");", "})();", "");
|
|
526
|
+
};
|
|
527
|
+
Interpreter.prototype.initString = function (globalObject) {
|
|
528
|
+
var thisInterpreter = this;
|
|
529
|
+
var wrapper;
|
|
530
|
+
wrapper = function String2(value) {
|
|
531
|
+
value = arguments.length ? Interpreter.nativeGlobal.String(value) : "";
|
|
532
|
+
if (thisInterpreter.calledWithNew()) {
|
|
533
|
+
this.data = value;
|
|
534
|
+
return this;
|
|
535
|
+
} else {
|
|
536
|
+
return value;
|
|
537
|
+
}
|
|
538
|
+
};
|
|
539
|
+
this.STRING = this.createNativeFunction(wrapper, true);
|
|
540
|
+
this.setProperty(globalObject, "String", this.STRING, Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
541
|
+
this.setProperty(this.STRING, "fromCharCode", this.createNativeFunction(String.fromCharCode, false), Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
542
|
+
var functions = ["charAt", "charCodeAt", "concat", "indexOf", "lastIndexOf", "slice", "substr", "substring", "toLocaleLowerCase", "toLocaleUpperCase", "toLowerCase", "toUpperCase", "trim"];
|
|
543
|
+
for (var i = 0; i < functions.length; i++) {
|
|
544
|
+
this.setNativeFunctionPrototype(this.STRING, functions[i], String.prototype[functions[i]]);
|
|
545
|
+
}
|
|
546
|
+
wrapper = function localeCompare(compareString, locales, options) {
|
|
547
|
+
locales = thisInterpreter.pseudoToNative(locales);
|
|
548
|
+
options = thisInterpreter.pseudoToNative(options);
|
|
549
|
+
try {
|
|
550
|
+
return String(this).localeCompare(compareString, locales, options);
|
|
551
|
+
} catch (e) {
|
|
552
|
+
thisInterpreter.throwException(thisInterpreter.ERROR, "localeCompare: " + e.message);
|
|
553
|
+
}
|
|
554
|
+
};
|
|
555
|
+
this.setNativeFunctionPrototype(this.STRING, "localeCompare", wrapper);
|
|
556
|
+
wrapper = function split(separator, limit, callback) {
|
|
557
|
+
var string = String(this);
|
|
558
|
+
limit = limit ? Number(limit) : void 0;
|
|
559
|
+
if (thisInterpreter.isa(separator, thisInterpreter.REGEXP)) {
|
|
560
|
+
separator = separator.data;
|
|
561
|
+
thisInterpreter.maybeThrowRegExp(separator, callback);
|
|
562
|
+
if (thisInterpreter["REGEXP_MODE"] === 2) {
|
|
563
|
+
if (Interpreter.vm) {
|
|
564
|
+
var sandbox = {
|
|
565
|
+
string,
|
|
566
|
+
separator,
|
|
567
|
+
limit
|
|
568
|
+
};
|
|
569
|
+
var code = "string.split(separator, limit)";
|
|
570
|
+
var jsList = thisInterpreter.vmCall(code, sandbox, separator, callback);
|
|
571
|
+
if (jsList !== Interpreter.REGEXP_TIMEOUT) {
|
|
572
|
+
callback(thisInterpreter.arrayNativeToPseudo(jsList));
|
|
573
|
+
}
|
|
574
|
+
} else {
|
|
575
|
+
var splitWorker = thisInterpreter.createWorker();
|
|
576
|
+
var pid = thisInterpreter.regExpTimeout(separator, splitWorker, callback);
|
|
577
|
+
splitWorker.onmessage = function (e) {
|
|
578
|
+
clearTimeout(pid);
|
|
579
|
+
callback(thisInterpreter.arrayNativeToPseudo(e.data));
|
|
580
|
+
};
|
|
581
|
+
splitWorker.postMessage(["split", string, separator, limit]);
|
|
582
|
+
}
|
|
583
|
+
return;
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
var jsList = string.split(separator, limit);
|
|
587
|
+
callback(thisInterpreter.arrayNativeToPseudo(jsList));
|
|
588
|
+
};
|
|
589
|
+
this.setAsyncFunctionPrototype(this.STRING, "split", wrapper);
|
|
590
|
+
wrapper = function match(regexp, callback) {
|
|
591
|
+
var string = String(this);
|
|
592
|
+
regexp = thisInterpreter.isa(regexp, thisInterpreter.REGEXP) ? regexp.data : new RegExp(regexp);
|
|
593
|
+
thisInterpreter.maybeThrowRegExp(regexp, callback);
|
|
594
|
+
if (thisInterpreter["REGEXP_MODE"] === 2) {
|
|
595
|
+
if (Interpreter.vm) {
|
|
596
|
+
var sandbox = {
|
|
597
|
+
string,
|
|
598
|
+
regexp
|
|
599
|
+
};
|
|
600
|
+
var code = "string.match(regexp)";
|
|
601
|
+
var m = thisInterpreter.vmCall(code, sandbox, regexp, callback);
|
|
602
|
+
if (m !== Interpreter.REGEXP_TIMEOUT) {
|
|
603
|
+
callback(m && thisInterpreter.arrayNativeToPseudo(m));
|
|
604
|
+
}
|
|
605
|
+
} else {
|
|
606
|
+
var matchWorker = thisInterpreter.createWorker();
|
|
607
|
+
var pid = thisInterpreter.regExpTimeout(regexp, matchWorker, callback);
|
|
608
|
+
matchWorker.onmessage = function (e) {
|
|
609
|
+
clearTimeout(pid);
|
|
610
|
+
callback(e.data && thisInterpreter.arrayNativeToPseudo(e.data));
|
|
611
|
+
};
|
|
612
|
+
matchWorker.postMessage(["match", string, regexp]);
|
|
613
|
+
}
|
|
614
|
+
return;
|
|
615
|
+
}
|
|
616
|
+
var m = string.match(regexp);
|
|
617
|
+
callback(m && thisInterpreter.arrayNativeToPseudo(m));
|
|
618
|
+
};
|
|
619
|
+
this.setAsyncFunctionPrototype(this.STRING, "match", wrapper);
|
|
620
|
+
wrapper = function search(regexp, callback) {
|
|
621
|
+
var string = String(this);
|
|
622
|
+
if (thisInterpreter.isa(regexp, thisInterpreter.REGEXP)) {
|
|
623
|
+
regexp = regexp.data;
|
|
624
|
+
} else {
|
|
625
|
+
regexp = new RegExp(regexp);
|
|
626
|
+
}
|
|
627
|
+
thisInterpreter.maybeThrowRegExp(regexp, callback);
|
|
628
|
+
if (thisInterpreter["REGEXP_MODE"] === 2) {
|
|
629
|
+
if (Interpreter.vm) {
|
|
630
|
+
var sandbox = {
|
|
631
|
+
string,
|
|
632
|
+
regexp
|
|
633
|
+
};
|
|
634
|
+
var code = "string.search(regexp)";
|
|
635
|
+
var n = thisInterpreter.vmCall(code, sandbox, regexp, callback);
|
|
636
|
+
if (n !== Interpreter.REGEXP_TIMEOUT) {
|
|
637
|
+
callback(n);
|
|
638
|
+
}
|
|
639
|
+
} else {
|
|
640
|
+
var searchWorker = thisInterpreter.createWorker();
|
|
641
|
+
var pid = thisInterpreter.regExpTimeout(regexp, searchWorker, callback);
|
|
642
|
+
searchWorker.onmessage = function (e) {
|
|
643
|
+
clearTimeout(pid);
|
|
644
|
+
callback(e.data);
|
|
645
|
+
};
|
|
646
|
+
searchWorker.postMessage(["search", string, regexp]);
|
|
647
|
+
}
|
|
648
|
+
return;
|
|
649
|
+
}
|
|
650
|
+
callback(string.search(regexp));
|
|
651
|
+
};
|
|
652
|
+
this.setAsyncFunctionPrototype(this.STRING, "search", wrapper);
|
|
653
|
+
wrapper = function replace_(substr, newSubstr, callback) {
|
|
654
|
+
var string = String(this);
|
|
655
|
+
newSubstr = String(newSubstr);
|
|
656
|
+
if (thisInterpreter.isa(substr, thisInterpreter.REGEXP)) {
|
|
657
|
+
substr = substr.data;
|
|
658
|
+
thisInterpreter.maybeThrowRegExp(substr, callback);
|
|
659
|
+
if (thisInterpreter["REGEXP_MODE"] === 2) {
|
|
660
|
+
if (Interpreter.vm) {
|
|
661
|
+
var sandbox = {
|
|
662
|
+
string,
|
|
663
|
+
substr,
|
|
664
|
+
newSubstr
|
|
665
|
+
};
|
|
666
|
+
var code = "string.replace(substr, newSubstr)";
|
|
667
|
+
var str = thisInterpreter.vmCall(code, sandbox, substr, callback);
|
|
668
|
+
if (str !== Interpreter.REGEXP_TIMEOUT) {
|
|
669
|
+
callback(str);
|
|
670
|
+
}
|
|
671
|
+
} else {
|
|
672
|
+
var replaceWorker = thisInterpreter.createWorker();
|
|
673
|
+
var pid = thisInterpreter.regExpTimeout(substr, replaceWorker, callback);
|
|
674
|
+
replaceWorker.onmessage = function (e) {
|
|
675
|
+
clearTimeout(pid);
|
|
676
|
+
callback(e.data);
|
|
677
|
+
};
|
|
678
|
+
replaceWorker.postMessage(["replace", string, substr, newSubstr]);
|
|
679
|
+
}
|
|
680
|
+
return;
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
callback(string.replace(substr, newSubstr));
|
|
684
|
+
};
|
|
685
|
+
this.setAsyncFunctionPrototype(this.STRING, "replace", wrapper);
|
|
686
|
+
this.polyfills_.push("(function() {", "var replace_ = String.prototype.replace;", "String.prototype.replace = function replace(substr, newSubstr) {", "if (typeof newSubstr !== 'function') {", "return replace_.call(this, substr, newSubstr);", "}", "var str = this;", "if (substr instanceof RegExp) {", "var subs = [];", "var m = substr.exec(str);", "while (m) {", "m.push(m.index, str);", "var inject = newSubstr.apply(null, m);", "subs.push([m.index, m[0].length, inject]);", "m = substr.global ? substr.exec(str) : null;", "}", "for (var i = subs.length - 1; i >= 0; i--) {", "str = str.substring(0, subs[i][0]) + subs[i][2] + str.substring(subs[i][0] + subs[i][1]);", "}", "} else {", "var i = str.indexOf(substr);", "if (i !== -1) {", "var inject = newSubstr(str.substr(i, substr.length), i, str);", "str = str.substring(0, i) + inject + str.substring(i + substr.length);", "}", "}", "return str;", "};", "})();", "");
|
|
687
|
+
};
|
|
688
|
+
Interpreter.prototype.initBoolean = function (globalObject) {
|
|
689
|
+
var thisInterpreter = this;
|
|
690
|
+
var wrapper;
|
|
691
|
+
wrapper = function Boolean2(value) {
|
|
692
|
+
value = Interpreter.nativeGlobal.Boolean(value);
|
|
693
|
+
if (thisInterpreter.calledWithNew()) {
|
|
694
|
+
this.data = value;
|
|
695
|
+
return this;
|
|
696
|
+
} else {
|
|
697
|
+
return value;
|
|
698
|
+
}
|
|
699
|
+
};
|
|
700
|
+
this.BOOLEAN = this.createNativeFunction(wrapper, true);
|
|
701
|
+
this.setProperty(globalObject, "Boolean", this.BOOLEAN, Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
702
|
+
};
|
|
703
|
+
Interpreter.prototype.initNumber = function (globalObject) {
|
|
704
|
+
var thisInterpreter = this;
|
|
705
|
+
var wrapper;
|
|
706
|
+
wrapper = function Number2(value) {
|
|
707
|
+
value = arguments.length ? Interpreter.nativeGlobal.Number(value) : 0;
|
|
708
|
+
if (thisInterpreter.calledWithNew()) {
|
|
709
|
+
this.data = value;
|
|
710
|
+
return this;
|
|
711
|
+
} else {
|
|
712
|
+
return value;
|
|
713
|
+
}
|
|
714
|
+
};
|
|
715
|
+
this.NUMBER = this.createNativeFunction(wrapper, true);
|
|
716
|
+
this.setProperty(globalObject, "Number", this.NUMBER, Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
717
|
+
var numConsts = ["MAX_VALUE", "MIN_VALUE", "NaN", "NEGATIVE_INFINITY", "POSITIVE_INFINITY"];
|
|
718
|
+
for (var i = 0; i < numConsts.length; i++) {
|
|
719
|
+
this.setProperty(this.NUMBER, numConsts[i], Number[numConsts[i]], Interpreter.NONCONFIGURABLE_READONLY_NONENUMERABLE_DESCRIPTOR);
|
|
720
|
+
}
|
|
721
|
+
wrapper = function toExponential(fractionDigits) {
|
|
722
|
+
try {
|
|
723
|
+
return Number(this).toExponential(fractionDigits);
|
|
724
|
+
} catch (e) {
|
|
725
|
+
thisInterpreter.throwException(thisInterpreter.ERROR, e.message);
|
|
726
|
+
}
|
|
727
|
+
};
|
|
728
|
+
this.setNativeFunctionPrototype(this.NUMBER, "toExponential", wrapper);
|
|
729
|
+
wrapper = function toFixed(digits) {
|
|
730
|
+
try {
|
|
731
|
+
return Number(this).toFixed(digits);
|
|
732
|
+
} catch (e) {
|
|
733
|
+
thisInterpreter.throwException(thisInterpreter.ERROR, e.message);
|
|
734
|
+
}
|
|
735
|
+
};
|
|
736
|
+
this.setNativeFunctionPrototype(this.NUMBER, "toFixed", wrapper);
|
|
737
|
+
wrapper = function toPrecision(precision) {
|
|
738
|
+
try {
|
|
739
|
+
return Number(this).toPrecision(precision);
|
|
740
|
+
} catch (e) {
|
|
741
|
+
thisInterpreter.throwException(thisInterpreter.ERROR, e.message);
|
|
742
|
+
}
|
|
743
|
+
};
|
|
744
|
+
this.setNativeFunctionPrototype(this.NUMBER, "toPrecision", wrapper);
|
|
745
|
+
wrapper = function toString(radix) {
|
|
746
|
+
try {
|
|
747
|
+
return Number(this).toString(radix);
|
|
748
|
+
} catch (e) {
|
|
749
|
+
thisInterpreter.throwException(thisInterpreter.ERROR, e.message);
|
|
750
|
+
}
|
|
751
|
+
};
|
|
752
|
+
this.setNativeFunctionPrototype(this.NUMBER, "toString", wrapper);
|
|
753
|
+
wrapper = function toLocaleString(locales, options) {
|
|
754
|
+
locales = locales ? thisInterpreter.pseudoToNative(locales) : void 0;
|
|
755
|
+
options = options ? thisInterpreter.pseudoToNative(options) : void 0;
|
|
756
|
+
try {
|
|
757
|
+
return Number(this).toLocaleString(locales, options);
|
|
758
|
+
} catch (e) {
|
|
759
|
+
thisInterpreter.throwException(thisInterpreter.ERROR, "toLocaleString: " + e.message);
|
|
760
|
+
}
|
|
761
|
+
};
|
|
762
|
+
this.setNativeFunctionPrototype(this.NUMBER, "toLocaleString", wrapper);
|
|
763
|
+
};
|
|
764
|
+
Interpreter.prototype.initDate = function (globalObject) {
|
|
765
|
+
var thisInterpreter = this;
|
|
766
|
+
var wrapper;
|
|
767
|
+
wrapper = function Date2(_value, var_args) {
|
|
768
|
+
if (!thisInterpreter.calledWithNew()) {
|
|
769
|
+
return Interpreter.nativeGlobal.Date();
|
|
770
|
+
}
|
|
771
|
+
var args = [null].concat(Array.from(arguments));
|
|
772
|
+
this.data = new (Function.prototype.bind.apply(Interpreter.nativeGlobal.Date, args))();
|
|
773
|
+
return this;
|
|
774
|
+
};
|
|
775
|
+
this.DATE = this.createNativeFunction(wrapper, true);
|
|
776
|
+
this.DATE_PROTO = this.DATE.properties["prototype"];
|
|
777
|
+
this.setProperty(globalObject, "Date", this.DATE, Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
778
|
+
this.setProperty(this.DATE, "now", this.createNativeFunction(Date.now, false), Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
779
|
+
this.setProperty(this.DATE, "parse", this.createNativeFunction(Date.parse, false), Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
780
|
+
this.setProperty(this.DATE, "UTC", this.createNativeFunction(Date.UTC, false), Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
781
|
+
var functions = ["getDate", "getDay", "getFullYear", "getHours", "getMilliseconds", "getMinutes", "getMonth", "getSeconds", "getTime", "getTimezoneOffset", "getUTCDate", "getUTCDay", "getUTCFullYear", "getUTCHours", "getUTCMilliseconds", "getUTCMinutes", "getUTCMonth", "getUTCSeconds", "getYear", "setDate", "setFullYear", "setHours", "setMilliseconds", "setMinutes", "setMonth", "setSeconds", "setTime", "setUTCDate", "setUTCFullYear", "setUTCHours", "setUTCMilliseconds", "setUTCMinutes", "setUTCMonth", "setUTCSeconds", "setYear", "toDateString", "toISOString", "toJSON", "toGMTString", "toLocaleDateString", "toLocaleString", "toLocaleTimeString", "toTimeString", "toUTCString"];
|
|
782
|
+
for (var i = 0; i < functions.length; i++) {
|
|
783
|
+
wrapper = function (nativeFunc) {
|
|
784
|
+
return function (var_args) {
|
|
785
|
+
var date = this.data;
|
|
786
|
+
if (!(date instanceof Date)) {
|
|
787
|
+
thisInterpreter.throwException(thisInterpreter.TYPE_ERROR, nativeFunc + " not called on a Date");
|
|
788
|
+
}
|
|
789
|
+
var args = [];
|
|
790
|
+
for (var i2 = 0; i2 < arguments.length; i2++) {
|
|
791
|
+
args[i2] = thisInterpreter.pseudoToNative(arguments[i2]);
|
|
792
|
+
}
|
|
793
|
+
return date[nativeFunc].apply(date, args);
|
|
794
|
+
};
|
|
795
|
+
}(functions[i]);
|
|
796
|
+
this.setNativeFunctionPrototype(this.DATE, functions[i], wrapper);
|
|
797
|
+
}
|
|
798
|
+
};
|
|
799
|
+
Interpreter.prototype.initRegExp = function (globalObject) {
|
|
800
|
+
var thisInterpreter = this;
|
|
801
|
+
var wrapper;
|
|
802
|
+
wrapper = function RegExp2(pattern, flags) {
|
|
803
|
+
if (thisInterpreter.calledWithNew()) {
|
|
804
|
+
var rgx = this;
|
|
805
|
+
} else {
|
|
806
|
+
if (flags === void 0 && thisInterpreter.isa(pattern, thisInterpreter.REGEXP)) {
|
|
807
|
+
return pattern;
|
|
808
|
+
}
|
|
809
|
+
var rgx = thisInterpreter.createObjectProto(thisInterpreter.REGEXP_PROTO);
|
|
810
|
+
}
|
|
811
|
+
pattern = pattern === void 0 ? "" : String(pattern);
|
|
812
|
+
flags = flags ? String(flags) : "";
|
|
813
|
+
if (!/^[gmi]*$/.test(flags)) {
|
|
814
|
+
thisInterpreter.throwException(thisInterpreter.SYNTAX_ERROR, "Invalid regexp flag: " + flags);
|
|
815
|
+
}
|
|
816
|
+
try {
|
|
817
|
+
var nativeRegExp = new Interpreter.nativeGlobal.RegExp(pattern, flags);
|
|
818
|
+
} catch (e) {
|
|
819
|
+
thisInterpreter.throwException(thisInterpreter.SYNTAX_ERROR, e.message);
|
|
820
|
+
}
|
|
821
|
+
thisInterpreter.populateRegExp(rgx, nativeRegExp);
|
|
822
|
+
return rgx;
|
|
823
|
+
};
|
|
824
|
+
this.REGEXP = this.createNativeFunction(wrapper, true);
|
|
825
|
+
this.REGEXP_PROTO = this.REGEXP.properties["prototype"];
|
|
826
|
+
this.setProperty(globalObject, "RegExp", this.REGEXP, Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
827
|
+
this.setProperty(this.REGEXP.properties["prototype"], "global", void 0, Interpreter.READONLY_NONENUMERABLE_DESCRIPTOR);
|
|
828
|
+
this.setProperty(this.REGEXP.properties["prototype"], "ignoreCase", void 0, Interpreter.READONLY_NONENUMERABLE_DESCRIPTOR);
|
|
829
|
+
this.setProperty(this.REGEXP.properties["prototype"], "multiline", void 0, Interpreter.READONLY_NONENUMERABLE_DESCRIPTOR);
|
|
830
|
+
this.setProperty(this.REGEXP.properties["prototype"], "source", "(?:)", Interpreter.READONLY_NONENUMERABLE_DESCRIPTOR);
|
|
831
|
+
this.polyfills_.push("Object.defineProperty(RegExp.prototype, 'test',", "{configurable: true, writable: true, value:", "function test(str) {", "return !!this.exec(str);", "}", "});");
|
|
832
|
+
wrapper = function exec(string, callback) {
|
|
833
|
+
var regexp = this.data;
|
|
834
|
+
string = String(string);
|
|
835
|
+
regexp.lastIndex = Number(thisInterpreter.getProperty(this, "lastIndex"));
|
|
836
|
+
thisInterpreter.maybeThrowRegExp(regexp, callback);
|
|
837
|
+
if (thisInterpreter["REGEXP_MODE"] === 2) {
|
|
838
|
+
if (Interpreter.vm) {
|
|
839
|
+
var sandbox = {
|
|
840
|
+
string,
|
|
841
|
+
regexp
|
|
842
|
+
};
|
|
843
|
+
var code = "regexp.exec(string)";
|
|
844
|
+
var match = thisInterpreter.vmCall(code, sandbox, regexp, callback);
|
|
845
|
+
if (match !== Interpreter.REGEXP_TIMEOUT) {
|
|
846
|
+
thisInterpreter.setProperty(this, "lastIndex", regexp.lastIndex);
|
|
847
|
+
callback(matchToPseudo(match));
|
|
848
|
+
}
|
|
849
|
+
} else {
|
|
850
|
+
var execWorker = thisInterpreter.createWorker();
|
|
851
|
+
var pid = thisInterpreter.regExpTimeout(regexp, execWorker, callback);
|
|
852
|
+
var thisPseudoRegExp = this;
|
|
853
|
+
execWorker.onmessage = function (e) {
|
|
854
|
+
clearTimeout(pid);
|
|
855
|
+
thisInterpreter.setProperty(thisPseudoRegExp, "lastIndex", e.data[1]);
|
|
856
|
+
callback(matchToPseudo(e.data[0]));
|
|
857
|
+
};
|
|
858
|
+
execWorker.postMessage(["exec", regexp, regexp.lastIndex, string]);
|
|
859
|
+
}
|
|
860
|
+
return;
|
|
861
|
+
}
|
|
862
|
+
var match = regexp.exec(string);
|
|
863
|
+
thisInterpreter.setProperty(this, "lastIndex", regexp.lastIndex);
|
|
864
|
+
callback(matchToPseudo(match));
|
|
865
|
+
function matchToPseudo(match2) {
|
|
866
|
+
if (match2) {
|
|
867
|
+
var result = thisInterpreter.arrayNativeToPseudo(match2);
|
|
868
|
+
thisInterpreter.setProperty(result, "index", match2.index);
|
|
869
|
+
thisInterpreter.setProperty(result, "input", match2.input);
|
|
870
|
+
return result;
|
|
871
|
+
}
|
|
872
|
+
return null;
|
|
873
|
+
}
|
|
874
|
+
};
|
|
875
|
+
this.setAsyncFunctionPrototype(this.REGEXP, "exec", wrapper);
|
|
876
|
+
};
|
|
877
|
+
Interpreter.prototype.initError = function (globalObject) {
|
|
878
|
+
var thisInterpreter = this;
|
|
879
|
+
this.ERROR = this.createNativeFunction(function Error2(opt_message) {
|
|
880
|
+
if (thisInterpreter.calledWithNew()) {
|
|
881
|
+
var newError = this;
|
|
882
|
+
} else {
|
|
883
|
+
var newError = thisInterpreter.createObject(thisInterpreter.ERROR);
|
|
884
|
+
}
|
|
885
|
+
thisInterpreter.populateError(newError, opt_message);
|
|
886
|
+
return newError;
|
|
887
|
+
}, true);
|
|
888
|
+
this.setProperty(globalObject, "Error", this.ERROR, Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
889
|
+
this.setProperty(this.ERROR.properties["prototype"], "message", "", Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
890
|
+
this.setProperty(this.ERROR.properties["prototype"], "name", "Error", Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
891
|
+
var createErrorSubclass = function (name) {
|
|
892
|
+
var constructor = thisInterpreter.createNativeFunction(function (opt_message) {
|
|
893
|
+
if (thisInterpreter.calledWithNew()) {
|
|
894
|
+
var newError = this;
|
|
895
|
+
} else {
|
|
896
|
+
var newError = thisInterpreter.createObject(constructor);
|
|
897
|
+
}
|
|
898
|
+
thisInterpreter.populateError(newError, opt_message);
|
|
899
|
+
return newError;
|
|
900
|
+
}, true);
|
|
901
|
+
thisInterpreter.setProperty(constructor, "prototype", thisInterpreter.createObject(thisInterpreter.ERROR), Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
902
|
+
thisInterpreter.setProperty(constructor.properties["prototype"], "name", name, Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
903
|
+
thisInterpreter.setProperty(globalObject, name, constructor, Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
904
|
+
return constructor;
|
|
905
|
+
};
|
|
906
|
+
this.EVAL_ERROR = createErrorSubclass("EvalError");
|
|
907
|
+
this.RANGE_ERROR = createErrorSubclass("RangeError");
|
|
908
|
+
this.REFERENCE_ERROR = createErrorSubclass("ReferenceError");
|
|
909
|
+
this.SYNTAX_ERROR = createErrorSubclass("SyntaxError");
|
|
910
|
+
this.TYPE_ERROR = createErrorSubclass("TypeError");
|
|
911
|
+
this.URI_ERROR = createErrorSubclass("URIError");
|
|
912
|
+
};
|
|
913
|
+
Interpreter.prototype.initMath = function (globalObject) {
|
|
914
|
+
var myMath = this.createObjectProto(this.OBJECT_PROTO);
|
|
915
|
+
this.setProperty(globalObject, "Math", myMath, Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
916
|
+
var mathConsts = ["E", "LN2", "LN10", "LOG2E", "LOG10E", "PI", "SQRT1_2", "SQRT2"];
|
|
917
|
+
for (var i = 0; i < mathConsts.length; i++) {
|
|
918
|
+
this.setProperty(myMath, mathConsts[i], Math[mathConsts[i]], Interpreter.READONLY_NONENUMERABLE_DESCRIPTOR);
|
|
919
|
+
}
|
|
920
|
+
var numFunctions = ["abs", "acos", "asin", "atan", "atan2", "ceil", "cos", "exp", "floor", "log", "max", "min", "pow", "random", "round", "sin", "sqrt", "tan"];
|
|
921
|
+
for (var i = 0; i < numFunctions.length; i++) {
|
|
922
|
+
this.setProperty(myMath, numFunctions[i], this.createNativeFunction(Math[numFunctions[i]], false), Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
923
|
+
}
|
|
924
|
+
};
|
|
925
|
+
Interpreter.prototype.initJSON = function (globalObject) {
|
|
926
|
+
var wrapper;
|
|
927
|
+
var thisInterpreter = this;
|
|
928
|
+
var myJSON = thisInterpreter.createObjectProto(this.OBJECT_PROTO);
|
|
929
|
+
this.setProperty(globalObject, "JSON", myJSON, Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
930
|
+
wrapper = function parse2(text) {
|
|
931
|
+
try {
|
|
932
|
+
var nativeObj = JSON.parse(String(text));
|
|
933
|
+
} catch (e) {
|
|
934
|
+
thisInterpreter.throwException(thisInterpreter.SYNTAX_ERROR, e.message);
|
|
935
|
+
}
|
|
936
|
+
return thisInterpreter.nativeToPseudo(nativeObj);
|
|
937
|
+
};
|
|
938
|
+
this.setProperty(myJSON, "parse", this.createNativeFunction(wrapper, false));
|
|
939
|
+
wrapper = function stringify(value, replacer, space) {
|
|
940
|
+
if (replacer && replacer.class === "Function") {
|
|
941
|
+
thisInterpreter.throwException(thisInterpreter.TYPE_ERROR, "Function replacer on JSON.stringify not supported");
|
|
942
|
+
} else if (replacer && replacer.class === "Array") {
|
|
943
|
+
replacer = thisInterpreter.arrayPseudoToNative(replacer);
|
|
944
|
+
replacer = replacer.filter(function (word) {
|
|
945
|
+
return typeof word === "string" || typeof word === "number";
|
|
946
|
+
});
|
|
947
|
+
} else {
|
|
948
|
+
replacer = null;
|
|
949
|
+
}
|
|
950
|
+
if (typeof space !== "string" && typeof space !== "number") {
|
|
951
|
+
space = void 0;
|
|
952
|
+
}
|
|
953
|
+
var nativeObj = thisInterpreter.pseudoToNative(value);
|
|
954
|
+
try {
|
|
955
|
+
var str = JSON.stringify(nativeObj, replacer, space);
|
|
956
|
+
} catch (e) {
|
|
957
|
+
thisInterpreter.throwException(thisInterpreter.TYPE_ERROR, e.message);
|
|
958
|
+
}
|
|
959
|
+
return str;
|
|
960
|
+
};
|
|
961
|
+
this.setProperty(myJSON, "stringify", this.createNativeFunction(wrapper, false));
|
|
962
|
+
};
|
|
963
|
+
Interpreter.prototype.isa = function (child, constructor) {
|
|
964
|
+
if (child === null || child === void 0 || !constructor) {
|
|
965
|
+
return false;
|
|
966
|
+
}
|
|
967
|
+
var proto = constructor.properties["prototype"];
|
|
968
|
+
if (child === proto) {
|
|
969
|
+
return true;
|
|
970
|
+
}
|
|
971
|
+
child = this.getPrototype(child);
|
|
972
|
+
while (child) {
|
|
973
|
+
if (child === proto) {
|
|
974
|
+
return true;
|
|
975
|
+
}
|
|
976
|
+
child = child.proto;
|
|
977
|
+
}
|
|
978
|
+
return false;
|
|
979
|
+
};
|
|
980
|
+
Interpreter.prototype.populateRegExp = function (pseudoRegexp, nativeRegexp) {
|
|
981
|
+
pseudoRegexp.data = new RegExp(nativeRegexp.source, nativeRegexp.flags);
|
|
982
|
+
this.setProperty(pseudoRegexp, "lastIndex", nativeRegexp.lastIndex, Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
983
|
+
this.setProperty(pseudoRegexp, "source", nativeRegexp.source, Interpreter.READONLY_NONENUMERABLE_DESCRIPTOR);
|
|
984
|
+
this.setProperty(pseudoRegexp, "global", nativeRegexp.global, Interpreter.READONLY_NONENUMERABLE_DESCRIPTOR);
|
|
985
|
+
this.setProperty(pseudoRegexp, "ignoreCase", nativeRegexp.ignoreCase, Interpreter.READONLY_NONENUMERABLE_DESCRIPTOR);
|
|
986
|
+
this.setProperty(pseudoRegexp, "multiline", nativeRegexp.multiline, Interpreter.READONLY_NONENUMERABLE_DESCRIPTOR);
|
|
987
|
+
};
|
|
988
|
+
Interpreter.prototype.populateError = function (pseudoError, opt_message) {
|
|
989
|
+
if (opt_message) {
|
|
990
|
+
this.setProperty(pseudoError, "message", String(opt_message), Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
991
|
+
}
|
|
992
|
+
var tracebackData = [];
|
|
993
|
+
for (var i = this.stateStack.length - 1; i >= 0; i--) {
|
|
994
|
+
var state = this.stateStack[i];
|
|
995
|
+
var node = state.node;
|
|
996
|
+
if (node.type === "CallExpression") {
|
|
997
|
+
var func = state.func_;
|
|
998
|
+
if (func && tracebackData.length) {
|
|
999
|
+
tracebackData[tracebackData.length - 1].datumName = this.getProperty(func, "name");
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
if (node.loc && (!tracebackData.length || node.type === "CallExpression")) {
|
|
1003
|
+
tracebackData.push({
|
|
1004
|
+
datumLoc: node.loc
|
|
1005
|
+
});
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
var errorName = String(this.getProperty(pseudoError, "name"));
|
|
1009
|
+
var errorMessage = String(this.getProperty(pseudoError, "message"));
|
|
1010
|
+
var stackString = errorName + ": " + errorMessage + "\n";
|
|
1011
|
+
for (var i = 0; i < tracebackData.length; i++) {
|
|
1012
|
+
var loc = tracebackData[i].datumLoc;
|
|
1013
|
+
var name = tracebackData[i].datumName;
|
|
1014
|
+
var locString = loc.source + ":" + loc.start.line + ":" + loc.start.column;
|
|
1015
|
+
if (name) {
|
|
1016
|
+
stackString += " at " + name + " (" + locString + ")\n";
|
|
1017
|
+
} else {
|
|
1018
|
+
stackString += " at " + locString + "\n";
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1021
|
+
this.setProperty(pseudoError, "stack", stackString.trim(), Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
1022
|
+
};
|
|
1023
|
+
Interpreter.prototype.createWorker = function () {
|
|
1024
|
+
var blob = this.createWorker.blob_;
|
|
1025
|
+
if (!blob) {
|
|
1026
|
+
blob = new Blob([Interpreter.WORKER_CODE.join("\n")], {
|
|
1027
|
+
type: "application/javascript"
|
|
1028
|
+
});
|
|
1029
|
+
this.createWorker.blob_ = blob;
|
|
1030
|
+
}
|
|
1031
|
+
return new Worker(URL.createObjectURL(blob));
|
|
1032
|
+
};
|
|
1033
|
+
Interpreter.prototype.vmCall = function (code, sandbox, nativeRegExp, callback) {
|
|
1034
|
+
var options = {
|
|
1035
|
+
timeout: this["REGEXP_THREAD_TIMEOUT"]
|
|
1036
|
+
};
|
|
1037
|
+
try {
|
|
1038
|
+
return Interpreter.vm["runInNewContext"](code, sandbox, options);
|
|
1039
|
+
} catch (_e) {
|
|
1040
|
+
callback(null);
|
|
1041
|
+
this.throwException(this.ERROR, "RegExp Timeout: " + nativeRegExp);
|
|
1042
|
+
}
|
|
1043
|
+
return Interpreter.REGEXP_TIMEOUT;
|
|
1044
|
+
};
|
|
1045
|
+
Interpreter.prototype.maybeThrowRegExp = function (nativeRegExp, callback) {
|
|
1046
|
+
var ok;
|
|
1047
|
+
if (this["REGEXP_MODE"] === 0) {
|
|
1048
|
+
ok = false;
|
|
1049
|
+
} else if (this["REGEXP_MODE"] === 1) {
|
|
1050
|
+
ok = true;
|
|
1051
|
+
} else {
|
|
1052
|
+
if (Interpreter.vm) {
|
|
1053
|
+
ok = true;
|
|
1054
|
+
} else if (typeof Worker === "function" && typeof URL === "function") {
|
|
1055
|
+
ok = true;
|
|
1056
|
+
} else if (typeof require === "function") {
|
|
1057
|
+
try {
|
|
1058
|
+
Interpreter.vm = require("vm");
|
|
1059
|
+
} catch (_e) {}
|
|
1060
|
+
ok = !!Interpreter.vm;
|
|
1061
|
+
} else {
|
|
1062
|
+
ok = false;
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1065
|
+
if (!ok) {
|
|
1066
|
+
callback(null);
|
|
1067
|
+
this.throwException(this.ERROR, "Regular expressions not supported: " + nativeRegExp);
|
|
1068
|
+
}
|
|
1069
|
+
};
|
|
1070
|
+
Interpreter.prototype.regExpTimeout = function (nativeRegExp, worker, callback) {
|
|
1071
|
+
var thisInterpreter = this;
|
|
1072
|
+
return setTimeout(function () {
|
|
1073
|
+
worker.terminate();
|
|
1074
|
+
callback(null);
|
|
1075
|
+
try {
|
|
1076
|
+
thisInterpreter.throwException(thisInterpreter.ERROR, "RegExp Timeout: " + nativeRegExp);
|
|
1077
|
+
} catch (_e) {}
|
|
1078
|
+
}, this["REGEXP_THREAD_TIMEOUT"]);
|
|
1079
|
+
};
|
|
1080
|
+
Interpreter.prototype.createObject = function (constructor) {
|
|
1081
|
+
return this.createObjectProto(constructor && constructor.properties["prototype"]);
|
|
1082
|
+
};
|
|
1083
|
+
Interpreter.prototype.createObjectProto = function (proto) {
|
|
1084
|
+
if (typeof proto !== "object") {
|
|
1085
|
+
throw Error("Non object prototype");
|
|
1086
|
+
}
|
|
1087
|
+
var obj = new Interpreter.Object(proto);
|
|
1088
|
+
if (this.isa(obj, this.ERROR)) {
|
|
1089
|
+
obj.class = "Error";
|
|
1090
|
+
}
|
|
1091
|
+
return obj;
|
|
1092
|
+
};
|
|
1093
|
+
Interpreter.prototype.createArray = function () {
|
|
1094
|
+
var array = this.createObjectProto(this.ARRAY_PROTO);
|
|
1095
|
+
this.setProperty(array, "length", 0, {
|
|
1096
|
+
configurable: false,
|
|
1097
|
+
enumerable: false,
|
|
1098
|
+
writable: true
|
|
1099
|
+
});
|
|
1100
|
+
array.class = "Array";
|
|
1101
|
+
return array;
|
|
1102
|
+
};
|
|
1103
|
+
Interpreter.prototype.createFunctionBase_ = function (argumentLength, isConstructor) {
|
|
1104
|
+
var func = this.createObjectProto(this.FUNCTION_PROTO);
|
|
1105
|
+
if (isConstructor) {
|
|
1106
|
+
var proto = this.createObjectProto(this.OBJECT_PROTO);
|
|
1107
|
+
this.setProperty(func, "prototype", proto, Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
1108
|
+
this.setProperty(proto, "constructor", func, Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
1109
|
+
} else {
|
|
1110
|
+
func.illegalConstructor = true;
|
|
1111
|
+
}
|
|
1112
|
+
this.setProperty(func, "length", argumentLength, Interpreter.READONLY_NONENUMERABLE_DESCRIPTOR);
|
|
1113
|
+
func.class = "Function";
|
|
1114
|
+
return func;
|
|
1115
|
+
};
|
|
1116
|
+
Interpreter.prototype.createFunction = function (node, scope, opt_name) {
|
|
1117
|
+
var func = this.createFunctionBase_(node.params.length, true);
|
|
1118
|
+
func.parentScope = scope;
|
|
1119
|
+
func.node = node;
|
|
1120
|
+
var name = node.id ? String(node.id.name) : opt_name || "";
|
|
1121
|
+
this.setProperty(func, "name", name, Interpreter.READONLY_NONENUMERABLE_DESCRIPTOR);
|
|
1122
|
+
return func;
|
|
1123
|
+
};
|
|
1124
|
+
Interpreter.prototype.createNativeFunction = function (nativeFunc, isConstructor) {
|
|
1125
|
+
var func = this.createFunctionBase_(nativeFunc.length, isConstructor);
|
|
1126
|
+
func.nativeFunc = nativeFunc;
|
|
1127
|
+
nativeFunc.id = this.functionCounter_++;
|
|
1128
|
+
this.setProperty(func, "name", nativeFunc.name, Interpreter.READONLY_NONENUMERABLE_DESCRIPTOR);
|
|
1129
|
+
return func;
|
|
1130
|
+
};
|
|
1131
|
+
Interpreter.prototype.createAsyncFunction = function (asyncFunc) {
|
|
1132
|
+
var func = this.createFunctionBase_(asyncFunc.length, true);
|
|
1133
|
+
func.asyncFunc = asyncFunc;
|
|
1134
|
+
asyncFunc.id = this.functionCounter_++;
|
|
1135
|
+
this.setProperty(func, "name", asyncFunc.name, Interpreter.READONLY_NONENUMERABLE_DESCRIPTOR);
|
|
1136
|
+
return func;
|
|
1137
|
+
};
|
|
1138
|
+
Interpreter.prototype.nativeToPseudo = function (nativeObj) {
|
|
1139
|
+
if (nativeObj instanceof Interpreter.Object) {
|
|
1140
|
+
throw Error("Object is already pseudo");
|
|
1141
|
+
}
|
|
1142
|
+
if (nativeObj === null || nativeObj === void 0 || nativeObj === true || nativeObj === false || typeof nativeObj === "string" || typeof nativeObj === "number") {
|
|
1143
|
+
return nativeObj;
|
|
1144
|
+
}
|
|
1145
|
+
if (nativeObj instanceof RegExp) {
|
|
1146
|
+
var pseudoRegexp = this.createObjectProto(this.REGEXP_PROTO);
|
|
1147
|
+
this.populateRegExp(pseudoRegexp, nativeObj);
|
|
1148
|
+
return pseudoRegexp;
|
|
1149
|
+
}
|
|
1150
|
+
if (nativeObj instanceof Date) {
|
|
1151
|
+
var pseudoDate = this.createObjectProto(this.DATE_PROTO);
|
|
1152
|
+
pseudoDate.data = new Date(nativeObj.valueOf());
|
|
1153
|
+
return pseudoDate;
|
|
1154
|
+
}
|
|
1155
|
+
if (typeof nativeObj === "function") {
|
|
1156
|
+
var thisInterpreter = this;
|
|
1157
|
+
var wrapper = function () {
|
|
1158
|
+
var args = Array.prototype.slice.call(arguments).map(function (i2) {
|
|
1159
|
+
return thisInterpreter.pseudoToNative(i2);
|
|
1160
|
+
});
|
|
1161
|
+
var value = nativeObj.apply(thisInterpreter, args);
|
|
1162
|
+
return thisInterpreter.nativeToPseudo(value);
|
|
1163
|
+
};
|
|
1164
|
+
var prototype = Object.getOwnPropertyDescriptor(nativeObj, "prototype");
|
|
1165
|
+
return this.createNativeFunction(wrapper, !!prototype);
|
|
1166
|
+
}
|
|
1167
|
+
if (Array.isArray(nativeObj)) {
|
|
1168
|
+
var pseudoArray = this.createArray();
|
|
1169
|
+
for (var i = 0; i < nativeObj.length; i++) {
|
|
1170
|
+
if (i in nativeObj) {
|
|
1171
|
+
this.setProperty(pseudoArray, i, this.nativeToPseudo(nativeObj[i]));
|
|
1172
|
+
}
|
|
1173
|
+
}
|
|
1174
|
+
return pseudoArray;
|
|
1175
|
+
}
|
|
1176
|
+
var pseudoObj = this.createObjectProto(this.OBJECT_PROTO);
|
|
1177
|
+
for (var key in nativeObj) {
|
|
1178
|
+
this.setProperty(pseudoObj, key, this.nativeToPseudo(nativeObj[key]));
|
|
1179
|
+
}
|
|
1180
|
+
return pseudoObj;
|
|
1181
|
+
};
|
|
1182
|
+
Interpreter.prototype.pseudoToNative = function (pseudoObj, opt_cycles) {
|
|
1183
|
+
if (typeof pseudoObj !== "object" && typeof pseudoObj !== "function" || pseudoObj === null) {
|
|
1184
|
+
return pseudoObj;
|
|
1185
|
+
}
|
|
1186
|
+
if (!(pseudoObj instanceof Interpreter.Object)) {
|
|
1187
|
+
throw Error("Object is not pseudo");
|
|
1188
|
+
}
|
|
1189
|
+
if (this.isa(pseudoObj, this.REGEXP)) {
|
|
1190
|
+
var nativeRegExp = new RegExp(pseudoObj.data.source, pseudoObj.data.flags);
|
|
1191
|
+
nativeRegExp.lastIndex = pseudoObj.data.lastIndex;
|
|
1192
|
+
return nativeRegExp;
|
|
1193
|
+
}
|
|
1194
|
+
if (this.isa(pseudoObj, this.DATE)) {
|
|
1195
|
+
return new Date(pseudoObj.data.valueOf());
|
|
1196
|
+
}
|
|
1197
|
+
var cycles = opt_cycles || {
|
|
1198
|
+
pseudo: [],
|
|
1199
|
+
native: []
|
|
1200
|
+
};
|
|
1201
|
+
var index = cycles.pseudo.indexOf(pseudoObj);
|
|
1202
|
+
if (index !== -1) {
|
|
1203
|
+
return cycles.native[index];
|
|
1204
|
+
}
|
|
1205
|
+
cycles.pseudo.push(pseudoObj);
|
|
1206
|
+
var nativeObj;
|
|
1207
|
+
if (this.isa(pseudoObj, this.ARRAY)) {
|
|
1208
|
+
nativeObj = [];
|
|
1209
|
+
cycles.native.push(nativeObj);
|
|
1210
|
+
var len = this.getProperty(pseudoObj, "length");
|
|
1211
|
+
for (var i = 0; i < len; i++) {
|
|
1212
|
+
if (this.hasProperty(pseudoObj, i)) {
|
|
1213
|
+
nativeObj[i] = this.pseudoToNative(this.getProperty(pseudoObj, i), cycles);
|
|
1214
|
+
}
|
|
1215
|
+
}
|
|
1216
|
+
} else {
|
|
1217
|
+
nativeObj = {};
|
|
1218
|
+
cycles.native.push(nativeObj);
|
|
1219
|
+
var val;
|
|
1220
|
+
for (var key in pseudoObj.properties) {
|
|
1221
|
+
val = this.pseudoToNative(pseudoObj.properties[key], cycles);
|
|
1222
|
+
Object.defineProperty(nativeObj, key, {
|
|
1223
|
+
value: val,
|
|
1224
|
+
writable: true,
|
|
1225
|
+
enumerable: true,
|
|
1226
|
+
configurable: true
|
|
1227
|
+
});
|
|
1228
|
+
}
|
|
1229
|
+
}
|
|
1230
|
+
cycles.pseudo.pop();
|
|
1231
|
+
cycles.native.pop();
|
|
1232
|
+
return nativeObj;
|
|
1233
|
+
};
|
|
1234
|
+
Interpreter.prototype.arrayNativeToPseudo = function (nativeArray) {
|
|
1235
|
+
var pseudoArray = this.createArray();
|
|
1236
|
+
var props = Object.getOwnPropertyNames(nativeArray);
|
|
1237
|
+
for (var i = 0; i < props.length; i++) {
|
|
1238
|
+
this.setProperty(pseudoArray, props[i], nativeArray[props[i]]);
|
|
1239
|
+
}
|
|
1240
|
+
return pseudoArray;
|
|
1241
|
+
};
|
|
1242
|
+
Interpreter.prototype.arrayPseudoToNative = function (pseudoArray) {
|
|
1243
|
+
var nativeArray = [];
|
|
1244
|
+
for (var key in pseudoArray.properties) {
|
|
1245
|
+
nativeArray[key] = this.getProperty(pseudoArray, key);
|
|
1246
|
+
}
|
|
1247
|
+
nativeArray.length = Interpreter.legalArrayLength(this.getProperty(pseudoArray, "length")) || 0;
|
|
1248
|
+
return nativeArray;
|
|
1249
|
+
};
|
|
1250
|
+
Interpreter.prototype.getPrototype = function (value) {
|
|
1251
|
+
switch (typeof value) {
|
|
1252
|
+
case "number":
|
|
1253
|
+
return this.NUMBER.properties["prototype"];
|
|
1254
|
+
case "boolean":
|
|
1255
|
+
return this.BOOLEAN.properties["prototype"];
|
|
1256
|
+
case "string":
|
|
1257
|
+
return this.STRING.properties["prototype"];
|
|
1258
|
+
}
|
|
1259
|
+
if (value) {
|
|
1260
|
+
return value.proto;
|
|
1261
|
+
}
|
|
1262
|
+
return null;
|
|
1263
|
+
};
|
|
1264
|
+
Interpreter.prototype.getProperty = function (obj, name) {
|
|
1265
|
+
if (this.getterStep_) {
|
|
1266
|
+
throw Error("Getter not supported in that context");
|
|
1267
|
+
}
|
|
1268
|
+
name = String(name);
|
|
1269
|
+
if (obj === void 0 || obj === null) {
|
|
1270
|
+
this.throwException(this.TYPE_ERROR, "Cannot read property '" + name + "' of " + obj);
|
|
1271
|
+
}
|
|
1272
|
+
if (typeof obj === "object" && !(obj instanceof Interpreter.Object)) {
|
|
1273
|
+
throw TypeError("Expecting native value or pseudo object");
|
|
1274
|
+
}
|
|
1275
|
+
if (name === "length") {
|
|
1276
|
+
if (this.isa(obj, this.STRING)) {
|
|
1277
|
+
return String(obj).length;
|
|
1278
|
+
}
|
|
1279
|
+
} else if (name.charCodeAt(0) < 64) {
|
|
1280
|
+
if (this.isa(obj, this.STRING)) {
|
|
1281
|
+
var n = Interpreter.legalArrayIndex(name);
|
|
1282
|
+
if (!isNaN(n) && n < String(obj).length) {
|
|
1283
|
+
return String(obj)[n];
|
|
1284
|
+
}
|
|
1285
|
+
}
|
|
1286
|
+
}
|
|
1287
|
+
do {
|
|
1288
|
+
if (obj.properties && name in obj.properties) {
|
|
1289
|
+
var getter = obj.getter[name];
|
|
1290
|
+
if (getter) {
|
|
1291
|
+
this.getterStep_ = true;
|
|
1292
|
+
return getter;
|
|
1293
|
+
}
|
|
1294
|
+
return obj.properties[name];
|
|
1295
|
+
}
|
|
1296
|
+
} while (obj = this.getPrototype(obj));
|
|
1297
|
+
return void 0;
|
|
1298
|
+
};
|
|
1299
|
+
Interpreter.prototype.hasProperty = function (obj, name) {
|
|
1300
|
+
if (!(obj instanceof Interpreter.Object)) {
|
|
1301
|
+
throw TypeError("Primitive data type has no properties");
|
|
1302
|
+
}
|
|
1303
|
+
name = String(name);
|
|
1304
|
+
if (name === "length" && this.isa(obj, this.STRING)) {
|
|
1305
|
+
return true;
|
|
1306
|
+
}
|
|
1307
|
+
if (this.isa(obj, this.STRING)) {
|
|
1308
|
+
var n = Interpreter.legalArrayIndex(name);
|
|
1309
|
+
if (!isNaN(n) && n < String(obj).length) {
|
|
1310
|
+
return true;
|
|
1311
|
+
}
|
|
1312
|
+
}
|
|
1313
|
+
do {
|
|
1314
|
+
if (obj.properties && name in obj.properties) {
|
|
1315
|
+
return true;
|
|
1316
|
+
}
|
|
1317
|
+
} while (obj = this.getPrototype(obj));
|
|
1318
|
+
return false;
|
|
1319
|
+
};
|
|
1320
|
+
Interpreter.prototype.setProperty = function (obj, name, value, opt_descriptor) {
|
|
1321
|
+
if (this.setterStep_) {
|
|
1322
|
+
throw Error("Setter not supported in that context");
|
|
1323
|
+
}
|
|
1324
|
+
name = String(name);
|
|
1325
|
+
if (obj === void 0 || obj === null) {
|
|
1326
|
+
this.throwException(this.TYPE_ERROR, "Cannot set property '" + name + "' of " + obj);
|
|
1327
|
+
}
|
|
1328
|
+
if (typeof obj === "object" && !(obj instanceof Interpreter.Object)) {
|
|
1329
|
+
throw TypeError("Expecting native value or pseudo object");
|
|
1330
|
+
}
|
|
1331
|
+
if (opt_descriptor && ("get" in opt_descriptor || "set" in opt_descriptor) && ("value" in opt_descriptor || "writable" in opt_descriptor)) {
|
|
1332
|
+
this.throwException(this.TYPE_ERROR, "Invalid property descriptor. Cannot both specify accessors and a value or writable attribute");
|
|
1333
|
+
}
|
|
1334
|
+
var strict = !this.stateStack || this.getScope().strict;
|
|
1335
|
+
if (!(obj instanceof Interpreter.Object)) {
|
|
1336
|
+
if (strict) {
|
|
1337
|
+
this.throwException(this.TYPE_ERROR, "Can't create property '" + name + "' on '" + obj + "'");
|
|
1338
|
+
}
|
|
1339
|
+
return;
|
|
1340
|
+
}
|
|
1341
|
+
if (this.isa(obj, this.STRING)) {
|
|
1342
|
+
var n = Interpreter.legalArrayIndex(name);
|
|
1343
|
+
if (name === "length" || !isNaN(n) && n < String(obj).length) {
|
|
1344
|
+
if (strict) {
|
|
1345
|
+
this.throwException(this.TYPE_ERROR, "Cannot assign to read only property '" + name + "' of String '" + obj.data + "'");
|
|
1346
|
+
}
|
|
1347
|
+
return;
|
|
1348
|
+
}
|
|
1349
|
+
}
|
|
1350
|
+
if (obj.class === "Array") {
|
|
1351
|
+
var len = obj.properties.length;
|
|
1352
|
+
var i;
|
|
1353
|
+
if (name === "length") {
|
|
1354
|
+
if (opt_descriptor) {
|
|
1355
|
+
if (!("value" in opt_descriptor)) {
|
|
1356
|
+
return;
|
|
1357
|
+
}
|
|
1358
|
+
value = opt_descriptor["value"];
|
|
1359
|
+
}
|
|
1360
|
+
value = Interpreter.legalArrayLength(value);
|
|
1361
|
+
if (isNaN(value)) {
|
|
1362
|
+
this.throwException(this.RANGE_ERROR, "Invalid array length");
|
|
1363
|
+
}
|
|
1364
|
+
if (value < len) {
|
|
1365
|
+
for (i in obj.properties) {
|
|
1366
|
+
i = Interpreter.legalArrayIndex(i);
|
|
1367
|
+
if (!isNaN(i) && value <= i) {
|
|
1368
|
+
delete obj.properties[i];
|
|
1369
|
+
}
|
|
1370
|
+
}
|
|
1371
|
+
}
|
|
1372
|
+
} else if (!isNaN(i = Interpreter.legalArrayIndex(name))) {
|
|
1373
|
+
obj.properties.length = Math.max(len, i + 1);
|
|
1374
|
+
}
|
|
1375
|
+
}
|
|
1376
|
+
if (obj.preventExtensions && !(name in obj.properties)) {
|
|
1377
|
+
if (strict) {
|
|
1378
|
+
this.throwException(this.TYPE_ERROR, "Can't add property '" + name + "', object is not extensible");
|
|
1379
|
+
}
|
|
1380
|
+
return;
|
|
1381
|
+
}
|
|
1382
|
+
if (opt_descriptor) {
|
|
1383
|
+
var descriptor = {};
|
|
1384
|
+
if ("get" in opt_descriptor && opt_descriptor["get"]) {
|
|
1385
|
+
obj.getter[name] = opt_descriptor["get"];
|
|
1386
|
+
descriptor["get"] = this.setProperty.placeholderGet_;
|
|
1387
|
+
}
|
|
1388
|
+
if ("set" in opt_descriptor && opt_descriptor["set"]) {
|
|
1389
|
+
obj.setter[name] = opt_descriptor["set"];
|
|
1390
|
+
descriptor["set"] = this.setProperty.placeholderSet_;
|
|
1391
|
+
}
|
|
1392
|
+
if ("configurable" in opt_descriptor) {
|
|
1393
|
+
descriptor["configurable"] = opt_descriptor["configurable"];
|
|
1394
|
+
}
|
|
1395
|
+
if ("enumerable" in opt_descriptor) {
|
|
1396
|
+
descriptor["enumerable"] = opt_descriptor["enumerable"];
|
|
1397
|
+
}
|
|
1398
|
+
if ("writable" in opt_descriptor) {
|
|
1399
|
+
descriptor["writable"] = opt_descriptor["writable"];
|
|
1400
|
+
delete obj.getter[name];
|
|
1401
|
+
delete obj.setter[name];
|
|
1402
|
+
}
|
|
1403
|
+
if ("value" in opt_descriptor) {
|
|
1404
|
+
descriptor["value"] = opt_descriptor["value"];
|
|
1405
|
+
delete obj.getter[name];
|
|
1406
|
+
delete obj.setter[name];
|
|
1407
|
+
} else if (value !== Interpreter.VALUE_IN_DESCRIPTOR) {
|
|
1408
|
+
descriptor["value"] = value;
|
|
1409
|
+
delete obj.getter[name];
|
|
1410
|
+
delete obj.setter[name];
|
|
1411
|
+
}
|
|
1412
|
+
try {
|
|
1413
|
+
Object.defineProperty(obj.properties, name, descriptor);
|
|
1414
|
+
} catch (e) {
|
|
1415
|
+
this.throwException(this.TYPE_ERROR, "Cannot redefine property: " + name);
|
|
1416
|
+
}
|
|
1417
|
+
if ("get" in opt_descriptor && !opt_descriptor["get"]) {
|
|
1418
|
+
delete obj.getter[name];
|
|
1419
|
+
}
|
|
1420
|
+
if ("set" in opt_descriptor && !opt_descriptor["set"]) {
|
|
1421
|
+
delete obj.setter[name];
|
|
1422
|
+
}
|
|
1423
|
+
} else {
|
|
1424
|
+
if (value === Interpreter.VALUE_IN_DESCRIPTOR) {
|
|
1425
|
+
throw ReferenceError("Value not specified");
|
|
1426
|
+
}
|
|
1427
|
+
var defObj = obj;
|
|
1428
|
+
while (!(name in defObj.properties)) {
|
|
1429
|
+
defObj = this.getPrototype(defObj);
|
|
1430
|
+
if (!defObj) {
|
|
1431
|
+
defObj = obj;
|
|
1432
|
+
break;
|
|
1433
|
+
}
|
|
1434
|
+
}
|
|
1435
|
+
if (defObj.setter && defObj.setter[name]) {
|
|
1436
|
+
this.setterStep_ = true;
|
|
1437
|
+
return defObj.setter[name];
|
|
1438
|
+
}
|
|
1439
|
+
if (defObj.getter && defObj.getter[name]) {
|
|
1440
|
+
if (strict) {
|
|
1441
|
+
this.throwException(this.TYPE_ERROR, "Cannot set property '" + name + "' of object '" + obj + "' which only has a getter");
|
|
1442
|
+
}
|
|
1443
|
+
} else {
|
|
1444
|
+
try {
|
|
1445
|
+
obj.properties[name] = value;
|
|
1446
|
+
} catch (_e) {
|
|
1447
|
+
if (strict) {
|
|
1448
|
+
this.throwException(this.TYPE_ERROR, "Cannot assign to read only property '" + name + "' of object '" + obj + "'");
|
|
1449
|
+
}
|
|
1450
|
+
}
|
|
1451
|
+
}
|
|
1452
|
+
}
|
|
1453
|
+
};
|
|
1454
|
+
Interpreter.prototype.setProperty.placeholderGet_ = function () {
|
|
1455
|
+
throw Error("Placeholder getter");
|
|
1456
|
+
};
|
|
1457
|
+
Interpreter.prototype.setProperty.placeholderSet_ = function () {
|
|
1458
|
+
throw Error("Placeholder setter");
|
|
1459
|
+
};
|
|
1460
|
+
Interpreter.prototype.setNativeFunctionPrototype = function (obj, name, wrapper) {
|
|
1461
|
+
this.setProperty(obj.properties["prototype"], name, this.createNativeFunction(wrapper, false), Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
1462
|
+
};
|
|
1463
|
+
Interpreter.prototype.setAsyncFunctionPrototype = function (obj, name, wrapper) {
|
|
1464
|
+
this.setProperty(obj.properties["prototype"], name, this.createAsyncFunction(wrapper), Interpreter.NONENUMERABLE_DESCRIPTOR);
|
|
1465
|
+
};
|
|
1466
|
+
Interpreter.prototype.getScope = function () {
|
|
1467
|
+
var scope = this.stateStack[this.stateStack.length - 1].scope;
|
|
1468
|
+
if (!scope) {
|
|
1469
|
+
throw Error("No scope found");
|
|
1470
|
+
}
|
|
1471
|
+
return scope;
|
|
1472
|
+
};
|
|
1473
|
+
Interpreter.prototype.createScope = function (node, parentScope) {
|
|
1474
|
+
var strict = false;
|
|
1475
|
+
if (parentScope && parentScope.strict) {
|
|
1476
|
+
strict = true;
|
|
1477
|
+
} else {
|
|
1478
|
+
var firstNode = node.body && node.body[0];
|
|
1479
|
+
if (firstNode && firstNode.expression && firstNode.expression.type === "Literal" && firstNode.expression.value === "use strict") {
|
|
1480
|
+
strict = true;
|
|
1481
|
+
}
|
|
1482
|
+
}
|
|
1483
|
+
var object = this.createObjectProto(null);
|
|
1484
|
+
var scope = new Interpreter.Scope(parentScope, strict, object);
|
|
1485
|
+
if (!parentScope) {
|
|
1486
|
+
this.initGlobal(scope.object);
|
|
1487
|
+
}
|
|
1488
|
+
this.populateScope_(node, scope);
|
|
1489
|
+
return scope;
|
|
1490
|
+
};
|
|
1491
|
+
Interpreter.prototype.createSpecialScope = function (parentScope, opt_object) {
|
|
1492
|
+
if (!parentScope) {
|
|
1493
|
+
throw Error("parentScope required");
|
|
1494
|
+
}
|
|
1495
|
+
var object = opt_object || this.createObjectProto(null);
|
|
1496
|
+
return new Interpreter.Scope(parentScope, parentScope.strict, object);
|
|
1497
|
+
};
|
|
1498
|
+
Interpreter.prototype.getValueFromScope = function (name) {
|
|
1499
|
+
var scope = this.getScope();
|
|
1500
|
+
while (scope && scope !== this.globalScope) {
|
|
1501
|
+
if (name in scope.object.properties) {
|
|
1502
|
+
return scope.object.properties[name];
|
|
1503
|
+
}
|
|
1504
|
+
scope = scope.parentScope;
|
|
1505
|
+
}
|
|
1506
|
+
if (scope === this.globalScope && this.hasProperty(scope.object, name)) {
|
|
1507
|
+
return this.getProperty(scope.object, name);
|
|
1508
|
+
}
|
|
1509
|
+
var prevNode = this.stateStack[this.stateStack.length - 1].node;
|
|
1510
|
+
if (prevNode.type === "UnaryExpression" && prevNode.operator === "typeof") {
|
|
1511
|
+
return void 0;
|
|
1512
|
+
}
|
|
1513
|
+
this.throwException(this.REFERENCE_ERROR, name + " is not defined");
|
|
1514
|
+
};
|
|
1515
|
+
Interpreter.prototype.setValueToScope = function (name, value) {
|
|
1516
|
+
var scope = this.getScope();
|
|
1517
|
+
var strict = scope.strict;
|
|
1518
|
+
while (scope && scope !== this.globalScope) {
|
|
1519
|
+
if (name in scope.object.properties) {
|
|
1520
|
+
try {
|
|
1521
|
+
scope.object.properties[name] = value;
|
|
1522
|
+
} catch (_e) {
|
|
1523
|
+
if (strict) {
|
|
1524
|
+
this.throwException(this.TYPE_ERROR, "Cannot assign to read only variable '" + name + "'");
|
|
1525
|
+
}
|
|
1526
|
+
}
|
|
1527
|
+
return void 0;
|
|
1528
|
+
}
|
|
1529
|
+
scope = scope.parentScope;
|
|
1530
|
+
}
|
|
1531
|
+
if (scope === this.globalScope && (!strict || this.hasProperty(scope.object, name))) {
|
|
1532
|
+
return this.setProperty(scope.object, name, value);
|
|
1533
|
+
}
|
|
1534
|
+
this.throwException(this.REFERENCE_ERROR, name + " is not defined");
|
|
1535
|
+
};
|
|
1536
|
+
Interpreter.prototype.populateScope_ = function (node, scope) {
|
|
1537
|
+
var variableCache;
|
|
1538
|
+
if (!node.variableCache_) {
|
|
1539
|
+
variableCache = Object.create(null);
|
|
1540
|
+
switch (node.type) {
|
|
1541
|
+
case "VariableDeclaration":
|
|
1542
|
+
for (var i = 0; i < node.declarations.length; i++) {
|
|
1543
|
+
variableCache[node.declarations[i].id.name] = true;
|
|
1544
|
+
}
|
|
1545
|
+
break;
|
|
1546
|
+
case "FunctionDeclaration":
|
|
1547
|
+
variableCache[node.id.name] = node;
|
|
1548
|
+
break;
|
|
1549
|
+
case "BlockStatement":
|
|
1550
|
+
case "CatchClause":
|
|
1551
|
+
case "DoWhileStatement":
|
|
1552
|
+
case "ForInStatement":
|
|
1553
|
+
case "ForStatement":
|
|
1554
|
+
case "IfStatement":
|
|
1555
|
+
case "LabeledStatement":
|
|
1556
|
+
case "Program":
|
|
1557
|
+
case "SwitchCase":
|
|
1558
|
+
case "SwitchStatement":
|
|
1559
|
+
case "TryStatement":
|
|
1560
|
+
case "WithStatement":
|
|
1561
|
+
case "WhileStatement":
|
|
1562
|
+
var nodeClass = node.constructor;
|
|
1563
|
+
for (var name in node) {
|
|
1564
|
+
if (name === "loc") continue;
|
|
1565
|
+
var prop = node[name];
|
|
1566
|
+
if (prop && typeof prop === "object") {
|
|
1567
|
+
var childCache;
|
|
1568
|
+
if (Array.isArray(prop)) {
|
|
1569
|
+
for (var i = 0; i < prop.length; i++) {
|
|
1570
|
+
if (prop[i] && prop[i].constructor === nodeClass) {
|
|
1571
|
+
childCache = this.populateScope_(prop[i], scope);
|
|
1572
|
+
for (var name in childCache) {
|
|
1573
|
+
variableCache[name] = childCache[name];
|
|
1574
|
+
}
|
|
1575
|
+
}
|
|
1576
|
+
}
|
|
1577
|
+
} else {
|
|
1578
|
+
if (prop.constructor === nodeClass) {
|
|
1579
|
+
childCache = this.populateScope_(prop, scope);
|
|
1580
|
+
for (var name in childCache) {
|
|
1581
|
+
variableCache[name] = childCache[name];
|
|
1582
|
+
}
|
|
1583
|
+
}
|
|
1584
|
+
}
|
|
1585
|
+
}
|
|
1586
|
+
}
|
|
1587
|
+
}
|
|
1588
|
+
node.variableCache_ = variableCache;
|
|
1589
|
+
} else {
|
|
1590
|
+
variableCache = node.variableCache_;
|
|
1591
|
+
}
|
|
1592
|
+
for (var name in variableCache) {
|
|
1593
|
+
if (variableCache[name] === true) {
|
|
1594
|
+
this.setProperty(scope.object, name, void 0, Interpreter.VARIABLE_DESCRIPTOR);
|
|
1595
|
+
} else {
|
|
1596
|
+
this.setProperty(scope.object, name, this.createFunction(variableCache[name], scope), Interpreter.VARIABLE_DESCRIPTOR);
|
|
1597
|
+
}
|
|
1598
|
+
}
|
|
1599
|
+
return variableCache;
|
|
1600
|
+
};
|
|
1601
|
+
Interpreter.prototype.calledWithNew = function () {
|
|
1602
|
+
return this.stateStack[this.stateStack.length - 1].isConstructor;
|
|
1603
|
+
};
|
|
1604
|
+
Interpreter.prototype.getValue = function (ref) {
|
|
1605
|
+
if (ref[0] === Interpreter.SCOPE_REFERENCE) {
|
|
1606
|
+
return this.getValueFromScope(ref[1]);
|
|
1607
|
+
} else {
|
|
1608
|
+
return this.getProperty(ref[0], ref[1]);
|
|
1609
|
+
}
|
|
1610
|
+
};
|
|
1611
|
+
Interpreter.prototype.setValue = function (ref, value) {
|
|
1612
|
+
if (ref[0] === Interpreter.SCOPE_REFERENCE) {
|
|
1613
|
+
return this.setValueToScope(ref[1], value);
|
|
1614
|
+
}
|
|
1615
|
+
return this.setProperty(ref[0], ref[1], value);
|
|
1616
|
+
};
|
|
1617
|
+
Interpreter.prototype.throwException = function (errorClass, opt_message) {
|
|
1618
|
+
if (!this.globalScope) {
|
|
1619
|
+
throw opt_message === void 0 ? errorClass : opt_message;
|
|
1620
|
+
}
|
|
1621
|
+
if (opt_message === void 0 || !(errorClass instanceof Interpreter.Object)) {
|
|
1622
|
+
var error = errorClass;
|
|
1623
|
+
} else {
|
|
1624
|
+
var error = this.createObject(errorClass);
|
|
1625
|
+
this.populateError(error, opt_message);
|
|
1626
|
+
}
|
|
1627
|
+
this.unwind(Interpreter.Completion.THROW, error, void 0);
|
|
1628
|
+
throw Interpreter.STEP_ERROR;
|
|
1629
|
+
};
|
|
1630
|
+
Interpreter.prototype.unwind = function (type, value, label) {
|
|
1631
|
+
if (type === Interpreter.Completion.NORMAL) {
|
|
1632
|
+
throw TypeError("Should not unwind for NORMAL completions");
|
|
1633
|
+
}
|
|
1634
|
+
loop: for (var stack = this.stateStack; stack.length > 0; stack.pop()) {
|
|
1635
|
+
var state = stack[stack.length - 1];
|
|
1636
|
+
switch (state.node.type) {
|
|
1637
|
+
case "TryStatement":
|
|
1638
|
+
state.cv = {
|
|
1639
|
+
type,
|
|
1640
|
+
value,
|
|
1641
|
+
label
|
|
1642
|
+
};
|
|
1643
|
+
return;
|
|
1644
|
+
case "CallExpression":
|
|
1645
|
+
case "NewExpression":
|
|
1646
|
+
if (type === Interpreter.Completion.RETURN) {
|
|
1647
|
+
state.value = value;
|
|
1648
|
+
return;
|
|
1649
|
+
} else if (type !== Interpreter.Completion.THROW) {
|
|
1650
|
+
throw Error("Unsynatctic break/continue not rejected by Acorn");
|
|
1651
|
+
}
|
|
1652
|
+
break;
|
|
1653
|
+
case "Program":
|
|
1654
|
+
state.done = true;
|
|
1655
|
+
break loop;
|
|
1656
|
+
}
|
|
1657
|
+
if (type === Interpreter.Completion.BREAK) {
|
|
1658
|
+
if (label ? state.labels && state.labels.indexOf(label) !== -1 : state.isLoop || state.isSwitch) {
|
|
1659
|
+
stack.pop();
|
|
1660
|
+
return;
|
|
1661
|
+
}
|
|
1662
|
+
} else if (type === Interpreter.Completion.CONTINUE) {
|
|
1663
|
+
if (label ? state.labels && state.labels.indexOf(label) !== -1 : state.isLoop) {
|
|
1664
|
+
return;
|
|
1665
|
+
}
|
|
1666
|
+
}
|
|
1667
|
+
}
|
|
1668
|
+
var realError;
|
|
1669
|
+
if (this.isa(value, this.ERROR)) {
|
|
1670
|
+
var errorTable = {
|
|
1671
|
+
EvalError,
|
|
1672
|
+
RangeError,
|
|
1673
|
+
ReferenceError,
|
|
1674
|
+
SyntaxError,
|
|
1675
|
+
TypeError,
|
|
1676
|
+
URIError
|
|
1677
|
+
};
|
|
1678
|
+
var name = String(this.getProperty(value, "name"));
|
|
1679
|
+
var message = this.getProperty(value, "message").valueOf();
|
|
1680
|
+
var errorConstructor = errorTable[name] || Error;
|
|
1681
|
+
realError = errorConstructor(message);
|
|
1682
|
+
realError.stack = String(this.getProperty(value, "stack"));
|
|
1683
|
+
} else {
|
|
1684
|
+
realError = String(value);
|
|
1685
|
+
}
|
|
1686
|
+
this.value = realError;
|
|
1687
|
+
throw realError;
|
|
1688
|
+
};
|
|
1689
|
+
Interpreter.prototype.nodeSummary = function (node) {
|
|
1690
|
+
switch (node.type) {
|
|
1691
|
+
case "ArrayExpression":
|
|
1692
|
+
return "[...]";
|
|
1693
|
+
case "BinaryExpression":
|
|
1694
|
+
case "LogicalExpression":
|
|
1695
|
+
return this.nodeSummary(node.left) + " " + node.operator + " " + this.nodeSummary(node.right);
|
|
1696
|
+
case "CallExpression":
|
|
1697
|
+
return this.nodeSummary(node.callee) + "(...)";
|
|
1698
|
+
case "ConditionalExpression":
|
|
1699
|
+
return this.nodeSummary(node.test) + " ? " + this.nodeSummary(node.consequent) + " : " + this.nodeSummary(node.alternate);
|
|
1700
|
+
case "Identifier":
|
|
1701
|
+
return node.name;
|
|
1702
|
+
case "Literal":
|
|
1703
|
+
return node.raw;
|
|
1704
|
+
case "MemberExpression":
|
|
1705
|
+
var obj = this.nodeSummary(node.object);
|
|
1706
|
+
var prop = this.nodeSummary(node.property);
|
|
1707
|
+
return node.computed ? obj + "[" + prop + "]" : obj + "." + prop;
|
|
1708
|
+
case "NewExpression":
|
|
1709
|
+
return "new " + this.nodeSummary(node.callee) + "(...)";
|
|
1710
|
+
case "ObjectExpression":
|
|
1711
|
+
return "{...}";
|
|
1712
|
+
case "ThisExpression":
|
|
1713
|
+
return "this";
|
|
1714
|
+
case "UnaryExpression":
|
|
1715
|
+
return node.operator + " " + this.nodeSummary(node.argument);
|
|
1716
|
+
case "UpdateExpression":
|
|
1717
|
+
var argument = this.nodeSummary(node.argument);
|
|
1718
|
+
return node.prefix ? node.operator + argument : argument + node.operator;
|
|
1719
|
+
}
|
|
1720
|
+
return "???";
|
|
1721
|
+
};
|
|
1722
|
+
Interpreter.prototype.createGetter_ = function (func, left) {
|
|
1723
|
+
if (!this.getterStep_) {
|
|
1724
|
+
throw Error("Unexpected call to createGetter");
|
|
1725
|
+
}
|
|
1726
|
+
this.getterStep_ = false;
|
|
1727
|
+
var funcThis = Array.isArray(left) ? left[0] : left;
|
|
1728
|
+
var node = this.newNode();
|
|
1729
|
+
node.type = "CallExpression";
|
|
1730
|
+
var state = new Interpreter.State(node, this.stateStack[this.stateStack.length - 1].scope);
|
|
1731
|
+
state.doneCallee_ = 2;
|
|
1732
|
+
state.funcThis_ = funcThis;
|
|
1733
|
+
state.func_ = func;
|
|
1734
|
+
state.doneArgs_ = true;
|
|
1735
|
+
state.arguments_ = [];
|
|
1736
|
+
return state;
|
|
1737
|
+
};
|
|
1738
|
+
Interpreter.prototype.createSetter_ = function (func, left, value) {
|
|
1739
|
+
if (!this.setterStep_) {
|
|
1740
|
+
throw Error("Unexpected call to createSetter");
|
|
1741
|
+
}
|
|
1742
|
+
this.setterStep_ = false;
|
|
1743
|
+
var funcThis = Array.isArray(left) ? left[0] : this.globalObject;
|
|
1744
|
+
var node = this.newNode();
|
|
1745
|
+
node.type = "CallExpression";
|
|
1746
|
+
var state = new Interpreter.State(node, this.stateStack[this.stateStack.length - 1].scope);
|
|
1747
|
+
state.doneCallee_ = 2;
|
|
1748
|
+
state.funcThis_ = funcThis;
|
|
1749
|
+
state.func_ = func;
|
|
1750
|
+
state.doneArgs_ = true;
|
|
1751
|
+
state.arguments_ = [value];
|
|
1752
|
+
return state;
|
|
1753
|
+
};
|
|
1754
|
+
Interpreter.prototype.boxThis_ = function (value) {
|
|
1755
|
+
if (value === void 0 || value === null) {
|
|
1756
|
+
return this.globalObject;
|
|
1757
|
+
}
|
|
1758
|
+
if (!(value instanceof Interpreter.Object)) {
|
|
1759
|
+
var box = this.createObjectProto(this.getPrototype(value));
|
|
1760
|
+
box.data = value;
|
|
1761
|
+
return box;
|
|
1762
|
+
}
|
|
1763
|
+
return value;
|
|
1764
|
+
};
|
|
1765
|
+
Interpreter.prototype.getGlobalScope = function () {
|
|
1766
|
+
return this.globalScope;
|
|
1767
|
+
};
|
|
1768
|
+
Interpreter.prototype.getStateStack = function () {
|
|
1769
|
+
return this.stateStack;
|
|
1770
|
+
};
|
|
1771
|
+
Interpreter.prototype.setStateStack = function (newStack) {
|
|
1772
|
+
this.stateStack = newStack;
|
|
1773
|
+
};
|
|
1774
|
+
Interpreter.Value;
|
|
1775
|
+
Interpreter.State = function (node, scope) {
|
|
1776
|
+
this.node = node;
|
|
1777
|
+
this.scope = scope;
|
|
1778
|
+
};
|
|
1779
|
+
Interpreter.Scope = function (parentScope, strict, object) {
|
|
1780
|
+
this.parentScope = parentScope;
|
|
1781
|
+
this.strict = strict;
|
|
1782
|
+
this.object = object;
|
|
1783
|
+
};
|
|
1784
|
+
Interpreter.Object = function (proto) {
|
|
1785
|
+
this.getter = Object.create(null);
|
|
1786
|
+
this.setter = Object.create(null);
|
|
1787
|
+
this.properties = Object.create(null);
|
|
1788
|
+
this.proto = proto;
|
|
1789
|
+
};
|
|
1790
|
+
Interpreter.Object.prototype.proto = null;
|
|
1791
|
+
Interpreter.Object.prototype.class = "Object";
|
|
1792
|
+
Interpreter.Object.prototype.data = null;
|
|
1793
|
+
Interpreter.Object.prototype.toString = function () {
|
|
1794
|
+
if (!Interpreter.currentInterpreter_) {
|
|
1795
|
+
return "[object Interpreter.Object]";
|
|
1796
|
+
}
|
|
1797
|
+
if (!(this instanceof Interpreter.Object)) {
|
|
1798
|
+
return String(this);
|
|
1799
|
+
}
|
|
1800
|
+
if (this.class === "Array") {
|
|
1801
|
+
var cycles = Interpreter.toStringCycles_;
|
|
1802
|
+
cycles.push(this);
|
|
1803
|
+
try {
|
|
1804
|
+
var strs = [];
|
|
1805
|
+
var maxLength = this.properties.length;
|
|
1806
|
+
var truncated = false;
|
|
1807
|
+
if (maxLength > 1024) {
|
|
1808
|
+
maxLength = 1e3;
|
|
1809
|
+
truncated = true;
|
|
1810
|
+
}
|
|
1811
|
+
for (var i = 0; i < maxLength; i++) {
|
|
1812
|
+
var value = this.properties[i];
|
|
1813
|
+
strs[i] = value instanceof Interpreter.Object && cycles.indexOf(value) !== -1 ? "..." : value;
|
|
1814
|
+
}
|
|
1815
|
+
if (truncated) {
|
|
1816
|
+
strs.push("...");
|
|
1817
|
+
}
|
|
1818
|
+
} finally {
|
|
1819
|
+
cycles.pop();
|
|
1820
|
+
}
|
|
1821
|
+
return strs.join(",");
|
|
1822
|
+
}
|
|
1823
|
+
if (this.class === "Error") {
|
|
1824
|
+
var cycles = Interpreter.toStringCycles_;
|
|
1825
|
+
if (cycles.indexOf(this) !== -1) {
|
|
1826
|
+
return "[object Error]";
|
|
1827
|
+
}
|
|
1828
|
+
var name, message;
|
|
1829
|
+
var obj = this;
|
|
1830
|
+
do {
|
|
1831
|
+
if ("name" in obj.properties) {
|
|
1832
|
+
name = obj.properties.name;
|
|
1833
|
+
break;
|
|
1834
|
+
}
|
|
1835
|
+
} while (obj = obj.proto);
|
|
1836
|
+
obj = this;
|
|
1837
|
+
do {
|
|
1838
|
+
if ("message" in obj.properties) {
|
|
1839
|
+
message = obj.properties.message;
|
|
1840
|
+
break;
|
|
1841
|
+
}
|
|
1842
|
+
} while (obj = obj.proto);
|
|
1843
|
+
cycles.push(this);
|
|
1844
|
+
try {
|
|
1845
|
+
name = name && String(name);
|
|
1846
|
+
message = message && String(message);
|
|
1847
|
+
} finally {
|
|
1848
|
+
cycles.pop();
|
|
1849
|
+
}
|
|
1850
|
+
return message ? name + ": " + message : String(name);
|
|
1851
|
+
}
|
|
1852
|
+
if (this.data !== null) {
|
|
1853
|
+
return String(this.data);
|
|
1854
|
+
}
|
|
1855
|
+
return "[object " + this.class + "]";
|
|
1856
|
+
};
|
|
1857
|
+
Interpreter.Object.prototype.valueOf = function () {
|
|
1858
|
+
if (!Interpreter.currentInterpreter_) {
|
|
1859
|
+
return this;
|
|
1860
|
+
}
|
|
1861
|
+
if (this.data === void 0 || this.data === null || this.data instanceof RegExp) {
|
|
1862
|
+
return this;
|
|
1863
|
+
}
|
|
1864
|
+
if (this.data instanceof Date) {
|
|
1865
|
+
return this.data.valueOf();
|
|
1866
|
+
}
|
|
1867
|
+
return this.data;
|
|
1868
|
+
};
|
|
1869
|
+
Interpreter.prototype["stepArrayExpression"] = function (stack, state, node) {
|
|
1870
|
+
var elements = node.elements;
|
|
1871
|
+
var n = state.n_ || 0;
|
|
1872
|
+
if (!state.array_) {
|
|
1873
|
+
state.array_ = this.createArray();
|
|
1874
|
+
state.array_.properties.length = elements.length;
|
|
1875
|
+
} else {
|
|
1876
|
+
this.setProperty(state.array_, n, state.value);
|
|
1877
|
+
n++;
|
|
1878
|
+
}
|
|
1879
|
+
while (n < elements.length) {
|
|
1880
|
+
if (elements[n]) {
|
|
1881
|
+
state.n_ = n;
|
|
1882
|
+
return new Interpreter.State(elements[n], state.scope);
|
|
1883
|
+
}
|
|
1884
|
+
n++;
|
|
1885
|
+
}
|
|
1886
|
+
stack.pop();
|
|
1887
|
+
stack[stack.length - 1].value = state.array_;
|
|
1888
|
+
};
|
|
1889
|
+
Interpreter.prototype["stepAssignmentExpression"] = function (stack, state, node) {
|
|
1890
|
+
if (!state.doneLeft_) {
|
|
1891
|
+
state.doneLeft_ = true;
|
|
1892
|
+
var nextState = new Interpreter.State(node.left, state.scope);
|
|
1893
|
+
nextState.components = true;
|
|
1894
|
+
return nextState;
|
|
1895
|
+
}
|
|
1896
|
+
if (!state.doneRight_) {
|
|
1897
|
+
if (!state.leftReference_) {
|
|
1898
|
+
state.leftReference_ = state.value;
|
|
1899
|
+
}
|
|
1900
|
+
if (state.doneGetter_) {
|
|
1901
|
+
state.leftValue_ = state.value;
|
|
1902
|
+
}
|
|
1903
|
+
if (!state.doneGetter_ && node.operator !== "=") {
|
|
1904
|
+
var leftValue = this.getValue(state.leftReference_);
|
|
1905
|
+
state.leftValue_ = leftValue;
|
|
1906
|
+
if (this.getterStep_) {
|
|
1907
|
+
state.doneGetter_ = true;
|
|
1908
|
+
var func = leftValue;
|
|
1909
|
+
return this.createGetter_(func, state.leftReference_);
|
|
1910
|
+
}
|
|
1911
|
+
}
|
|
1912
|
+
state.doneRight_ = true;
|
|
1913
|
+
if (node.operator === "=" && node.left.type === "Identifier") {
|
|
1914
|
+
state.destinationName = node.left.name;
|
|
1915
|
+
}
|
|
1916
|
+
return new Interpreter.State(node.right, state.scope);
|
|
1917
|
+
}
|
|
1918
|
+
if (state.doneSetter_) {
|
|
1919
|
+
stack.pop();
|
|
1920
|
+
stack[stack.length - 1].value = state.setterValue_;
|
|
1921
|
+
return;
|
|
1922
|
+
}
|
|
1923
|
+
var value = state.leftValue_;
|
|
1924
|
+
var rightValue = state.value;
|
|
1925
|
+
switch (node.operator) {
|
|
1926
|
+
case "=":
|
|
1927
|
+
value = rightValue;
|
|
1928
|
+
break;
|
|
1929
|
+
case "+=":
|
|
1930
|
+
value += rightValue;
|
|
1931
|
+
break;
|
|
1932
|
+
case "-=":
|
|
1933
|
+
value -= rightValue;
|
|
1934
|
+
break;
|
|
1935
|
+
case "*=":
|
|
1936
|
+
value *= rightValue;
|
|
1937
|
+
break;
|
|
1938
|
+
case "/=":
|
|
1939
|
+
value /= rightValue;
|
|
1940
|
+
break;
|
|
1941
|
+
case "%=":
|
|
1942
|
+
value %= rightValue;
|
|
1943
|
+
break;
|
|
1944
|
+
case "<<=":
|
|
1945
|
+
value <<= rightValue;
|
|
1946
|
+
break;
|
|
1947
|
+
case ">>=":
|
|
1948
|
+
value >>= rightValue;
|
|
1949
|
+
break;
|
|
1950
|
+
case ">>>=":
|
|
1951
|
+
value >>>= rightValue;
|
|
1952
|
+
break;
|
|
1953
|
+
case "&=":
|
|
1954
|
+
value &= rightValue;
|
|
1955
|
+
break;
|
|
1956
|
+
case "^=":
|
|
1957
|
+
value ^= rightValue;
|
|
1958
|
+
break;
|
|
1959
|
+
case "|=":
|
|
1960
|
+
value |= rightValue;
|
|
1961
|
+
break;
|
|
1962
|
+
default:
|
|
1963
|
+
throw SyntaxError("Unknown assignment expression: " + node.operator);
|
|
1964
|
+
}
|
|
1965
|
+
var setter = this.setValue(state.leftReference_, value);
|
|
1966
|
+
if (setter) {
|
|
1967
|
+
state.doneSetter_ = true;
|
|
1968
|
+
state.setterValue_ = value;
|
|
1969
|
+
return this.createSetter_(setter, state.leftReference_, value);
|
|
1970
|
+
}
|
|
1971
|
+
stack.pop();
|
|
1972
|
+
stack[stack.length - 1].value = value;
|
|
1973
|
+
};
|
|
1974
|
+
Interpreter.prototype["stepBinaryExpression"] = function (stack, state, node) {
|
|
1975
|
+
if (!state.doneLeft_) {
|
|
1976
|
+
state.doneLeft_ = true;
|
|
1977
|
+
return new Interpreter.State(node.left, state.scope);
|
|
1978
|
+
}
|
|
1979
|
+
if (!state.doneRight_) {
|
|
1980
|
+
state.doneRight_ = true;
|
|
1981
|
+
state.leftValue_ = state.value;
|
|
1982
|
+
return new Interpreter.State(node.right, state.scope);
|
|
1983
|
+
}
|
|
1984
|
+
stack.pop();
|
|
1985
|
+
var leftValue = state.leftValue_;
|
|
1986
|
+
var rightValue = state.value;
|
|
1987
|
+
var value;
|
|
1988
|
+
switch (node.operator) {
|
|
1989
|
+
case "==":
|
|
1990
|
+
value = leftValue == rightValue;
|
|
1991
|
+
break;
|
|
1992
|
+
case "!=":
|
|
1993
|
+
value = leftValue != rightValue;
|
|
1994
|
+
break;
|
|
1995
|
+
case "===":
|
|
1996
|
+
value = leftValue === rightValue;
|
|
1997
|
+
break;
|
|
1998
|
+
case "!==":
|
|
1999
|
+
value = leftValue !== rightValue;
|
|
2000
|
+
break;
|
|
2001
|
+
case ">":
|
|
2002
|
+
value = leftValue > rightValue;
|
|
2003
|
+
break;
|
|
2004
|
+
case ">=":
|
|
2005
|
+
value = leftValue >= rightValue;
|
|
2006
|
+
break;
|
|
2007
|
+
case "<":
|
|
2008
|
+
value = leftValue < rightValue;
|
|
2009
|
+
break;
|
|
2010
|
+
case "<=":
|
|
2011
|
+
value = leftValue <= rightValue;
|
|
2012
|
+
break;
|
|
2013
|
+
case "+":
|
|
2014
|
+
value = leftValue + rightValue;
|
|
2015
|
+
break;
|
|
2016
|
+
case "-":
|
|
2017
|
+
value = leftValue - rightValue;
|
|
2018
|
+
break;
|
|
2019
|
+
case "*":
|
|
2020
|
+
value = leftValue * rightValue;
|
|
2021
|
+
break;
|
|
2022
|
+
case "/":
|
|
2023
|
+
value = leftValue / rightValue;
|
|
2024
|
+
break;
|
|
2025
|
+
case "%":
|
|
2026
|
+
value = leftValue % rightValue;
|
|
2027
|
+
break;
|
|
2028
|
+
case "&":
|
|
2029
|
+
value = leftValue & rightValue;
|
|
2030
|
+
break;
|
|
2031
|
+
case "|":
|
|
2032
|
+
value = leftValue | rightValue;
|
|
2033
|
+
break;
|
|
2034
|
+
case "^":
|
|
2035
|
+
value = leftValue ^ rightValue;
|
|
2036
|
+
break;
|
|
2037
|
+
case "<<":
|
|
2038
|
+
value = leftValue << rightValue;
|
|
2039
|
+
break;
|
|
2040
|
+
case ">>":
|
|
2041
|
+
value = leftValue >> rightValue;
|
|
2042
|
+
break;
|
|
2043
|
+
case ">>>":
|
|
2044
|
+
value = leftValue >>> rightValue;
|
|
2045
|
+
break;
|
|
2046
|
+
case "in":
|
|
2047
|
+
if (!(rightValue instanceof Interpreter.Object)) {
|
|
2048
|
+
this.throwException(this.TYPE_ERROR, "'in' expects an object, not '" + rightValue + "'");
|
|
2049
|
+
}
|
|
2050
|
+
value = this.hasProperty(rightValue, leftValue);
|
|
2051
|
+
break;
|
|
2052
|
+
case "instanceof":
|
|
2053
|
+
if (!this.isa(rightValue, this.FUNCTION)) {
|
|
2054
|
+
this.throwException(this.TYPE_ERROR, "'instanceof' expects an object, not '" + rightValue + "'");
|
|
2055
|
+
}
|
|
2056
|
+
value = leftValue instanceof Interpreter.Object ? this.isa(leftValue, rightValue) : false;
|
|
2057
|
+
break;
|
|
2058
|
+
default:
|
|
2059
|
+
throw SyntaxError("Unknown binary operator: " + node.operator);
|
|
2060
|
+
}
|
|
2061
|
+
stack[stack.length - 1].value = value;
|
|
2062
|
+
};
|
|
2063
|
+
Interpreter.prototype["stepBlockStatement"] = function (stack, state, node) {
|
|
2064
|
+
var n = state.n_ || 0;
|
|
2065
|
+
var expression = node.body[n];
|
|
2066
|
+
if (expression) {
|
|
2067
|
+
state.n_ = n + 1;
|
|
2068
|
+
return new Interpreter.State(expression, state.scope);
|
|
2069
|
+
}
|
|
2070
|
+
stack.pop();
|
|
2071
|
+
};
|
|
2072
|
+
Interpreter.prototype["stepBreakStatement"] = function (stack, state, node) {
|
|
2073
|
+
var label = node.label && node.label.name;
|
|
2074
|
+
this.unwind(Interpreter.Completion.BREAK, void 0, label);
|
|
2075
|
+
};
|
|
2076
|
+
Interpreter.prototype.evalCodeNumber_ = 0;
|
|
2077
|
+
Interpreter.prototype["stepCallExpression"] = function (stack, state, node) {
|
|
2078
|
+
if (!state.doneCallee_) {
|
|
2079
|
+
state.doneCallee_ = 1;
|
|
2080
|
+
var nextState = new Interpreter.State(node.callee, state.scope);
|
|
2081
|
+
nextState.components = true;
|
|
2082
|
+
return nextState;
|
|
2083
|
+
}
|
|
2084
|
+
if (state.doneCallee_ === 1) {
|
|
2085
|
+
state.doneCallee_ = 2;
|
|
2086
|
+
var func = state.value;
|
|
2087
|
+
if (Array.isArray(func)) {
|
|
2088
|
+
state.func_ = this.getValue(func);
|
|
2089
|
+
if (func[0] === Interpreter.SCOPE_REFERENCE) {
|
|
2090
|
+
state.directEval_ = func[1] === "eval";
|
|
2091
|
+
} else {
|
|
2092
|
+
state.funcThis_ = func[0];
|
|
2093
|
+
}
|
|
2094
|
+
func = state.func_;
|
|
2095
|
+
if (this.getterStep_) {
|
|
2096
|
+
state.doneCallee_ = 1;
|
|
2097
|
+
return this.createGetter_(func, state.value);
|
|
2098
|
+
}
|
|
2099
|
+
} else {
|
|
2100
|
+
state.func_ = func;
|
|
2101
|
+
}
|
|
2102
|
+
state.arguments_ = [];
|
|
2103
|
+
state.n_ = 0;
|
|
2104
|
+
}
|
|
2105
|
+
var func = state.func_;
|
|
2106
|
+
if (!state.doneArgs_) {
|
|
2107
|
+
if (state.n_ !== 0) {
|
|
2108
|
+
state.arguments_.push(state.value);
|
|
2109
|
+
}
|
|
2110
|
+
if (node.arguments[state.n_]) {
|
|
2111
|
+
return new Interpreter.State(node.arguments[state.n_++], state.scope);
|
|
2112
|
+
}
|
|
2113
|
+
if (node.type === "NewExpression") {
|
|
2114
|
+
if (!(func instanceof Interpreter.Object) || func.illegalConstructor) {
|
|
2115
|
+
this.throwException(this.TYPE_ERROR, this.nodeSummary(node.callee) + " is not a constructor");
|
|
2116
|
+
}
|
|
2117
|
+
if (func === this.ARRAY) {
|
|
2118
|
+
state.funcThis_ = this.createArray();
|
|
2119
|
+
} else {
|
|
2120
|
+
var proto = func.properties["prototype"];
|
|
2121
|
+
if (typeof proto !== "object" || proto === null) {
|
|
2122
|
+
proto = this.OBJECT_PROTO;
|
|
2123
|
+
}
|
|
2124
|
+
state.funcThis_ = this.createObjectProto(proto);
|
|
2125
|
+
}
|
|
2126
|
+
state.isConstructor = true;
|
|
2127
|
+
}
|
|
2128
|
+
state.doneArgs_ = true;
|
|
2129
|
+
}
|
|
2130
|
+
if (!state.doneExec_) {
|
|
2131
|
+
state.doneExec_ = true;
|
|
2132
|
+
if (!(func instanceof Interpreter.Object)) {
|
|
2133
|
+
this.throwException(this.TYPE_ERROR, this.nodeSummary(node.callee) + " is not a function");
|
|
2134
|
+
}
|
|
2135
|
+
var funcNode = func.node;
|
|
2136
|
+
if (funcNode) {
|
|
2137
|
+
var scope = this.createScope(funcNode.body, func.parentScope);
|
|
2138
|
+
var argsList = this.createArray();
|
|
2139
|
+
for (var i = 0; i < state.arguments_.length; i++) {
|
|
2140
|
+
this.setProperty(argsList, i, state.arguments_[i]);
|
|
2141
|
+
}
|
|
2142
|
+
this.setProperty(scope.object, "arguments", argsList);
|
|
2143
|
+
for (var i = 0; i < funcNode.params.length; i++) {
|
|
2144
|
+
var paramName = funcNode.params[i].name;
|
|
2145
|
+
var paramValue = state.arguments_.length > i ? state.arguments_[i] : void 0;
|
|
2146
|
+
this.setProperty(scope.object, paramName, paramValue);
|
|
2147
|
+
}
|
|
2148
|
+
if (!scope.strict) {
|
|
2149
|
+
state.funcThis_ = this.boxThis_(state.funcThis_);
|
|
2150
|
+
}
|
|
2151
|
+
this.setProperty(scope.object, "this", state.funcThis_, Interpreter.READONLY_DESCRIPTOR);
|
|
2152
|
+
state.value = void 0;
|
|
2153
|
+
return new Interpreter.State(funcNode.body, scope);
|
|
2154
|
+
} else if (func.eval) {
|
|
2155
|
+
var code = state.arguments_[0];
|
|
2156
|
+
if (typeof code !== "string") {
|
|
2157
|
+
state.value = code;
|
|
2158
|
+
} else {
|
|
2159
|
+
try {
|
|
2160
|
+
var ast = this.parse_(String(code), "eval" + this.evalCodeNumber_++);
|
|
2161
|
+
} catch (e) {
|
|
2162
|
+
this.throwException(this.SYNTAX_ERROR, "Invalid code: " + e.message);
|
|
2163
|
+
}
|
|
2164
|
+
var evalNode = this.newNode();
|
|
2165
|
+
evalNode.type = "EvalProgram_";
|
|
2166
|
+
evalNode.body = ast.body;
|
|
2167
|
+
Interpreter.stripLocations_(evalNode, node.start, node.end);
|
|
2168
|
+
var scope = state.directEval_ ? state.scope : this.globalScope;
|
|
2169
|
+
if (scope.strict) {
|
|
2170
|
+
scope = this.createScope(ast, scope);
|
|
2171
|
+
} else {
|
|
2172
|
+
this.populateScope_(ast, scope);
|
|
2173
|
+
}
|
|
2174
|
+
this.value = void 0;
|
|
2175
|
+
return new Interpreter.State(evalNode, scope);
|
|
2176
|
+
}
|
|
2177
|
+
} else if (func.nativeFunc) {
|
|
2178
|
+
if (!state.scope.strict) {
|
|
2179
|
+
state.funcThis_ = this.boxThis_(state.funcThis_);
|
|
2180
|
+
}
|
|
2181
|
+
state.value = func.nativeFunc.apply(state.funcThis_, state.arguments_);
|
|
2182
|
+
} else if (func.asyncFunc) {
|
|
2183
|
+
var thisInterpreter = this;
|
|
2184
|
+
var callback = function (value) {
|
|
2185
|
+
state.value = value;
|
|
2186
|
+
thisInterpreter.paused_ = false;
|
|
2187
|
+
};
|
|
2188
|
+
var argLength = func.asyncFunc.length - 1;
|
|
2189
|
+
var argsWithCallback = state.arguments_.concat(new Array(argLength)).slice(0, argLength);
|
|
2190
|
+
argsWithCallback.push(callback);
|
|
2191
|
+
this.paused_ = true;
|
|
2192
|
+
if (!state.scope.strict) {
|
|
2193
|
+
state.funcThis_ = this.boxThis_(state.funcThis_);
|
|
2194
|
+
}
|
|
2195
|
+
func.asyncFunc.apply(state.funcThis_, argsWithCallback);
|
|
2196
|
+
return;
|
|
2197
|
+
} else {
|
|
2198
|
+
this.throwException(this.TYPE_ERROR, this.nodeSummary(node.callee) + " is not callable");
|
|
2199
|
+
}
|
|
2200
|
+
} else {
|
|
2201
|
+
stack.pop();
|
|
2202
|
+
if (state.isConstructor && typeof state.value !== "object") {
|
|
2203
|
+
stack[stack.length - 1].value = state.funcThis_;
|
|
2204
|
+
} else {
|
|
2205
|
+
stack[stack.length - 1].value = state.value;
|
|
2206
|
+
}
|
|
2207
|
+
}
|
|
2208
|
+
};
|
|
2209
|
+
Interpreter.prototype["stepConditionalExpression"] = function (stack, state, node) {
|
|
2210
|
+
var mode = state.mode_ || 0;
|
|
2211
|
+
if (mode === 0) {
|
|
2212
|
+
state.mode_ = 1;
|
|
2213
|
+
return new Interpreter.State(node.test, state.scope);
|
|
2214
|
+
}
|
|
2215
|
+
if (mode === 1) {
|
|
2216
|
+
state.mode_ = 2;
|
|
2217
|
+
var value = Boolean(state.value);
|
|
2218
|
+
if (value && node.consequent) {
|
|
2219
|
+
return new Interpreter.State(node.consequent, state.scope);
|
|
2220
|
+
} else if (!value && node.alternate) {
|
|
2221
|
+
return new Interpreter.State(node.alternate, state.scope);
|
|
2222
|
+
}
|
|
2223
|
+
this.value = void 0;
|
|
2224
|
+
}
|
|
2225
|
+
stack.pop();
|
|
2226
|
+
if (node.type === "ConditionalExpression") {
|
|
2227
|
+
stack[stack.length - 1].value = state.value;
|
|
2228
|
+
}
|
|
2229
|
+
};
|
|
2230
|
+
Interpreter.prototype["stepContinueStatement"] = function (stack, state, node) {
|
|
2231
|
+
var label = node.label && node.label.name;
|
|
2232
|
+
this.unwind(Interpreter.Completion.CONTINUE, void 0, label);
|
|
2233
|
+
};
|
|
2234
|
+
Interpreter.prototype["stepDebuggerStatement"] = function (stack, state, node) {
|
|
2235
|
+
stack.pop();
|
|
2236
|
+
};
|
|
2237
|
+
Interpreter.prototype["stepDoWhileStatement"] = function (stack, state, node) {
|
|
2238
|
+
if (node.type === "DoWhileStatement" && state.test_ === void 0) {
|
|
2239
|
+
state.value = true;
|
|
2240
|
+
state.test_ = true;
|
|
2241
|
+
}
|
|
2242
|
+
if (!state.test_) {
|
|
2243
|
+
state.test_ = true;
|
|
2244
|
+
return new Interpreter.State(node.test, state.scope);
|
|
2245
|
+
}
|
|
2246
|
+
if (!state.value) {
|
|
2247
|
+
stack.pop();
|
|
2248
|
+
} else if (node.body) {
|
|
2249
|
+
state.test_ = false;
|
|
2250
|
+
state.isLoop = true;
|
|
2251
|
+
return new Interpreter.State(node.body, state.scope);
|
|
2252
|
+
}
|
|
2253
|
+
};
|
|
2254
|
+
Interpreter.prototype["stepEmptyStatement"] = function (stack, state, node) {
|
|
2255
|
+
stack.pop();
|
|
2256
|
+
};
|
|
2257
|
+
Interpreter.prototype["stepEvalProgram_"] = function (stack, state, node) {
|
|
2258
|
+
var n = state.n_ || 0;
|
|
2259
|
+
var expression = node.body[n];
|
|
2260
|
+
if (expression) {
|
|
2261
|
+
state.n_ = n + 1;
|
|
2262
|
+
return new Interpreter.State(expression, state.scope);
|
|
2263
|
+
}
|
|
2264
|
+
stack.pop();
|
|
2265
|
+
stack[stack.length - 1].value = this.value;
|
|
2266
|
+
};
|
|
2267
|
+
Interpreter.prototype["stepExpressionStatement"] = function (stack, state, node) {
|
|
2268
|
+
if (!state.done_) {
|
|
2269
|
+
this.value = void 0;
|
|
2270
|
+
state.done_ = true;
|
|
2271
|
+
return new Interpreter.State(node.expression, state.scope);
|
|
2272
|
+
}
|
|
2273
|
+
stack.pop();
|
|
2274
|
+
this.value = state.value;
|
|
2275
|
+
};
|
|
2276
|
+
Interpreter.prototype["stepForInStatement"] = function (stack, state, node) {
|
|
2277
|
+
if (!state.doneInit_) {
|
|
2278
|
+
state.doneInit_ = true;
|
|
2279
|
+
if (node.left.declarations && node.left.declarations[0].init) {
|
|
2280
|
+
if (state.scope.strict) {
|
|
2281
|
+
this.throwException(this.SYNTAX_ERROR, "for-in loop variable declaration may not have an initializer");
|
|
2282
|
+
}
|
|
2283
|
+
return new Interpreter.State(node.left, state.scope);
|
|
2284
|
+
}
|
|
2285
|
+
}
|
|
2286
|
+
if (!state.doneObject_) {
|
|
2287
|
+
state.doneObject_ = true;
|
|
2288
|
+
if (!state.variable_) {
|
|
2289
|
+
state.variable_ = state.value;
|
|
2290
|
+
}
|
|
2291
|
+
return new Interpreter.State(node.right, state.scope);
|
|
2292
|
+
}
|
|
2293
|
+
if (!state.isLoop) {
|
|
2294
|
+
state.isLoop = true;
|
|
2295
|
+
state.object_ = state.value;
|
|
2296
|
+
state.visited_ = Object.create(null);
|
|
2297
|
+
}
|
|
2298
|
+
if (state.name_ === void 0) {
|
|
2299
|
+
gotPropName: while (true) {
|
|
2300
|
+
if (state.object_ instanceof Interpreter.Object) {
|
|
2301
|
+
if (!state.props_) {
|
|
2302
|
+
state.props_ = Object.getOwnPropertyNames(state.object_.properties);
|
|
2303
|
+
}
|
|
2304
|
+
while (true) {
|
|
2305
|
+
var prop = state.props_.shift();
|
|
2306
|
+
if (prop === void 0) {
|
|
2307
|
+
break;
|
|
2308
|
+
}
|
|
2309
|
+
if (!Object.prototype.hasOwnProperty.call(state.object_.properties, prop)) {
|
|
2310
|
+
continue;
|
|
2311
|
+
}
|
|
2312
|
+
if (state.visited_[prop]) {
|
|
2313
|
+
continue;
|
|
2314
|
+
}
|
|
2315
|
+
state.visited_[prop] = true;
|
|
2316
|
+
if (!Object.prototype.propertyIsEnumerable.call(state.object_.properties, prop)) {
|
|
2317
|
+
continue;
|
|
2318
|
+
}
|
|
2319
|
+
state.name_ = prop;
|
|
2320
|
+
break gotPropName;
|
|
2321
|
+
}
|
|
2322
|
+
} else if (state.object_ !== null && state.object_ !== void 0) {
|
|
2323
|
+
if (!state.props_) {
|
|
2324
|
+
state.props_ = Object.getOwnPropertyNames(state.object_);
|
|
2325
|
+
}
|
|
2326
|
+
while (true) {
|
|
2327
|
+
var prop = state.props_.shift();
|
|
2328
|
+
if (prop === void 0) {
|
|
2329
|
+
break;
|
|
2330
|
+
}
|
|
2331
|
+
state.visited_[prop] = true;
|
|
2332
|
+
if (!Object.prototype.propertyIsEnumerable.call(state.object_, prop)) {
|
|
2333
|
+
continue;
|
|
2334
|
+
}
|
|
2335
|
+
state.name_ = prop;
|
|
2336
|
+
break gotPropName;
|
|
2337
|
+
}
|
|
2338
|
+
}
|
|
2339
|
+
state.object_ = this.getPrototype(state.object_);
|
|
2340
|
+
state.props_ = null;
|
|
2341
|
+
if (state.object_ === null) {
|
|
2342
|
+
stack.pop();
|
|
2343
|
+
return;
|
|
2344
|
+
}
|
|
2345
|
+
}
|
|
2346
|
+
}
|
|
2347
|
+
if (!state.doneVariable_) {
|
|
2348
|
+
state.doneVariable_ = true;
|
|
2349
|
+
var left = node.left;
|
|
2350
|
+
if (left.type === "VariableDeclaration") {
|
|
2351
|
+
state.variable_ = [Interpreter.SCOPE_REFERENCE, left.declarations[0].id.name];
|
|
2352
|
+
} else {
|
|
2353
|
+
state.variable_ = null;
|
|
2354
|
+
var nextState = new Interpreter.State(left, state.scope);
|
|
2355
|
+
nextState.components = true;
|
|
2356
|
+
return nextState;
|
|
2357
|
+
}
|
|
2358
|
+
}
|
|
2359
|
+
if (!state.variable_) {
|
|
2360
|
+
state.variable_ = state.value;
|
|
2361
|
+
}
|
|
2362
|
+
if (!state.doneSetter_) {
|
|
2363
|
+
state.doneSetter_ = true;
|
|
2364
|
+
var value = state.name_;
|
|
2365
|
+
var setter = this.setValue(state.variable_, value);
|
|
2366
|
+
if (setter) {
|
|
2367
|
+
return this.createSetter_(setter, state.variable_, value);
|
|
2368
|
+
}
|
|
2369
|
+
}
|
|
2370
|
+
state.name_ = void 0;
|
|
2371
|
+
state.doneVariable_ = false;
|
|
2372
|
+
state.doneSetter_ = false;
|
|
2373
|
+
if (node.body) {
|
|
2374
|
+
return new Interpreter.State(node.body, state.scope);
|
|
2375
|
+
}
|
|
2376
|
+
};
|
|
2377
|
+
Interpreter.prototype["stepForStatement"] = function (stack, state, node) {
|
|
2378
|
+
switch (state.mode_) {
|
|
2379
|
+
default:
|
|
2380
|
+
state.mode_ = 1;
|
|
2381
|
+
if (node.init) {
|
|
2382
|
+
return new Interpreter.State(node.init, state.scope);
|
|
2383
|
+
}
|
|
2384
|
+
break;
|
|
2385
|
+
case 1:
|
|
2386
|
+
state.mode_ = 2;
|
|
2387
|
+
if (node.test) {
|
|
2388
|
+
return new Interpreter.State(node.test, state.scope);
|
|
2389
|
+
}
|
|
2390
|
+
break;
|
|
2391
|
+
case 2:
|
|
2392
|
+
state.mode_ = 3;
|
|
2393
|
+
if (node.test && !state.value) {
|
|
2394
|
+
stack.pop();
|
|
2395
|
+
} else {
|
|
2396
|
+
state.isLoop = true;
|
|
2397
|
+
return new Interpreter.State(node.body, state.scope);
|
|
2398
|
+
}
|
|
2399
|
+
break;
|
|
2400
|
+
case 3:
|
|
2401
|
+
state.mode_ = 1;
|
|
2402
|
+
if (node.update) {
|
|
2403
|
+
return new Interpreter.State(node.update, state.scope);
|
|
2404
|
+
}
|
|
2405
|
+
break;
|
|
2406
|
+
}
|
|
2407
|
+
};
|
|
2408
|
+
Interpreter.prototype["stepFunctionDeclaration"] = function (stack, state, node) {
|
|
2409
|
+
stack.pop();
|
|
2410
|
+
};
|
|
2411
|
+
Interpreter.prototype["stepFunctionExpression"] = function (stack, state, node) {
|
|
2412
|
+
stack.pop();
|
|
2413
|
+
state = stack[stack.length - 1];
|
|
2414
|
+
var parentScope = state.scope;
|
|
2415
|
+
if (node.id) {
|
|
2416
|
+
parentScope = this.createSpecialScope(parentScope);
|
|
2417
|
+
}
|
|
2418
|
+
state.value = this.createFunction(node, parentScope, state.destinationName);
|
|
2419
|
+
if (node.id) {
|
|
2420
|
+
this.setProperty(parentScope.object, node.id.name, state.value, Interpreter.READONLY_DESCRIPTOR);
|
|
2421
|
+
}
|
|
2422
|
+
};
|
|
2423
|
+
Interpreter.prototype["stepIdentifier"] = function (stack, state, node) {
|
|
2424
|
+
stack.pop();
|
|
2425
|
+
if (state.components) {
|
|
2426
|
+
stack[stack.length - 1].value = [Interpreter.SCOPE_REFERENCE, node.name];
|
|
2427
|
+
return;
|
|
2428
|
+
}
|
|
2429
|
+
var value = this.getValueFromScope(node.name);
|
|
2430
|
+
if (this.getterStep_) {
|
|
2431
|
+
var func = value;
|
|
2432
|
+
return this.createGetter_(func, this.globalObject);
|
|
2433
|
+
}
|
|
2434
|
+
stack[stack.length - 1].value = value;
|
|
2435
|
+
};
|
|
2436
|
+
Interpreter.prototype["stepIfStatement"] = Interpreter.prototype["stepConditionalExpression"];
|
|
2437
|
+
Interpreter.prototype["stepLabeledStatement"] = function (stack, state, node) {
|
|
2438
|
+
stack.pop();
|
|
2439
|
+
var labels = state.labels || [];
|
|
2440
|
+
labels.push(node.label.name);
|
|
2441
|
+
var nextState = new Interpreter.State(node.body, state.scope);
|
|
2442
|
+
nextState.labels = labels;
|
|
2443
|
+
return nextState;
|
|
2444
|
+
};
|
|
2445
|
+
Interpreter.prototype["stepLiteral"] = function (stack, state, node) {
|
|
2446
|
+
stack.pop();
|
|
2447
|
+
var value = node.value;
|
|
2448
|
+
if (value instanceof RegExp) {
|
|
2449
|
+
var pseudoRegexp = this.createObjectProto(this.REGEXP_PROTO);
|
|
2450
|
+
this.populateRegExp(pseudoRegexp, value);
|
|
2451
|
+
value = pseudoRegexp;
|
|
2452
|
+
}
|
|
2453
|
+
stack[stack.length - 1].value = value;
|
|
2454
|
+
};
|
|
2455
|
+
Interpreter.prototype["stepLogicalExpression"] = function (stack, state, node) {
|
|
2456
|
+
if (node.operator !== "&&" && node.operator !== "||") {
|
|
2457
|
+
throw SyntaxError("Unknown logical operator: " + node.operator);
|
|
2458
|
+
}
|
|
2459
|
+
if (!state.doneLeft_) {
|
|
2460
|
+
state.doneLeft_ = true;
|
|
2461
|
+
return new Interpreter.State(node.left, state.scope);
|
|
2462
|
+
}
|
|
2463
|
+
if (!state.doneRight_) {
|
|
2464
|
+
if (node.operator === "&&" && !state.value || node.operator === "||" && state.value) {
|
|
2465
|
+
stack.pop();
|
|
2466
|
+
stack[stack.length - 1].value = state.value;
|
|
2467
|
+
} else {
|
|
2468
|
+
state.doneRight_ = true;
|
|
2469
|
+
return new Interpreter.State(node.right, state.scope);
|
|
2470
|
+
}
|
|
2471
|
+
} else {
|
|
2472
|
+
stack.pop();
|
|
2473
|
+
stack[stack.length - 1].value = state.value;
|
|
2474
|
+
}
|
|
2475
|
+
};
|
|
2476
|
+
Interpreter.prototype["stepMemberExpression"] = function (stack, state, node) {
|
|
2477
|
+
if (!state.doneObject_) {
|
|
2478
|
+
state.doneObject_ = true;
|
|
2479
|
+
return new Interpreter.State(node.object, state.scope);
|
|
2480
|
+
}
|
|
2481
|
+
var propName;
|
|
2482
|
+
if (!node.computed) {
|
|
2483
|
+
state.object_ = state.value;
|
|
2484
|
+
propName = node.property.name;
|
|
2485
|
+
} else if (!state.doneProperty_) {
|
|
2486
|
+
state.object_ = state.value;
|
|
2487
|
+
state.doneProperty_ = true;
|
|
2488
|
+
return new Interpreter.State(node.property, state.scope);
|
|
2489
|
+
} else {
|
|
2490
|
+
propName = state.value;
|
|
2491
|
+
}
|
|
2492
|
+
stack.pop();
|
|
2493
|
+
if (state.components) {
|
|
2494
|
+
stack[stack.length - 1].value = [state.object_, propName];
|
|
2495
|
+
} else {
|
|
2496
|
+
var value = this.getProperty(state.object_, propName);
|
|
2497
|
+
if (this.getterStep_) {
|
|
2498
|
+
var func = value;
|
|
2499
|
+
return this.createGetter_(func, state.object_);
|
|
2500
|
+
}
|
|
2501
|
+
stack[stack.length - 1].value = value;
|
|
2502
|
+
}
|
|
2503
|
+
};
|
|
2504
|
+
Interpreter.prototype["stepNewExpression"] = Interpreter.prototype["stepCallExpression"];
|
|
2505
|
+
Interpreter.prototype["stepObjectExpression"] = function (stack, state, node) {
|
|
2506
|
+
var n = state.n_ || 0;
|
|
2507
|
+
var property = node.properties[n];
|
|
2508
|
+
if (!state.object_) {
|
|
2509
|
+
state.object_ = this.createObjectProto(this.OBJECT_PROTO);
|
|
2510
|
+
state.properties_ = Object.create(null);
|
|
2511
|
+
} else {
|
|
2512
|
+
var propName = state.destinationName;
|
|
2513
|
+
if (!state.properties_[propName]) {
|
|
2514
|
+
state.properties_[propName] = {};
|
|
2515
|
+
}
|
|
2516
|
+
state.properties_[propName][property.kind] = state.value;
|
|
2517
|
+
state.n_ = ++n;
|
|
2518
|
+
property = node.properties[n];
|
|
2519
|
+
}
|
|
2520
|
+
if (property) {
|
|
2521
|
+
var key = property.key;
|
|
2522
|
+
if (key.type === "Identifier") {
|
|
2523
|
+
var propName = key.name;
|
|
2524
|
+
} else if (key.type === "Literal") {
|
|
2525
|
+
var propName = key.value;
|
|
2526
|
+
} else {
|
|
2527
|
+
throw SyntaxError("Unknown object structure: " + key.type);
|
|
2528
|
+
}
|
|
2529
|
+
state.destinationName = propName;
|
|
2530
|
+
return new Interpreter.State(property.value, state.scope);
|
|
2531
|
+
}
|
|
2532
|
+
for (var key in state.properties_) {
|
|
2533
|
+
var kinds = state.properties_[key];
|
|
2534
|
+
if ("get" in kinds || "set" in kinds) {
|
|
2535
|
+
var descriptor = {
|
|
2536
|
+
configurable: true,
|
|
2537
|
+
enumerable: true,
|
|
2538
|
+
get: kinds["get"],
|
|
2539
|
+
set: kinds["set"]
|
|
2540
|
+
};
|
|
2541
|
+
this.setProperty(state.object_, key, Interpreter.VALUE_IN_DESCRIPTOR, descriptor);
|
|
2542
|
+
} else {
|
|
2543
|
+
this.setProperty(state.object_, key, kinds["init"]);
|
|
2544
|
+
}
|
|
2545
|
+
}
|
|
2546
|
+
stack.pop();
|
|
2547
|
+
stack[stack.length - 1].value = state.object_;
|
|
2548
|
+
};
|
|
2549
|
+
Interpreter.prototype["stepProgram"] = function (stack, state, node) {
|
|
2550
|
+
var expression = node.body.shift();
|
|
2551
|
+
if (expression) {
|
|
2552
|
+
state.done = false;
|
|
2553
|
+
return new Interpreter.State(expression, state.scope);
|
|
2554
|
+
}
|
|
2555
|
+
state.done = true;
|
|
2556
|
+
};
|
|
2557
|
+
Interpreter.prototype["stepReturnStatement"] = function (stack, state, node) {
|
|
2558
|
+
if (node.argument && !state.done_) {
|
|
2559
|
+
state.done_ = true;
|
|
2560
|
+
return new Interpreter.State(node.argument, state.scope);
|
|
2561
|
+
}
|
|
2562
|
+
this.unwind(Interpreter.Completion.RETURN, state.value, void 0);
|
|
2563
|
+
};
|
|
2564
|
+
Interpreter.prototype["stepSequenceExpression"] = function (stack, state, node) {
|
|
2565
|
+
var n = state.n_ || 0;
|
|
2566
|
+
var expression = node.expressions[n];
|
|
2567
|
+
if (expression) {
|
|
2568
|
+
state.n_ = n + 1;
|
|
2569
|
+
return new Interpreter.State(expression, state.scope);
|
|
2570
|
+
}
|
|
2571
|
+
stack.pop();
|
|
2572
|
+
stack[stack.length - 1].value = state.value;
|
|
2573
|
+
};
|
|
2574
|
+
Interpreter.prototype["stepSwitchStatement"] = function (stack, state, node) {
|
|
2575
|
+
if (!state.test_) {
|
|
2576
|
+
state.test_ = 1;
|
|
2577
|
+
return new Interpreter.State(node.discriminant, state.scope);
|
|
2578
|
+
}
|
|
2579
|
+
if (state.test_ === 1) {
|
|
2580
|
+
state.test_ = 2;
|
|
2581
|
+
state.switchValue_ = state.value;
|
|
2582
|
+
state.defaultCase_ = -1;
|
|
2583
|
+
}
|
|
2584
|
+
while (true) {
|
|
2585
|
+
var index = state.index_ || 0;
|
|
2586
|
+
var switchCase = node.cases[index];
|
|
2587
|
+
if (!state.matched_ && switchCase && !switchCase.test) {
|
|
2588
|
+
state.defaultCase_ = index;
|
|
2589
|
+
state.index_ = index + 1;
|
|
2590
|
+
continue;
|
|
2591
|
+
}
|
|
2592
|
+
if (!switchCase && !state.matched_ && state.defaultCase_ !== -1) {
|
|
2593
|
+
state.matched_ = true;
|
|
2594
|
+
state.index_ = state.defaultCase_;
|
|
2595
|
+
continue;
|
|
2596
|
+
}
|
|
2597
|
+
if (switchCase) {
|
|
2598
|
+
if (!state.matched_ && !state.tested_ && switchCase.test) {
|
|
2599
|
+
state.tested_ = true;
|
|
2600
|
+
return new Interpreter.State(switchCase.test, state.scope);
|
|
2601
|
+
}
|
|
2602
|
+
if (state.matched_ || state.value === state.switchValue_) {
|
|
2603
|
+
state.matched_ = true;
|
|
2604
|
+
var n = state.n_ || 0;
|
|
2605
|
+
if (switchCase.consequent[n]) {
|
|
2606
|
+
state.isSwitch = true;
|
|
2607
|
+
state.n_ = n + 1;
|
|
2608
|
+
return new Interpreter.State(switchCase.consequent[n], state.scope);
|
|
2609
|
+
}
|
|
2610
|
+
}
|
|
2611
|
+
state.tested_ = false;
|
|
2612
|
+
state.n_ = 0;
|
|
2613
|
+
state.index_ = index + 1;
|
|
2614
|
+
} else {
|
|
2615
|
+
stack.pop();
|
|
2616
|
+
return;
|
|
2617
|
+
}
|
|
2618
|
+
}
|
|
2619
|
+
};
|
|
2620
|
+
Interpreter.prototype["stepThisExpression"] = function (stack, state, node) {
|
|
2621
|
+
stack.pop();
|
|
2622
|
+
stack[stack.length - 1].value = this.getValueFromScope("this");
|
|
2623
|
+
};
|
|
2624
|
+
Interpreter.prototype["stepThrowStatement"] = function (stack, state, node) {
|
|
2625
|
+
if (!state.done_) {
|
|
2626
|
+
state.done_ = true;
|
|
2627
|
+
return new Interpreter.State(node.argument, state.scope);
|
|
2628
|
+
} else {
|
|
2629
|
+
this.throwException(state.value);
|
|
2630
|
+
}
|
|
2631
|
+
};
|
|
2632
|
+
Interpreter.prototype["stepTryStatement"] = function (stack, state, node) {
|
|
2633
|
+
if (!state.doneBlock_) {
|
|
2634
|
+
state.doneBlock_ = true;
|
|
2635
|
+
return new Interpreter.State(node.block, state.scope);
|
|
2636
|
+
}
|
|
2637
|
+
if (state.cv && state.cv.type === Interpreter.Completion.THROW && !state.doneHandler_ && node.handler) {
|
|
2638
|
+
state.doneHandler_ = true;
|
|
2639
|
+
var scope = this.createSpecialScope(state.scope);
|
|
2640
|
+
this.setProperty(scope.object, node.handler.param.name, state.cv.value);
|
|
2641
|
+
state.cv = void 0;
|
|
2642
|
+
return new Interpreter.State(node.handler.body, scope);
|
|
2643
|
+
}
|
|
2644
|
+
if (!state.doneFinalizer_ && node.finalizer) {
|
|
2645
|
+
state.doneFinalizer_ = true;
|
|
2646
|
+
return new Interpreter.State(node.finalizer, state.scope);
|
|
2647
|
+
}
|
|
2648
|
+
stack.pop();
|
|
2649
|
+
if (state.cv) {
|
|
2650
|
+
this.unwind(state.cv.type, state.cv.value, state.cv.label);
|
|
2651
|
+
}
|
|
2652
|
+
};
|
|
2653
|
+
Interpreter.prototype["stepUnaryExpression"] = function (stack, state, node) {
|
|
2654
|
+
if (!state.done_) {
|
|
2655
|
+
state.done_ = true;
|
|
2656
|
+
var nextState = new Interpreter.State(node.argument, state.scope);
|
|
2657
|
+
nextState.components = node.operator === "delete";
|
|
2658
|
+
return nextState;
|
|
2659
|
+
}
|
|
2660
|
+
stack.pop();
|
|
2661
|
+
var value = state.value;
|
|
2662
|
+
switch (node.operator) {
|
|
2663
|
+
case "-":
|
|
2664
|
+
value = -value;
|
|
2665
|
+
break;
|
|
2666
|
+
case "+":
|
|
2667
|
+
value = +value;
|
|
2668
|
+
break;
|
|
2669
|
+
case "!":
|
|
2670
|
+
value = !value;
|
|
2671
|
+
break;
|
|
2672
|
+
case "~":
|
|
2673
|
+
value = ~value;
|
|
2674
|
+
break;
|
|
2675
|
+
case "delete":
|
|
2676
|
+
var result = true;
|
|
2677
|
+
if (Array.isArray(value)) {
|
|
2678
|
+
var obj = value[0];
|
|
2679
|
+
if (obj === Interpreter.SCOPE_REFERENCE) {
|
|
2680
|
+
obj = state.scope;
|
|
2681
|
+
}
|
|
2682
|
+
var name = String(value[1]);
|
|
2683
|
+
try {
|
|
2684
|
+
delete obj.properties[name];
|
|
2685
|
+
} catch (_e) {
|
|
2686
|
+
if (state.scope.strict) {
|
|
2687
|
+
this.throwException(this.TYPE_ERROR, "Cannot delete property '" + name + "' of '" + obj + "'");
|
|
2688
|
+
} else {
|
|
2689
|
+
result = false;
|
|
2690
|
+
}
|
|
2691
|
+
}
|
|
2692
|
+
}
|
|
2693
|
+
value = result;
|
|
2694
|
+
break;
|
|
2695
|
+
case "typeof":
|
|
2696
|
+
value = value && value.class === "Function" ? "function" : typeof value;
|
|
2697
|
+
break;
|
|
2698
|
+
case "void":
|
|
2699
|
+
value = void 0;
|
|
2700
|
+
break;
|
|
2701
|
+
default:
|
|
2702
|
+
throw SyntaxError("Unknown unary operator: " + node.operator);
|
|
2703
|
+
}
|
|
2704
|
+
stack[stack.length - 1].value = value;
|
|
2705
|
+
};
|
|
2706
|
+
Interpreter.prototype["stepUpdateExpression"] = function (stack, state, node) {
|
|
2707
|
+
if (!state.doneLeft_) {
|
|
2708
|
+
state.doneLeft_ = true;
|
|
2709
|
+
var nextState = new Interpreter.State(node.argument, state.scope);
|
|
2710
|
+
nextState.components = true;
|
|
2711
|
+
return nextState;
|
|
2712
|
+
}
|
|
2713
|
+
if (!state.leftSide_) {
|
|
2714
|
+
state.leftSide_ = state.value;
|
|
2715
|
+
}
|
|
2716
|
+
if (state.doneGetter_) {
|
|
2717
|
+
state.leftValue_ = state.value;
|
|
2718
|
+
}
|
|
2719
|
+
if (!state.doneGetter_) {
|
|
2720
|
+
var leftValue = this.getValue(state.leftSide_);
|
|
2721
|
+
state.leftValue_ = leftValue;
|
|
2722
|
+
if (this.getterStep_) {
|
|
2723
|
+
state.doneGetter_ = true;
|
|
2724
|
+
var func = leftValue;
|
|
2725
|
+
return this.createGetter_(func, state.leftSide_);
|
|
2726
|
+
}
|
|
2727
|
+
}
|
|
2728
|
+
if (state.doneSetter_) {
|
|
2729
|
+
stack.pop();
|
|
2730
|
+
stack[stack.length - 1].value = state.setterValue_;
|
|
2731
|
+
return;
|
|
2732
|
+
}
|
|
2733
|
+
var leftValue = Number(state.leftValue_);
|
|
2734
|
+
var changeValue;
|
|
2735
|
+
if (node.operator === "++") {
|
|
2736
|
+
changeValue = leftValue + 1;
|
|
2737
|
+
} else if (node.operator === "--") {
|
|
2738
|
+
changeValue = leftValue - 1;
|
|
2739
|
+
} else {
|
|
2740
|
+
throw SyntaxError("Unknown update expression: " + node.operator);
|
|
2741
|
+
}
|
|
2742
|
+
var returnValue = node.prefix ? changeValue : leftValue;
|
|
2743
|
+
var setter = this.setValue(state.leftSide_, changeValue);
|
|
2744
|
+
if (setter) {
|
|
2745
|
+
state.doneSetter_ = true;
|
|
2746
|
+
state.setterValue_ = returnValue;
|
|
2747
|
+
return this.createSetter_(setter, state.leftSide_, changeValue);
|
|
2748
|
+
}
|
|
2749
|
+
stack.pop();
|
|
2750
|
+
stack[stack.length - 1].value = returnValue;
|
|
2751
|
+
};
|
|
2752
|
+
Interpreter.prototype["stepVariableDeclaration"] = function (stack, state, node) {
|
|
2753
|
+
var declarations = node.declarations;
|
|
2754
|
+
var n = state.n_ || 0;
|
|
2755
|
+
var declarationNode = declarations[n];
|
|
2756
|
+
if (state.init_ && declarationNode) {
|
|
2757
|
+
this.setValueToScope(declarationNode.id.name, state.value);
|
|
2758
|
+
state.init_ = false;
|
|
2759
|
+
declarationNode = declarations[++n];
|
|
2760
|
+
}
|
|
2761
|
+
while (declarationNode) {
|
|
2762
|
+
if (declarationNode.init) {
|
|
2763
|
+
state.n_ = n;
|
|
2764
|
+
state.init_ = true;
|
|
2765
|
+
state.destinationName = declarationNode.id.name;
|
|
2766
|
+
return new Interpreter.State(declarationNode.init, state.scope);
|
|
2767
|
+
}
|
|
2768
|
+
declarationNode = declarations[++n];
|
|
2769
|
+
}
|
|
2770
|
+
stack.pop();
|
|
2771
|
+
};
|
|
2772
|
+
Interpreter.prototype["stepWithStatement"] = function (stack, state, node) {
|
|
2773
|
+
if (!state.doneObject_) {
|
|
2774
|
+
state.doneObject_ = true;
|
|
2775
|
+
return new Interpreter.State(node.object, state.scope);
|
|
2776
|
+
}
|
|
2777
|
+
stack.pop();
|
|
2778
|
+
var scope = this.createSpecialScope(state.scope, state.value);
|
|
2779
|
+
return new Interpreter.State(node.body, scope);
|
|
2780
|
+
};
|
|
2781
|
+
Interpreter.prototype["stepWhileStatement"] = Interpreter.prototype["stepDoWhileStatement"];
|
|
2782
|
+
Interpreter.nativeGlobal["Interpreter"] = Interpreter;
|
|
2783
|
+
Interpreter.prototype["step"] = Interpreter.prototype.step;
|
|
2784
|
+
Interpreter.prototype["run"] = Interpreter.prototype.run;
|
|
2785
|
+
Interpreter.prototype["appendCode"] = Interpreter.prototype.appendCode;
|
|
2786
|
+
Interpreter.prototype["createObject"] = Interpreter.prototype.createObject;
|
|
2787
|
+
Interpreter.prototype["createObjectProto"] = Interpreter.prototype.createObjectProto;
|
|
2788
|
+
Interpreter.prototype["createAsyncFunction"] = Interpreter.prototype.createAsyncFunction;
|
|
2789
|
+
Interpreter.prototype["createNativeFunction"] = Interpreter.prototype.createNativeFunction;
|
|
2790
|
+
Interpreter.prototype["getProperty"] = Interpreter.prototype.getProperty;
|
|
2791
|
+
Interpreter.prototype["setProperty"] = Interpreter.prototype.setProperty;
|
|
2792
|
+
Interpreter.prototype["nativeToPseudo"] = Interpreter.prototype.nativeToPseudo;
|
|
2793
|
+
Interpreter.prototype["pseudoToNative"] = Interpreter.prototype.pseudoToNative;
|
|
2794
|
+
Interpreter.prototype["getGlobalScope"] = Interpreter.prototype.getGlobalScope;
|
|
2795
|
+
Interpreter.prototype["getStateStack"] = Interpreter.prototype.getStateStack;
|
|
2796
|
+
Interpreter.prototype["setStateStack"] = Interpreter.prototype.setStateStack;
|
|
2797
|
+
Interpreter["VALUE_IN_DESCRIPTOR"] = Interpreter.VALUE_IN_DESCRIPTOR;
|
|
2798
|
+
import { parse } from "acorn";
|
|
2799
|
+
Interpreter.nativeGlobal.acornParse = parse;
|
|
2800
|
+
var stdin_default = Interpreter;
|
|
2801
|
+
export { stdin_default as default }
|