1

So I've read Execute JavaScript code stored as a string and looked at this simple answer.

My question, what if the JScript code, stored in the string, expects arguments? how to I pass it?

First try:

var jsStr = "WScript.Echo(WScript.Arguments[0]);";
eval(jsStr);

Second try:

var F = new Function(jsStr);
F.call(this,'test str');

But this poor attempt failed.

Note:

Let's assume the I have no control on jsStr value. So basically, reformatting it is out of the question at this moment.

Community
  • 1
  • 1
idanshmu
  • 5,061
  • 6
  • 46
  • 92
  • I would imagine you would have to do a replace... ie `jsStr.Replace("{0}", myParameter);`. A JS string.Format function will do just fine. – Johan Dec 02 '14 at 13:18
  • How would you know which arguments it expects? I cannot see any parameters in your `jsStr`. – Bergi Dec 02 '14 at 13:19
  • 2
    why do you want this? if you want help, include the full code, what is WScript? – micnic Dec 02 '14 at 13:20
  • 2
    @micnic this script is independent, meaning, is not run as a web application but run using `Windows Script Host`. Reference: [Wscript](http://msdn.microsoft.com/en-us/library/at5ydy31%28v=vs.84%29.aspx) – idanshmu Dec 02 '14 at 13:21
  • @Bergi Assume that I would save `jsStr` to a file and execute the file with command line arguments. that is what I want to achieve without having to save it to a file. – idanshmu Dec 02 '14 at 13:22
  • 1
    Don't know, what is `WScript`, but try `(new Function('WScript.Echo(arguments[0])'))('test str')` – vp_arth Dec 02 '14 at 13:23
  • 1
    Your original code works in the context of a jsfiddle using `arguments[0]` as @Johan says - the issue is more that `WScript.Arguments` won't work with function arguments. The question should probably be re-tagged with WScript and reworded to describe calling WScript.Arguments when the script is in a string – Rhumborl Dec 02 '14 at 13:29
  • @idanshmu, does your environment (WScript) does not offer you an `require` in the same way like Node.JS does to include and execute files? eval is evil! – micnic Dec 02 '14 at 13:30
  • @idanshmu: But if you `eval` some js code, it *does* have access to your global `Wscript.Arguments`? If you want to execute it as a separate script, it seems you need to create a file with the code and [`Run`](http://msdn.microsoft.com/en-us/library/d5fk67ky(v=vs.84).aspx) or [`Exec`](http://msdn.microsoft.com/en-us/library/ateytk4a(v=vs.84).aspx) it. – Bergi Dec 02 '14 at 13:52
  • @Bergi But creating a file is exactly what I'm trying to avoid. It seems odd to me that I need to create a file just because `jsStr` expect command line arguments. is this the only case where `eval` is not the solution for? if so, How do I solve it without creating a file? – idanshmu Dec 02 '14 at 13:55

1 Answers1

3

The problem is caused by the []. WScript.Arguments is an object (provided by the script host runtime), but not an array. To access its elements, you have to use (), i.e. call its Item() function. Evidence:

var jsStr;
jsStr = "WScript.Echo('a', WScript.Arguments(0));"; eval(jsStr);
jsStr = "WScript.Echo('b', WScript.Arguments.Item(0));"; eval(jsStr);

var expr;
expr = "typeof WScript.Arguments"; WScript.Echo(0, expr, eval(expr));
expr = "typeof WScript.Arguments(0)"; WScript.Echo(1, expr, eval(expr));
expr = "typeof WScript.Arguments[0]"; WScript.Echo(2, expr, eval(expr));

output:

cscript 27250366.js pipapo
a pipapo
b pipapo
0 typeof WScript.Arguments object
1 typeof WScript.Arguments(0) string
2 typeof WScript.Arguments[0] undefined
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96