1

I am using ClearScript to compile some JavaScript and then I would like to serialize it to store it in SQL. But it is marked as not Serializable, what should I do?

V8ScriptEngine engine = new V8ScriptEngine();
V8Script compiled = engine.Compile("var a = 'test'");
using (MemoryStream ms = new MemoryStream())
{
    new BinaryFormatter().Serialize(ms, compiled);
    string compiledString = Convert.ToBase64String(ms.ToArray());
}

I get this error:

Additional information: Type 'Microsoft.ClearScript.V8.V8ScriptImpl' in Assembly 'ClearScriptV8-32, Version=5.4.6.0, Culture=neutral, PublicKeyToken=935d0c957da47c73' is not marked as serializable.
Bill Software Engineer
  • 7,362
  • 23
  • 91
  • 174
  • 2
    From V8's perspective, it doesn't make any sense to serialize it. It's not something you can use again later, as it requires a lot of state inside of V8. Not sure what concepts are exposed to c#, but that's what's going on under the hood. – xaxxon Sep 21 '16 at 02:40
  • Also, there are ways to snapshot the state of a V8 isolate/context after you pre-load certain things into them, but that isn't going to be related to C# serialization -- that's a V8 thing. Docs for that (for native API) are here: http://v8project.blogspot.com/2015/09/custom-startup-snapshots.html – xaxxon Sep 21 '16 at 02:49

1 Answers1

3

A V8 compiled script is tied to the isolate instance that created it, so it makes no sense to serialize it. You can't reuse it in a different process, nor even with another isolate in the same process. There's more information here and here.

BitCortex
  • 3,328
  • 1
  • 15
  • 19