UNPKG

newbox-eval

Version:

A Safe Eval But With Variable Saving

33 lines (28 loc) 832 B
/* NewBox Alpha 0.1 Do Not Copy! Copyright NiceJSProgrammer® 2020 */ var vm = require('vm') var sandbox = {logC: function(ll){console.log(ll)}} vm.createContext(sandbox); module.exports = function boxEval (code, context, opts) { var resultKey = 'NEW_BOX_EVAL' sandbox[resultKey] = {} var clearContext = ` (function(){ const keys = Object.getOwnPropertyNames(this).concat(['constructor']); keys.forEach((key) => { const item = this[key]; if(!item || typeof item.constructor !== 'function') return; this[key].constructor = undefined; }); })(); ` code = clearContext + resultKey + '=' + code if (context) { Object.keys(context).forEach(function (key) { sandbox[key] = context[key] }) } vm.runInContext(code, sandbox, opts) return sandbox[resultKey] }