@ -1,25 +1,50 @@
"use strict" ;
"use strict" ;
const vm = {
const VM_VARS = {
runInContext ( code , context ) {
"module" : true ,
"process" : true ,
"VM_VARS" : true ,
"PREFACE_VARS" : true ,
"generateDeclaration" : true ,
"runInContext" : true ,
"code" : true ,
"preface" : true ,
const peg$context = typeof context !== "undefined" ? context : { } ;
} ;
const PREFACE _VARS = Object . keys ( VM _VARS )
. map ( name => ` var ${ name } = void 0; ` )
. join ( " " ) ;
function generateDeclaration ( name ) {
return ( ! VM _VARS [ name ] ? "var " : "" )
+ ` ${ name } = vm $ context. ${ name } ; ` ;
}
module . exports = {
/* eslint indent: 0 */
// `eval` the given code, using properties found in `context` as top-level
return eval ( `
// variables, while hiding some variables in this module from the code.
$ {
//
Object . keys ( peg$context )
// Based on `vm.runInContext` found in Node.js, this is a cross-env solution.
. map ( key => ` var ${ key } = peg $ context. ${ key } ; ` )
runInContext ( code , vm$context ) {
. join ( "" )
}
let preface = PREFACE _VARS ;
var vm = null , code = null , context = null ;
if ( typeof vm$context === "object" ) {
preface += Object . keys ( vm$context )
. map ( generateDeclaration )
. join ( " " ) ;
}
return eval ( preface + code ) ;
$ { code }
` );
} ,
} ,
} ;
} ;
module . exports = vm ;