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

Serializing a compiled script

Aug 8, 2014 at 4:27 AM
Edited Aug 8, 2014 at 4:29 AM
Is it possible to serialize a compiled script? For instance do something like:
byte[] result = null;
using (var clearscriptV8 = new V8Runtime())
using (var stream = new MemoryStream())
    var compiled = clearscriptV8.Compile(jsCode);
    var formatter = new BinaryFormatter();
    formatter.Serialize(stream, compiled);
    stream.Position = 0;
    result = stream.ToArray();
and then at some point later on deserialize it and call a function within the script?
V8Script compiledScript;

using (var stream = new MemoryStream(serializedScript))
    var formatter = new BinaryFormatter();
    stream.Seek(0, SeekOrigin.Begin);
    compiledScript = formatter.Deserialize(stream) as V8Script;

using (var engine = new V8ScriptEngine(V8ScriptEngineFlags.DisableGlobalMembers))
    return engine.Script.MyMethod(new { Something=true }) as IEnumerable<Result>;
Aug 8, 2014 at 3:10 PM

Unfortunately V8Script instances are not serializable. More information here.

Aug 8, 2014 at 3:20 PM
Is this something that can be changed in ClearScript or is it a limitation of V8?
Aug 8, 2014 at 3:44 PM
its a limitation of .net
Aug 8, 2014 at 4:35 PM
Hi again,

V8 doesn't support serialization of compiled scripts, but it does have a somewhat similar feature that ClearScript currently doesn't leverage.

In V8, script compilation optionally produces a binary artifact that supports offline storage and can be used to speed up the recompilation of the same script. This data could be combined with the original script code to produce a single object that supports standard .NET serialization and can reconstitute the compiled script faster than normal compilation.

Support for this feature is on our long-term to-do list.

Oct 15, 2016 at 8:38 PM
Is this still int eh works?
Oct 17, 2016 at 11:57 AM
It's still on the backlog. You're only the second person to express interest in this feature :)
Nov 2, 2016 at 4:04 PM
Hi Uniphix,

ClearScript 5.4.8 supports V8 caching, although serialization of the cache data remains the host's responsibility.