Skip to content

Commit 9f687aa

Browse files
committed
v1.4.3
1. Fix with statement scope assignment invalid 2. rootContext adjusted to read-only
1 parent b88217d commit 9f687aa

File tree

6 files changed

+65
-55
lines changed

6 files changed

+65
-55
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "eval5",
3-
"version": "1.4.2",
3+
"version": "1.4.3",
44
"description": "A JavaScript interpreter written in JavaScript",
55
"main": "./dist/cjs/index.js",
66
"module": "./dist/esm/index.js",

src/interpreter/main.ts

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ function internalEval(
118118

119119
const options: Options = {
120120
timeout: opts.timeout,
121-
_initEnv: function(this: Interpreter) {
121+
_initEnv: function (this: Interpreter) {
122122
// set caller context
123123
if (!useGlobalScope) {
124124
this.setCurrentContext(instance.getCurrentContext());
@@ -221,6 +221,10 @@ function createScope(parent: Scope | null = null, name?: string): Scope {
221221
return new Scope(Object.create(null), parent, name);
222222
}
223223

224+
function createRootContext(data: Context): Context {
225+
return Object.create(data);
226+
}
227+
224228
const BuildInObjects: ScopeData = {
225229
NaN,
226230
Infinity,
@@ -354,7 +358,11 @@ export class Interpreter {
354358
const superScope = this.createSuperScope(ctx);
355359

356360
if (this.options.rootContext) {
357-
rootScope = new Scope(this.options.rootContext, superScope, "rootScope");
361+
rootScope = new Scope(
362+
createRootContext(this.options.rootContext),
363+
superScope,
364+
"rootScope"
365+
);
358366
}
359367

360368
scope = new Scope(ctx, rootScope || superScope, "globalScope");
@@ -757,14 +765,14 @@ export class Interpreter {
757765
};
758766
}
759767

760-
protected isRootScope(node: ESTree.Expression | ESTree.Pattern): boolean {
761-
if (node.type === "Identifier") {
762-
const scope = this.getScopeFromName(node.name, this.getCurrentScope());
763-
return scope.name === "rootScope";
764-
}
768+
// protected isRootScope(node: ESTree.Expression | ESTree.Pattern): boolean {
769+
// if (node.type === "Identifier") {
770+
// const scope = this.getScopeFromName(node.name, this.getCurrentScope());
771+
// return scope.name === "rootScope";
772+
// }
765773

766-
return false;
767-
}
774+
// return false;
775+
// }
768776

769777
// typeof a !a()
770778
protected unaryExpressionHandler(node: ESTree.UnaryExpression): BaseClosure {
@@ -775,9 +783,10 @@ export class Interpreter {
775783

776784
return () => {
777785
// not allowed to delete root scope property
778-
if (this.isRootScope(node.argument)) {
779-
return false;
780-
}
786+
// rootContext has move to prototype chai, so no judgment required
787+
// if (this.isRootScope(node.argument)) {
788+
// return false;
789+
// }
781790

782791
let obj = objectGetter();
783792
const name = nameGetter();
@@ -901,8 +910,8 @@ export class Interpreter {
901910
const key = item.key;
902911
const kinds = properties[key];
903912
const value = kinds.init ? kinds.init() : undefined;
904-
const getter = kinds.get ? kinds.get() : function() {};
905-
const setter = kinds.set ? kinds.set() : function(a: any) {};
913+
const getter = kinds.get ? kinds.get() : function () {};
914+
const setter = kinds.set ? kinds.set() : function (a: any) {};
906915

907916
if ("set" in kinds || "get" in kinds) {
908917
const descriptor = {
@@ -1112,7 +1121,7 @@ export class Interpreter {
11121121
const oldDeclFuncs = this.collectDeclFuncs;
11131122
this.collectDeclVars = Object.create(null);
11141123
this.collectDeclFuncs = Object.create(null);
1115-
const name = node.id ? node.id.name : "" /**anonymous*/;
1124+
const name = node.id ? node.id.name : ""; /**anonymous*/
11161125
const paramLength = node.params.length;
11171126

11181127
const paramsGetter = node.params.map(param => this.createParamNameGetter(param));
@@ -1129,7 +1138,7 @@ export class Interpreter {
11291138
// bind current scope
11301139
const runtimeScope = self.getCurrentScope();
11311140

1132-
const func = function(...args: any[]) {
1141+
const func = function (...args: any[]) {
11331142
self.callStack.push(`${name}`);
11341143

11351144
const prevScope = self.getCurrentScope();
@@ -1635,16 +1644,15 @@ export class Interpreter {
16351644
const bodyClosure = this.createClosure(node.body);
16361645

16371646
return () => {
1647+
const data = objectClosure() as ScopeData;
16381648
const currentScope = this.getCurrentScope();
1639-
const newScope = createScope(currentScope, "with");
1649+
const newScope = new Scope(data, currentScope, "with");
16401650

1641-
const data = objectClosure();
1642-
1643-
// newScope.data = data;
1651+
// const data = objectClosure();
16441652
// copy all properties
1645-
for (let k in data) {
1646-
newScope.data[k] = data[k];
1647-
}
1653+
// for (let k in data) {
1654+
// newScope.data[k] = data[k];
1655+
// }
16481656

16491657
this.setCurrentScope(newScope);
16501658

@@ -1679,7 +1687,7 @@ export class Interpreter {
16791687
const callStack: string[] = this.callStack.concat([]);
16801688
let result: any = EmptyStatementReturn;
16811689
let finalReturn: any;
1682-
let throwError;
1690+
let throwError: any;
16831691

16841692
const reset = () => {
16851693
this.setCurrentScope(currentScope); //reset scope
@@ -1919,7 +1927,7 @@ export class Interpreter {
19191927
getter = this.createClosure(node);
19201928
}
19211929

1922-
return function() {
1930+
return function () {
19231931
return getter();
19241932
};
19251933
}

umd/eval5.js

Lines changed: 27 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

umd/eval5.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

umd/eval5.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

umd/eval5.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)