This project has moved and is read-only. For the latest updates, please go here.

Serializing the entire engine

May 12, 2014 at 1:11 AM
So I know you can't do what the subject suggests.

What I'd like is the next best thing.

I'm trying to save as much of the running engine instance as possible so that I can re-instantiate it later and begin invoking functions. I recognize that I'm going to have to be doing something like scrapping the engine instance's root objects, saving them somehow, and then reinjecting them into the engine before resuming execution of script. So, I'm looking for two things:

1) How do I get access to all root objects from inside the CLR?
2) Any advice on things to look out for? I recognize I'm going to have it tricky with regards to tracking down and persisting references, and especially things like 'this' references in JavaScript, and function instances.

What I'm basically implementing is a W3C user agent for XForms. Imagine it as an actual web browser. Except I need to save my engine state and resume it where its left off. My implementation is basically done, except I'm looking for a suitable JavaScript engine to add into it. ClearScript is nice, but I need to be able to accomplish this.
May 12, 2014 at 2:31 PM

1) How do I get access to all root objects from inside the CLR?

You can do something like this:
foreach (var name in engine.Script.GetDynamicMemberNames()) {
    var obj = engine.Script[name];
    // do something with obj
This should return all global properties that aren't standard JavaScript built-ins.

2) Any advice on things to look out for?

Not knowing what kind of objects might be in your script environment, it's difficult to say. Would you expect many .NET objects, and if so, what types? What about script functions? Some script functions may be serializable, but some may not (e.g., those created via bind()). One thing you should definitely avoid is using JSON.stringify() to serialize .NET objects.

Good luck, and please let us know if you encounter any specific issues!