I have a C++ executable that periodically calls on user-supplied
JavaScript code. Since that code can do anything, it's able to blow
memory usage. I want to not abort the whole process, but to simply cut
off V8 execution and then start it over.
There will never be more than one active V8 "session", but when a code
reload is requested, I need to set aside the old session, load up a
new one, and switch over only once the new code has been loaded
successfully (no exceptions thrown, basic sanity checks passed).
Currently, I use v8::Context for this.
In order to provide certain facilities, I also create one object of a
unique type, and store it in the global object. It has several
accessors that implement the features I'm providing to the script.
My code looks something like this:
Persistent<Context> ctx; //Global
V8::SetCaptureStackTraceForUncaughtExceptions(1);
V8::IgnoreOutOfMemoryException();
// to initialize scripts:
Persistent<Context> newctx=Context::New();
Context::Scope scope(newctx);
Handle<ObjectTemplate> cls=ObjectTemplate::New();
// ... cls->SetAccessor a few times ...
newctx->Global()->Set(String::New("special_name"),cls->NewInstance());
TryCatch err;
Handle<Script>
script=Script::Compile(String::New(block_of_code,length_of_that_code));
script->Run();
if (exception_thrown_or_sancheck_failed) {newctx.Dispose();
bail_out_and_go_do_other_things;}
ctx.Dispose();
ctx=newctx;
// end initialization
Execution of code is followed by:
if (ctx->HasOutOfMemoryException()) {report_error; ctx.Dispose();
mark_scripts_as_down;}
The problem comes when V8 detects an out-of-memory situation. It's
detected just fine, and my code responds to it, reporting the error
correctly. But the next time script initialization is called upon, the
instantiation of the special object fails (returning an empty handle),
and my process then segfaults setting that into the global object.
Somehow, the ObjectTemplate can be created, but calling NewInstance()
on that template fails.
What is the proper way to handle this? According to the docs for
V8::IgnoreOutOfMemoryException, *contexts* other than the one that had
trouble ought to be able to continue. So in theory, this basic
technique should work - but it seems I need something else in there
somewhere before it'll work.
Assistance or examples would be much appreciated! Thanks!
Chris Angelico
--
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users