node.js - Run code in a Proxy context -
i'm experimenting harmony proxies , i'd run code in proxy context, means global object of code proxy. example, if call function foo() in code, managed proxy get() method.
but using proxy.create() , vm.runinnewcontext() doesn't work, seems proxy object overwritten new context object , looses properties.
var vm = require('vm'); var proxy = proxy.create({ get: function(a, name){ return function(){ console.log(arguments); } } }); vm.runinnewcontext("foo('bar')", proxy); // referenceerror: foo not defined is there way achieve i'm trying do?
// edit
(function() { eval("this.foo('bar')"); }).call(proxy); the above code works well, i'd able not use this statement, , directly refer global context.
this possible in 2 ways. in node, able modified contextify. contextify module allows turning arbitrary objects global contexts run similar vm module. creating global object has named property interceptor forwards accesses object, it's able keep reference "live" instead of copying properties node's builtin vm does. modification made change these accesses trigger correct proxy traps, ie changing ctx->sandbox->getrealnamedproperty(property) (which doesn't trigger proxy trap) ctx->sandbox->get(property). similar changes has, set, etc. property enumeration doesn't work quite right (nor in contextify normally) because ability hand property listing (for getownpropertynames @ least) isn't exposed api.
contextify: https://github.com/brianmcd/contextify fork: https://github.com/benvie/contextify pull request: https://github.com/brianmcd/contextify/pull/23
the second method work universally doesn't result in proxy global. create proxies each existing object in global , load desired code inside function created shadows properties function parameters. like:
var globals = object.getownpropertynames(global); var proxies = globals.map(function(key){ return forwardingproxy(global[key]); }); globals.push(codetorun); var compiled = function.apply(null, globals); var returnvalue = compiled.apply(forwardingproxy(global), proxies);
Comments
Post a Comment