Skip to content

Commit

Permalink
use proxy value for callbacks when static-eval fails
Browse files Browse the repository at this point in the history
builds on #35, but when static-eval cannot evaluate a callback function
because it is unsafe, this passes a proxy value. when the proxy callback
function is called, it throws an error, but when it is stringified (eg
in the generated output) it'll work.

this works with brfs, i haven't tried others yet.
  • Loading branch information
goto-bus-stop committed Nov 18, 2017
1 parent d6c8972 commit 8d7ba21
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,29 @@ module.exports = function parse (modules, opts) {

var xvars = copy(vars);
xvars[node.name] = val;

var res = evaluate(cur, xvars);
if (res === undefined && cur.type === 'CallExpression') {
// static-module can't safely evaluate code with callbacks, so do it manually in a safe way
var callee = evaluate(cur.callee, xvars);
var args = cur.arguments.map(function (arg) {
// Return a function stub for callbacks so that `static-module` users
// can do `callback.toString()` and get the original source
if (arg.type === 'FunctionExpression' || arg.type === 'ArrowFunctionExpression') {
var fn = function () {
throw new Error('static-module: cannot call callbacks defined inside source code');
};
fn.toString = function () {
return body.slice(arg.start, arg.end);
};
return fn;
}
return evaluate(arg, xvars);
});

res = callee.apply(null, args)
}

if (res !== undefined) {
updates.push({
start: cur.start,
Expand Down

0 comments on commit 8d7ba21

Please sign in to comment.