node.js - Javascript VM object returned has wrong reference? -
back again tedious problem. it's easier if explain context before problem. i'm trying build project (in node.js) allows program itself, inside via "input" (this network/console or really, lets assume via console this).
so let's imagine have object:
var database = { 'test': 123 }
now console type:
;database.myfunc = function() { return database.test; }
i run using vm.runincontext
, copy database object afterwards (it needs sandbox'd because it's non-privileged users running it). works fine, , code works fine when executed in next command:
;database.myfunc() => 123
all far. @ point built own object dumper can export functions strings (var str = new string(database.myfunc)
), works , exports them strings.
this issue comes when import code file. i've tried code this:
code = "retval = " + data.tostring(); sandbox = vm.createcontext({ "database": sb.database }); result = vm.runinnewcontext(code, sandbox); sb.database = result; console.log(sb.database.myfunc());
it appears code runs, has access database copied sandbox. isn't want, want access same db had @ first runtime. suggestions how i'd that?
as name implies, vm.runinnewcontext
creates different context every time call it. don't use that! instead, use vm.createcontext
(once only) vm.runincontext
. you'll need maintain reference context
returned createcontext()
.
Comments
Post a Comment