Skip to content

Instantly share code, notes, and snippets.

@Qard Qard/context.cc Secret
Last active Aug 29, 2015

Embed
What would you like to do?
NAN_METHOD(Context::createEvent) {
NanScope();
Local<Object> obj = NanNew<Object>();
NanSetInternalFieldPointer(obj, 1, Context::get());
Handle<Value> argv[1] = { obj };
// Handle<Value> argv[1] = { NanNew<External>((void *) Context::get()) };
NanReturnValue(NanNew(Event::constructor)->NewInstance(1, argv));
}
// I'm trying to approximate this;
Context.prototype.createEvent = function () {
return new Event(this)
}
NAN_METHOD(Event::New) {
NanScope();
if (!args.IsConstructCall()) {
return NanThrowError("Event() must be called as a constructor");
}
Event* event;
if (args.Length() == 1) {
void* ptr = NanGetInternalFieldPointer(args[0].As<Object>(), 1);
metadata_t* context = static_cast<metadata_t*>(ptr);
// ptr is a nullptr here....
event = new Event(context, false);
} else {
event = new Event();
}
event->Wrap(args.This());
NanSetInternalFieldPointer(args.This(), 1, &event->event);
NanReturnValue(args.This());
}
void Event::Init(Handle<Object> exports) {
NanScope();
// Prepare constructor template
Handle<FunctionTemplate> ctor = NanNew<FunctionTemplate>(New);
ctor->InstanceTemplate()->SetInternalFieldCount(2);
ctor->SetClassName(NanNew<String>("Event"));
NanAssignPersistent(constructor, ctor->GetFunction());
exports->Set(NanNew<String>("Event"), ctor->GetFunction());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.