2024-06-22 08:02:23 -05:00
|
|
|
import evaluate from '@/astride/evaluate.js';
|
|
|
|
import Scope from '@/astride/scope.js';
|
2024-06-30 10:03:50 -05:00
|
|
|
import traverse from '@/astride/traverse.js';
|
2024-06-16 08:01:01 -05:00
|
|
|
|
2024-06-30 10:03:50 -05:00
|
|
|
const YIELD_NONE = 0;
|
|
|
|
const YIELD_PROMISE = 1;
|
|
|
|
const YIELD_LOOP_UPDATE = 2;
|
|
|
|
const YIELD_RETURN = 3;
|
2024-07-12 18:32:55 -05:00
|
|
|
const YIELD_BREAK = 4;
|
2024-06-30 10:03:50 -05:00
|
|
|
|
2024-06-16 08:01:01 -05:00
|
|
|
export default class Sandbox {
|
|
|
|
|
|
|
|
ast;
|
2024-06-30 10:03:50 -05:00
|
|
|
$$execution;
|
2024-06-16 08:01:01 -05:00
|
|
|
scopes;
|
|
|
|
|
|
|
|
constructor(ast, context = {}) {
|
|
|
|
this.ast = ast;
|
|
|
|
this.$$context = context;
|
|
|
|
this.compile();
|
|
|
|
}
|
|
|
|
|
2024-07-04 09:24:49 -05:00
|
|
|
clone() {
|
|
|
|
return new this.constructor(
|
|
|
|
this.ast,
|
|
|
|
{
|
|
|
|
...this.$$context,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-06-16 08:01:01 -05:00
|
|
|
compile() {
|
|
|
|
let scope = new Scope();
|
|
|
|
scope.context = this.$$context;
|
2024-06-29 12:35:54 -05:00
|
|
|
this.scopes = new Map([[this.ast, scope]]);
|
2024-06-16 08:01:01 -05:00
|
|
|
traverse(
|
|
|
|
this.ast,
|
|
|
|
(node, verb) => {
|
|
|
|
if (
|
2024-06-30 14:20:37 -05:00
|
|
|
'BlockStatement' === node.type
|
|
|
|
|| 'ForStatement' === node.type
|
2024-07-12 17:34:57 -05:00
|
|
|
|| 'ForOfStatement' === node.type
|
2024-06-16 08:01:01 -05:00
|
|
|
) {
|
|
|
|
switch (verb) {
|
|
|
|
case 'enter': {
|
|
|
|
scope = new Scope(scope);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'exit': {
|
|
|
|
scope = scope.parent;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ('enter' === verb) {
|
|
|
|
this.scopes.set(node, scope);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
get context() {
|
2024-06-22 04:05:09 -05:00
|
|
|
return this.$$context;
|
2024-06-16 08:01:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
destructureArray(id, init) {
|
|
|
|
const scope = this.scopes.get(id);
|
|
|
|
const {elements} = id;
|
|
|
|
for (let i = 0; i < elements.length; ++i) {
|
|
|
|
const element = elements[i];
|
|
|
|
if (null === element) {
|
|
|
|
continue;
|
|
|
|
}
|
2024-07-12 04:04:09 -05:00
|
|
|
switch (element.type) {
|
|
|
|
case 'ArrayPattern': {
|
|
|
|
this.destructureArray(element, init[i]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'ObjectPattern': {
|
|
|
|
this.destructureObject(element, init[i]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'Identifier': {
|
|
|
|
scope.allocate(element.name, init[i]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* v8 ignore next 2 */
|
|
|
|
default:
|
|
|
|
throw new Error(`destructureArray(): Can't array destructure type ${element.type}`);
|
2024-06-16 08:01:01 -05:00
|
|
|
}
|
|
|
|
}
|
2024-06-30 10:03:50 -05:00
|
|
|
return init;
|
2024-06-16 08:01:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
destructureObject(id, init) {
|
|
|
|
const scope = this.scopes.get(id);
|
|
|
|
const {properties} = id;
|
|
|
|
for (let i = 0; i < properties.length; ++i) {
|
|
|
|
const property = properties[i];
|
2024-06-30 14:20:37 -05:00
|
|
|
if ('ObjectPattern' === property.value.type) {
|
2024-06-30 10:03:50 -05:00
|
|
|
this.destructureObject(property.value, init[property.key.name]);
|
2024-06-16 08:01:01 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
scope.allocate(
|
2024-06-22 10:47:17 -05:00
|
|
|
property.value.name,
|
|
|
|
init[property.key.name],
|
2024-06-16 08:01:01 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2024-06-30 10:03:50 -05:00
|
|
|
return init;
|
2024-06-16 08:01:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
evaluate(node) {
|
|
|
|
return evaluate(
|
|
|
|
node,
|
|
|
|
{scope: this.scopes.get(node)},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-06-30 10:03:50 -05:00
|
|
|
evaluateToResult(node) {
|
|
|
|
const {async, value} = this.evaluate(node);
|
|
|
|
return {
|
|
|
|
value,
|
2024-06-30 13:05:53 -05:00
|
|
|
yield: async ? YIELD_PROMISE : YIELD_NONE,
|
2024-06-30 10:03:50 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
executeSync(node, depth) {
|
|
|
|
let result;
|
|
|
|
const isReplaying = depth < this.$$execution.stack.length;
|
|
|
|
const isReplayingThisLevel = depth === this.$$execution.stack.length - 1;
|
|
|
|
if (!isReplaying) {
|
|
|
|
this.$$execution.stack.push(node);
|
|
|
|
}
|
|
|
|
// Substitute executing the node for its deferred result.
|
|
|
|
else if (isReplayingThisLevel) {
|
|
|
|
if (this.$$execution.stack[depth] === node) {
|
|
|
|
result = this.$$execution.deferred.get(node);
|
|
|
|
this.$$execution.deferred.delete(node);
|
|
|
|
this.$$execution.stack.pop();
|
|
|
|
return result;
|
2024-06-16 08:01:01 -05:00
|
|
|
}
|
2024-06-30 10:03:50 -05:00
|
|
|
}
|
|
|
|
switch (node.type) {
|
|
|
|
case 'ArrayExpression':
|
|
|
|
case 'AssignmentExpression':
|
|
|
|
case 'BinaryExpression':
|
|
|
|
case 'CallExpression':
|
2024-06-30 13:05:53 -05:00
|
|
|
case 'ChainExpression':
|
2024-06-30 10:03:50 -05:00
|
|
|
case 'ObjectExpression':
|
|
|
|
case 'Identifier':
|
2024-06-30 13:05:53 -05:00
|
|
|
case 'MemberExpression':
|
2024-07-25 09:00:54 -05:00
|
|
|
case 'NewExpression':
|
2024-06-30 10:03:50 -05:00
|
|
|
case 'UpdateExpression': {
|
|
|
|
result = this.evaluateToResult(node);
|
|
|
|
if (result.yield) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'AwaitExpression': {
|
|
|
|
const coerce = !this.$$execution.deferred.has(node.argument);
|
|
|
|
result = this.executeSync(node.argument, depth + 1);
|
|
|
|
if (coerce) {
|
|
|
|
result = {
|
|
|
|
value: result.value,
|
2024-06-30 13:05:53 -05:00
|
|
|
yield: YIELD_PROMISE,
|
2024-06-30 10:03:50 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
if (result.yield) {
|
|
|
|
return result;
|
|
|
|
}
|
2024-06-30 14:20:37 -05:00
|
|
|
/* v8 ignore next 2 */
|
2024-06-30 10:03:50 -05:00
|
|
|
break;
|
|
|
|
}
|
2024-07-13 17:37:37 -05:00
|
|
|
case 'LogicalExpression': {
|
|
|
|
const shouldVisitChild = (child) => (
|
|
|
|
isReplaying
|
|
|
|
? (!this.$$execution.stack[depth + 1] || this.$$execution.stack[depth + 1] === child)
|
|
|
|
: true
|
|
|
|
);
|
|
|
|
if (shouldVisitChild(node.left)) {
|
|
|
|
const left = this.executeSync(node.left, depth + 1);
|
|
|
|
if (left.yield) {
|
|
|
|
return left;
|
|
|
|
}
|
|
|
|
this.$$execution.deferred.set(node.left, left);
|
|
|
|
}
|
|
|
|
const left = this.$$execution.deferred.get(node.left);
|
|
|
|
this.$$execution.deferred.delete(node.left);
|
|
|
|
if ('||' === node.operator && left.value) {
|
|
|
|
result = {
|
|
|
|
value: true,
|
|
|
|
yield: YIELD_NONE,
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ('&&' === node.operator && !left.value) {
|
|
|
|
result = {
|
|
|
|
value: false,
|
|
|
|
yield: YIELD_NONE,
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const right = this.executeSync(node.right, depth + 1);
|
|
|
|
if (right.yield) {
|
|
|
|
return right;
|
|
|
|
}
|
|
|
|
result = {
|
|
|
|
value: !!right.value,
|
|
|
|
yield: YIELD_NONE,
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
}
|
2024-06-30 10:03:50 -05:00
|
|
|
case 'BlockStatement': {
|
2024-06-30 13:05:53 -05:00
|
|
|
result = {
|
|
|
|
value: undefined,
|
|
|
|
yield: YIELD_NONE,
|
|
|
|
};
|
2024-06-30 10:03:50 -05:00
|
|
|
let skipping = isReplaying;
|
|
|
|
for (const child of node.body) {
|
|
|
|
if (skipping && child === this.$$execution.stack[depth + 1]) {
|
|
|
|
skipping = false;
|
2024-06-16 08:01:01 -05:00
|
|
|
}
|
2024-06-30 14:20:37 -05:00
|
|
|
/* v8 ignore next 3 */
|
2024-06-30 10:03:50 -05:00
|
|
|
if (skipping) {
|
|
|
|
continue;
|
2024-06-16 08:01:01 -05:00
|
|
|
}
|
2024-06-30 10:03:50 -05:00
|
|
|
result = this.executeSync(child, depth + 1);
|
|
|
|
if (result.yield) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2024-07-12 18:32:55 -05:00
|
|
|
case 'BreakStatement': {
|
|
|
|
walkUp: while (this.$$execution.stack.length > 0) {
|
|
|
|
const frame = this.$$execution.stack[this.$$execution.stack.length - 1];
|
|
|
|
switch (frame.type) {
|
|
|
|
case 'ForStatement': {
|
|
|
|
break walkUp;
|
|
|
|
}
|
|
|
|
case 'ForOfStatement': {
|
|
|
|
break walkUp;
|
|
|
|
}
|
|
|
|
default: this.$$execution.stack.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
value: undefined,
|
|
|
|
yield: YIELD_BREAK,
|
|
|
|
};
|
|
|
|
}
|
2024-06-30 10:03:50 -05:00
|
|
|
case 'ConditionalExpression': {
|
2024-06-30 13:05:53 -05:00
|
|
|
const shouldVisitChild = (child) => (
|
|
|
|
isReplaying
|
|
|
|
? (!this.$$execution.stack[depth + 1] || this.$$execution.stack[depth + 1] === child)
|
|
|
|
: true
|
|
|
|
);
|
|
|
|
if (shouldVisitChild(node.test)) {
|
|
|
|
const test = this.executeSync(node.test, depth + 1);
|
|
|
|
if (test.yield) {
|
|
|
|
return test;
|
|
|
|
}
|
|
|
|
this.$$execution.deferred.set(node.test, test);
|
2024-06-30 10:03:50 -05:00
|
|
|
}
|
2024-06-30 13:05:53 -05:00
|
|
|
const test = this.$$execution.deferred.get(node.test);
|
2024-06-30 10:03:50 -05:00
|
|
|
if (test.value) {
|
|
|
|
result = this.executeSync(node.consequent, depth + 1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = this.executeSync(node.alternate, depth + 1);
|
|
|
|
}
|
|
|
|
if (result.yield) {
|
|
|
|
return result;
|
2024-06-16 08:01:01 -05:00
|
|
|
}
|
2024-06-30 10:03:50 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'DoWhileStatement': {
|
|
|
|
const shouldVisitChild = (child) => (
|
|
|
|
isReplaying
|
|
|
|
? (!this.$$execution.stack[depth + 1] || this.$$execution.stack[depth + 1] === child)
|
|
|
|
: true
|
|
|
|
);
|
2024-06-30 13:05:53 -05:00
|
|
|
if (this.$$execution.stack[depth + 1] === node) {
|
2024-06-30 10:03:50 -05:00
|
|
|
this.$$execution.stack.pop();
|
|
|
|
}
|
|
|
|
result = this.$$execution.deferred.get(node.body);
|
|
|
|
if (shouldVisitChild(node.body)) {
|
|
|
|
const body = this.executeSync(node.body, depth + 1);
|
|
|
|
if (body.yield) {
|
|
|
|
return body;
|
2024-06-16 08:01:01 -05:00
|
|
|
}
|
2024-06-30 10:03:50 -05:00
|
|
|
this.$$execution.deferred.set(node.body, body);
|
2024-06-16 08:01:01 -05:00
|
|
|
}
|
2024-06-30 10:03:50 -05:00
|
|
|
if (shouldVisitChild(node.test)) {
|
|
|
|
const test = this.executeSync(node.test, depth + 1);
|
|
|
|
if (test.yield) {
|
|
|
|
return test;
|
|
|
|
}
|
|
|
|
if (!test.value) {
|
|
|
|
this.$$execution.deferred.delete(node.body);
|
|
|
|
break;
|
2024-06-16 08:01:01 -05:00
|
|
|
}
|
|
|
|
}
|
2024-06-30 10:03:50 -05:00
|
|
|
// Yield
|
2024-06-30 13:05:53 -05:00
|
|
|
this.$$execution.stack.push(node);
|
2024-06-30 10:03:50 -05:00
|
|
|
return {
|
|
|
|
value: undefined,
|
|
|
|
yield: YIELD_LOOP_UPDATE,
|
2024-06-16 08:01:01 -05:00
|
|
|
};
|
|
|
|
}
|
2024-06-30 10:03:50 -05:00
|
|
|
case 'ExpressionStatement': {
|
|
|
|
result = this.executeSync(node.expression, depth + 1);
|
|
|
|
if (result.yield) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
break;
|
2024-06-16 08:01:01 -05:00
|
|
|
}
|
2024-06-30 10:03:50 -05:00
|
|
|
case 'ForStatement': {
|
|
|
|
const shouldVisitChild = (child) => (
|
|
|
|
isReplaying
|
|
|
|
? (!this.$$execution.stack[depth + 1] || this.$$execution.stack[depth + 1] === child)
|
|
|
|
: true
|
|
|
|
);
|
|
|
|
if (shouldVisitChild(node.init)) {
|
|
|
|
const init = this.executeSync(node.init, depth + 1);
|
|
|
|
if (init.yield) {
|
|
|
|
return init;
|
|
|
|
}
|
|
|
|
}
|
2024-06-30 13:05:53 -05:00
|
|
|
if (this.$$execution.stack[depth + 1] === node) {
|
2024-06-30 10:03:50 -05:00
|
|
|
this.$$execution.stack.pop();
|
|
|
|
}
|
|
|
|
result = this.$$execution.deferred.get(node.body) || {
|
|
|
|
value: undefined,
|
|
|
|
yield: YIELD_NONE,
|
|
|
|
};
|
|
|
|
if (shouldVisitChild(node.test)) {
|
|
|
|
const test = this.executeSync(node.test, depth + 1);
|
|
|
|
if (test.yield) {
|
|
|
|
return test;
|
|
|
|
}
|
|
|
|
if (!test.value) {
|
|
|
|
this.$$execution.deferred.delete(node.body);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (shouldVisitChild(node.body)) {
|
|
|
|
const body = this.executeSync(node.body, depth + 1);
|
|
|
|
if (body.yield) {
|
2024-07-12 18:32:55 -05:00
|
|
|
if (YIELD_BREAK === body.yield) {
|
|
|
|
this.$$execution.deferred.delete(node.body);
|
|
|
|
break;
|
|
|
|
}
|
2024-06-30 10:03:50 -05:00
|
|
|
return body;
|
|
|
|
}
|
|
|
|
this.$$execution.deferred.set(node.body, body);
|
|
|
|
}
|
|
|
|
if (shouldVisitChild(node.update)) {
|
|
|
|
const update = this.executeSync(node.update, depth + 1);
|
|
|
|
if (update.yield) {
|
|
|
|
return update;
|
|
|
|
}
|
|
|
|
this.$$execution.deferred.set(node.update, update);
|
|
|
|
}
|
|
|
|
// Yield
|
2024-06-30 13:05:53 -05:00
|
|
|
this.$$execution.stack.push(node);
|
2024-06-30 10:03:50 -05:00
|
|
|
const update = this.$$execution.deferred.get(node.update);
|
|
|
|
this.$$execution.deferred.delete(node.update);
|
|
|
|
return {
|
|
|
|
value: update.value,
|
|
|
|
yield: YIELD_LOOP_UPDATE,
|
|
|
|
};
|
2024-07-12 03:31:22 -05:00
|
|
|
}
|
|
|
|
case 'ForOfStatement': {
|
|
|
|
const shouldVisitChild = (child) => (
|
|
|
|
isReplaying
|
|
|
|
? (!this.$$execution.stack[depth + 1] || this.$$execution.stack[depth + 1] === child)
|
|
|
|
: true
|
|
|
|
);
|
|
|
|
const scope = this.scopes.get(node);
|
|
|
|
if (shouldVisitChild(node.right)) {
|
|
|
|
const right = this.executeSync(node.right, depth + 1);
|
|
|
|
if (right.yield) {
|
|
|
|
return right;
|
|
|
|
}
|
|
|
|
scope.allocate('@@iterator', right.value[Symbol.iterator]());
|
|
|
|
}
|
|
|
|
result = this.$$execution.deferred.get(node.body) || {
|
|
|
|
value: undefined,
|
|
|
|
yield: YIELD_NONE,
|
|
|
|
};
|
2024-07-12 17:34:57 -05:00
|
|
|
if (shouldVisitChild(node)) {
|
|
|
|
this.$$execution.deferred.set(node.left, scope.get('@@iterator').next());
|
|
|
|
}
|
|
|
|
if (this.$$execution.stack[depth + 1] === node) {
|
|
|
|
this.$$execution.stack.pop();
|
|
|
|
}
|
|
|
|
const {done, value} = this.$$execution.deferred.get(node.left);
|
2024-07-12 03:31:22 -05:00
|
|
|
if (done) {
|
2024-07-12 18:32:55 -05:00
|
|
|
this.$$execution.deferred.delete(node.left);
|
2024-07-12 03:31:22 -05:00
|
|
|
this.$$execution.deferred.delete(node.body);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (node.left.type) {
|
|
|
|
case 'ArrayPattern': {
|
|
|
|
this.destructureArray(node.left, value)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'ObjectPattern': {
|
|
|
|
this.destructureObject(node.left, value)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'VariableDeclaration': {
|
|
|
|
const [declaration] = node.left.declarations;
|
|
|
|
switch (declaration.id.type) {
|
|
|
|
case 'Identifier': {
|
|
|
|
scope.set(declaration.id.name, value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'ArrayPattern': {
|
|
|
|
this.destructureArray(declaration.id, value)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'ObjectPattern': {
|
|
|
|
this.destructureObject(declaration.id, value)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'Identifier': {
|
|
|
|
scope.set(node.left.name, value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (shouldVisitChild(node.body)) {
|
|
|
|
const body = this.executeSync(node.body, depth + 1);
|
|
|
|
if (body.yield) {
|
2024-07-12 18:32:55 -05:00
|
|
|
if (YIELD_BREAK === body.yield) {
|
|
|
|
this.$$execution.deferred.delete(node.left);
|
|
|
|
this.$$execution.deferred.delete(node.body);
|
|
|
|
break;
|
|
|
|
}
|
2024-07-12 03:31:22 -05:00
|
|
|
return body;
|
|
|
|
}
|
|
|
|
this.$$execution.deferred.set(node.body, body);
|
|
|
|
}
|
|
|
|
// Yield
|
|
|
|
this.$$execution.stack.push(node);
|
|
|
|
return {
|
|
|
|
value: undefined,
|
|
|
|
yield: YIELD_LOOP_UPDATE,
|
|
|
|
};
|
2024-06-16 08:01:01 -05:00
|
|
|
}
|
2024-06-30 10:03:50 -05:00
|
|
|
case 'IfStatement': {
|
2024-06-30 13:05:53 -05:00
|
|
|
const shouldVisitChild = (child) => (
|
|
|
|
isReplaying
|
|
|
|
? (!this.$$execution.stack[depth + 1] || this.$$execution.stack[depth + 1] === child)
|
|
|
|
: true
|
|
|
|
);
|
2024-06-30 11:27:42 -05:00
|
|
|
result = {
|
|
|
|
value: undefined,
|
2024-06-30 13:05:53 -05:00
|
|
|
yield: YIELD_NONE,
|
2024-06-30 11:27:42 -05:00
|
|
|
};
|
2024-06-30 13:05:53 -05:00
|
|
|
if (shouldVisitChild(node.test)) {
|
|
|
|
const test = this.executeSync(node.test, depth + 1);
|
|
|
|
if (test.yield) {
|
|
|
|
return test;
|
|
|
|
}
|
|
|
|
this.$$execution.deferred.set(node.test, test);
|
|
|
|
}
|
|
|
|
const test = this.$$execution.deferred.get(node.test);
|
2024-06-30 10:03:50 -05:00
|
|
|
if (test.value) {
|
|
|
|
result = this.executeSync(node.consequent, depth + 1);
|
2024-06-16 08:01:01 -05:00
|
|
|
}
|
2024-06-30 11:27:42 -05:00
|
|
|
else if (node.alternate) {
|
2024-06-30 10:03:50 -05:00
|
|
|
result = this.executeSync(node.alternate, depth + 1);
|
2024-06-16 08:01:01 -05:00
|
|
|
}
|
2024-06-30 10:03:50 -05:00
|
|
|
if (result.yield) {
|
|
|
|
return result;
|
|
|
|
}
|
2024-06-30 13:05:53 -05:00
|
|
|
this.$$execution.deferred.delete(node.test);
|
2024-06-30 10:03:50 -05:00
|
|
|
break;
|
2024-06-16 08:01:01 -05:00
|
|
|
}
|
2024-06-30 10:03:50 -05:00
|
|
|
case 'Literal': {
|
|
|
|
result = {value: node.value, yield: YIELD_NONE};
|
|
|
|
break;
|
2024-06-16 08:01:01 -05:00
|
|
|
}
|
2024-06-30 10:03:50 -05:00
|
|
|
case 'Program': {
|
2024-07-03 21:57:52 -05:00
|
|
|
result = {value: undefined, yield: YIELD_NONE};
|
2024-06-30 10:03:50 -05:00
|
|
|
let skipping = isReplaying;
|
|
|
|
for (const child of node.body) {
|
|
|
|
if (skipping && child === this.$$execution.stack[depth + 1]) {
|
|
|
|
skipping = false;
|
|
|
|
}
|
|
|
|
if (skipping) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
result = this.executeSync(child, depth + 1);
|
|
|
|
if (result.yield) {
|
|
|
|
return result;
|
|
|
|
}
|
2024-06-16 08:01:01 -05:00
|
|
|
}
|
2024-06-30 13:05:53 -05:00
|
|
|
result = {value: result.value, yield: YIELD_RETURN};
|
2024-06-30 10:03:50 -05:00
|
|
|
break;
|
2024-06-16 08:01:01 -05:00
|
|
|
}
|
2024-06-30 10:03:50 -05:00
|
|
|
case 'ReturnStatement': {
|
|
|
|
if (!node.argument) {
|
|
|
|
return {
|
|
|
|
value: undefined,
|
|
|
|
yield: YIELD_RETURN,
|
|
|
|
};
|
2024-06-16 08:01:01 -05:00
|
|
|
}
|
2024-06-30 10:03:50 -05:00
|
|
|
const argument = this.executeSync(node.argument, depth + 1);
|
|
|
|
if (argument.yield) {
|
|
|
|
return argument;
|
2024-06-18 22:29:40 -05:00
|
|
|
}
|
2024-06-30 13:05:53 -05:00
|
|
|
result = {
|
2024-06-30 10:03:50 -05:00
|
|
|
value: argument.value,
|
|
|
|
yield: YIELD_RETURN,
|
|
|
|
};
|
2024-06-30 13:05:53 -05:00
|
|
|
break;
|
2024-06-16 08:01:01 -05:00
|
|
|
}
|
2024-07-25 06:29:57 -05:00
|
|
|
case 'UnaryExpression': {
|
|
|
|
if ('delete' === node.operator) {
|
|
|
|
let property;
|
|
|
|
if (node.argument.computed) {
|
|
|
|
property = this.executeSync(node.argument.property, depth + 1);
|
|
|
|
if (property.yield) {
|
|
|
|
return property;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
property = {value: node.argument.property.name, yield: YIELD_NONE};
|
|
|
|
}
|
|
|
|
const scope = this.scopes.get(node);
|
|
|
|
const object = scope.get(node.argument.object.name, undefined);
|
|
|
|
delete object[property.value];
|
|
|
|
result = {value: true, yield: YIELD_NONE};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = this.evaluateToResult(node);
|
|
|
|
if (result.yield) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2024-06-30 10:03:50 -05:00
|
|
|
case 'VariableDeclaration': {
|
|
|
|
let skipping = isReplaying;
|
|
|
|
for (const child of node.declarations) {
|
|
|
|
if (skipping && child === this.$$execution.stack[depth + 1]) {
|
|
|
|
skipping = false;
|
|
|
|
}
|
2024-06-30 14:20:37 -05:00
|
|
|
/* v8 ignore next 3 */
|
2024-06-30 10:03:50 -05:00
|
|
|
if (skipping) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const result = this.executeSync(child, depth + 1);
|
|
|
|
if (result.yield) {
|
|
|
|
return result;
|
|
|
|
}
|
2024-06-16 08:01:01 -05:00
|
|
|
}
|
2024-06-30 10:03:50 -05:00
|
|
|
result = {value: undefined, yield: YIELD_NONE};
|
|
|
|
break;
|
2024-06-16 08:01:01 -05:00
|
|
|
}
|
2024-06-30 10:03:50 -05:00
|
|
|
case 'VariableDeclarator': {
|
|
|
|
const {id} = node;
|
|
|
|
const scope = this.scopes.get(node);
|
|
|
|
if (null === node.init) {
|
|
|
|
scope.allocate(id.name, undefined);
|
|
|
|
result = {value: undefined, yield: YIELD_NONE};
|
2024-06-16 08:01:01 -05:00
|
|
|
}
|
|
|
|
else {
|
2024-06-30 10:03:50 -05:00
|
|
|
const init = this.executeSync(node.init, depth + 1);
|
2024-06-30 14:20:37 -05:00
|
|
|
if ('Identifier' === id.type) {
|
2024-06-30 10:03:50 -05:00
|
|
|
if (init.yield) {
|
|
|
|
return {
|
|
|
|
value: Promise.resolve(init.value)
|
|
|
|
.then((value) => scope.allocate(id.name, value)),
|
|
|
|
yield: init.yield,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = {
|
|
|
|
value: scope.allocate(id.name, init.value),
|
|
|
|
yield: YIELD_NONE,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2024-06-30 14:20:37 -05:00
|
|
|
else if ('ArrayPattern' === id.type) {
|
2024-06-30 10:03:50 -05:00
|
|
|
if (init.yield) {
|
|
|
|
return {
|
|
|
|
value: Promise.resolve(init.value)
|
|
|
|
.then((value) => this.destructureArray(id, value)),
|
|
|
|
yield: init.yield,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = {
|
|
|
|
value: this.destructureArray(id, init.value),
|
|
|
|
yield: YIELD_NONE,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2024-06-30 14:20:37 -05:00
|
|
|
else if ('ObjectPattern' === id.type) {
|
2024-06-30 10:03:50 -05:00
|
|
|
if (init.yield) {
|
|
|
|
return {
|
|
|
|
value: Promise.resolve(init.value)
|
|
|
|
.then((value) => this.destructureObject(id, value)),
|
|
|
|
yield: init.yield,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = {
|
|
|
|
value: this.destructureObject(id, init.value),
|
|
|
|
yield: YIELD_NONE,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2024-06-16 08:01:01 -05:00
|
|
|
}
|
2024-06-30 10:03:50 -05:00
|
|
|
break;
|
2024-06-16 08:01:01 -05:00
|
|
|
}
|
2024-06-30 10:03:50 -05:00
|
|
|
case 'WhileStatement': {
|
|
|
|
const shouldVisitChild = (child) => (
|
|
|
|
isReplaying
|
|
|
|
? (!this.$$execution.stack[depth + 1] || this.$$execution.stack[depth + 1] === child)
|
|
|
|
: true
|
|
|
|
);
|
2024-06-30 13:05:53 -05:00
|
|
|
if (this.$$execution.stack[depth + 1] === node) {
|
2024-06-30 10:03:50 -05:00
|
|
|
this.$$execution.stack.pop();
|
|
|
|
}
|
|
|
|
result = this.$$execution.deferred.get(node.body) || {
|
|
|
|
value: undefined,
|
|
|
|
yield: YIELD_NONE,
|
|
|
|
};
|
|
|
|
if (shouldVisitChild(node.test)) {
|
|
|
|
const test = this.executeSync(node.test, depth + 1);
|
|
|
|
if (test.yield) {
|
|
|
|
return test;
|
|
|
|
}
|
|
|
|
if (!test.value) {
|
|
|
|
this.$$execution.deferred.delete(node.body);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (shouldVisitChild(node.body)) {
|
|
|
|
const body = this.executeSync(node.body, depth + 1);
|
|
|
|
if (body.yield) {
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
this.$$execution.deferred.set(node.body, body);
|
|
|
|
}
|
|
|
|
// Yield
|
2024-06-30 13:05:53 -05:00
|
|
|
this.$$execution.stack.push(node);
|
2024-06-30 10:03:50 -05:00
|
|
|
return {
|
|
|
|
value: undefined,
|
|
|
|
yield: YIELD_LOOP_UPDATE,
|
|
|
|
};
|
|
|
|
}
|
2024-06-30 14:20:37 -05:00
|
|
|
/* v8 ignore next 7 */
|
2024-06-30 10:03:50 -05:00
|
|
|
default:
|
|
|
|
console.log(
|
|
|
|
node.type,
|
|
|
|
Object.keys(node)
|
|
|
|
.filter((key) => !['start', 'end'].includes(key)),
|
|
|
|
);
|
|
|
|
throw new Error('not implemented');
|
2024-06-16 08:01:01 -05:00
|
|
|
}
|
2024-06-30 10:03:50 -05:00
|
|
|
this.$$execution.stack.pop();
|
|
|
|
return result;
|
2024-06-16 08:01:01 -05:00
|
|
|
}
|
|
|
|
|
2024-06-27 13:56:43 -05:00
|
|
|
reset() {
|
2024-06-30 13:05:53 -05:00
|
|
|
this.$$execution = undefined;
|
2024-06-29 12:35:54 -05:00
|
|
|
for (const scope of this.scopes.values()) {
|
|
|
|
scope.context = {};
|
|
|
|
}
|
|
|
|
this.scopes.get(this.ast).context = this.$$context;
|
2024-06-27 13:56:43 -05:00
|
|
|
}
|
|
|
|
|
2024-06-16 08:01:01 -05:00
|
|
|
run(ops = 1000) {
|
|
|
|
let result;
|
|
|
|
for (let i = 0; i < ops; ++i) {
|
|
|
|
result = this.step();
|
2024-06-30 13:05:53 -05:00
|
|
|
if (result.done || result.async) {
|
2024-06-16 08:01:01 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
step() {
|
2024-06-30 10:03:50 -05:00
|
|
|
if (!this.$$execution) {
|
|
|
|
this.$$execution = {
|
|
|
|
deferred: new Map(),
|
|
|
|
stack: [],
|
|
|
|
};
|
2024-06-16 08:01:01 -05:00
|
|
|
}
|
2024-07-28 10:37:07 -05:00
|
|
|
const result = this.executeSync(this.ast, 0);
|
2024-06-30 10:03:50 -05:00
|
|
|
const stepResult = {async: false, done: false, value: undefined};
|
|
|
|
switch (result.yield) {
|
|
|
|
case YIELD_PROMISE: {
|
|
|
|
stepResult.async = true;
|
2024-08-01 13:00:21 -05:00
|
|
|
const promise = result.value instanceof Promise
|
|
|
|
? result.value
|
|
|
|
: Promise.resolve(result.value);
|
|
|
|
promise
|
2024-06-30 13:05:53 -05:00
|
|
|
.then((value) => {
|
|
|
|
const top = this.$$execution.stack[this.$$execution.stack.length - 1];
|
|
|
|
this.$$execution.deferred.set(top, {
|
|
|
|
value,
|
|
|
|
yield: YIELD_NONE,
|
|
|
|
});
|
2024-06-30 10:03:50 -05:00
|
|
|
});
|
2024-08-01 13:00:21 -05:00
|
|
|
stepResult.value = promise;
|
2024-06-30 10:03:50 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case YIELD_LOOP_UPDATE: {
|
|
|
|
stepResult.value = result.value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case YIELD_RETURN: {
|
|
|
|
stepResult.done = true;
|
|
|
|
stepResult.value = result.value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (stepResult.done) {
|
2024-06-27 13:56:43 -05:00
|
|
|
this.reset();
|
2024-06-16 08:01:01 -05:00
|
|
|
}
|
2024-06-30 10:03:50 -05:00
|
|
|
return stepResult;
|
2024-06-16 08:01:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|