@enclave-vm/core 2.19.0 → 2.19.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/adapters/interpreter-adapter.d.ts +10 -1
- package/esm/index.mjs +103 -34
- package/esm/worker.mjs +106 -37
- package/index.js +103 -34
- package/interpreter/interpreter.d.ts +43 -2
- package/package.json +3 -3
- package/src/adapters/worker-pool/package.json +3 -3
- package/worker.js +106 -37
|
@@ -2,8 +2,17 @@ import type { ExecutionContext, ExecutionResult, SandboxAdapter } from '../types
|
|
|
2
2
|
export interface InterpreterAdapterOptions {
|
|
3
3
|
/** Override the instruction budget (default 5,000,000). */
|
|
4
4
|
maxSteps?: number;
|
|
5
|
-
/**
|
|
5
|
+
/**
|
|
6
|
+
* How deep the calls of a script may nest (default 256). Counted along each chain of calls: a
|
|
7
|
+
* callback is a call of the function that invokes it, and the tasks of a `parallel` batch each
|
|
8
|
+
* continue the chain that started the batch, so a batch does not add up.
|
|
9
|
+
*/
|
|
6
10
|
maxCallDepth?: number;
|
|
11
|
+
/**
|
|
12
|
+
* How many calls of an execution may be unfinished at once, running or waiting, over all its
|
|
13
|
+
* chains (default 4096). It bounds what the waiting calls of nested `parallel` batches hold.
|
|
14
|
+
*/
|
|
15
|
+
maxPendingCalls?: number;
|
|
7
16
|
/**
|
|
8
17
|
* The most a script may allocate, in bytes, as the interpreter estimates it (default 64 MB).
|
|
9
18
|
* An execution's own `memoryLimit` applies when it is lower.
|
package/esm/index.mjs
CHANGED
|
@@ -14839,7 +14839,14 @@ var BLOCKED_KEYS = /* @__PURE__ */ new Set([
|
|
|
14839
14839
|
"__lookupSetter__"
|
|
14840
14840
|
]);
|
|
14841
14841
|
function isBlockedKey(key) {
|
|
14842
|
-
return BLOCKED_KEYS.has(key);
|
|
14842
|
+
return key === "__proto__" || BLOCKED_KEYS.has(key);
|
|
14843
|
+
}
|
|
14844
|
+
function assertKeyAllowed(key) {
|
|
14845
|
+
if (isBlockedKey(key)) throw new InterpreterError(`Forbidden property access: ${key}`);
|
|
14846
|
+
}
|
|
14847
|
+
function readProperty2(object, key) {
|
|
14848
|
+
if (key === "__proto__" || BLOCKED_KEYS.has(key)) throw new InterpreterError(`Forbidden property access: ${key}`);
|
|
14849
|
+
return object[key];
|
|
14843
14850
|
}
|
|
14844
14851
|
var MAX_STRING_OP_LENGTH = 1e6;
|
|
14845
14852
|
var MAX_JOIN_LENGTH = 16e6;
|
|
@@ -15026,13 +15033,23 @@ function isScriptError(value) {
|
|
|
15026
15033
|
return typeof value === "object" && value !== null && SCRIPT_ERRORS.has(value);
|
|
15027
15034
|
}
|
|
15028
15035
|
var Scope = class _Scope {
|
|
15029
|
-
constructor(parent) {
|
|
15036
|
+
constructor(parent, depth) {
|
|
15030
15037
|
this.parent = parent;
|
|
15038
|
+
this.depth = depth ?? parent?.depth ?? 0;
|
|
15031
15039
|
}
|
|
15032
15040
|
vars = /* @__PURE__ */ new Map();
|
|
15041
|
+
/**
|
|
15042
|
+
* How many calls deep the code of this scope runs: 0 for the program, and for the body of a
|
|
15043
|
+
* function one more than where it was called from. A block inside has the depth around it.
|
|
15044
|
+
*/
|
|
15045
|
+
depth;
|
|
15033
15046
|
child() {
|
|
15034
15047
|
return new _Scope(this);
|
|
15035
15048
|
}
|
|
15049
|
+
/** The scope of one call of a function defined in this scope, running `depth` calls deep. */
|
|
15050
|
+
activation(depth) {
|
|
15051
|
+
return new _Scope(this, depth);
|
|
15052
|
+
}
|
|
15036
15053
|
declare(name, value, mutable) {
|
|
15037
15054
|
this.vars.set(name, { value, mutable });
|
|
15038
15055
|
}
|
|
@@ -15059,15 +15076,28 @@ var Scope = class _Scope {
|
|
|
15059
15076
|
throw new InterpreterError(`'${name}' is not defined`);
|
|
15060
15077
|
}
|
|
15061
15078
|
};
|
|
15079
|
+
var DEFAULT_MAX_PENDING_CALLS = 4096;
|
|
15062
15080
|
function ignoreRejection2() {
|
|
15063
15081
|
}
|
|
15064
15082
|
var Interpreter = class {
|
|
15065
15083
|
constructor(options) {
|
|
15066
15084
|
this.options = options;
|
|
15067
15085
|
this.globals = options.globals;
|
|
15086
|
+
this.maxPendingCalls = options.maxPendingCalls ?? DEFAULT_MAX_PENDING_CALLS;
|
|
15068
15087
|
}
|
|
15069
15088
|
steps = 0;
|
|
15070
|
-
|
|
15089
|
+
/** Calls that have not returned: the running ones and the ones waiting on an `await` */
|
|
15090
|
+
pendingCalls = 0;
|
|
15091
|
+
/**
|
|
15092
|
+
* The depth of whatever is making a call right now, for the function of the script that call
|
|
15093
|
+
* starts. A function of the script is a host function too, and a built-in, `parallel` or a
|
|
15094
|
+
* bound function invokes it without saying how deep it is: it reads this as it starts, in the
|
|
15095
|
+
* same synchronous step as the call, and runs one deeper. Set by {@link withCallerDepth} for
|
|
15096
|
+
* the synchronous part of a call only; a call that is waiting does not hold it, which is what
|
|
15097
|
+
* keeps the depth of one chain apart from the depth of another. 0 when the host itself calls.
|
|
15098
|
+
*/
|
|
15099
|
+
callerDepth = 0;
|
|
15100
|
+
maxPendingCalls;
|
|
15071
15101
|
globals;
|
|
15072
15102
|
/** The error that ended the run (step budget, abort, allocation budget); set once, never caught */
|
|
15073
15103
|
fatal;
|
|
@@ -15081,7 +15111,7 @@ var Interpreter = class {
|
|
|
15081
15111
|
call(fn, args) {
|
|
15082
15112
|
if (typeof fn !== "function") throw new InterpreterError("Attempted to call a non-function");
|
|
15083
15113
|
if (this.interpretedFunctions.has(fn)) return fn(...args);
|
|
15084
|
-
return this.callFunction(fn, void 0, args);
|
|
15114
|
+
return this.callFunction(fn, void 0, args, this.callerDepth);
|
|
15085
15115
|
}
|
|
15086
15116
|
/** Number of AST nodes evaluated so far (for execution stats). */
|
|
15087
15117
|
get stepCount() {
|
|
@@ -15403,7 +15433,7 @@ var Interpreter = class {
|
|
|
15403
15433
|
}
|
|
15404
15434
|
case "MemberExpression": {
|
|
15405
15435
|
const { object, key } = await this.evalMemberTarget(node, scope);
|
|
15406
|
-
return object
|
|
15436
|
+
return readProperty2(object, key);
|
|
15407
15437
|
}
|
|
15408
15438
|
case "CallExpression":
|
|
15409
15439
|
return this.evalCall(node, scope);
|
|
@@ -15442,6 +15472,7 @@ var Interpreter = class {
|
|
|
15442
15472
|
if (node.argument.type !== "MemberExpression") throw new InterpreterError("Only a property can be deleted");
|
|
15443
15473
|
const { object, key } = await this.evalMemberTarget(node.argument, scope);
|
|
15444
15474
|
this.assertChangeable(object);
|
|
15475
|
+
assertKeyAllowed(key);
|
|
15445
15476
|
return delete object[key];
|
|
15446
15477
|
}
|
|
15447
15478
|
const v = await this.evalExpr(node.argument, scope);
|
|
@@ -15564,7 +15595,7 @@ var Interpreter = class {
|
|
|
15564
15595
|
if (node.type === "MemberExpression") {
|
|
15565
15596
|
const { object, key } = await this.evalMemberTarget(node, scope);
|
|
15566
15597
|
return {
|
|
15567
|
-
get: () => object
|
|
15598
|
+
get: () => readProperty2(object, key),
|
|
15568
15599
|
set: (value) => this.writeMember(object, key, value)
|
|
15569
15600
|
};
|
|
15570
15601
|
}
|
|
@@ -15583,6 +15614,7 @@ var Interpreter = class {
|
|
|
15583
15614
|
}
|
|
15584
15615
|
writeMember(object, key, value) {
|
|
15585
15616
|
this.assertChangeable(object);
|
|
15617
|
+
assertKeyAllowed(key);
|
|
15586
15618
|
if (Array.isArray(object)) {
|
|
15587
15619
|
const grownTo = arrayLengthAfterWrite(object, key, value);
|
|
15588
15620
|
if (grownTo > MAX_ARRAY_LENGTH2) {
|
|
@@ -15628,7 +15660,7 @@ var Interpreter = class {
|
|
|
15628
15660
|
let member;
|
|
15629
15661
|
if (node.callee.type === "MemberExpression") {
|
|
15630
15662
|
const target = await this.evalMemberTarget(node.callee, scope);
|
|
15631
|
-
member = { ...target, value: target.object
|
|
15663
|
+
member = { ...target, value: readProperty2(target.object, target.key) };
|
|
15632
15664
|
fn = member.value;
|
|
15633
15665
|
thisArg = member.object;
|
|
15634
15666
|
} else if (node.callee.type === "Super") {
|
|
@@ -15642,14 +15674,16 @@ var Interpreter = class {
|
|
|
15642
15674
|
}
|
|
15643
15675
|
const args = await this.evalArguments(node.arguments, scope);
|
|
15644
15676
|
if (typeof fn !== "function") throw new InterpreterError("Attempted to call a non-function");
|
|
15645
|
-
return this.callFunction(fn, thisArg, args);
|
|
15677
|
+
return this.callFunction(fn, thisArg, args, scope.depth);
|
|
15646
15678
|
}
|
|
15647
15679
|
/**
|
|
15648
15680
|
* Call a function with a receiver and arguments: an interpreted function, or a built-in or host
|
|
15649
15681
|
* function after the checks that apply to it. `call` and `apply` are resolved to the function
|
|
15650
15682
|
* they invoke first, so those checks cannot be passed by the way a call is written.
|
|
15683
|
+
*
|
|
15684
|
+
* @param depth - How many calls deep the code that makes this call runs
|
|
15651
15685
|
*/
|
|
15652
|
-
async callFunction(fn, thisArg, args) {
|
|
15686
|
+
async callFunction(fn, thisArg, args, depth) {
|
|
15653
15687
|
for (let hops = 0; fn === FunctionProtoCall || fn === FunctionProtoApply || fn === FunctionProtoBind; hops++) {
|
|
15654
15688
|
const name = fn === FunctionProtoCall ? "call" : fn === FunctionProtoApply ? "apply" : "bind";
|
|
15655
15689
|
if (typeof thisArg !== "function" || hops >= 8) {
|
|
@@ -15665,20 +15699,20 @@ var Interpreter = class {
|
|
|
15665
15699
|
fn = target;
|
|
15666
15700
|
}
|
|
15667
15701
|
if (typeof fn !== "function") throw new InterpreterError("Attempted to call a non-function");
|
|
15668
|
-
if (
|
|
15669
|
-
|
|
15670
|
-
|
|
15702
|
+
if (this.interpretedFunctions.has(fn)) {
|
|
15703
|
+
const interpreted = fn;
|
|
15704
|
+
return await this.withCallerDepth(depth, () => interpreted.apply(thisArg, args));
|
|
15671
15705
|
}
|
|
15706
|
+
const own = this.enterCall(depth);
|
|
15672
15707
|
try {
|
|
15673
|
-
if (this.interpretedFunctions.has(fn)) {
|
|
15674
|
-
return await fn.apply(thisArg, args);
|
|
15675
|
-
}
|
|
15676
15708
|
args = this.checkBuiltinCall(fn, thisArg, args);
|
|
15677
15709
|
const arrayMethod = ARRAY_METHODS.get(fn);
|
|
15678
15710
|
if (arrayMethod !== void 0 && HIGHER_ORDER_ARRAY_METHODS.has(arrayMethod) && typeof args[0] === "function") {
|
|
15679
|
-
return await this.callArrayMethod(thisArg, arrayMethod, args);
|
|
15711
|
+
return await this.callArrayMethod(thisArg, arrayMethod, args, own);
|
|
15680
15712
|
}
|
|
15681
|
-
const
|
|
15713
|
+
const builtin = fn;
|
|
15714
|
+
const builtinArgs = args;
|
|
15715
|
+
const result = await this.withCallerDepth(own, () => builtin.apply(thisArg, builtinArgs));
|
|
15682
15716
|
if (arrayMethod === void 0 && STRING_METHODS.get(fn) === "split" && Array.isArray(result)) {
|
|
15683
15717
|
if (result.length > MAX_ARRAY_LENGTH2) {
|
|
15684
15718
|
throw new InterpreterError(`An array cannot be longer than ${MAX_ARRAY_LENGTH2} items`);
|
|
@@ -15687,7 +15721,36 @@ var Interpreter = class {
|
|
|
15687
15721
|
this.chargeValue(result);
|
|
15688
15722
|
return result;
|
|
15689
15723
|
} finally {
|
|
15690
|
-
this.
|
|
15724
|
+
this.pendingCalls--;
|
|
15725
|
+
}
|
|
15726
|
+
}
|
|
15727
|
+
/**
|
|
15728
|
+
* Count a call that starts `callerDepth` calls deep, until it returns.
|
|
15729
|
+
*
|
|
15730
|
+
* @returns The depth the call itself runs at
|
|
15731
|
+
*/
|
|
15732
|
+
enterCall(callerDepth) {
|
|
15733
|
+
const depth = callerDepth + 1;
|
|
15734
|
+
if (depth > this.options.maxCallDepth) {
|
|
15735
|
+
throw new InterpreterError(`Max call depth exceeded (${this.options.maxCallDepth})`);
|
|
15736
|
+
}
|
|
15737
|
+
if (this.pendingCalls >= this.maxPendingCalls) {
|
|
15738
|
+
throw new InterpreterError(`Too many calls in progress at once (${this.maxPendingCalls})`);
|
|
15739
|
+
}
|
|
15740
|
+
this.pendingCalls++;
|
|
15741
|
+
return depth;
|
|
15742
|
+
}
|
|
15743
|
+
/**
|
|
15744
|
+
* Make a call with `depth` as the depth a function of the script is called from, if one starts
|
|
15745
|
+
* during it. It covers the synchronous part of the call, which is where a function starts.
|
|
15746
|
+
*/
|
|
15747
|
+
withCallerDepth(depth, call) {
|
|
15748
|
+
const previous = this.callerDepth;
|
|
15749
|
+
this.callerDepth = depth;
|
|
15750
|
+
try {
|
|
15751
|
+
return call();
|
|
15752
|
+
} finally {
|
|
15753
|
+
this.callerDepth = previous;
|
|
15691
15754
|
}
|
|
15692
15755
|
}
|
|
15693
15756
|
/** The argument list of `apply`: an array, or nothing. */
|
|
@@ -15704,7 +15767,7 @@ var Interpreter = class {
|
|
|
15704
15767
|
bindFunction(target, boundThis, boundArgs) {
|
|
15705
15768
|
this.charge((boundArgs.length + 2) * 8);
|
|
15706
15769
|
return (...rest) => {
|
|
15707
|
-
const result = this.callFunction(target, boundThis, [...boundArgs, ...rest]);
|
|
15770
|
+
const result = this.callFunction(target, boundThis, [...boundArgs, ...rest], this.callerDepth);
|
|
15708
15771
|
result.then(void 0, ignoreRejection2);
|
|
15709
15772
|
return result;
|
|
15710
15773
|
};
|
|
@@ -15867,9 +15930,10 @@ var Interpreter = class {
|
|
|
15867
15930
|
* callbacks (which are async — they may `await` tool calls) are awaited. Each
|
|
15868
15931
|
* iteration ticks the step budget.
|
|
15869
15932
|
*/
|
|
15870
|
-
async callArrayMethod(arr, method, args) {
|
|
15933
|
+
async callArrayMethod(arr, method, args, depth) {
|
|
15871
15934
|
const callback = args[0];
|
|
15872
|
-
const
|
|
15935
|
+
const interpreted = this.interpretedFunctions.has(callback) ? callback : void 0;
|
|
15936
|
+
const invoke = interpreted ? (...callbackArgs) => this.withCallerDepth(depth, () => interpreted(...callbackArgs)) : (...callbackArgs) => this.callFunction(callback, void 0, callbackArgs, depth);
|
|
15873
15937
|
const cb = invoke;
|
|
15874
15938
|
const each = (i) => {
|
|
15875
15939
|
this.tick();
|
|
@@ -16045,7 +16109,7 @@ var Interpreter = class {
|
|
|
16045
16109
|
else throw new InterpreterError("Unsupported destructuring key");
|
|
16046
16110
|
if (BLOCKED_KEYS.has(key)) throw new InterpreterError(`Forbidden destructured key: ${key}`);
|
|
16047
16111
|
taken.add(key);
|
|
16048
|
-
await this.applyPattern(prop.value, source
|
|
16112
|
+
await this.applyPattern(prop.value, readProperty2(source, key), scope, mode);
|
|
16049
16113
|
}
|
|
16050
16114
|
return;
|
|
16051
16115
|
}
|
|
@@ -16058,17 +16122,17 @@ var Interpreter = class {
|
|
|
16058
16122
|
if (node.generator) throw new InterpreterError("Generator functions are not supported");
|
|
16059
16123
|
const selfName = node.type === "FunctionExpression" && node.id ? node.id.name : void 0;
|
|
16060
16124
|
const outer = selfName === void 0 ? closure : closure.child();
|
|
16061
|
-
const call = async (args) => {
|
|
16062
|
-
const fnScope = outer.
|
|
16063
|
-
for (let i = 0; i < node.params.length; i++) {
|
|
16064
|
-
const param = node.params[i];
|
|
16065
|
-
if (param.type === "RestElement") {
|
|
16066
|
-
await this.applyPattern(param.argument, args.slice(i), fnScope, "let");
|
|
16067
|
-
break;
|
|
16068
|
-
}
|
|
16069
|
-
await this.applyPattern(param, args[i], fnScope, "let");
|
|
16070
|
-
}
|
|
16125
|
+
const call = async (args, callerDepth) => {
|
|
16126
|
+
const fnScope = outer.activation(this.enterCall(callerDepth));
|
|
16071
16127
|
try {
|
|
16128
|
+
for (let i = 0; i < node.params.length; i++) {
|
|
16129
|
+
const param = node.params[i];
|
|
16130
|
+
if (param.type === "RestElement") {
|
|
16131
|
+
await this.applyPattern(param.argument, args.slice(i), fnScope, "let");
|
|
16132
|
+
break;
|
|
16133
|
+
}
|
|
16134
|
+
await this.applyPattern(param, args[i], fnScope, "let");
|
|
16135
|
+
}
|
|
16072
16136
|
if (node.body.type === "BlockStatement") {
|
|
16073
16137
|
await this.execBlock(node.body.body, fnScope);
|
|
16074
16138
|
return void 0;
|
|
@@ -16077,10 +16141,13 @@ var Interpreter = class {
|
|
|
16077
16141
|
} catch (e) {
|
|
16078
16142
|
if (e instanceof ReturnSignal) return e.value;
|
|
16079
16143
|
throw e;
|
|
16144
|
+
} finally {
|
|
16145
|
+
this.pendingCalls--;
|
|
16080
16146
|
}
|
|
16081
16147
|
};
|
|
16148
|
+
const depthOfCaller = () => this.callerDepth;
|
|
16082
16149
|
const interpreted = function interpreted2(...args) {
|
|
16083
|
-
const result = call(args);
|
|
16150
|
+
const result = call(args, depthOfCaller());
|
|
16084
16151
|
result.then(void 0, ignoreRejection2);
|
|
16085
16152
|
return result;
|
|
16086
16153
|
};
|
|
@@ -16100,7 +16167,7 @@ var Interpreter = class {
|
|
|
16100
16167
|
throw new InterpreterError("Unsupported property key");
|
|
16101
16168
|
}
|
|
16102
16169
|
readGlobal(name) {
|
|
16103
|
-
if (Object.prototype.hasOwnProperty.call(this.globals, name)) return this.globals[name];
|
|
16170
|
+
if (name !== "__proto__" && Object.prototype.hasOwnProperty.call(this.globals, name)) return this.globals[name];
|
|
16104
16171
|
throw new InterpreterError(`'${name}' is not defined`);
|
|
16105
16172
|
}
|
|
16106
16173
|
};
|
|
@@ -16111,6 +16178,7 @@ function isIterable(v) {
|
|
|
16111
16178
|
return v != null && typeof v[Symbol.iterator] === "function";
|
|
16112
16179
|
}
|
|
16113
16180
|
function isScriptData(value) {
|
|
16181
|
+
if (value === Object.prototype || value === Array.prototype) return false;
|
|
16114
16182
|
if (Array.isArray(value)) return true;
|
|
16115
16183
|
if (value === null || typeof value !== "object") return false;
|
|
16116
16184
|
const prototype = Object.getPrototypeOf(value);
|
|
@@ -16552,6 +16620,7 @@ var InterpreterAdapter = class {
|
|
|
16552
16620
|
instanceChecks: safe.instanceChecks,
|
|
16553
16621
|
maxSteps: this.options.maxSteps ?? DEFAULT_MAX_STEPS,
|
|
16554
16622
|
maxCallDepth: this.options.maxCallDepth ?? DEFAULT_MAX_CALL_DEPTH,
|
|
16623
|
+
maxPendingCalls: this.options.maxPendingCalls,
|
|
16555
16624
|
signal: context.abortController.signal,
|
|
16556
16625
|
charge
|
|
16557
16626
|
});
|
package/esm/worker.mjs
CHANGED
|
@@ -107,7 +107,14 @@ var BLOCKED_KEYS = /* @__PURE__ */ new Set([
|
|
|
107
107
|
"__lookupSetter__"
|
|
108
108
|
]);
|
|
109
109
|
function isBlockedKey(key) {
|
|
110
|
-
return BLOCKED_KEYS.has(key);
|
|
110
|
+
return key === "__proto__" || BLOCKED_KEYS.has(key);
|
|
111
|
+
}
|
|
112
|
+
function assertKeyAllowed(key) {
|
|
113
|
+
if (isBlockedKey(key)) throw new InterpreterError(`Forbidden property access: ${key}`);
|
|
114
|
+
}
|
|
115
|
+
function readProperty(object, key) {
|
|
116
|
+
if (key === "__proto__" || BLOCKED_KEYS.has(key)) throw new InterpreterError(`Forbidden property access: ${key}`);
|
|
117
|
+
return object[key];
|
|
111
118
|
}
|
|
112
119
|
var MAX_STRING_OP_LENGTH = 1e6;
|
|
113
120
|
var MAX_JOIN_LENGTH = 16e6;
|
|
@@ -294,13 +301,23 @@ function isScriptError(value) {
|
|
|
294
301
|
return typeof value === "object" && value !== null && SCRIPT_ERRORS.has(value);
|
|
295
302
|
}
|
|
296
303
|
var Scope = class _Scope {
|
|
297
|
-
constructor(parent) {
|
|
304
|
+
constructor(parent, depth) {
|
|
298
305
|
this.parent = parent;
|
|
306
|
+
this.depth = depth ?? parent?.depth ?? 0;
|
|
299
307
|
}
|
|
300
308
|
vars = /* @__PURE__ */ new Map();
|
|
309
|
+
/**
|
|
310
|
+
* How many calls deep the code of this scope runs: 0 for the program, and for the body of a
|
|
311
|
+
* function one more than where it was called from. A block inside has the depth around it.
|
|
312
|
+
*/
|
|
313
|
+
depth;
|
|
301
314
|
child() {
|
|
302
315
|
return new _Scope(this);
|
|
303
316
|
}
|
|
317
|
+
/** The scope of one call of a function defined in this scope, running `depth` calls deep. */
|
|
318
|
+
activation(depth) {
|
|
319
|
+
return new _Scope(this, depth);
|
|
320
|
+
}
|
|
304
321
|
declare(name, value, mutable) {
|
|
305
322
|
this.vars.set(name, { value, mutable });
|
|
306
323
|
}
|
|
@@ -327,15 +344,28 @@ var Scope = class _Scope {
|
|
|
327
344
|
throw new InterpreterError(`'${name}' is not defined`);
|
|
328
345
|
}
|
|
329
346
|
};
|
|
347
|
+
var DEFAULT_MAX_PENDING_CALLS = 4096;
|
|
330
348
|
function ignoreRejection2() {
|
|
331
349
|
}
|
|
332
350
|
var Interpreter = class {
|
|
333
351
|
constructor(options) {
|
|
334
352
|
this.options = options;
|
|
335
353
|
this.globals = options.globals;
|
|
354
|
+
this.maxPendingCalls = options.maxPendingCalls ?? DEFAULT_MAX_PENDING_CALLS;
|
|
336
355
|
}
|
|
337
356
|
steps = 0;
|
|
338
|
-
|
|
357
|
+
/** Calls that have not returned: the running ones and the ones waiting on an `await` */
|
|
358
|
+
pendingCalls = 0;
|
|
359
|
+
/**
|
|
360
|
+
* The depth of whatever is making a call right now, for the function of the script that call
|
|
361
|
+
* starts. A function of the script is a host function too, and a built-in, `parallel` or a
|
|
362
|
+
* bound function invokes it without saying how deep it is: it reads this as it starts, in the
|
|
363
|
+
* same synchronous step as the call, and runs one deeper. Set by {@link withCallerDepth} for
|
|
364
|
+
* the synchronous part of a call only; a call that is waiting does not hold it, which is what
|
|
365
|
+
* keeps the depth of one chain apart from the depth of another. 0 when the host itself calls.
|
|
366
|
+
*/
|
|
367
|
+
callerDepth = 0;
|
|
368
|
+
maxPendingCalls;
|
|
339
369
|
globals;
|
|
340
370
|
/** The error that ended the run (step budget, abort, allocation budget); set once, never caught */
|
|
341
371
|
fatal;
|
|
@@ -349,7 +379,7 @@ var Interpreter = class {
|
|
|
349
379
|
call(fn, args) {
|
|
350
380
|
if (typeof fn !== "function") throw new InterpreterError("Attempted to call a non-function");
|
|
351
381
|
if (this.interpretedFunctions.has(fn)) return fn(...args);
|
|
352
|
-
return this.callFunction(fn, void 0, args);
|
|
382
|
+
return this.callFunction(fn, void 0, args, this.callerDepth);
|
|
353
383
|
}
|
|
354
384
|
/** Number of AST nodes evaluated so far (for execution stats). */
|
|
355
385
|
get stepCount() {
|
|
@@ -671,7 +701,7 @@ var Interpreter = class {
|
|
|
671
701
|
}
|
|
672
702
|
case "MemberExpression": {
|
|
673
703
|
const { object, key } = await this.evalMemberTarget(node, scope);
|
|
674
|
-
return object
|
|
704
|
+
return readProperty(object, key);
|
|
675
705
|
}
|
|
676
706
|
case "CallExpression":
|
|
677
707
|
return this.evalCall(node, scope);
|
|
@@ -710,6 +740,7 @@ var Interpreter = class {
|
|
|
710
740
|
if (node.argument.type !== "MemberExpression") throw new InterpreterError("Only a property can be deleted");
|
|
711
741
|
const { object, key } = await this.evalMemberTarget(node.argument, scope);
|
|
712
742
|
this.assertChangeable(object);
|
|
743
|
+
assertKeyAllowed(key);
|
|
713
744
|
return delete object[key];
|
|
714
745
|
}
|
|
715
746
|
const v = await this.evalExpr(node.argument, scope);
|
|
@@ -832,7 +863,7 @@ var Interpreter = class {
|
|
|
832
863
|
if (node.type === "MemberExpression") {
|
|
833
864
|
const { object, key } = await this.evalMemberTarget(node, scope);
|
|
834
865
|
return {
|
|
835
|
-
get: () => object
|
|
866
|
+
get: () => readProperty(object, key),
|
|
836
867
|
set: (value) => this.writeMember(object, key, value)
|
|
837
868
|
};
|
|
838
869
|
}
|
|
@@ -851,6 +882,7 @@ var Interpreter = class {
|
|
|
851
882
|
}
|
|
852
883
|
writeMember(object, key, value) {
|
|
853
884
|
this.assertChangeable(object);
|
|
885
|
+
assertKeyAllowed(key);
|
|
854
886
|
if (Array.isArray(object)) {
|
|
855
887
|
const grownTo = arrayLengthAfterWrite(object, key, value);
|
|
856
888
|
if (grownTo > MAX_ARRAY_LENGTH) {
|
|
@@ -896,7 +928,7 @@ var Interpreter = class {
|
|
|
896
928
|
let member;
|
|
897
929
|
if (node.callee.type === "MemberExpression") {
|
|
898
930
|
const target = await this.evalMemberTarget(node.callee, scope);
|
|
899
|
-
member = { ...target, value: target.object
|
|
931
|
+
member = { ...target, value: readProperty(target.object, target.key) };
|
|
900
932
|
fn = member.value;
|
|
901
933
|
thisArg = member.object;
|
|
902
934
|
} else if (node.callee.type === "Super") {
|
|
@@ -910,14 +942,16 @@ var Interpreter = class {
|
|
|
910
942
|
}
|
|
911
943
|
const args = await this.evalArguments(node.arguments, scope);
|
|
912
944
|
if (typeof fn !== "function") throw new InterpreterError("Attempted to call a non-function");
|
|
913
|
-
return this.callFunction(fn, thisArg, args);
|
|
945
|
+
return this.callFunction(fn, thisArg, args, scope.depth);
|
|
914
946
|
}
|
|
915
947
|
/**
|
|
916
948
|
* Call a function with a receiver and arguments: an interpreted function, or a built-in or host
|
|
917
949
|
* function after the checks that apply to it. `call` and `apply` are resolved to the function
|
|
918
950
|
* they invoke first, so those checks cannot be passed by the way a call is written.
|
|
951
|
+
*
|
|
952
|
+
* @param depth - How many calls deep the code that makes this call runs
|
|
919
953
|
*/
|
|
920
|
-
async callFunction(fn, thisArg, args) {
|
|
954
|
+
async callFunction(fn, thisArg, args, depth) {
|
|
921
955
|
for (let hops = 0; fn === FunctionProtoCall || fn === FunctionProtoApply || fn === FunctionProtoBind; hops++) {
|
|
922
956
|
const name = fn === FunctionProtoCall ? "call" : fn === FunctionProtoApply ? "apply" : "bind";
|
|
923
957
|
if (typeof thisArg !== "function" || hops >= 8) {
|
|
@@ -933,20 +967,20 @@ var Interpreter = class {
|
|
|
933
967
|
fn = target;
|
|
934
968
|
}
|
|
935
969
|
if (typeof fn !== "function") throw new InterpreterError("Attempted to call a non-function");
|
|
936
|
-
if (
|
|
937
|
-
|
|
938
|
-
|
|
970
|
+
if (this.interpretedFunctions.has(fn)) {
|
|
971
|
+
const interpreted = fn;
|
|
972
|
+
return await this.withCallerDepth(depth, () => interpreted.apply(thisArg, args));
|
|
939
973
|
}
|
|
974
|
+
const own = this.enterCall(depth);
|
|
940
975
|
try {
|
|
941
|
-
if (this.interpretedFunctions.has(fn)) {
|
|
942
|
-
return await fn.apply(thisArg, args);
|
|
943
|
-
}
|
|
944
976
|
args = this.checkBuiltinCall(fn, thisArg, args);
|
|
945
977
|
const arrayMethod = ARRAY_METHODS.get(fn);
|
|
946
978
|
if (arrayMethod !== void 0 && HIGHER_ORDER_ARRAY_METHODS.has(arrayMethod) && typeof args[0] === "function") {
|
|
947
|
-
return await this.callArrayMethod(thisArg, arrayMethod, args);
|
|
979
|
+
return await this.callArrayMethod(thisArg, arrayMethod, args, own);
|
|
948
980
|
}
|
|
949
|
-
const
|
|
981
|
+
const builtin = fn;
|
|
982
|
+
const builtinArgs = args;
|
|
983
|
+
const result = await this.withCallerDepth(own, () => builtin.apply(thisArg, builtinArgs));
|
|
950
984
|
if (arrayMethod === void 0 && STRING_METHODS.get(fn) === "split" && Array.isArray(result)) {
|
|
951
985
|
if (result.length > MAX_ARRAY_LENGTH) {
|
|
952
986
|
throw new InterpreterError(`An array cannot be longer than ${MAX_ARRAY_LENGTH} items`);
|
|
@@ -955,7 +989,36 @@ var Interpreter = class {
|
|
|
955
989
|
this.chargeValue(result);
|
|
956
990
|
return result;
|
|
957
991
|
} finally {
|
|
958
|
-
this.
|
|
992
|
+
this.pendingCalls--;
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
/**
|
|
996
|
+
* Count a call that starts `callerDepth` calls deep, until it returns.
|
|
997
|
+
*
|
|
998
|
+
* @returns The depth the call itself runs at
|
|
999
|
+
*/
|
|
1000
|
+
enterCall(callerDepth) {
|
|
1001
|
+
const depth = callerDepth + 1;
|
|
1002
|
+
if (depth > this.options.maxCallDepth) {
|
|
1003
|
+
throw new InterpreterError(`Max call depth exceeded (${this.options.maxCallDepth})`);
|
|
1004
|
+
}
|
|
1005
|
+
if (this.pendingCalls >= this.maxPendingCalls) {
|
|
1006
|
+
throw new InterpreterError(`Too many calls in progress at once (${this.maxPendingCalls})`);
|
|
1007
|
+
}
|
|
1008
|
+
this.pendingCalls++;
|
|
1009
|
+
return depth;
|
|
1010
|
+
}
|
|
1011
|
+
/**
|
|
1012
|
+
* Make a call with `depth` as the depth a function of the script is called from, if one starts
|
|
1013
|
+
* during it. It covers the synchronous part of the call, which is where a function starts.
|
|
1014
|
+
*/
|
|
1015
|
+
withCallerDepth(depth, call) {
|
|
1016
|
+
const previous = this.callerDepth;
|
|
1017
|
+
this.callerDepth = depth;
|
|
1018
|
+
try {
|
|
1019
|
+
return call();
|
|
1020
|
+
} finally {
|
|
1021
|
+
this.callerDepth = previous;
|
|
959
1022
|
}
|
|
960
1023
|
}
|
|
961
1024
|
/** The argument list of `apply`: an array, or nothing. */
|
|
@@ -972,7 +1035,7 @@ var Interpreter = class {
|
|
|
972
1035
|
bindFunction(target, boundThis, boundArgs) {
|
|
973
1036
|
this.charge((boundArgs.length + 2) * 8);
|
|
974
1037
|
return (...rest) => {
|
|
975
|
-
const result = this.callFunction(target, boundThis, [...boundArgs, ...rest]);
|
|
1038
|
+
const result = this.callFunction(target, boundThis, [...boundArgs, ...rest], this.callerDepth);
|
|
976
1039
|
result.then(void 0, ignoreRejection2);
|
|
977
1040
|
return result;
|
|
978
1041
|
};
|
|
@@ -1135,9 +1198,10 @@ var Interpreter = class {
|
|
|
1135
1198
|
* callbacks (which are async — they may `await` tool calls) are awaited. Each
|
|
1136
1199
|
* iteration ticks the step budget.
|
|
1137
1200
|
*/
|
|
1138
|
-
async callArrayMethod(arr, method, args) {
|
|
1201
|
+
async callArrayMethod(arr, method, args, depth) {
|
|
1139
1202
|
const callback = args[0];
|
|
1140
|
-
const
|
|
1203
|
+
const interpreted = this.interpretedFunctions.has(callback) ? callback : void 0;
|
|
1204
|
+
const invoke = interpreted ? (...callbackArgs) => this.withCallerDepth(depth, () => interpreted(...callbackArgs)) : (...callbackArgs) => this.callFunction(callback, void 0, callbackArgs, depth);
|
|
1141
1205
|
const cb = invoke;
|
|
1142
1206
|
const each = (i) => {
|
|
1143
1207
|
this.tick();
|
|
@@ -1313,7 +1377,7 @@ var Interpreter = class {
|
|
|
1313
1377
|
else throw new InterpreterError("Unsupported destructuring key");
|
|
1314
1378
|
if (BLOCKED_KEYS.has(key)) throw new InterpreterError(`Forbidden destructured key: ${key}`);
|
|
1315
1379
|
taken.add(key);
|
|
1316
|
-
await this.applyPattern(prop.value, source
|
|
1380
|
+
await this.applyPattern(prop.value, readProperty(source, key), scope, mode);
|
|
1317
1381
|
}
|
|
1318
1382
|
return;
|
|
1319
1383
|
}
|
|
@@ -1326,17 +1390,17 @@ var Interpreter = class {
|
|
|
1326
1390
|
if (node.generator) throw new InterpreterError("Generator functions are not supported");
|
|
1327
1391
|
const selfName = node.type === "FunctionExpression" && node.id ? node.id.name : void 0;
|
|
1328
1392
|
const outer = selfName === void 0 ? closure : closure.child();
|
|
1329
|
-
const call = async (args) => {
|
|
1330
|
-
const fnScope = outer.
|
|
1331
|
-
for (let i = 0; i < node.params.length; i++) {
|
|
1332
|
-
const param = node.params[i];
|
|
1333
|
-
if (param.type === "RestElement") {
|
|
1334
|
-
await this.applyPattern(param.argument, args.slice(i), fnScope, "let");
|
|
1335
|
-
break;
|
|
1336
|
-
}
|
|
1337
|
-
await this.applyPattern(param, args[i], fnScope, "let");
|
|
1338
|
-
}
|
|
1393
|
+
const call = async (args, callerDepth) => {
|
|
1394
|
+
const fnScope = outer.activation(this.enterCall(callerDepth));
|
|
1339
1395
|
try {
|
|
1396
|
+
for (let i = 0; i < node.params.length; i++) {
|
|
1397
|
+
const param = node.params[i];
|
|
1398
|
+
if (param.type === "RestElement") {
|
|
1399
|
+
await this.applyPattern(param.argument, args.slice(i), fnScope, "let");
|
|
1400
|
+
break;
|
|
1401
|
+
}
|
|
1402
|
+
await this.applyPattern(param, args[i], fnScope, "let");
|
|
1403
|
+
}
|
|
1340
1404
|
if (node.body.type === "BlockStatement") {
|
|
1341
1405
|
await this.execBlock(node.body.body, fnScope);
|
|
1342
1406
|
return void 0;
|
|
@@ -1345,10 +1409,13 @@ var Interpreter = class {
|
|
|
1345
1409
|
} catch (e) {
|
|
1346
1410
|
if (e instanceof ReturnSignal) return e.value;
|
|
1347
1411
|
throw e;
|
|
1412
|
+
} finally {
|
|
1413
|
+
this.pendingCalls--;
|
|
1348
1414
|
}
|
|
1349
1415
|
};
|
|
1416
|
+
const depthOfCaller = () => this.callerDepth;
|
|
1350
1417
|
const interpreted = function interpreted2(...args) {
|
|
1351
|
-
const result = call(args);
|
|
1418
|
+
const result = call(args, depthOfCaller());
|
|
1352
1419
|
result.then(void 0, ignoreRejection2);
|
|
1353
1420
|
return result;
|
|
1354
1421
|
};
|
|
@@ -1368,7 +1435,7 @@ var Interpreter = class {
|
|
|
1368
1435
|
throw new InterpreterError("Unsupported property key");
|
|
1369
1436
|
}
|
|
1370
1437
|
readGlobal(name) {
|
|
1371
|
-
if (Object.prototype.hasOwnProperty.call(this.globals, name)) return this.globals[name];
|
|
1438
|
+
if (name !== "__proto__" && Object.prototype.hasOwnProperty.call(this.globals, name)) return this.globals[name];
|
|
1372
1439
|
throw new InterpreterError(`'${name}' is not defined`);
|
|
1373
1440
|
}
|
|
1374
1441
|
};
|
|
@@ -1379,6 +1446,7 @@ function isIterable(v) {
|
|
|
1379
1446
|
return v != null && typeof v[Symbol.iterator] === "function";
|
|
1380
1447
|
}
|
|
1381
1448
|
function isScriptData(value) {
|
|
1449
|
+
if (value === Object.prototype || value === Array.prototype) return false;
|
|
1382
1450
|
if (Array.isArray(value)) return true;
|
|
1383
1451
|
if (value === null || typeof value !== "object") return false;
|
|
1384
1452
|
const prototype = Object.getPrototypeOf(value);
|
|
@@ -1635,12 +1703,12 @@ function estimateSerializedSize(value, maxBytes = 0, depth = 0, maxDepth = 2e3,
|
|
|
1635
1703
|
}
|
|
1636
1704
|
currentPath.add(value);
|
|
1637
1705
|
try {
|
|
1638
|
-
const rawLength =
|
|
1706
|
+
const rawLength = readProperty2(value, "length");
|
|
1639
1707
|
const length = typeof rawLength === "number" && NumberIsFinite(rawLength) && rawLength >= 0 ? MathFloor(rawLength) : 0;
|
|
1640
1708
|
let size = 2;
|
|
1641
1709
|
for (let i = 0; i < length; i++) {
|
|
1642
1710
|
if (i > 0) size += 1;
|
|
1643
|
-
let elementSize = estimateSerializedSize(
|
|
1711
|
+
let elementSize = estimateSerializedSize(readProperty2(value, i), maxBytes, depth + 1, maxDepth, currentPath);
|
|
1644
1712
|
if (elementSize === 0) elementSize = 4;
|
|
1645
1713
|
size += elementSize;
|
|
1646
1714
|
if (maxBytes > 0 && size > maxBytes) {
|
|
@@ -1733,7 +1801,7 @@ function estimateSerializedSize(value, maxBytes = 0, depth = 0, maxDepth = 2e3,
|
|
|
1733
1801
|
}
|
|
1734
1802
|
return 0;
|
|
1735
1803
|
}
|
|
1736
|
-
function
|
|
1804
|
+
function readProperty2(target, key) {
|
|
1737
1805
|
try {
|
|
1738
1806
|
return ReflectGet(target, key);
|
|
1739
1807
|
} catch {
|
|
@@ -3241,6 +3309,7 @@ var InterpreterAdapter = class {
|
|
|
3241
3309
|
instanceChecks: safe.instanceChecks,
|
|
3242
3310
|
maxSteps: this.options.maxSteps ?? DEFAULT_MAX_STEPS,
|
|
3243
3311
|
maxCallDepth: this.options.maxCallDepth ?? DEFAULT_MAX_CALL_DEPTH,
|
|
3312
|
+
maxPendingCalls: this.options.maxPendingCalls,
|
|
3244
3313
|
signal: context.abortController.signal,
|
|
3245
3314
|
charge
|
|
3246
3315
|
});
|