Commits

Anonymous committed cc9b137

add serializeEnabled property
support to deserialize from string

Comments (0)

Files changed (2)

 
 void CEngine::Expose(void)
 {
-  v8::V8::Initialize();
+  //v8::V8::Initialize();
   //v8::V8::SetFatalErrorHandler(ReportFatalError);
   //v8::V8::AddMessageListener(ReportMessage);
 
     .add_static_property("currentThreadId", &v8::V8::GetCurrentThreadId,
                          "the V8 thread id of the calling thread.")
 
+    .add_static_property("serializeEnabled", &CEngine::IsSerializeEnabled, &CEngine::SetSerializeEnable)
+
     .def("serialize", &CEngine::Serialize)
     .staticmethod("serialize")
 
   return &m_counters[name];
 }
 
+void CEngine::SetSerializeEnable(bool value)
+{
+  if (value)
+    v8::internal::Serializer::Enable();
+  else
+    v8::internal::Serializer::Disable();
+}
+bool CEngine::IsSerializeEnabled(void)
+{
+  return v8::internal::Serializer::enabled();
+}
+
 py::object CEngine::Serialize(void)
 {
   v8::internal::byte* data = NULL;
 
   v8::internal::StatsTable::SetCounterFunction(&CEngine::CounterLookup);
 
-  v8::internal::Serializer::Enable();
-
-  v8::internal::Heap::CollectAllGarbage(false);
-
   v8::internal::Serializer serializer;
 
   serializer.Serialize();
 }
 void CEngine::Deserialize(py::object snapshot)
 {
+  const void *buf = NULL;
+  Py_ssize_t len = 0;
+
   if (PyBuffer_Check(snapshot.ptr()))
   {
-    const void *buf = NULL;
-    Py_ssize_t len = 0;
+    if (0 != ::PyObject_AsReadBuffer(snapshot.ptr(), &buf, &len))
+    {
+      buf = NULL;
+    }
+  }
+  else if(PyString_CheckExact(snapshot.ptr()) || PyUnicode_CheckExact(snapshot.ptr()))
+  {
+    if (0 != ::PyString_AsStringAndSize(snapshot.ptr(), (char **)&buf, &len))
+    {
+      buf = NULL;
+    }
+  }
 
-    if (0 == ::PyObject_AsReadBuffer(snapshot.ptr(), &buf, &len) && buf && len > 0)
-    {
-      Py_BEGIN_ALLOW_THREADS
+  if (buf && len > 0)
+  {
+    Py_BEGIN_ALLOW_THREADS
 
-      v8::internal::Deserializer deserializer((const v8::internal::byte *) buf, len);
+    v8::internal::Deserializer deserializer((const v8::internal::byte *) buf, len);
 
-      deserializer.GetFlags();
-      
-      deserializer.Deserialize();
+    deserializer.GetFlags();
 
-      v8::internal::StubCache::Clear();
+    v8::internal::V8::Initialize(&deserializer);
 
-      Py_END_ALLOW_THREADS 
-    }
+    Py_END_ALLOW_THREADS 
   }
 }
 
 
   py::object ExecuteScript(v8::Handle<v8::Script> script);
 
+  static void SetSerializeEnable(bool value);
+  static bool IsSerializeEnabled(void);
+
   static py::object Serialize(void);
   static void Deserialize(py::object snapshot);
 };