Skip to content

Commit

Permalink
[js] Code evaluation changes to prevent context polution
Browse files Browse the repository at this point in the history
  • Loading branch information
pmurias committed Oct 12, 2018
1 parent 6cf2cd9 commit c2f07ca
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/vm/js/Compiler.nqp
Expand Up @@ -1748,7 +1748,7 @@ class QAST::CompilerJS does DWIMYNameMangling does SerializeOnce {
@setup.push("new nqp.EvalResult({$body.expr}, nqp.createArray(cuids))");
}

@setup.unshift((!$instant ?? '' !! "module.exports = ") ~ "nqp.run({self.async}function() \{\n");
@setup.unshift((!$instant ?? '/* return */' !! "module.exports = ") ~ "nqp.run({self.async}function() \{\n");
@setup.push("\});\n");
Chunk.new($T_VOID, "", @setup);
}
Expand Down
18 changes: 11 additions & 7 deletions src/vm/js/nqp-runtime/core.js
Expand Up @@ -661,7 +661,7 @@ function toJS(obj) {

const nqp = require('./runtime.js');

const Script = process.browser ? null : require('vm').Script;
const vm = process.browser ? null : require('vm');

const sourceMaps = {};
const evaledP6Sources = {};
Expand Down Expand Up @@ -789,6 +789,11 @@ exports.buildSourceMap = new BuildSourceMap();


class JavaScriptCompiler extends NQPObject {
$$fixupRun(code) {
const fixedUp = code.replace(/\/\*\s*return\s\*\/\s*nqp\.run/, 'return nqp.run');
return fixedUp;
}

eval(ctx, _NAMED, self, code) {
if (!(_NAMED !== null && _NAMED.hasOwnProperty('mapping'))) {
return fromJS(eval(nqp.arg_s(ctx, code)));
Expand Down Expand Up @@ -824,7 +829,7 @@ class JavaScriptCompiler extends NQPObject {
evaledP6Filenames[fakeFilename] = nqp.toStr(_NAMED.file, ctx);
}

const script = new Script(preamble + codeStr, {filename: fakeFilename});
const compiled = vm.compileFunction(preamble + this.$$fixupRun(codeStr), [], {filename: fakeFilename});

global.nqpModule = module;

Expand All @@ -835,19 +840,18 @@ class JavaScriptCompiler extends NQPObject {
return require(path);
};


const ret = fromJS(script.runInThisContext());
const ret = fromJS(compiled());
global.nqpRequire = oldNqpRequire;

return ret;
}

compile(ctx, _NAMED, self, code) {
const script = new Script(code);
const compiled = vm.compileFunction(this.$$fixupRun(code));

const codeRef = new CodeRef();
codeRef.$$call = function(ctx, _NAMED) {
return fromJS(script.runInThisContext());
return fromJS(compiled());
};
return codeRef;
}
Expand Down Expand Up @@ -1613,7 +1617,7 @@ op.ctxouterskipthunks = function(ctx) {
let outer = ctx.$$skipHandlers().$$outer;

// FIXME - ctxs that don't have a codeRef
while (outer && outer.codeRef().staticCode.isThunk) {
while (outer && (outer.codeRef() && outer.codeRef().staticCode.isThunk)) {
outer = outer.$$outer;
if (outer) outer = outer.$$skipHandlers();
}
Expand Down

0 comments on commit c2f07ca

Please sign in to comment.