Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion 1_hello_world/nan/hello.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ void Method(const Nan::FunctionCallbackInfo<v8::Value>& info) {
void Init(v8::Local<v8::Object> exports) {
v8::Local<v8::Context> context = exports->CreationContext();
exports->Set(Nan::New("hello").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(Method)->GetFunction(context).ToLocalChecked());
Nan::New<v8::FunctionTemplate>(Method)
->GetFunction(context)
.ToLocalChecked());
}

NODE_MODULE(hello, Init)
4 changes: 2 additions & 2 deletions 1_hello_world/napi/hello.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <node_api.h>
#include <assert.h>
#include <node_api.h>

napi_value Method(napi_env env, napi_callback_info info) {
napi_status status;
Expand All @@ -9,7 +9,7 @@ napi_value Method(napi_env env, napi_callback_info info) {
return world;
}

#define DECLARE_NAPI_METHOD(name, func) \
#define DECLARE_NAPI_METHOD(name, func) \
{ name, 0, func, 0, 0, 0, napi_default, 0 }

napi_value Init(napi_env env, napi_value exports) {
Expand Down
4 changes: 3 additions & 1 deletion 2_function_arguments/nan/addon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ void Add(const Nan::FunctionCallbackInfo<v8::Value>& info) {
void Init(v8::Local<v8::Object> exports) {
v8::Local<v8::Context> context = exports->CreationContext();
exports->Set(Nan::New("add").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(Add)->GetFunction(context).ToLocalChecked());
Nan::New<v8::FunctionTemplate>(Add)
->GetFunction(context)
.ToLocalChecked());
}

NODE_MODULE(addon, Init)
4 changes: 2 additions & 2 deletions 2_function_arguments/napi/addon.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <node_api.h>
#include <assert.h>
#include <node_api.h>
#include <stdio.h>
napi_value Add(napi_env env, napi_callback_info info) {
napi_status status;
Expand Down Expand Up @@ -42,7 +42,7 @@ napi_value Add(napi_env env, napi_callback_info info) {
return sum;
}

#define DECLARE_NAPI_METHOD(name, func) \
#define DECLARE_NAPI_METHOD(name, func) \
{ name, 0, func, 0, 0, 0, napi_default, 0 }

napi_value Init(napi_env env, napi_value exports) {
Expand Down
6 changes: 3 additions & 3 deletions 2_function_arguments/node-addon-api/addon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ Napi::Value Add(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();

if (info.Length() < 2) {
Napi::TypeError::New(env, "Wrong number of arguments").ThrowAsJavaScriptException();
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
return env.Null();
}

Expand All @@ -21,8 +22,7 @@ Napi::Value Add(const Napi::CallbackInfo& info) {
}

Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports.Set(Napi::String::New(env, "add"),
Napi::Function::New(env, Add));
exports.Set(Napi::String::New(env, "add"), Napi::Function::New(env, Add));
return exports;
}

Expand Down
2 changes: 1 addition & 1 deletion 3_callbacks/nan/addon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
void RunCallback(const Nan::FunctionCallbackInfo<v8::Value>& info) {
v8::Local<v8::Function> cb = info[0].As<v8::Function>();
const unsigned argc = 1;
v8::Local<v8::Value> argv[argc] = { Nan::New("hello world").ToLocalChecked() };
v8::Local<v8::Value> argv[argc] = {Nan::New("hello world").ToLocalChecked()};
Nan::MakeCallback(Nan::GetCurrentContext()->Global(), cb, argc, argv);
}

Expand Down
6 changes: 3 additions & 3 deletions 3_callbacks/napi/addon.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <node_api.h>
#include <assert.h>
#include <node_api.h>

napi_value RunCallback(napi_env env, const napi_callback_info info) {
napi_status status;
Expand Down Expand Up @@ -28,8 +28,8 @@ napi_value RunCallback(napi_env env, const napi_callback_info info) {

napi_value Init(napi_env env, napi_value exports) {
napi_value new_exports;
napi_status status =
napi_create_function(env, "", NAPI_AUTO_LENGTH, RunCallback, nullptr, &new_exports);
napi_status status = napi_create_function(
env, "", NAPI_AUTO_LENGTH, RunCallback, nullptr, &new_exports);
assert(status == napi_ok);
return new_exports;
}
Expand Down
2 changes: 1 addition & 1 deletion 3_callbacks/node-addon-api/addon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
void RunCallback(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::Function cb = info[0].As<Napi::Function>();
cb.Call(env.Global(), { Napi::String::New(env, "hello world") });
cb.Call(env.Global(), {Napi::String::New(env, "hello world")});
}

Napi::Object Init(Napi::Env env, Napi::Object exports) {
Expand Down
7 changes: 5 additions & 2 deletions 4_object_factory/nan/addon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
void CreateObject(const Nan::FunctionCallbackInfo<v8::Value>& info) {
v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext();
v8::Local<v8::Object> obj = Nan::New<v8::Object>();
obj->Set(Nan::New("msg").ToLocalChecked(), info[0]->ToString(context).ToLocalChecked());
obj->Set(Nan::New("msg").ToLocalChecked(),
info[0]->ToString(context).ToLocalChecked());

info.GetReturnValue().Set(obj);
}

void Init(v8::Local<v8::Object> exports, v8::Local<v8::Object> module) {
v8::Local<v8::Context> context = exports->CreationContext();
module->Set(Nan::New("exports").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(CreateObject)->GetFunction(context).ToLocalChecked());
Nan::New<v8::FunctionTemplate>(CreateObject)
->GetFunction(context)
.ToLocalChecked());
}

NODE_MODULE(addon, Init)
6 changes: 3 additions & 3 deletions 4_object_factory/napi/addon.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <node_api.h>
#include <assert.h>
#include <node_api.h>

napi_value CreateObject(napi_env env, const napi_callback_info info) {
napi_status status;
Expand All @@ -21,8 +21,8 @@ napi_value CreateObject(napi_env env, const napi_callback_info info) {

napi_value Init(napi_env env, napi_value exports) {
napi_value new_exports;
napi_status status =
napi_create_function(env, "", NAPI_AUTO_LENGTH, CreateObject, nullptr, &new_exports);
napi_status status = napi_create_function(
env, "", NAPI_AUTO_LENGTH, CreateObject, nullptr, &new_exports);
assert(status == napi_ok);
return new_exports;
}
Expand Down
3 changes: 2 additions & 1 deletion 5_function_factory/nan/addon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ void MyFunction(const Nan::FunctionCallbackInfo<v8::Value>& info) {

void CreateFunction(const Nan::FunctionCallbackInfo<v8::Value>& info) {
v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext();
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(MyFunction);
v8::Local<v8::FunctionTemplate> tpl =
Nan::New<v8::FunctionTemplate>(MyFunction);
v8::Local<v8::Function> fn = tpl->GetFunction(context).ToLocalChecked();

// omit this to make it anonymous
Expand Down
7 changes: 4 additions & 3 deletions 5_function_factory/napi/addon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@ napi_value CreateFunction(napi_env env, napi_callback_info info) {
napi_status status;

napi_value fn;
status = napi_create_function(env, "theFunction", NAPI_AUTO_LENGTH, MyFunction, nullptr, &fn);
status = napi_create_function(
env, "theFunction", NAPI_AUTO_LENGTH, MyFunction, nullptr, &fn);
assert(status == napi_ok);

return fn;
}

napi_value Init(napi_env env, napi_value exports) {
napi_value new_exports;
napi_status status =
napi_create_function(env, "", NAPI_AUTO_LENGTH, CreateFunction, nullptr, &new_exports);
napi_status status = napi_create_function(
env, "", NAPI_AUTO_LENGTH, CreateFunction, nullptr, &new_exports);
assert(status == napi_ok);
return new_exports;
}
Expand Down
18 changes: 9 additions & 9 deletions 6_object_wrap/nan/myobject.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

Nan::Persistent<v8::Function> MyObject::constructor;

MyObject::MyObject(double value) : value_(value) {
}
MyObject::MyObject(double value) : value_(value) {}

MyObject::~MyObject() {
}
MyObject::~MyObject() {}

void MyObject::Init(v8::Local<v8::Object> exports) {
v8::Local<v8::Context> context = exports->CreationContext();
Expand All @@ -24,21 +22,22 @@ void MyObject::Init(v8::Local<v8::Object> exports) {

constructor.Reset(tpl->GetFunction(context).ToLocalChecked());
exports->Set(Nan::New("MyObject").ToLocalChecked(),
tpl->GetFunction(context).ToLocalChecked());
tpl->GetFunction(context).ToLocalChecked());
}

void MyObject::New(const Nan::FunctionCallbackInfo<v8::Value>& info) {
v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext();
if (info.IsConstructCall()) {
// Invoked as constructor: `new MyObject(...)`
double value = info[0]->IsUndefined() ? 0 : info[0]->NumberValue(context).FromJust();
double value =
info[0]->IsUndefined() ? 0 : info[0]->NumberValue(context).FromJust();
MyObject* obj = new MyObject(value);
obj->Wrap(info.This());
info.GetReturnValue().Set(info.This());
} else {
// Invoked as plain function `MyObject(...)`, turn into construct call.
const int argc = 1;
v8::Local<v8::Value> argv[argc] = { info[0] };
v8::Local<v8::Value> argv[argc] = {info[0]};
v8::Local<v8::Function> cons = Nan::New<v8::Function>(constructor);
info.GetReturnValue().Set(
cons->NewInstance(context, argc, argv).ToLocalChecked());
Expand All @@ -59,12 +58,13 @@ void MyObject::PlusOne(const Nan::FunctionCallbackInfo<v8::Value>& info) {
void MyObject::Multiply(const Nan::FunctionCallbackInfo<v8::Value>& info) {
v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext();
MyObject* obj = ObjectWrap::Unwrap<MyObject>(info.Holder());
double multiple = info[0]->IsUndefined() ? 1 : info[0]->NumberValue(context).FromJust();
double multiple =
info[0]->IsUndefined() ? 1 : info[0]->NumberValue(context).FromJust();

v8::Local<v8::Function> cons = Nan::New<v8::Function>(constructor);

const int argc = 1;
v8::Local<v8::Value> argv[argc] = { Nan::New(obj->value_ * multiple) };
v8::Local<v8::Value> argv[argc] = {Nan::New(obj->value_ * multiple)};

info.GetReturnValue().Set(
cons->NewInstance(context, argc, argv).ToLocalChecked());
Expand Down
17 changes: 10 additions & 7 deletions 6_object_wrap/napi/myobject.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,30 @@ napi_ref MyObject::constructor;
MyObject::MyObject(double value)
: value_(value), env_(nullptr), wrapper_(nullptr) {}

MyObject::~MyObject() { napi_delete_reference(env_, wrapper_); }
MyObject::~MyObject() {
napi_delete_reference(env_, wrapper_);
}

void MyObject::Destructor(napi_env env, void* nativeObject, void* /*finalize_hint*/) {
void MyObject::Destructor(napi_env env,
void* nativeObject,
void* /*finalize_hint*/) {
reinterpret_cast<MyObject*>(nativeObject)->~MyObject();
}

#define DECLARE_NAPI_METHOD(name, func) \
#define DECLARE_NAPI_METHOD(name, func) \
{ name, 0, func, 0, 0, 0, napi_default, 0 }

napi_value MyObject::Init(napi_env env, napi_value exports) {
napi_status status;
napi_property_descriptor properties[] = {
{ "value", 0, 0, GetValue, SetValue, 0, napi_default, 0 },
{"value", 0, 0, GetValue, SetValue, 0, napi_default, 0},
DECLARE_NAPI_METHOD("plusOne", PlusOne),
DECLARE_NAPI_METHOD("multiply", Multiply),
};

napi_value cons;
status =
napi_define_class(env, "MyObject", NAPI_AUTO_LENGTH, New, nullptr, 3, properties, &cons);
status = napi_define_class(
env, "MyObject", NAPI_AUTO_LENGTH, New, nullptr, 3, properties, &cons);
assert(status == napi_ok);

status = napi_create_reference(env, cons, 1, &constructor);
Expand Down Expand Up @@ -124,7 +128,6 @@ napi_value MyObject::SetValue(napi_env env, napi_callback_info info) {
status = napi_get_cb_info(env, info, &argc, &value, &jsthis, nullptr);
assert(status == napi_ok);


MyObject* obj;
status = napi_unwrap(env, jsthis, reinterpret_cast<void**>(&obj));
assert(status == napi_ok);
Expand Down
17 changes: 10 additions & 7 deletions 6_object_wrap/node-addon-api/myobject.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ Napi::FunctionReference MyObject::constructor;
Napi::Object MyObject::Init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);

Napi::Function func = DefineClass(env, "MyObject", {
InstanceMethod("plusOne", &MyObject::PlusOne),
InstanceMethod("value", &MyObject::GetValue),
InstanceMethod("multiply", &MyObject::Multiply)
});
Napi::Function func =
DefineClass(env,
"MyObject",
{InstanceMethod("plusOne", &MyObject::PlusOne),
InstanceMethod("value", &MyObject::GetValue),
InstanceMethod("multiply", &MyObject::Multiply)});

constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
Expand All @@ -18,7 +19,8 @@ Napi::Object MyObject::Init(Napi::Env env, Napi::Object exports) {
return exports;
}

MyObject::MyObject(const Napi::CallbackInfo& info) : Napi::ObjectWrap<MyObject>(info) {
MyObject::MyObject(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<MyObject>(info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);

Expand Down Expand Up @@ -52,7 +54,8 @@ Napi::Value MyObject::Multiply(const Napi::CallbackInfo& info) {
multiple = info[0].As<Napi::Number>();
}

Napi::Object obj = constructor.New({ Napi::Number::New(info.Env(), this->value_ * multiple.DoubleValue()) });
Napi::Object obj = constructor.New(
{Napi::Number::New(info.Env(), this->value_ * multiple.DoubleValue())});

return obj;
}
1 change: 0 additions & 1 deletion 6_object_wrap/node-addon-api/myobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class MyObject : public Napi::ObjectWrap<MyObject> {
Napi::Value Multiply(const Napi::CallbackInfo& info);

double value_;

};

#endif
4 changes: 3 additions & 1 deletion 7_factory_wrap/nan/addon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ void InitAll(v8::Local<v8::Object> exports, v8::Local<v8::Object> module) {
MyObject::Init();

module->Set(Nan::New("exports").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(CreateObject)->GetFunction(context).ToLocalChecked());
Nan::New<v8::FunctionTemplate>(CreateObject)
->GetFunction(context)
.ToLocalChecked());
}

NODE_MODULE(addon, InitAll)
17 changes: 9 additions & 8 deletions 7_factory_wrap/nan/myobject.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include <nan.h>
#include "myobject.h"
#include <nan.h>

using namespace v8;

MyObject::MyObject() {};
MyObject::~MyObject() {};
MyObject::MyObject(){};
MyObject::~MyObject(){};

Nan::Persistent<v8::Function> MyObject::constructor;

Expand All @@ -17,27 +17,28 @@ void MyObject::Init() {
tpl->InstanceTemplate()->SetInternalFieldCount(1);
// Prototype
tpl->PrototypeTemplate()->Set(Nan::New("plusOne").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(PlusOne));
Nan::New<v8::FunctionTemplate>(PlusOne));

constructor.Reset(tpl->GetFunction(Nan::GetCurrentContext()).ToLocalChecked());
constructor.Reset(
tpl->GetFunction(Nan::GetCurrentContext()).ToLocalChecked());
}

void MyObject::New(const Nan::FunctionCallbackInfo<v8::Value>& info) {
v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext();

MyObject* obj = new MyObject();
obj->counter_ = info[0]->IsUndefined() ? 0 : info[0]->NumberValue(context).FromJust();
obj->counter_ =
info[0]->IsUndefined() ? 0 : info[0]->NumberValue(context).FromJust();
obj->Wrap(info.This());

info.GetReturnValue().Set(info.This());
}


v8::Local<v8::Object> MyObject::NewInstance(v8::Local<v8::Value> arg) {
Nan::EscapableHandleScope scope;

const unsigned argc = 1;
v8::Local<v8::Value> argv[argc] = { arg };
v8::Local<v8::Value> argv[argc] = {arg};
v8::Local<v8::Function> cons = Nan::New<v8::Function>(constructor);
v8::Local<v8::Context> context =
v8::Isolate::GetCurrent()->GetCurrentContext();
Expand Down
Loading