Class ScriptableOutputStream is an ObjectOutputStream used to serialize JavaScript objects and functions.
Class ScriptableOutputStream is an ObjectOutputStream used to serialize JavaScript objects and functions. Note that compiled functions currently cannot be serialized, only interpreted functions. The top-level scope containing the object is not written out, but is instead replaced with another top-level object when the ScriptableInputStream reads in this object. Also, object corresponding to names added to the exclude list are not written out but instead are looked up during deserialization. This approach avoids the creation of duplicate copies of standard objects during deserialization.
Inheritance: Sharpen.ObjectOutputStream
Esempio n. 1
0
		public virtual void TestContinuationsInlineFunctionsSerialization()
		{
			Scriptable globalScope;
			Context cx = Context.Enter();
			try
			{
				globalScope = cx.InitStandardObjects();
				cx.SetOptimizationLevel(-1);
				// must use interpreter mode
				globalScope.Put("myObject", globalScope, Context.JavaToJS(new ContinuationsApiTest.MyClass(), globalScope));
			}
			finally
			{
				Context.Exit();
			}
			cx = Context.Enter();
			try
			{
				cx.SetOptimizationLevel(-1);
				// must use interpreter mode
				cx.EvaluateString(globalScope, "function f(a) { var k = eval(myObject.h()); var t = []; return k; }", "function test source", 1, null);
				Function f = (Function)globalScope.Get("f", globalScope);
				object[] args = new object[] { 7 };
				cx.CallFunctionWithContinuations(f, globalScope, args);
				NUnit.Framework.Assert.Fail("Should throw ContinuationPending");
			}
			catch (ContinuationPending pending)
			{
				// serialize
				MemoryStream baos = new MemoryStream();
				ScriptableOutputStream sos = new ScriptableOutputStream(baos, globalScope);
				sos.WriteObject(globalScope);
				sos.WriteObject(pending.GetContinuation());
				sos.Close();
				baos.Close();
				byte[] serializedData = baos.ToArray();
				// deserialize
				MemoryStream bais = new MemoryStream(serializedData);
				ScriptableInputStream sis = new ScriptableInputStream(bais, globalScope);
				globalScope = (Scriptable)sis.ReadObject();
				object continuation = sis.ReadObject();
				sis.Close();
				bais.Close();
				object result = cx.ResumeContinuation(continuation, globalScope, "2+3");
				NUnit.Framework.Assert.AreEqual(5, System.Convert.ToInt32(((Number)result)));
			}
			finally
			{
				Context.Exit();
			}
		}
Esempio n. 2
0
		/// <exception cref="System.IO.IOException"></exception>
		public static void Serialize(Context cx, Scriptable thisObj, object[] args, Function funObj)
		{
			if (args.Length < 2)
			{
				throw Context.ReportRuntimeError("Expected an object to serialize and a filename to write " + "the serialization to");
			}
			object obj = args[0];
			string filename = Context.ToString(args[1]);
			FileOutputStream fos = new FileOutputStream(filename);
			Scriptable scope = ScriptableObject.GetTopLevelScope(thisObj);
			ScriptableOutputStream @out = new ScriptableOutputStream(fos, scope);
			@out.WriteObject(obj);
			@out.Close();
		}