Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. #include "Foo.h"
  2.  
  3. Nan::Persistent<v8::Function> Foo::constructor;
  4.  
  5. Foo::Foo () { }
  6. Foo::~Foo () { }
  7.  
  8. void Foo::Init(v8::Local<v8::Object> exports) {
  9. Nan::HandleScope scope;
  10.  
  11. // Prepare constructor template
  12. v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(New);
  13. tpl->SetClassName(Nan::New("Foo").ToLocalChecked());
  14. tpl->InstanceTemplate()->SetInternalFieldCount(1);
  15.  
  16. // Prototype
  17. Nan::SetPrototypeMethod(tpl, "bar", Bar);
  18. Nan::SetPrototypeMethod(tpl, "on", Emitter::On); // Emitter's prototype method
  19.  
  20. constructor.Reset(tpl->GetFunction());
  21. exports->Set(Nan::New("Foo").ToLocalChecked(), tpl->GetFunction());
  22. }
  23.  
  24. void Foo::New(const Nan::FunctionCallbackInfo<v8::Value>& info) {
  25. if (info.IsConstructCall()) {
  26. Foo *foo = new Foo();
  27. foo->Wrap(info.This());
  28. info.GetReturnValue().Set(info.This());
  29. } else {
  30. Nan::ThrowError("nope");
  31. }
  32. }
  33.  
  34. void Foo::Bar(const Nan::FunctionCallbackInfo<v8::Value>& info) {
  35. Foo *foo = ObjectWrap::Unwrap<Foo>(info.Holder());
  36.  
  37. foo->Emit(Nan::New("sus").ToLocalChecked(), Nan::New("hi").ToLocalChecked(), Nan::New(6));
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement