Comparing version 1.0.8 to 1.0.9
@@ -40,3 +40,4 @@ import { MessageItem } from "./messages"; | ||
callStack: string[]; | ||
collectDeclarations: CollectDeclarations; | ||
collectDeclVars: CollectDeclarations; | ||
collectDeclFuncs: CollectDeclarations; | ||
protected isVarDeclMode: boolean; | ||
@@ -46,2 +47,4 @@ protected lastExecNode: Node | null; | ||
protected execEndTime: number; | ||
static version: string; | ||
static rootContext: undefined; | ||
static global: any; | ||
@@ -133,3 +136,3 @@ constructor(context?: Context, options?: Options); | ||
funcDeclaration(name: string, func: () => any): void; | ||
addDeclarationsToScope(declarations: CollectDeclarations, scope: Scope): void; | ||
addDeclarationsToScope(declVars: CollectDeclarations, declFuncs: CollectDeclarations, scope: Scope): void; | ||
getScopeValue(name: string, startScope: Scope): any; | ||
@@ -136,0 +139,0 @@ getScopeDataFromName(name: string, startScope: Scope): ScopeData; |
@@ -91,3 +91,4 @@ import _construct from "@babel/runtime/helpers/construct"; | ||
this.collectDeclarations = {}; | ||
this.collectDeclVars = Object.create(null); | ||
this.collectDeclFuncs = Object.create(null); | ||
this.isVarDeclMode = false; | ||
@@ -124,3 +125,4 @@ this.lastExecNode = null; | ||
this.collectDeclarations = {}; | ||
this.collectDeclVars = Object.create(null); | ||
this.collectDeclFuncs = Object.create(null); | ||
this.execStartTime = Date.now(); | ||
@@ -161,5 +163,6 @@ this.execEndTime = this.execStartTime; | ||
this.addDeclarationsToScope(this.collectDeclarations, this.getCurrentScope()); // reset | ||
this.addDeclarationsToScope(this.collectDeclVars, this.collectDeclFuncs, this.getCurrentScope()); // reset | ||
this.collectDeclarations = {}; // start run | ||
this.collectDeclVars = Object.create(null); | ||
this.collectDeclFuncs = Object.create(null); // start run | ||
@@ -720,8 +723,8 @@ try { | ||
} // function call | ||
// this = rootContext | ||
// this = undefined | ||
// tips: | ||
// test(...) === test.call(this.rootContext, ...) | ||
// test(...) === test.call(undefined, ...) | ||
return func.bind(_this8.rootContext); | ||
return func.bind(Interpreter.rootContext); | ||
}; | ||
@@ -751,4 +754,6 @@ } | ||
var self = this; | ||
var oldDecls = this.collectDeclarations; | ||
this.collectDeclarations = {}; | ||
var oldDeclVars = this.collectDeclVars; | ||
var oldDeclFuncs = this.collectDeclFuncs; | ||
this.collectDeclVars = Object.create(null); | ||
this.collectDeclFuncs = Object.create(null); | ||
var name = node.id ? node.id.name : ""; | ||
@@ -761,4 +766,6 @@ var paramLength = node.params.length; | ||
var bodyClosure = this.createClosure(node.body); | ||
var declarations = this.collectDeclarations; | ||
this.collectDeclarations = oldDecls; | ||
var declVars = this.collectDeclVars; | ||
var declFuncs = this.collectDeclFuncs; | ||
this.collectDeclVars = oldDeclVars; | ||
this.collectDeclFuncs = oldDeclFuncs; | ||
return function () { | ||
@@ -777,3 +784,3 @@ // bind current scope | ||
self.setCurrentScope(currentScope); | ||
self.addDeclarationsToScope(declarations, currentScope); // var t = function(){ typeof t } // function | ||
self.addDeclarationsToScope(declVars, declFuncs, currentScope); // var t = function(){ typeof t } // function | ||
// t = function(){ typeof t } // function | ||
@@ -1083,3 +1090,3 @@ // z = function tx(){ typeof tx } // function | ||
_proto.assertVariable = function assertVariable(data, name, node) { | ||
if (data === this.rootScope.data && !(name in data)) { | ||
if (data === this.rootScope.data && name !== "undefined" && !(name in data)) { | ||
throw this.createInternalThrowError(Messages.VariableUndefinedReferenceError, name, node); | ||
@@ -1650,27 +1657,22 @@ } | ||
_proto.varDeclaration = function varDeclaration(name) { | ||
var context = this.collectDeclarations; | ||
if (!hasOwnProperty.call(context, name)) { | ||
context[name] = undefined; | ||
} | ||
var context = this.collectDeclVars; | ||
context[name] = undefined; | ||
}; | ||
_proto.funcDeclaration = function funcDeclaration(name, func) { | ||
var _a; | ||
var context = this.collectDeclFuncs; | ||
context[name] = func; | ||
}; | ||
var context = this.collectDeclarations; | ||
_proto.addDeclarationsToScope = function addDeclarationsToScope(declVars, declFuncs, scope) { | ||
var scopeData = scope.data; | ||
if (!hasOwnProperty.call(context, name) || context[name] === undefined || ((_a = context[name]) === null || _a === void 0 ? void 0 : _a.isFunctionDeclareClosure)) { | ||
context[name] = func; | ||
for (var key in declFuncs) { | ||
var value = declFuncs[key]; | ||
scopeData[key] = value ? value() : value; | ||
} | ||
}; | ||
_proto.addDeclarationsToScope = function addDeclarationsToScope(declarations, scope) { | ||
var scopeData = scope.data; | ||
var isRootScope = this.rootScope === scope; | ||
for (var key in declarations) { | ||
if (hasOwnProperty.call(declarations, key) && (isRootScope ? !(key in scopeData) : !hasOwnProperty.call(scopeData, key))) { | ||
var value = declarations[key]; | ||
scopeData[key] = value ? value() : value; | ||
for (var _key2 in declVars) { | ||
if (!(_key2 in scopeData)) { | ||
scopeData[_key2] = void 0; | ||
} | ||
@@ -1726,2 +1728,4 @@ } | ||
}(); | ||
Interpreter.version = "1.0.9"; | ||
Interpreter.rootContext = void 0; | ||
Interpreter.global = getGlobal(); |
@@ -40,3 +40,4 @@ import { MessageItem } from "./messages"; | ||
callStack: string[]; | ||
collectDeclarations: CollectDeclarations; | ||
collectDeclVars: CollectDeclarations; | ||
collectDeclFuncs: CollectDeclarations; | ||
protected isVarDeclMode: boolean; | ||
@@ -46,2 +47,4 @@ protected lastExecNode: Node | null; | ||
protected execEndTime: number; | ||
static version: string; | ||
static rootContext: undefined; | ||
static global: any; | ||
@@ -133,3 +136,3 @@ constructor(context?: Context, options?: Options); | ||
funcDeclaration(name: string, func: () => any): void; | ||
addDeclarationsToScope(declarations: CollectDeclarations, scope: Scope): void; | ||
addDeclarationsToScope(declVars: CollectDeclarations, declFuncs: CollectDeclarations, scope: Scope): void; | ||
getScopeValue(name: string, startScope: Scope): any; | ||
@@ -136,0 +139,0 @@ getScopeDataFromName(name: string, startScope: Scope): ScopeData; |
@@ -102,3 +102,4 @@ "use strict"; | ||
this.collectDeclarations = {}; | ||
this.collectDeclVars = Object.create(null); | ||
this.collectDeclFuncs = Object.create(null); | ||
this.isVarDeclMode = false; | ||
@@ -135,3 +136,4 @@ this.lastExecNode = null; | ||
this.collectDeclarations = {}; | ||
this.collectDeclVars = Object.create(null); | ||
this.collectDeclFuncs = Object.create(null); | ||
this.execStartTime = Date.now(); | ||
@@ -172,5 +174,6 @@ this.execEndTime = this.execStartTime; | ||
this.addDeclarationsToScope(this.collectDeclarations, this.getCurrentScope()); // reset | ||
this.addDeclarationsToScope(this.collectDeclVars, this.collectDeclFuncs, this.getCurrentScope()); // reset | ||
this.collectDeclarations = {}; // start run | ||
this.collectDeclVars = Object.create(null); | ||
this.collectDeclFuncs = Object.create(null); // start run | ||
@@ -731,8 +734,8 @@ try { | ||
} // function call | ||
// this = rootContext | ||
// this = undefined | ||
// tips: | ||
// test(...) === test.call(this.rootContext, ...) | ||
// test(...) === test.call(undefined, ...) | ||
return func.bind(_this8.rootContext); | ||
return func.bind(Interpreter.rootContext); | ||
}; | ||
@@ -762,4 +765,6 @@ } | ||
var self = this; | ||
var oldDecls = this.collectDeclarations; | ||
this.collectDeclarations = {}; | ||
var oldDeclVars = this.collectDeclVars; | ||
var oldDeclFuncs = this.collectDeclFuncs; | ||
this.collectDeclVars = Object.create(null); | ||
this.collectDeclFuncs = Object.create(null); | ||
var name = node.id ? node.id.name : ""; | ||
@@ -772,4 +777,6 @@ var paramLength = node.params.length; | ||
var bodyClosure = this.createClosure(node.body); | ||
var declarations = this.collectDeclarations; | ||
this.collectDeclarations = oldDecls; | ||
var declVars = this.collectDeclVars; | ||
var declFuncs = this.collectDeclFuncs; | ||
this.collectDeclVars = oldDeclVars; | ||
this.collectDeclFuncs = oldDeclFuncs; | ||
return function () { | ||
@@ -788,3 +795,3 @@ // bind current scope | ||
self.setCurrentScope(currentScope); | ||
self.addDeclarationsToScope(declarations, currentScope); // var t = function(){ typeof t } // function | ||
self.addDeclarationsToScope(declVars, declFuncs, currentScope); // var t = function(){ typeof t } // function | ||
// t = function(){ typeof t } // function | ||
@@ -1094,3 +1101,3 @@ // z = function tx(){ typeof tx } // function | ||
_proto.assertVariable = function assertVariable(data, name, node) { | ||
if (data === this.rootScope.data && !(name in data)) { | ||
if (data === this.rootScope.data && name !== "undefined" && !(name in data)) { | ||
throw this.createInternalThrowError(_messages.Messages.VariableUndefinedReferenceError, name, node); | ||
@@ -1661,27 +1668,22 @@ } | ||
_proto.varDeclaration = function varDeclaration(name) { | ||
var context = this.collectDeclarations; | ||
if (!hasOwnProperty.call(context, name)) { | ||
context[name] = undefined; | ||
} | ||
var context = this.collectDeclVars; | ||
context[name] = undefined; | ||
}; | ||
_proto.funcDeclaration = function funcDeclaration(name, func) { | ||
var _a; | ||
var context = this.collectDeclFuncs; | ||
context[name] = func; | ||
}; | ||
var context = this.collectDeclarations; | ||
_proto.addDeclarationsToScope = function addDeclarationsToScope(declVars, declFuncs, scope) { | ||
var scopeData = scope.data; | ||
if (!hasOwnProperty.call(context, name) || context[name] === undefined || ((_a = context[name]) === null || _a === void 0 ? void 0 : _a.isFunctionDeclareClosure)) { | ||
context[name] = func; | ||
for (var key in declFuncs) { | ||
var value = declFuncs[key]; | ||
scopeData[key] = value ? value() : value; | ||
} | ||
}; | ||
_proto.addDeclarationsToScope = function addDeclarationsToScope(declarations, scope) { | ||
var scopeData = scope.data; | ||
var isRootScope = this.rootScope === scope; | ||
for (var key in declarations) { | ||
if (hasOwnProperty.call(declarations, key) && (isRootScope ? !(key in scopeData) : !hasOwnProperty.call(scopeData, key))) { | ||
var value = declarations[key]; | ||
scopeData[key] = value ? value() : value; | ||
for (var _key2 in declVars) { | ||
if (!(_key2 in scopeData)) { | ||
scopeData[_key2] = void 0; | ||
} | ||
@@ -1739,2 +1741,4 @@ } | ||
exports.Interpreter = Interpreter; | ||
Interpreter.version = "1.0.9"; | ||
Interpreter.rootContext = void 0; | ||
Interpreter.global = getGlobal(); |
{ | ||
"name": "eval5", | ||
"version": "1.0.8", | ||
"version": "1.0.9", | ||
"description": "A JavaScript interpreter, written completely in JavaScript", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -7,3 +7,3 @@ # eval5 | ||
## usage | ||
## Usage | ||
@@ -18,3 +18,4 @@ `npm install --save eval5` | ||
evaluate("1+1"); // 2 | ||
//或 evaluate("1+1", Object.create(window)); | ||
evaluate("1+1", window); // 2 | ||
@@ -32,3 +33,4 @@ const func = new Function('a','b', 'return a+b;'); | ||
try { | ||
result = interpreter.evaluate('1+1') | ||
result = interpreter.evaluate('1+1'); | ||
console.log(result) //2 | ||
} catch(e) { | ||
@@ -38,4 +40,2 @@ //.. | ||
console.log(result) //2 | ||
``` | ||
@@ -49,2 +49,9 @@ | ||
例如: | ||
``` | ||
Interpreter.global = window; | ||
``` | ||
### `constructor`(ctx: {}, options?: { timeout?: number}) | ||
@@ -54,10 +61,23 @@ | ||
``` | ||
var interpreter = new Interpreter(window); | ||
``` | ||
### `evaluate`(code: string, ctx?: {}): any | ||
返回脚本中执行的最后一个表达式结果的结果 | ||
返回脚本中执行的最后一个表达式结果 | ||
## evaluate(code, ctx?) | ||
``` | ||
var interpreter = new Interpreter(window); | ||
interpreter.evaluate('alert(1+1)') | ||
``` | ||
执行给定的字符串脚本 | ||
## evaluate(code: string, ctx?: {}) | ||
执行给定的字符串脚本,返回脚本中执行的最后一个表达式结果 | ||
``` | ||
evaluate('console.log(1+1)', {console: console}) | ||
``` | ||
## Function | ||
@@ -88,4 +108,8 @@ | ||
## 感谢 | ||
## Support | ||
- ECMA5 | ||
## Related | ||
[evaljs][] | ||
@@ -92,0 +116,0 @@ [closure-interpreter][] |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
949793
10289
113