Serializing a compiled script

Aug 8, 2014 at 3:27 AM
Edited Aug 8, 2014 at 3: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.Flush();
    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))
{
    engine.Evaluate(compiledScript);
    return engine.Script.MyMethod(new { Something=true }) as IEnumerable<Result>;
}
Coordinator
Aug 8, 2014 at 2:10 PM
Hello!

Unfortunately V8Script instances are not serializable. More information here.

Thanks!
Aug 8, 2014 at 2:20 PM
Is this something that can be changed in ClearScript or is it a limitation of V8?
Aug 8, 2014 at 2:44 PM
its a limitation of .net
Coordinator
Aug 8, 2014 at 3: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.

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

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

Cheers!