I've got a C++ object which manages the lifespan of the v8 engine. There
are a couple of methods for execution of code: one which executes in the
"global" execution context and another for execution in a one-off clean
environment. Each clean environment should have the initial configuration
of the standard environment. I've got something like this:
class Interp {
Handle<ObjectTempalte> global_template;
Persistent<Context> main_context;
public:
Interp() {
HandleScope sc;
createGlobals();
main_context = Context::New(NULL, global_template);
}
void runGlobal(const std::string& script) {
HandleScope sc;
Context::Scope csc(main_context);
Handle<String> scriptSource = String::New(script.c_str());
Handle<Script> scriptObj = Script::Compile(scriptSource);
scriptObj->run(); // there's actually some TryCatch error handling that I
left out
}
void runScoped(const std::string& script) {
HandleScope sc;
Persistent<Context> context = Context::New(NULL, global_template); //
This Fails
Context::Scope csc(context);
Handle<String> scriptSource = String::New(script.c_str());
Handle<Script> scriptObj = Script::Compile(scriptSource);
scriptObj->run(); // there's actually some TryCatch error handling that I
left out
context.Dispose();
}
void createGlobals() {
global_template = ObjectTemplate::New();
Local<ObjectTemplate> console = ObjectTempalte::New();
console->Set("log", FunctionTemplate::New(my_log_callback));
//... more of this sort of thing
global_template->Set("console", console);
}
};
The global execution works fine but the marked line fails with the message
below. I'm obviously doing something wrong but I can't figure it out. I
tried calling createGlobals() in the scoped version to completely recreate
the globals template and still had a crash.
#
# Fatal error in ...\v8\src\objects-inl.h, line 2266
# CHECK(object->IsForeign()) failed
#
==== Stack trace ============================================
Security context: 24740F95 <JS Object>#0#
1: /* anonymous */ [24708091 <undefined>:1] (this=2474103D <JS Global
Object>#1#)
==== Details ================================================
[1]: /* anonymous */ [24708091 <undefined>:1] (this=2474103D <JS Global
Object>#1#) {
// stack-allocated locals
var .result = 24708091 <undefined>
// expression stack (top to bottom)
[03] : 247411D9 <JS Function log>#2#
[02] : 2C61514D <String[3]: foo>
[01] : 2E70D811 <an Object>#3#
--------- s o u r c e c o d e ---------
console.log("foo")
-----------------------------------------
}
==== Key ============================================
#0# 24740F95: 24740F95 <JS Object>
#1# 2474103D: 2474103D <JS Global Object>
#2# 247411D9: 247411D9 <JS Function log>
#3# 2E70D811: 2E70D811 <an Object>
=====================
--
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users