I'm trying to add serialize/deserialize supports to pyv8, a two-way
python and v8 wrapper, base on the v8 internal APIs.

Till now, I could dump the snapshot with Serializer.Serialize, but
fail to use it to initialize v8 again. The
Deserializer::GetContextStack function failed, because the expected
format is wrong. I'm confused because the snapshot was generated with
same v8 version, even the size seems too smaller (262K) than cctest
generated serdes (432K).

Could you give me some hints, what I missed when dumping the snapshot?
I just refer the test-serialze.cc code and translate it to python
wrapper.

Thanks

# test code with pyv8
#!/usr/bin/env python
import sys

from PyV8 import *

if len(sys.argv) == 1:
    extSrc = """function hello(name) { return "hello " + name + " from
javascript"; }"""
    extJs = JSExtension("hello/javascript", extSrc)

    JSEngine.serializeEnabled = True

    with JSContext(extensions=['hello/javascript']) as ctxt:
        data = JSEngine.serialize()

        open('snapshot.dat', 'wb').write(data)

        print "write %d bytes to snapshot.dat" % len(data)
else:
    data = open(sys.argv[1], 'rb').read()

    print "read %d bytes from %s" % (len(data), sys.argv[1])

    raw_input()

    JSEngine.deserialize(data)

    with JSContext() as ctxt:
        print ctxt.eval("hello('flier')")

