Quantcast
Channel: ClearScript
Viewing all articles
Browse latest Browse all 2297

New Post: Serializing a compiled script

$
0
0
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>;
}

Viewing all articles
Browse latest Browse all 2297

Trending Articles