Is it possible to validate script syntax before executing it?

Dec 30, 2013 at 7:39 AM
Hi All,
probably it's due to my lack of experience with this very useful library, but I was not able to find method or a set of methods to validate scripts syntax before running them. Something like this: say we have a file (or even a string) containing the whole script, let us call this file "myscript.jscript", have a method like

engine.Compile("myscript.jscript")

to allow me an anticipated validation of script syntax.
Thank you
Coordinator
Dec 30, 2013 at 8:53 PM
Greetings!

ClearScript generally does not expose script parsing as a separate function from script execution. The methods that execute script code report syntax errors as well.

However, V8ScriptEngine.Compile() may be close to what you're looking for.

Cheers!
Jan 2, 2014 at 4:03 PM
Edited Jan 2, 2014 at 4:04 PM
Thank for answer and HNY to everyone,
I was also interested in JScript compilation facilities....

Just for a better understanding: is it a design choice to prevent syntax checking and script compilation from being called independently from script execution?

The reason of this question is: when executing, I agree that library makes the interpretation, and also syntax checking and compilation. But if execution leads to side effects on host objects, I'd prefer to make a syntax check before exec instead of realizing that script was wrong once several host objects have been changed.

WDYT?

My current workaround is: make a copy of host objects involved and make a "dry run" over them, and releasing after script is over, being execution successful or not.
I am not happy with this solution because if host objects are huge, duplicating them might be overwhelming in terms of memory.

Thanks in advance,
Tomaso
Coordinator
Jan 2, 2014 at 5:31 PM
Edited Jan 2, 2014 at 5:33 PM
Hi Tomaso,

is it a design choice to prevent syntax checking and script compilation from being called independently from script execution?

Originally ClearScript supported only JScript and VBScript. As far as we know, these script engines don't have syntax checking APIs, nor do they support compilation. V8 supports compilation, so we added V8ScriptEngine.Compile().

But if execution leads to side effects on host objects, I'd prefer to make a syntax check before exec instead of realizing that script was wrong once several host objects have been changed.

A script with syntax errors can't be executed, so it can't have harmful side effects. On the other hand, if a script originates from an untrusted source, checking its syntax won't guarantee that it's bug free and won't abuse your host objects. The best approach is to expose a host API that prevents scripts from doing damage by exposing read-only operations when possible, enforcing limits, etc.

Please send us your further thoughts.

Happy New Year!
Jan 3, 2014 at 11:25 AM
Thank you for the very clear and complete answer!

Reading it I start thinking that I am simply misunderstanding the way ClearScript works and worrying about a problem that does not exist. I'd make an example, let's assume this is the script to be executed (ho=an host object exposed which has 4 methods):
ho.Method1("One");
ho.Method2("Two");
hoMethod3("Three"); // the "dot" is missing - real syntax error
ho.Method4("Four");
How do things go in this case?
Does the engine recognize that Method3 call is syntactically wrong before starting the execution or when the 3-rd line is processed??

And also if this is the script:
ho.Method1("One");
ho.Method2("Two");
ho.Method34("Three"); // Typo, Method34 doesn't exist - syntax is correct (? maybe it's not from ClearScript point of view??), but host object doesn't expose this functionality
ho.Method4("Four");
Does it recognize that Method34 doesn't exist before starting the execution or when the 3-rd line is processed??

Best regards
Tomaso Tosolini
Coordinator
Jan 3, 2014 at 1:38 PM
Hi Tomaso,

hoMethod3("Three"); // the "dot" is missing - real syntax error

There's no syntax error here; this line simply invokes a function named "hoMethod3". If no such function exists when the line is executed, the script engine generates a runtime error. It may be semantically incorrect, but this line is 100% legal JavaScript, and because JavaScript has no static typing, the engine can't detect this kind of error prior to execution.

Does it recognize that Method34 doesn't exist before starting the execution or when the 3-rd line is processed??

As in the first example, the line is legal JavaScript, so the error would not be detected until runtime.

Cheers!
Jan 3, 2014 at 2:22 PM
Ok, thank you very much for all the clarifications :)