I appreciate the pointers thus far.
I re-cast my code to create an object template, then create an
instance of the object to add my C++ instance pointer too. I keep
getting a crash in the ObjectTemplate->NewInstance() method. I've
tried moving the NewInstance() call around to different places in the
code and re-ordering the steps of creating the templates and calling
Set(), but the crash always stays in the same place.
v8 version r1040:
[CODE]
// C++ Header
class MyClass {
public:
// Private constructor for singleton class.
MyClass();
~MyClass();
private:
// Initialize the singleton
void Init();
// Callbacks for functions registered as built-ins.
static v8::Handle<v8::Value> AddData(const v8::Arguments& args);
static v8::Handle<v8::Value> Load(const v8::Arguments& args);
static v8::Handle<v8::Value> Log(const v8::Arguments& args);
// Execution environment containing built-in functions.
v8::Persistent<v8::Context> execution_context_;
};
// C++ code for MyClass::Init
void MyClass::Init() {
v8::HandleScope handle_scope; // For allocation of local handles.
// Create a template for the global object.
v8::Handle<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New();
v8::Handle<v8::ObjectTemplate> myApi_template = v8::ObjectTemplate::New();
// Bind C++ funtions as callbacks
myApi_template->SetInternalFieldCount(1);
// Bind the global '__hLog' function to the C++ Print callback.
myApi_template->Set(v8::String::New("__hLog"),
v8::FunctionTemplate::New(&apu::MyClass::Log));
// Bind the global '__hLoad' function to the C++ Load callback.
myApi_template->Set(v8::String::New("__hLoad"),
v8::FunctionTemplate::New(&apu::MyClass::Load));
// Bind the '__hAddData' function
myApi_template->Set(v8::String::New("__hAddData"),
v8::FunctionTemplate::New(&apu::MyClass::AddData));
// *** myApi_template->NewInstance crashes
v8::Local<v8::Object> myApi_instance = myApi_template->NewInstance();
myApi_instance->SetInternalField(0, v8::External::New(this));
// Create a new execution environment containing the built-in
// functions
v8::Handle<v8::Context> context = v8::Context::New(NULL, global_template);
execution_context_ = v8::Persistent<v8::Context>::New(context);
// Create a top level object named 'myApi' in the global object
context->Global()->Set(v8::String::New("myApi"), myApi_instance);
...
[/CODE]
Stack trace:
npmyplugin.dll!v8::internal::Array::length() Line 1375 + 0xa bytes C++
npmyplugin.dll!v8::internal::FixedArray::get(int index=0x00000004)
Line 1157 + 0xe bytes C++
npmyplugin.dll!v8::internal::Context::global() Line 238 + 0xa bytes C++
npmyplugin.dll!v8::internal::Context::builtins() Line 37 + 0x8 bytes C++
npmyplugin.dll!v8::internal::Top::builtins() Line 267 + 0xb bytes C++
npmyplugin.dll!v8::internal::Execution::InstantiateObject(v8::internal::Handle<v8::internal::ObjectTemplateInfo>
data={...}, bool * exc=0x05e4f3ff) Line 473 + 0x13 bytes C++
npmyplugin.dll!v8::ObjectTemplate::NewInstance() Line 2360 + 0x20 bytes C++
> npmyplugin.dll!apu::MyClass::Init() Line 96 C++
...