Comparing version 1.0.3 to 2.0.0
448
index.js
@@ -0,1 +1,4 @@ | ||
/* jshint esversion: 6 */ | ||
/* jshint noyield: true */ | ||
"use strict"; | ||
@@ -10,2 +13,4 @@ | ||
var parse = require('acorn').parse; | ||
var util = require("util"); | ||
var EventEmitter = require("events").EventEmitter; | ||
@@ -15,3 +20,24 @@ function noop() {} | ||
function execute(func) { | ||
return func(); | ||
var result = func(); | ||
if ('' + result === 'null') { | ||
return result; | ||
} | ||
// FIXME: Convert to yield* | ||
if (result !== undefined) { | ||
if (result.next) { | ||
var iter = result; | ||
var res = iter.next(); | ||
while (!res.done) { | ||
res = iter.next(); | ||
} | ||
if ('' + res.value === 'null') { | ||
return res.value; | ||
} | ||
if ('' + res.value === 'undefined') { | ||
return res.value; | ||
} | ||
return res.value; | ||
} | ||
} | ||
return result; | ||
} | ||
@@ -36,2 +62,3 @@ | ||
function Environment(globalObjects) { | ||
EventEmitter.call(this); | ||
if (!Array.isArray(globalObjects)) { | ||
@@ -49,8 +76,10 @@ globalObjects = [globalObjects]; | ||
this._curThis = this._globalObj; | ||
this._boundGen = this._gen.bind(this); | ||
this.DEBUG = false; | ||
this.DELAY = 0; | ||
this.STATE = 'running'; | ||
} | ||
util.inherits(Environment, EventEmitter); | ||
function createVarStore(parent, vars) { | ||
@@ -65,6 +94,5 @@ vars = vars || {}; | ||
Environment.prototype.gen = function (node) { | ||
var opts = {}; | ||
if (this.DEBUG) { | ||
opts.locations = true; | ||
} | ||
var opts = { | ||
'locations': true | ||
}; | ||
if (typeof node === 'string') { | ||
@@ -104,3 +132,3 @@ node = parse(node, opts); | ||
IfStatement: this._genIfStmt, | ||
ConditionalExpression: this._genIfStmt, | ||
ConditionalExpression: this._genCondStmt, | ||
ForStatement: this._genLoopStmt, | ||
@@ -115,3 +143,3 @@ WhileStatement: this._genLoopStmt, | ||
BreakStatement: this._genBreakStmt, | ||
SwitchStatement: this._genSwitchStmt, | ||
SwitchStatement: this._genSwitchStmt | ||
}[node.type] || function () { | ||
@@ -129,3 +157,2 @@ console.warn("Not implemented yet: " + node.type); | ||
} | ||
var resp = closure(); | ||
@@ -144,30 +171,93 @@ info += '. Result:'; | ||
function* callExpr(expr) { | ||
var result; | ||
if (expr.constructor.name == 'GeneratorFunction') { | ||
result = yield* expr(); | ||
} else { | ||
result = expr(); | ||
} | ||
return result; | ||
} | ||
var cmp = { | ||
'==': function () {return a() == b(); }, | ||
'!=': function () {return a() != b(); }, | ||
'===': function () {return a() === b(); }, | ||
'!==': function () {return a() !== b(); }, | ||
'<': function () {return a() < b(); }, | ||
'<=': function () {return a() <= b(); }, | ||
'>': function () {return a() > b(); }, | ||
'>=': function () {return a() >= b(); }, | ||
'<<': function () {return a() << b(); }, | ||
'>>': function () {return a() >> b(); }, | ||
'>>>': function () {return a() >>> b(); }, | ||
'+': function () {return a() + b(); }, | ||
'-': function () {return a() - b(); }, | ||
'*': function () {return a() * b(); }, | ||
'/': function () {return a() / b(); }, | ||
'%': function () {return a() % b(); }, | ||
'|': function () {return a() | b(); }, | ||
'^': function () {return a() ^ b(); }, | ||
'&': function () {return a() & b(); }, | ||
'in': function () {return a() in b(); }, | ||
'instanceof': function () {return a() instanceof b(); }, | ||
'==': function* () { | ||
return (yield* callExpr(a)) == (yield* callExpr(b)); | ||
}, | ||
'!=': function* () { | ||
return (yield* callExpr(a)) != (yield* callExpr(b)); | ||
}, | ||
'===': function* () { | ||
return (yield* callExpr(a)) === (yield* callExpr(b)); | ||
}, | ||
'!==': function* () { | ||
return (yield* callExpr(a)) !== (yield* callExpr(b)); | ||
}, | ||
'<': function* () { | ||
return (yield* callExpr(a)) < (yield* callExpr(b)); | ||
}, | ||
'<=': function* () { | ||
return (yield* callExpr(a)) <= (yield* callExpr(b)); | ||
}, | ||
'>': function* () { | ||
return (yield* callExpr(a)) > (yield* callExpr(b)); | ||
}, | ||
'>=': function* () { | ||
return (yield* callExpr(a)) >= (yield* callExpr(b)); | ||
}, | ||
'<<': function* () { | ||
return (yield* callExpr(a)) << (yield* callExpr(b)); | ||
}, | ||
'>>': function* () { | ||
return (yield* callExpr(a)) >> (yield* callExpr(b)); | ||
}, | ||
'>>>': function* () { | ||
return (yield* callExpr(a)) >>> (yield* callExpr(b)); | ||
}, | ||
'+': function* () { | ||
return (yield* callExpr(a)) + (yield* callExpr(b)); | ||
}, | ||
'-': function* () { | ||
return (yield* callExpr(a)) - (yield* callExpr(b)); | ||
}, | ||
'*': function* () { | ||
return (yield* callExpr(a)) * (yield* callExpr(b)); | ||
}, | ||
'/': function* () { | ||
return (yield* callExpr(a)) / (yield* callExpr(b)); | ||
}, | ||
'%': function* () { | ||
return (yield* callExpr(a)) % (yield* callExpr(b)); | ||
}, | ||
'|': function* () { | ||
return (yield* callExpr(a)) | (yield* callExpr(b)); | ||
}, | ||
'^': function* () { | ||
return (yield* callExpr(a)) ^ (yield* callExpr(b)); | ||
}, | ||
'&': function* () { | ||
return (yield* callExpr(a)) & (yield* callExpr(b)); | ||
}, | ||
'in': function* () { | ||
return (yield* callExpr(a)) in (yield* callExpr(b)); | ||
}, | ||
'instanceof': function* () { | ||
return (yield* callExpr(a)) instanceof (yield* callExpr(b)); | ||
}, | ||
// logic expressions | ||
'||': function () {return a() || b(); }, | ||
'&&': function () {return a() && b(); }, | ||
'||': function* () { | ||
return (yield* callExpr(a)) || (yield* callExpr(b)); | ||
}, | ||
'&&': function* () { | ||
return (yield* callExpr(a)) && (yield* callExpr(b)); | ||
} | ||
}[node.operator]; | ||
return function () { | ||
return cmp(); | ||
// FIXME: Convert to yield* | ||
var iter = cmp(); | ||
var res = iter.next(); | ||
while (!res.done) { | ||
res = iter.next(); | ||
} | ||
return res.value; | ||
}; | ||
@@ -180,11 +270,22 @@ }; | ||
} | ||
var a = this._gen(node.argument); | ||
var op = { | ||
'-': function () {return -a(); }, | ||
'+': function () {return +a(); }, | ||
'!': function () {return !a(); }, | ||
'~': function () {return ~a(); }, | ||
'typeof': function () {return typeof a(); }, | ||
'void': function () {return void a(); }, | ||
'-': function () { | ||
return -a(); | ||
}, | ||
'+': function () { | ||
return +a(); | ||
}, | ||
'!': function () { | ||
return !a(); | ||
}, | ||
'~': function () { | ||
return ~a(); | ||
}, | ||
'typeof': function () { | ||
return typeof a(); | ||
}, | ||
'void': function () { | ||
return void a(); | ||
} | ||
}[node.operator]; | ||
@@ -200,2 +301,3 @@ | ||
var attr = this._genName(node.argument); | ||
return function () { | ||
@@ -219,5 +321,5 @@ return delete obj()[attr()]; | ||
}); | ||
return function () { | ||
var result = {}; | ||
items.forEach(function (item) { | ||
@@ -232,2 +334,3 @@ result[item.key] = item.getVal(); | ||
var items = node.elements.map(this._boundGen); | ||
return function () { | ||
@@ -245,3 +348,6 @@ return items.map(execute); | ||
} | ||
return function () {return key; }; | ||
return function () { | ||
return key; | ||
}; | ||
}; | ||
@@ -251,3 +357,2 @@ | ||
var self = this; | ||
var callee; | ||
@@ -265,4 +370,28 @@ if (node.callee.type === 'MemberExpression') { | ||
var args = node.arguments.map(self._gen.bind(self)); | ||
return function () { | ||
return callee().apply(self._globalObj, args.map(execute)); | ||
return function* () { | ||
self.emit('line', node.loc.start.line); | ||
var c = callee(); | ||
if (c === undefined) { | ||
return c; | ||
} | ||
var result; | ||
var res; | ||
if (c.next) { | ||
res = yield* c; | ||
result = res.apply(self._globalObj, args.map(execute)); | ||
} else { | ||
result = c.apply(self._globalObj, args.map(execute)); | ||
} | ||
if (result !== undefined) { | ||
if (result.next) { | ||
res = yield* result; | ||
return res; | ||
} | ||
} | ||
return result; | ||
}; | ||
@@ -274,16 +403,21 @@ }; | ||
var args = node.arguments.map(this._boundGen); | ||
return function () { | ||
return newWithArgs(callee(), args.map(execute)); | ||
var self = this; | ||
return function* () { | ||
self.emit('line', node.loc.start.line); | ||
var cl = callee(); | ||
var ar = args.map(execute); | ||
var newObject = Object.create(cl.prototype); | ||
var constructor = cl.apply(newObject, ar); | ||
yield* constructor; | ||
return newObject; | ||
}; | ||
}; | ||
function newWithArgs(Cls, args) { | ||
var allArgs = [Cls].concat(args); | ||
return new (Function.prototype.bind.apply(Cls, allArgs))(); | ||
} | ||
Environment.prototype._genMemExpr = function (node) { | ||
var self = this; | ||
var obj = this._gen(node.object); | ||
var property = this._memExprProperty(node); | ||
return function () { | ||
self.emit('line', node.loc.start.line); | ||
return obj()[property()]; | ||
@@ -299,3 +433,5 @@ }; | ||
var self = this; | ||
return function () {return self._curThis; }; | ||
return function () { | ||
return self._curThis; | ||
}; | ||
}; | ||
@@ -315,11 +451,22 @@ | ||
Environment.prototype._genUpdExpr = function (node) { | ||
var self = this; | ||
var update = { | ||
'--true': function (obj, name) {return --obj[name]; }, | ||
'--false': function (obj, name) {return obj[name]--; }, | ||
'++true': function (obj, name) {return ++obj[name]; }, | ||
'++false': function (obj, name) {return obj[name]++; }, | ||
'--true': function (obj, name) { | ||
return --obj[name]; | ||
}, | ||
'--false': function (obj, name) { | ||
return obj[name]--; | ||
}, | ||
'++true': function (obj, name) { | ||
return ++obj[name]; | ||
}, | ||
'++false': function (obj, name) { | ||
return obj[name]++; | ||
} | ||
}[node.operator + node.prefix]; | ||
var obj = this._genObj(node.argument); | ||
var name = this._genName(node.argument); | ||
return function () { | ||
return function* () { | ||
self.emit('line', node.loc.start.line); | ||
yield; | ||
return update(obj(), name()); | ||
@@ -342,3 +489,5 @@ }; | ||
if (node.type === 'Identifier') { | ||
return function () {return node.name; }; | ||
return function () { | ||
return node.name; | ||
}; | ||
} else if (node.type === 'MemberExpression') { | ||
@@ -378,15 +527,40 @@ return this._memExprProperty(node); | ||
Environment.prototype._genAssignExpr = function (node) { | ||
var self = this; | ||
var setter = { | ||
'=': function (obj, name, val) {return (obj[name] = val); }, | ||
'+=': function (obj, name, val) {return obj[name] += val; }, | ||
'-=': function (obj, name, val) {return obj[name] -= val; }, | ||
'*=': function (obj, name, val) {return obj[name] *= val; }, | ||
'/=': function (obj, name, val) {return obj[name] /= val; }, | ||
'%=': function (obj, name, val) {return obj[name] %= val; }, | ||
'<<=': function (obj, name, val) {return obj[name] <<= val; }, | ||
'>>=': function (obj, name, val) {return obj[name] >>= val; }, | ||
'>>>=': function (obj, name, val) {return obj[name] >>>= val; }, | ||
'|=': function (obj, name, val) {return obj[name] |= val; }, | ||
'^=': function (obj, name, val) {return obj[name] ^= val; }, | ||
'&=': function (obj, name, val) {return obj[name] &= val; }, | ||
'=': function (obj, name, val) { | ||
return (obj[name] = val); | ||
}, | ||
'+=': function (obj, name, val) { | ||
return obj[name] += val; | ||
}, | ||
'-=': function (obj, name, val) { | ||
return obj[name] -= val; | ||
}, | ||
'*=': function (obj, name, val) { | ||
return obj[name] *= val; | ||
}, | ||
'/=': function (obj, name, val) { | ||
return obj[name] /= val; | ||
}, | ||
'%=': function (obj, name, val) { | ||
return obj[name] %= val; | ||
}, | ||
'<<=': function (obj, name, val) { | ||
return obj[name] <<= val; | ||
}, | ||
'>>=': function (obj, name, val) { | ||
return obj[name] >>= val; | ||
}, | ||
'>>>=': function (obj, name, val) { | ||
return obj[name] >>>= val; | ||
}, | ||
'|=': function (obj, name, val) { | ||
return obj[name] |= val; | ||
}, | ||
'^=': function (obj, name, val) { | ||
return obj[name] ^= val; | ||
}, | ||
'&=': function (obj, name, val) { | ||
return obj[name] &= val; | ||
} | ||
}[node.operator]; | ||
@@ -396,4 +570,11 @@ var obj = this._genObj(node.left); | ||
var val = this._gen(node.right); | ||
return function () { | ||
return setter(obj(), name(), val()); | ||
return function* () { | ||
self.emit('line', node.left.loc.start.line); | ||
var v = val(); | ||
if (v !== undefined) { | ||
if (v.next) { | ||
v = yield* v; | ||
} | ||
} | ||
return setter(obj(), name(), v); | ||
}; | ||
@@ -404,4 +585,5 @@ }; | ||
this._curDeclarations[node.id.name] = this._genFuncExpr(node); | ||
return noop; | ||
return function* () { | ||
return noop; | ||
}; | ||
}; | ||
@@ -429,2 +611,10 @@ | ||
Environment.prototype.getState = function () { | ||
return this.STATE; | ||
}; | ||
Environment.prototype.setState = function (state) { | ||
this.STATE = state; | ||
}; | ||
Environment.prototype._genFuncExpr = function (node) { | ||
@@ -442,7 +632,7 @@ var self = this; | ||
var parent = self._curVarStore; | ||
return function () { | ||
return function* () { | ||
// build arguments object | ||
var args = new Arguments(); | ||
args.length = arguments.length; | ||
for (var i = 0; i < arguments.length; i ++) { | ||
for (var i = 0; i < arguments.length; i++) { | ||
args[i] = arguments[i]; | ||
@@ -466,3 +656,3 @@ } | ||
// run function body | ||
var result = body(); | ||
var result = yield* body(); | ||
@@ -493,6 +683,13 @@ // switch 'stack' back | ||
}); | ||
return function () { | ||
return function* () { | ||
var result; | ||
for (var i = 0; i < stmtClosures.length; i++) { | ||
result = stmtClosures[i](); | ||
if (stmtClosures[i].constructor.name === 'GeneratorFunction') { | ||
result = yield* stmtClosures[i](); | ||
yield; | ||
} else { | ||
result = stmtClosures[i](); | ||
yield; | ||
} | ||
if (result === Break || result === Continue || result instanceof Return) { | ||
@@ -516,4 +713,6 @@ break; | ||
Environment.prototype._genRetStmt = function (node) { | ||
var self = this; | ||
var arg = node.argument ? this._gen(node.argument) : noop; | ||
return function () { | ||
self.emit('line', node.loc.start.line); | ||
return new Return(arg()); | ||
@@ -524,4 +723,25 @@ }; | ||
Environment.prototype._genIfStmt = function (node) { | ||
var test = this._gen(node.test); | ||
var self = this; | ||
var test = function () { | ||
self.emit('line', node.loc.start.line); | ||
return self._gen(node.test)(); | ||
}; | ||
var consequent = this._gen(node.consequent); | ||
var alternate = node.alternate ? this._gen(node.alternate) : function* () { | ||
return noop; | ||
}; | ||
return function* () { | ||
var result = test() ? yield* consequent() : yield* alternate(); | ||
return result; | ||
}; | ||
}; | ||
Environment.prototype._genCondStmt = function (node) { | ||
var self = this; | ||
var test = function () { | ||
self.emit('line', node.loc.start.line); | ||
return self._gen(node.test)(); | ||
}; | ||
var consequent = this._gen(node.consequent); | ||
var alternate = node.alternate ? this._gen(node.alternate) : noop; | ||
@@ -535,13 +755,23 @@ | ||
Environment.prototype._genLoopStmt = function (node, body) { | ||
var init = node.init ? this._gen(node.init) : noop; | ||
var test = node.test ? this._gen(node.test) : function () { | ||
var self = this; | ||
var init = node.init ? this._gen(node.init) : function* () { | ||
return noop; | ||
}; | ||
var test = node.test ? function* () { | ||
self.emit('line', node.loc.start.line); | ||
return self._gen(node.test)(); | ||
} : function* () { | ||
return true; | ||
}; | ||
var update = node.update ? this._gen(node.update) : noop; | ||
var update = node.update ? this._gen(node.update) : function* () { | ||
return noop; | ||
}; | ||
body = body || this._gen(node.body); | ||
return function () { | ||
return function* () { | ||
self.emit('line', node.loc.start.line); | ||
var resp; | ||
for (init(); test(); update()) { | ||
var newResp = body(); | ||
for (yield* init(); yield* test(); yield* update()) { | ||
var newResp = yield* body(); | ||
if (newResp === Break) { | ||
@@ -566,5 +796,5 @@ break; | ||
return function () { | ||
body(); | ||
return loop(); | ||
return function* () { | ||
yield* body(); | ||
yield* loop(); | ||
}; | ||
@@ -583,6 +813,8 @@ }; | ||
} | ||
return function () { | ||
return function* () { | ||
self.emit('line', node.loc.start.line); | ||
var resp; | ||
for (var x in right()) { | ||
self._genAssignExpr({ | ||
self.emit('line', node.loc.start.line); | ||
yield* self._genAssignExpr({ | ||
operator: '=', | ||
@@ -595,3 +827,3 @@ left: left, | ||
})(); | ||
resp = body(); | ||
resp = yield* body(); | ||
} | ||
@@ -606,5 +838,5 @@ return resp; | ||
var body = self._gen(node.body); | ||
return function () { | ||
return function* () { | ||
self._curVarStore = createVarStore(self._curVarStore, obj()); | ||
var result = body(); | ||
var result = yield* body(); | ||
self._curVarStore = self._curVarStore.parent; | ||
@@ -655,7 +887,11 @@ return result; | ||
Environment.prototype._genContStmt = function () { | ||
return function () {return Continue; }; | ||
return function () { | ||
return Continue; | ||
}; | ||
}; | ||
Environment.prototype._genBreakStmt = function () { | ||
return function () {return Break; }; | ||
return function () { | ||
return Break; | ||
}; | ||
}; | ||
@@ -670,7 +906,7 @@ | ||
test: curCase.test ? self._gen(curCase.test) : null, | ||
code: self._genProgram({body: curCase.consequent}) | ||
code: self._genProgram({ body: curCase.consequent }) | ||
}; | ||
}); | ||
return function () { | ||
return function* () { | ||
var foundMatch = false; | ||
@@ -693,3 +929,3 @@ var discriminantVal = discriminant(); | ||
// foundMatch is guaranteed to be true here | ||
var newResp = curCase.code(); | ||
var newResp = yield* curCase.code(); | ||
if (newResp === Break) { | ||
@@ -704,3 +940,3 @@ return resp; | ||
if (!foundMatch && defaultCase) { | ||
return defaultCase.code(); | ||
return yield* defaultCase.code(); | ||
} | ||
@@ -713,6 +949,10 @@ }; | ||
var env = new Environment(global); | ||
var resp = env.gen(code)(); | ||
return resp; | ||
var iterator = env.gen(code)(); | ||
var result = iterator.next(); | ||
while (!result.done) { | ||
result = iterator.next(); | ||
} | ||
return result.value; | ||
}; | ||
//console.log(exports.evaluate("1 + 1")); |
{ | ||
"name": "evaljs", | ||
"version": "1.0.3", | ||
"version": "2.0.0", | ||
"description": "A JavaScript interpreter written in JavaScript", | ||
@@ -8,3 +8,4 @@ "main": "index.js", | ||
"dependencies": { | ||
"acorn": "^4.0.4" | ||
"acorn": "^4.0.4", | ||
"require-from-string": "^1.2.1" | ||
}, | ||
@@ -14,12 +15,12 @@ "devDependencies": { | ||
"esprima": "^3.1.2", | ||
"is-optimizable": "^1.1.1", | ||
"jshint": "^2.9.4", | ||
"regenerator": "^0.9.5", | ||
"uglify-js": "^2.7.5" | ||
}, | ||
"scripts": { | ||
"test": "npm run optimized && npm run jshint && node test.js", | ||
"optimized": "./node_modules/.bin/is-optimizable index.js test.js", | ||
"test": "npm run compile && npm run jshint && node test.js", | ||
"compile": "./node_modules/regenerator/bin/regenerator --include-runtime ./index.js > ./index-compiled.js", | ||
"jshint": "./node_modules/.bin/jshint index.js test.js theTest.js", | ||
"build": "mkdir -p dist && npm run build-js && npm run minify", | ||
"build-js": "./node_modules/.bin/browserify index.js -S evaljs -o dist/eval.js", | ||
"build-js": "./node_modules/.bin/browserify -r acorn -r ./index-compiled.js:evaljs -o dist/eval.js", | ||
"minify": "./node_modules/.bin/uglifyjs -mc -o dist/eval.min.js dist/eval.js" | ||
@@ -44,7 +45,8 @@ }, | ||
"contributors": [ | ||
{ "name" : "Jason Huggins", | ||
"email" : "jrhuggins@gmail.com", | ||
"url" : "http://www.hugs.io/" | ||
} | ||
{ | ||
"name": "Jason Huggins", | ||
"email": "jrhuggins@gmail.com", | ||
"url": "http://www.hugs.io/" | ||
} | ||
] | ||
} |
@@ -98,4 +98,4 @@ eval.js | ||
``DEBUG`` option). There are probably bugs. That said, it can run itself | ||
including acorn without modifications, so its supported subset of JS is | ||
usable. PRs containing improvements welcome! | ||
and acorn, so its supported subset of JS is usable. PRs containing | ||
improvements welcome! | ||
@@ -102,0 +102,0 @@ How slow is it? |
51
test.js
"use strict"; | ||
var fs = require('fs'); | ||
var evaljs = require('./index'); | ||
var fs = require('fs'); | ||
var parse = require('acorn').parse; | ||
var requireFromString = require('require-from-string'); | ||
function run(iter){ | ||
var result = iter.next(); | ||
while(!result.done) { | ||
result = iter.next(); | ||
} | ||
return result.value; | ||
} | ||
// basic | ||
@@ -12,31 +21,43 @@ console.log(evaljs.evaluate('1 + 1')); | ||
var code = fs.readFileSync('theTest.js', {encoding: 'UTF-8'}); | ||
var parsedCode = parse(code, {'locations': true}); | ||
var env = new evaljs.Environment([{console: console}]); | ||
env.gen(parse(code))(); | ||
var iter1 = env.gen(parsedCode)(); | ||
run(iter1); | ||
// index.js | ||
var code = fs.readFileSync('index.js', {encoding: 'UTF-8'}); | ||
var envGlobal = {console: console, Array: Array, Error: Error, Object: Object}; | ||
var code = fs.readFileSync('index-compiled.js', {encoding: 'UTF-8'}); | ||
var script = "var evaljs = requireFromString(code);" + | ||
"console.log(evaljs.evaluate('30 + 4'));"; | ||
var envGlobal = { | ||
code: code, | ||
console: console | ||
}; | ||
envGlobal.global = global; | ||
var modLocal = {require: require, exports: {}}; | ||
var modLocal = { | ||
requireFromString: requireFromString, | ||
}; | ||
var env = new evaljs.Environment([envGlobal, modLocal]); | ||
env.gen(code)(); | ||
var iter2 = env.gen(script)(); | ||
run(iter2); | ||
// acorn.js | ||
var code = fs.readFileSync(require.resolve('acorn'), {encoding: 'UTF-8'}); | ||
var env = new evaljs.Environment([global, {exports: {}, module: {}}]); | ||
//env.DEBUG = true; | ||
var envGlobal = { code: code }; | ||
var envModules = { requireFromString: requireFromString }; | ||
var env = new evaljs.Environment([envGlobal, envModules]); | ||
// load library | ||
env.gen(code)(); | ||
var iter3 = env.gen("var acorn = requireFromString(code);")(); | ||
run(iter3); | ||
// parse file | ||
var parsed = env.gen('exports.parse("1+1")')(); | ||
var iter4 = env.gen("acorn.parse('1 + 1');")(); | ||
var parsed = run(iter4); | ||
// for bonus points: run the parsed expression | ||
console.log(env.gen(parsed)()); | ||
var iter5 = env.gen(parsed)(); | ||
console.log(run(iter5)); | ||
// using esprima | ||
var esprima = require('esprima'); | ||
console.log(evaljs.evaluate(esprima.parse('1 + 1'))); | ||
console.log(evaljs.evaluate(esprima.parse('1 + 1'))); |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
437974
9599
2
9
+ Addedrequire-from-string@^1.2.1
+ Addedrequire-from-string@1.2.1(transitive)