You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.0 KiB
JavaScript

"use strict";
const VM_VARS = {
"module": true,
"process": true,
"VM_VARS": true,
"PREFACE_VARS": true,
"generateDeclaration": true,
"runInContext": true,
"code": true,
"preface": true,
};
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 = {
// `eval` the given code, using properties found in `context` as top-level
// variables, while hiding some variables in this module from the code.
//
// Based on `vm.runInContext` found in Node.js, this is a cross-env solution.
runInContext( code, vm$context ) {
let preface = PREFACE_VARS;
if ( typeof vm$context === "object" ) {
preface += Object.keys( vm$context )
.map( generateDeclaration )
.join( " " );
}
return eval( preface + code );
},
};