runInContext -> evalModule
parent
616749377b
commit
14b8b76a79
@ -1,26 +1,25 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* `eval` the given source, using properties found in `context` as top-level variables.
|
* `eval` the given source as a CommonJS module, using properties found in `context` as top-level variables.
|
||||||
*
|
*
|
||||||
* Based on `vm.runInContext` found in Node.js, this is a cross-env solution.
|
* Based on `vm.runInContext` found in Node.js, this is a cross-env solution.
|
||||||
*/
|
*/
|
||||||
function runInContext( source, context ) {
|
function evalModule( source, context ) {
|
||||||
|
|
||||||
const argumentKeys = Object.keys( context );
|
const argumentKeys = Object.keys( context );
|
||||||
const argumentValues = argumentKeys.map( argument => context[ argument ] );
|
const argumentValues = argumentKeys.map( argument => context[ argument ] );
|
||||||
|
|
||||||
const object = {};
|
const sandbox = { exports: {} };
|
||||||
argumentKeys.push( "_peg$object", `_peg$object.result = ${ source };` );
|
argumentKeys.push( "module", "exports", source );
|
||||||
argumentValues.push( object );
|
argumentValues.push( sandbox, sandbox.exports );
|
||||||
|
|
||||||
Function( ...argumentKeys )( ...argumentValues );
|
Function( ...argumentKeys )( ...argumentValues );
|
||||||
|
|
||||||
return object.result;
|
return sandbox.exports;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Exports
|
// Exports
|
||||||
|
|
||||||
module.exports = { runInContext };
|
module.exports = { evalModule };
|
||||||
|
Loading…
Reference in New Issue