Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I parse javascript code in Jint interpreter?

Tags:

c#

jint

I want to make sure javascript code is valid syntax:

string test = " var i = 0; ";
Jint.Engine ScrptingEngine = new Jint.Engine();
bool result = ScrptingEngine.Parse(test);
like image 711
Bill Software Engineer Avatar asked May 15 '26 00:05

Bill Software Engineer


1 Answers

You can use the Execute() method to "parse" your JavaScript and the GetValue() method to retrieve the value and then assert that it works as intended.

string test = " var i = 0; ";
Jint.Engine ScrptingEngine = new Jint.Engine();
var result = ScrptingEngine.Execute(test).GetValue("i");
Assert.AreEqual(0, result);

In addition, the Execute method will also throw a JavaScriptException if the JavaScript is invalid:

try
{
    ScrptingEngine.Execute("xx = ss == esfx = fuct()"); 
}
catch(JavaScriptException ex) {}
like image 56
David L Avatar answered May 17 '26 14:05

David L



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!