You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.
See the test case below, setTimeout does not accept strings in node.js (issue #550 closed), so when it is called with a string we pass to setTimeout new Function(string).
But doing this using runInContext causes setTimeout to execute in the global context.
There is a (kind of) workaround (see setTimeout2 below), but I am reporting this because this does not seem normal.
Test case :
varvm=require('vm');test2='test2';varf=function(){this.test="test";};g=newf();//globalg=vm.createContext(g);g.g=g;//assign in g context global var g referring to itg.console=console;g.setTimeout=function(a,b){if(typeof(a)=='string'){setTimeout(newFunction(a),b)}else{setTimeout(a,b)}};g.setTimeout2=function(a,b){if(typeof(a)=='string'){varfn=function(){returnvm.createScript(a,'').runInContext(g)};returnsetTimeout(fn,b);}else{setTimeout(a,b);}};varcode='setTimeout("console.log(test)",2000)';varcode2='setTimeout2("console.log(test)",2000)';vm.createScript(code,'').runInContext(g);//NOK test is undefinedvm.createScript(code2,'').runInContext(g);//OK display 'test'varcode='setTimeout("console.log(test2)",2000)';vm.createScript(code,'').runInContext(g);// display 'test2' --> setTimeout does execute in global contextvarcode='var fn=function() {console.log(test)};setTimeout(fn,2000)';vm.createScript(code,'').runInContext(g);//OK 'test'
The text was updated successfully, but these errors were encountered:
That's not really a bug. g.setTimeout is a function that's rooted in the main context. Thus, any function it creates is also rooted in said main context. The snippet below ties it to your custom context.
See the test case below, setTimeout does not accept strings in node.js (issue #550 closed), so when it is called with a string we pass to setTimeout new Function(string).
But doing this using runInContext causes setTimeout to execute in the global context.
There is a (kind of) workaround (see setTimeout2 below), but I am reporting this because this does not seem normal.
Test case :
The text was updated successfully, but these errors were encountered: