Skip to content

Commit 8d7ba21

Browse files
committed
use proxy value for callbacks when static-eval fails
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.
1 parent d6c8972 commit 8d7ba21

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

index.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,29 @@ module.exports = function parse (modules, opts) {
287287

288288
var xvars = copy(vars);
289289
xvars[node.name] = val;
290-
290+
291291
var res = evaluate(cur, xvars);
292+
if (res === undefined && cur.type === 'CallExpression') {
293+
// static-module can't safely evaluate code with callbacks, so do it manually in a safe way
294+
var callee = evaluate(cur.callee, xvars);
295+
var args = cur.arguments.map(function (arg) {
296+
// Return a function stub for callbacks so that `static-module` users
297+
// can do `callback.toString()` and get the original source
298+
if (arg.type === 'FunctionExpression' || arg.type === 'ArrowFunctionExpression') {
299+
var fn = function () {
300+
throw new Error('static-module: cannot call callbacks defined inside source code');
301+
};
302+
fn.toString = function () {
303+
return body.slice(arg.start, arg.end);
304+
};
305+
return fn;
306+
}
307+
return evaluate(arg, xvars);
308+
});
309+
310+
res = callee.apply(null, args)
311+
}
312+
292313
if (res !== undefined) {
293314
updates.push({
294315
start: cur.start,

0 commit comments

Comments
 (0)