1

Is there a way to fully emulate a global eval() call from within a function call? I've tried various methods and cannot find a suitable solution that works under wscript:

var Global = this;
function globalEval(code) {
    (1, eval).call(Global, code);
}
globalEval('var foo = "bar"');
WScript.echo(foo); // undefined

Other methods I've tried:

eval.call(Global, code);
Global.eval.call(Global, code);
new Function(code);
(1, eval)(code)
SReject
  • 3,774
  • 1
  • 25
  • 41
  • What do you mean by "fully emulate"? – Bergi Dec 18 '15 at 15:06
  • All variables, functions, etc created by the eval that are NOT wrapped in a function are created in the global scope – SReject Dec 18 '15 at 15:07
  • [`.call()` is totally not necessary](http://perfectionkills.com/global-eval-what-are-the-options/) – Bergi Dec 18 '15 at 15:08
  • So you don't want to emulate anything, you just want to make a global eval call but it doesn't work? – Bergi Dec 18 '15 at 15:09
  • yes, just a global scoped call; I tried without the `.call()` to no avail, just didn't remember to list it with the other methods tried – SReject Dec 18 '15 at 15:11
  • Possible duplicate of http://stackoverflow.com/q/11467759/692942 – user692942 Dec 18 '15 at 15:15
  • 1
    Suggested duplicate seems off as the specified question is talking about browser environments opposed to wscript; furthermore, no valid answer was given. – SReject Dec 18 '15 at 15:20
  • @SReject Correction the OP just never bothered to accept an answer, the issue is the same whether it's script in a browser or directly in a scripting engine. `ExecuteGlobal` in VBScript does exactly what the you are asking for there has no direct equivalent in JScript *(WSH doesn't use JavaScript, it uses Microsoft's own proprioraty version of ECMAScript)* but it can be done using `eval();`. – user692942 Dec 18 '15 at 15:26
  • @Hacketo Again WSH *(Windows Scripting Host)* uses JScript **not** JavaScript [you can't make direct comparisons](https://msdn.microsoft.com/en-us/library/4tc5a343.aspx). – user692942 Dec 18 '15 at 15:31
  • 1
    @Lankymart: You say it could be done using `eval` but apparently it doesn't work for the OP. Of course, `eval` works when executed in the global scope, but that's not what we have here. We would need indirect eval. Does JScript have that? – Bergi Dec 18 '15 at 15:35
  • 1
    @Bergi Agreed, but may be this will help - [Why does VBScript have Execute, ExecuteGlobal and Eval?](https://blogs.msdn.microsoft.com/ericlippert/2003/09/20/why-does-vbscript-have-execute-executeglobal-and-eval/) – user692942 Dec 18 '15 at 15:36
  • @Hacketo True but the point I'm making is they are different. `eval()` may indeed behave the same but according to the OP it doesn't. – user692942 Dec 18 '15 at 15:37
  • @Bergi Does JScript have indirect eval? Your guess is as good as mine. – user692942 Dec 18 '15 at 15:38
  • 3
    @Hacketo: `(1, eval)`, and everything with `.call`, is an indirect call, that's fine and *should* work (in JavaScript, at least) – Bergi Dec 18 '15 at 15:42
  • @Bergi If we are talking about JavaScript which we are not. This is where the confusion comes in, in that article I linked I think this line is probably the clincher *"The long and short of it is that if you want to affect the global name space, you have to do it explicitly"*. – user692942 Dec 18 '15 at 15:44
  • 1
    @Lankymart: thanks, that article is a good find. The statement you cited does indeed suggest that there is no way to do this. – Bergi Dec 18 '15 at 15:45
  • @Bergi Flexing my [Google Fu](http://english.stackexchange.com/a/19973/99025). – user692942 Dec 18 '15 at 15:46
  • Here's another interesting article from the man himself *([Eric Lippert](http://stackoverflow.com/users/88656/eric-lippert))* - [Eval is Evil, Part Three](https://blogs.msdn.microsoft.com/ericlippert/2004/01/26/eval-is-evil-part-three/) *(Bear in mind this Guy help develop VBScript, JScript and WScript amongst others)*. The article is talking about JScript.Net but the underlying engine is the same. – user692942 Dec 18 '15 at 16:02

0 Answers0