# implementation code in pyv8
# http://code.google.com/p/pyv8/source/browse/trunk/src/Engine.cpp
py::object CEngine::Serialize(void)
{
  v8::internal::byte* data = NULL;
  int size = 0;

  Py_BEGIN_ALLOW_THREADS

  v8::internal::StatsTable::SetCounterFunction
(&CEngine::CounterLookup);

  v8::internal::Serializer serializer;

  serializer.Serialize();

  serializer.Finalize(&data, &size);

  Py_END_ALLOW_THREADS

  py::object obj(py::handle<>(::PyBuffer_New(size)));

  void *buf = NULL;
  Py_ssize_t len = 0;

  if (0 == ::PyObject_AsWriteBuffer(obj.ptr(), &buf, &len) && buf &&
len > 0)
  {
    memcpy(buf, data, len);
  }
  else
  {
    obj = py::object();
  }

  return obj;
}
void CEngine::Deserialize(py::object snapshot)
{
  const void *buf = NULL;
  Py_ssize_t len = 0;

  if (PyBuffer_Check(snapshot.ptr()))
  {
    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 (buf && len > 0)
  {
    Py_BEGIN_ALLOW_THREADS

    v8::internal::Deserializer deserializer((const v8::internal::byte
*) buf, len);

    deserializer.GetFlags();

    v8::internal::V8::Initialize(&deserializer);

    Py_END_ALLOW_THREADS
  }
}

# error message when execute the test code
#
# Fatal error in d:\javascript\google-v8\src\serialize.h, line 221
# CHECK(c == expected) failed
#

# call stack for error
>       _PyV8.pyd!v8::internal::OS::DebugBreak()  Line 882      C++
        _PyV8.pyd!v8::internal::OS::Abort()  Line 877   C++
        _PyV8.pyd!V8_Fatal(const char * file=0x1052ddfc, int line=221, const
char * format=0x10527e68, ...)  Line 57 C++
        _PyV8.pyd!CheckHelper(const char * file=0x1052ddfc, int line=221,
const char * source=0x1052e0bc, bool condition=false)  Line 62 + 0x16
bytes   C++
        _PyV8.pyd!v8::internal::SnapshotReader::ExpectC(char expected='C')
Line 221 + 0x22 bytes   C++
        _PyV8.pyd!v8::internal::Deserializer::GetContextStack()  Line 1595      
C+
+
        _PyV8.pyd!v8::internal::Deserializer::Deserialize()  Line 1431  C++
        _PyV8.pyd!v8::internal::V8::Initialize(v8::internal::Deserializer *
des=0x0021f300)  Line 106       C++
        _PyV8.pyd!CEngine::Deserialize(boost::python::api::object snapshot=
{...})  Line 182 + 0xc bytes    C++
        _PyV8.pyd!boost::python::detail::invoke<int,void (__cdecl*)
(boost::python::api::object),boost::python::arg_from_python<boost::python::api::object>
>(boost::python::detail::invoke_tag_<1,0> __formal={...},
boost::python::detail::invoke_tag_<1,0> __formal={...}, void
(boost::python::api::object)* & f=0x101a8cf4,
boost::python::arg_from_python<boost::python::api::object> & ac0=
{...})  Line 81 + 0x1f bytes    C++
        _PyV8.pyd!boost::python::detail::caller_arity<1>::impl<void
(__cdecl*)
(boost::python::api::object),boost::python::default_call_policies,boost::mpl::vector2<void,boost::python::api::object>
>::operator()(_object * args_=0x01312610, _object *
__formal=0x00000000)  Line 223 + 0x37 bytes     C++
        _PyV8.pyd!
boost::python::objects::caller_py_function_impl<boost::python::detail::caller<void
(__cdecl*)
(boost::python::api::object),boost::python::default_call_policies,boost::mpl::vector2<void,boost::python::api::object>
> >::operator()(_object * args=0x01312610, _object * kw=0x00000000)
Line 39 C++
        _PyV8.pyd!boost::python::objects::py_function::operator()(_object *
args=0x01312610, _object * kw=0x00000000)  Line 144     C++
        _PyV8.pyd!boost::python::objects::function::call(_object *
args=0x01312610, _object * keywords=0x00000000)  Line 226 + 0x24 bytes
C++
        _PyV8.pyd!boost::python::objects::`anonymous
namespace'::bind_return::operator()()  Line 581 + 0x19 bytes    C++
        _PyV8.pyd!
boost::detail::function::void_function_ref_invoker0<boost::python::objects::`anonymous
namespace'::bind_return,void>::invoke
(boost::detail::function::function_buffer & function_obj_ptr={...})
Line 189        C++
        _PyV8.pyd!boost::function0<void>::operator()()  Line 1013 + 0x14
bytes   C++
        _PyV8.pyd!boost::python::detail::exception_handler::operator()(const
boost::function0<void> & f={...})  Line 75      C++
        _PyV8.pyd!
boost::python::detail::translate_exception<CJavascriptException,void
(__cdecl*)(CJavascriptException const &)>::operator()(const
boost::python::detail::exception_handler & handler={...}, const
boost::function0<void> & f={...}, void (const CJavascriptException &)*
const translate=0x101a98a2)  Line 46 + 0xc bytes        C++
        _PyV8.pyd!
boost::_bi::list3<boost::arg<1>,boost::arg<2>,boost::_bi::value<void
(__cdecl*)(CJavascriptException const &)> >::operator()
<bool,boost::python::detail::translate_exception<CJavascriptException,void
(__cdecl*)(CJavascriptException const &)
>,boost::_bi::list2<boost::python::detail::exception_handler const
&,boost::function0<void> const &> >(boost::_bi::type<bool> __formal=
{...},
boost::python::detail::translate_exception<CJavascriptException,void
(__cdecl*)(CJavascriptException const &)> & f={...},
boost::_bi::list2<boost::python::detail::exception_handler const
&,boost::function0<void> const &> & a={...}, boost::_bi::type<bool>
__formal={...})  Line 376       C++
        _PyV8.pyd!
boost::_bi::bind_t<bool,boost::python::detail::translate_exception<CJavascriptException,void
(__cdecl*)(CJavascriptException const &)
>,boost::_bi::list3<boost::arg<1>,boost::arg<2>,boost::_bi::value<void
(__cdecl*)(CJavascriptException const &)> > >::operator()
<boost::python::detail::exception_handler,boost::function0<void> >
(const boost::python::detail::exception_handler & a1={...}, const
boost::function0<void> & a2={...})  Line 103    C++
        _PyV8.pyd!
boost::detail::function::function_obj_invoker2<boost::_bi::bind_t<bool,boost::python::detail::translate_exception<CJavascriptException,void
(__cdecl*)(CJavascriptException const &)
>,boost::_bi::list3<boost::arg<1>,boost::arg<2>,boost::_bi::value<void
(__cdecl*)(CJavascriptException const &)> >
>,bool,boost::python::detail::exception_handler const
&,boost::function0<void> const &>::invoke
(boost::detail::function::function_buffer & function_obj_ptr={...},
const boost::python::detail::exception_handler & a0={...}, const
boost::function0<void> & a1={...})  Line 133    C++
        _PyV8.pyd!
boost::function2<bool,boost::python::detail::exception_handler const
&,boost::function0<void> const &>::operator()(const
boost::python::detail::exception_handler & a0={...}, const
boost::function0<void> & a1={...})  Line 1013 + 0x1c bytes      C++
        _PyV8.pyd!boost::python::detail::exception_handler::handle(const
boost::function0<void> & f={...})  Line 42      C++
        _PyV8.pyd!boost::python::handle_exception_impl
(boost::function0<void> f={...})  Line 24 + 0xf bytes   C++
        _PyV8.pyd!
boost::python::handle_exception<boost::python::objects::`anonymous
namespace'::bind_return>(boost::python::objects::`anonymous-
namespace'::bind_return f={...})  Line 29 + 0x2c bytes  C++
        _PyV8.pyd!function_call(_object * func=0x01bacb40, _object *
args=0x01312610, _object * kw=0x00000000)  Line 613 + 0x38 bytes        C++
        python26.dll!1e06b61c()
        [Frames below may be incorrect and/or missing, no symbols loaded for
python26.dll]
--~--~---------~--~----~------------~-------~--~----~
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to