Comparing version 1.1.5 to 1.2.0
@@ -16,5 +16,3 @@ "use strict"; | ||
//TODO: | ||
//appendCode | ||
var version = "1.1.5"; | ||
var version = "1.2.0"; | ||
@@ -151,2 +149,3 @@ function defineFunctionName(func, name) { | ||
this.sourceList = []; | ||
this.collectDeclVars = Object.create(null); | ||
@@ -162,2 +161,3 @@ this.collectDeclFuncs = Object.create(null); | ||
this.callStack = []; | ||
this.initEnvironment(context); | ||
} | ||
@@ -167,2 +167,39 @@ | ||
_proto.initEnvironment = function initEnvironment(ctx) { | ||
var scope; //init global scope | ||
if (ctx instanceof Scope) { | ||
scope = ctx; | ||
} else { | ||
var superScope = this.getSuperScope(ctx); // replace Interpreter.eval and Interpreter.Function | ||
Object.keys(ctx).forEach(function (key) { | ||
if (ctx[key] === IEval) { | ||
ctx[key] = superScope.data[IEval]; | ||
} | ||
if (ctx[key] === IFunction) { | ||
ctx[key] = superScope.data[IFunction]; | ||
} | ||
}); | ||
scope = new Scope(ctx, superScope, "root"); | ||
} | ||
this.rootScope = scope; | ||
this.currentScope = this.rootScope; //init global context == this | ||
this.rootContext = scope.data; | ||
this.currentContext = scope.data; // collect var/function declare | ||
this.collectDeclVars = Object.create(null); | ||
this.collectDeclFuncs = Object.create(null); | ||
this.execStartTime = Date.now(); | ||
this.execEndTime = this.execStartTime; | ||
var initEnv = this.options.initEnv; | ||
if (initEnv) { | ||
initEnv(this); | ||
} | ||
}; | ||
_proto.isInterruptThrow = function isInterruptThrow(err) { | ||
@@ -233,3 +270,3 @@ return err instanceof _messages.InterruptThrowError || err instanceof _messages.InterruptThrowReferenceError || err instanceof _messages.InterruptThrowSyntaxError; | ||
}); | ||
return new Scope(data, null, "root"); | ||
return new Scope(data, null, "superRoot"); | ||
}; | ||
@@ -245,40 +282,3 @@ | ||
_proto.initEnvironment = function initEnvironment(ctx) { | ||
var scope; //init global scope | ||
if (ctx instanceof Scope) { | ||
scope = ctx; | ||
} else { | ||
var superScope = this.getSuperScope(ctx); // replace Interpreter.eval and Interpreter.Function | ||
Object.keys(ctx).forEach(function (key) { | ||
if (ctx[key] === IEval) { | ||
ctx[key] = superScope.data[IEval]; | ||
} | ||
if (ctx[key] === IFunction) { | ||
ctx[key] = superScope.data[IFunction]; | ||
} | ||
}); | ||
scope = new Scope(ctx, superScope, "global"); | ||
} | ||
this.rootScope = scope; | ||
this.currentScope = this.rootScope; //init global context == this | ||
this.rootContext = scope.data; | ||
this.currentContext = scope.data; // collect var/function declare | ||
this.collectDeclVars = Object.create(null); | ||
this.collectDeclFuncs = Object.create(null); | ||
this.execStartTime = Date.now(); | ||
this.execEndTime = this.execStartTime; | ||
var initEnv = this.options.initEnv; | ||
if (initEnv) { | ||
initEnv(this); | ||
} | ||
}; | ||
_proto.evaluate = function evaluate(code, ctx) { | ||
_proto.evaluate = function evaluate(code) { | ||
if (code === void 0) { | ||
@@ -288,7 +288,4 @@ code = ""; | ||
if (ctx === void 0) { | ||
ctx = this.context; | ||
} | ||
var node; | ||
if (!code) return; | ||
node = (0, _acorn.parse)(code, { | ||
@@ -298,6 +295,10 @@ ranges: true, | ||
}); | ||
return this.evaluateNode(node, code, ctx); | ||
return this.evaluateNode(node, code); | ||
}; | ||
_proto.evaluateNode = function evaluateNode(node, source, ctx) { | ||
_proto.appendCode = function appendCode(code) { | ||
return this.evaluate(code); | ||
}; | ||
_proto.evaluateNode = function evaluateNode(node, source) { | ||
if (source === void 0) { | ||
@@ -307,9 +308,4 @@ source = ""; | ||
if (ctx === void 0) { | ||
ctx = this.context; | ||
} | ||
this.initEnvironment(ctx); | ||
this.source = source; | ||
this.ast = node; | ||
this.sourceList.push(source); | ||
var bodyClosure = this.createClosure(node); // add declares to data | ||
@@ -841,2 +837,3 @@ | ||
var keyGetter = this.createMemberKeyGetter(node); | ||
var source = this.source; | ||
return function () { | ||
@@ -849,4 +846,3 @@ var obj = objectGetter(); | ||
if (!func || !isFunction(func)) { | ||
var name = _this9.source.slice(node.start, node.end); | ||
var name = source.slice(node.start, node.end); | ||
throw _this9.createInternalThrowError(_messages.Messages.FunctionUndefinedReferenceError, name, node); | ||
@@ -930,2 +926,3 @@ } | ||
var self = this; | ||
var source = this.source; | ||
var oldDeclVars = this.collectDeclVars; | ||
@@ -1000,3 +997,3 @@ var oldDeclFuncs = this.collectDeclFuncs; | ||
value: function value() { | ||
return _this11.source.slice(node.start, node.end); | ||
return source.slice(node.start, node.end); | ||
}, | ||
@@ -1009,3 +1006,3 @@ writable: true, | ||
value: function value() { | ||
return _this11.source.slice(node.start, node.end); | ||
return source.slice(node.start, node.end); | ||
}, | ||
@@ -1024,2 +1021,3 @@ writable: true, | ||
var source = this.source; | ||
var expression = this.createClosure(node.callee); | ||
@@ -1034,5 +1032,3 @@ var args = node.arguments.map(function (arg) { | ||
var callee = node.callee; | ||
var name = _this12.source.slice(callee.start, callee.end); | ||
var name = source.slice(callee.start, callee.end); | ||
throw _this12.createInternalThrowError(_messages.Messages.IsNotConstructor, name, node); | ||
@@ -1039,0 +1035,0 @@ } |
import _construct from "@babel/runtime/helpers/construct"; | ||
import { parse } from "acorn"; | ||
import { Messages, InterruptThrowError, InterruptThrowReferenceError, InterruptThrowSyntaxError } from "./messages"; //TODO: | ||
//appendCode | ||
import { Messages, InterruptThrowError, InterruptThrowReferenceError, InterruptThrowSyntaxError } from "./messages"; | ||
var version = "1.2.0"; | ||
var version = "1.1.5"; | ||
function defineFunctionName(func, name) { | ||
@@ -138,2 +136,3 @@ Object.defineProperty(func, "name", { | ||
this.sourceList = []; | ||
this.collectDeclVars = Object.create(null); | ||
@@ -149,2 +148,3 @@ this.collectDeclFuncs = Object.create(null); | ||
this.callStack = []; | ||
this.initEnvironment(context); | ||
} | ||
@@ -154,2 +154,39 @@ | ||
_proto.initEnvironment = function initEnvironment(ctx) { | ||
var scope; //init global scope | ||
if (ctx instanceof Scope) { | ||
scope = ctx; | ||
} else { | ||
var superScope = this.getSuperScope(ctx); // replace Interpreter.eval and Interpreter.Function | ||
Object.keys(ctx).forEach(function (key) { | ||
if (ctx[key] === IEval) { | ||
ctx[key] = superScope.data[IEval]; | ||
} | ||
if (ctx[key] === IFunction) { | ||
ctx[key] = superScope.data[IFunction]; | ||
} | ||
}); | ||
scope = new Scope(ctx, superScope, "root"); | ||
} | ||
this.rootScope = scope; | ||
this.currentScope = this.rootScope; //init global context == this | ||
this.rootContext = scope.data; | ||
this.currentContext = scope.data; // collect var/function declare | ||
this.collectDeclVars = Object.create(null); | ||
this.collectDeclFuncs = Object.create(null); | ||
this.execStartTime = Date.now(); | ||
this.execEndTime = this.execStartTime; | ||
var initEnv = this.options.initEnv; | ||
if (initEnv) { | ||
initEnv(this); | ||
} | ||
}; | ||
_proto.isInterruptThrow = function isInterruptThrow(err) { | ||
@@ -220,3 +257,3 @@ return err instanceof InterruptThrowError || err instanceof InterruptThrowReferenceError || err instanceof InterruptThrowSyntaxError; | ||
}); | ||
return new Scope(data, null, "root"); | ||
return new Scope(data, null, "superRoot"); | ||
}; | ||
@@ -232,40 +269,3 @@ | ||
_proto.initEnvironment = function initEnvironment(ctx) { | ||
var scope; //init global scope | ||
if (ctx instanceof Scope) { | ||
scope = ctx; | ||
} else { | ||
var superScope = this.getSuperScope(ctx); // replace Interpreter.eval and Interpreter.Function | ||
Object.keys(ctx).forEach(function (key) { | ||
if (ctx[key] === IEval) { | ||
ctx[key] = superScope.data[IEval]; | ||
} | ||
if (ctx[key] === IFunction) { | ||
ctx[key] = superScope.data[IFunction]; | ||
} | ||
}); | ||
scope = new Scope(ctx, superScope, "global"); | ||
} | ||
this.rootScope = scope; | ||
this.currentScope = this.rootScope; //init global context == this | ||
this.rootContext = scope.data; | ||
this.currentContext = scope.data; // collect var/function declare | ||
this.collectDeclVars = Object.create(null); | ||
this.collectDeclFuncs = Object.create(null); | ||
this.execStartTime = Date.now(); | ||
this.execEndTime = this.execStartTime; | ||
var initEnv = this.options.initEnv; | ||
if (initEnv) { | ||
initEnv(this); | ||
} | ||
}; | ||
_proto.evaluate = function evaluate(code, ctx) { | ||
_proto.evaluate = function evaluate(code) { | ||
if (code === void 0) { | ||
@@ -275,7 +275,4 @@ code = ""; | ||
if (ctx === void 0) { | ||
ctx = this.context; | ||
} | ||
var node; | ||
if (!code) return; | ||
node = parse(code, { | ||
@@ -285,6 +282,10 @@ ranges: true, | ||
}); | ||
return this.evaluateNode(node, code, ctx); | ||
return this.evaluateNode(node, code); | ||
}; | ||
_proto.evaluateNode = function evaluateNode(node, source, ctx) { | ||
_proto.appendCode = function appendCode(code) { | ||
return this.evaluate(code); | ||
}; | ||
_proto.evaluateNode = function evaluateNode(node, source) { | ||
if (source === void 0) { | ||
@@ -294,9 +295,4 @@ source = ""; | ||
if (ctx === void 0) { | ||
ctx = this.context; | ||
} | ||
this.initEnvironment(ctx); | ||
this.source = source; | ||
this.ast = node; | ||
this.sourceList.push(source); | ||
var bodyClosure = this.createClosure(node); // add declares to data | ||
@@ -828,2 +824,3 @@ | ||
var keyGetter = this.createMemberKeyGetter(node); | ||
var source = this.source; | ||
return function () { | ||
@@ -836,4 +833,3 @@ var obj = objectGetter(); | ||
if (!func || !isFunction(func)) { | ||
var name = _this9.source.slice(node.start, node.end); | ||
var name = source.slice(node.start, node.end); | ||
throw _this9.createInternalThrowError(Messages.FunctionUndefinedReferenceError, name, node); | ||
@@ -917,2 +913,3 @@ } | ||
var self = this; | ||
var source = this.source; | ||
var oldDeclVars = this.collectDeclVars; | ||
@@ -987,3 +984,3 @@ var oldDeclFuncs = this.collectDeclFuncs; | ||
value: function value() { | ||
return _this11.source.slice(node.start, node.end); | ||
return source.slice(node.start, node.end); | ||
}, | ||
@@ -996,3 +993,3 @@ writable: true, | ||
value: function value() { | ||
return _this11.source.slice(node.start, node.end); | ||
return source.slice(node.start, node.end); | ||
}, | ||
@@ -1011,2 +1008,3 @@ writable: true, | ||
var source = this.source; | ||
var expression = this.createClosure(node.callee); | ||
@@ -1021,5 +1019,3 @@ var args = node.arguments.map(function (arg) { | ||
var callee = node.callee; | ||
var name = _this12.source.slice(callee.start, callee.end); | ||
var name = source.slice(callee.start, callee.end); | ||
throw _this12.createInternalThrowError(Messages.IsNotConstructor, name, node); | ||
@@ -1026,0 +1022,0 @@ } |
@@ -41,11 +41,11 @@ import { MessageItem } from "./messages"; | ||
export declare class Interpreter { | ||
context: Context | Scope; | ||
value: any; | ||
rootContext: Context; | ||
ast: ESTree.Program; | ||
source: string; | ||
currentScope: Scope; | ||
rootScope: Scope; | ||
currentContext: Context; | ||
options: Options; | ||
protected context: Context | Scope; | ||
protected rootContext: Context; | ||
protected source: string; | ||
protected sourceList: string[]; | ||
protected currentScope: Scope; | ||
protected rootScope: Scope; | ||
protected currentContext: Context; | ||
protected options: Options; | ||
protected callStack: string[]; | ||
@@ -58,3 +58,3 @@ protected collectDeclVars: CollectDeclarations; | ||
protected execEndTime: number; | ||
static readonly version = "1.1.5"; | ||
static readonly version = "1.2.0"; | ||
static readonly eval: symbol; | ||
@@ -65,2 +65,3 @@ static readonly Function: symbol; | ||
constructor(context?: Context | Scope, options?: Options); | ||
protected initEnvironment(ctx: Context | Scope): void; | ||
isInterruptThrow<T>(err: T): boolean; | ||
@@ -70,5 +71,5 @@ getSuperScope(ctx: Context): Scope; | ||
protected setCurrentScope(scope: Scope): void; | ||
protected initEnvironment(ctx: Context | Scope): void; | ||
evaluate(code?: string, ctx?: Context): any; | ||
evaluateNode(node: ESTree.Program, source?: string, ctx?: Context): any; | ||
evaluate(code?: string): any; | ||
appendCode(code: string): any; | ||
evaluateNode(node: ESTree.Program, source?: string): any; | ||
getExecutionTime(): number; | ||
@@ -75,0 +76,0 @@ createErrorMessage(msg: MessageItem, value: string | number, node?: Node): string; |
import { parse } from "acorn"; | ||
import { Messages, InterruptThrowError, InterruptThrowReferenceError, InterruptThrowSyntaxError, } from "./messages"; | ||
//TODO: | ||
//appendCode | ||
const version = "1.1.5"; | ||
const version = "1.2.0"; | ||
function defineFunctionName(func, name) { | ||
@@ -108,2 +106,3 @@ Object.defineProperty(func, "name", { | ||
constructor(context = Interpreter.global, options = {}) { | ||
this.sourceList = []; | ||
this.collectDeclVars = Object.create(null); | ||
@@ -119,3 +118,38 @@ this.collectDeclFuncs = Object.create(null); | ||
this.callStack = []; | ||
this.initEnvironment(context); | ||
} | ||
initEnvironment(ctx) { | ||
let scope; | ||
//init global scope | ||
if (ctx instanceof Scope) { | ||
scope = ctx; | ||
} | ||
else { | ||
const superScope = this.getSuperScope(ctx); | ||
// replace Interpreter.eval and Interpreter.Function | ||
Object.keys(ctx).forEach(key => { | ||
if (ctx[key] === IEval) { | ||
ctx[key] = superScope.data[IEval]; | ||
} | ||
if (ctx[key] === IFunction) { | ||
ctx[key] = superScope.data[IFunction]; | ||
} | ||
}); | ||
scope = new Scope(ctx, superScope, "root"); | ||
} | ||
this.rootScope = scope; | ||
this.currentScope = this.rootScope; | ||
//init global context == this | ||
this.rootContext = scope.data; | ||
this.currentContext = scope.data; | ||
// collect var/function declare | ||
this.collectDeclVars = Object.create(null); | ||
this.collectDeclFuncs = Object.create(null); | ||
this.execStartTime = Date.now(); | ||
this.execEndTime = this.execStartTime; | ||
const initEnv = this.options.initEnv; | ||
if (initEnv) { | ||
initEnv(this); | ||
} | ||
} | ||
isInterruptThrow(err) { | ||
@@ -178,3 +212,3 @@ return (err instanceof InterruptThrowError || | ||
}); | ||
return new Scope(data, null, "root"); | ||
return new Scope(data, null, "superRoot"); | ||
} | ||
@@ -187,38 +221,6 @@ setCurrentContext(ctx) { | ||
} | ||
initEnvironment(ctx) { | ||
let scope; | ||
//init global scope | ||
if (ctx instanceof Scope) { | ||
scope = ctx; | ||
} | ||
else { | ||
const superScope = this.getSuperScope(ctx); | ||
// replace Interpreter.eval and Interpreter.Function | ||
Object.keys(ctx).forEach(key => { | ||
if (ctx[key] === IEval) { | ||
ctx[key] = superScope.data[IEval]; | ||
} | ||
if (ctx[key] === IFunction) { | ||
ctx[key] = superScope.data[IFunction]; | ||
} | ||
}); | ||
scope = new Scope(ctx, superScope, "global"); | ||
} | ||
this.rootScope = scope; | ||
this.currentScope = this.rootScope; | ||
//init global context == this | ||
this.rootContext = scope.data; | ||
this.currentContext = scope.data; | ||
// collect var/function declare | ||
this.collectDeclVars = Object.create(null); | ||
this.collectDeclFuncs = Object.create(null); | ||
this.execStartTime = Date.now(); | ||
this.execEndTime = this.execStartTime; | ||
const initEnv = this.options.initEnv; | ||
if (initEnv) { | ||
initEnv(this); | ||
} | ||
} | ||
evaluate(code = "", ctx = this.context) { | ||
evaluate(code = "") { | ||
let node; | ||
if (!code) | ||
return; | ||
node = parse(code, { | ||
@@ -228,8 +230,10 @@ ranges: true, | ||
}); | ||
return this.evaluateNode(node, code, ctx); | ||
return this.evaluateNode(node, code); | ||
} | ||
evaluateNode(node, source = "", ctx = this.context) { | ||
this.initEnvironment(ctx); | ||
appendCode(code) { | ||
return this.evaluate(code); | ||
} | ||
evaluateNode(node, source = "") { | ||
this.source = source; | ||
this.ast = node; | ||
this.sourceList.push(source); | ||
const bodyClosure = this.createClosure(node); | ||
@@ -634,2 +638,3 @@ // add declares to data | ||
const keyGetter = this.createMemberKeyGetter(node); | ||
const source = this.source; | ||
return () => { | ||
@@ -640,3 +645,3 @@ const obj = objectGetter(); | ||
if (!func || !isFunction(func)) { | ||
const name = this.source.slice(node.start, node.end); | ||
const name = source.slice(node.start, node.end); | ||
throw this.createInternalThrowError(Messages.FunctionUndefinedReferenceError, name, node); | ||
@@ -701,2 +706,3 @@ } | ||
const self = this; | ||
const source = this.source; | ||
const oldDeclVars = this.collectDeclVars; | ||
@@ -759,3 +765,3 @@ const oldDeclFuncs = this.collectDeclFuncs; | ||
value: () => { | ||
return this.source.slice(node.start, node.end); | ||
return source.slice(node.start, node.end); | ||
}, | ||
@@ -768,3 +774,3 @@ writable: true, | ||
value: () => { | ||
return this.source.slice(node.start, node.end); | ||
return source.slice(node.start, node.end); | ||
}, | ||
@@ -780,2 +786,3 @@ writable: true, | ||
newExpressionHandler(node) { | ||
const source = this.source; | ||
const expression = this.createClosure(node.callee); | ||
@@ -787,3 +794,3 @@ const args = node.arguments.map(arg => this.createClosure(arg)); | ||
const callee = node.callee; | ||
const name = this.source.slice(callee.start, callee.end); | ||
const name = source.slice(callee.start, callee.end); | ||
throw this.createInternalThrowError(Messages.IsNotConstructor, name, node); | ||
@@ -790,0 +797,0 @@ } |
{ | ||
"name": "eval5", | ||
"version": "1.1.5", | ||
"version": "1.2.0", | ||
"description": "A JavaScript interpreter, written completely in JavaScript", | ||
@@ -5,0 +5,0 @@ "main": "./dist/cjs/index.js", |
@@ -102,3 +102,3 @@ # eval5 | ||
### `evaluate`(code: string, ctx?: {}): any | ||
### `evaluate`(code: string): any | ||
@@ -112,2 +112,6 @@ 返回脚本中执行的最后一个表达式结果 | ||
### appendCode(code: string): any | ||
作用同`evaluate` | ||
### setExecTimeout(timeout: number) | ||
@@ -114,0 +118,0 @@ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
1024832
12071
163