Serialize previously 'compiled' script functions for reuse later?

Jun 7, 2011 at 2:44 PM
Edited Jun 7, 2011 at 3:13 PM

I can do something like the following:

Object o = j.Run(@" function square(x) { return x * x; }");

and then these to call that function:

o = j.Run("square(3);"); MessageBox.Show(o.ToString());

o = j.Run("square(4);"); MessageBox.Show(o.ToString());

 

But I want to serialize the original square function to a disk file, so that it doesn't need recompiling each time I run the program. Is this possible?

Jun 7, 2011 at 4:42 PM

Almost. It's not compiled but at least the tree is not reinterpreted. There is a Parse method that returns the AST of the script, then you can reuse it without parsing again.

Jun 7, 2011 at 5:38 PM
Edited Jun 7, 2011 at 5:38 PM

Can you give me an example of how to go about this and use Parse? I searched the online documentation, but not results were returned for "Parse".

If I end up using Jint, I would like to give you 10-20% of profits from the product I make.

Jun 7, 2011 at 6:24 PM

var program = JintEngine.Compile("return 42;", false);
var engine = new JintEngine();
var result = engine.Run(program); 

And we are already thankful for your using it ;)

Nov 14, 2014 at 8:44 PM
Hello,
could you please suggest similar code in the latest version?
Thank you
Nov 14, 2014 at 9:38 PM
use the JavascriptParser class which has a Program() method. You can use the result of this method across different engines.