Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unify 1-getting-started/6_object_wrap samples to implements the same logics. #539

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
13 changes: 10 additions & 3 deletions src/1-getting-started/6_object_wrap/nan/addon.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@ console.log( obj.plusOne() ); // 11
console.log( obj.plusOne() ); // 12
console.log( obj.plusOne() ); // 13

console.log( obj.multiply().value() ); // 13
console.log( obj.multiply(10).value() ); // 130
console.log( obj.multiply().value ); // 13
console.log( obj.multiply().getValue() ); // 13
var multiplyed = obj.multiply(10);
console.log( multiplyed.value ); // 130
console.log( multiplyed.getValue() ); // 130

var newobj = obj.multiply(-1);
console.log( newobj.value() ); // -13
console.log( newobj.getValue() ); // -13
console.log( obj === newobj ); // false

// use the setter
obj.value = 99;
console.log(obj.getValue()); // 99
27 changes: 24 additions & 3 deletions src/1-getting-started/6_object_wrap/nan/myobject.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ void MyObject::Init(v8::Local<v8::Object> exports) {
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(New);
tpl->SetClassName(Nan::New("MyObject").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);

// Prototype
Nan::SetPrototypeMethod(tpl, "value", GetValue);
// define a filed with getter and setter
Nan::SetAccessor(tpl->InstanceTemplate(),
Nan::New("value").ToLocalChecked(),
GetValueAcc,
SetValueAcc);
Nan::SetPrototypeMethod(tpl, "getValue", GetValue);
Nan::SetPrototypeMethod(tpl, "plusOne", PlusOne);
Nan::SetPrototypeMethod(tpl, "multiply", Multiply);

Expand Down Expand Up @@ -45,6 +48,24 @@ void MyObject::New(const Nan::FunctionCallbackInfo<v8::Value>& info) {
cons->NewInstance(context, argc, argv).ToLocalChecked());
}
}
/**
* Nan Property Getter
*/
void MyObject::GetValueAcc(v8::Local<v8::String> property,
const Nan::PropertyCallbackInfo<v8::Value>& info) {
MyObject* obj = ObjectWrap::Unwrap<MyObject>(info.Holder());
info.GetReturnValue().Set(Nan::New(obj->value_));
}
/**
* Nan Property Setter
*/
void MyObject::SetValueAcc(v8::Local<v8::String> property,
v8::Local<v8::Value> value,
const Nan::PropertyCallbackInfo<void>& info) {
MyObject* obj = ObjectWrap::Unwrap<MyObject>(info.Holder());
obj->value_ =
value->NumberValue(info.GetIsolate()->GetCurrentContext()).FromJust();
}

void MyObject::GetValue(const Nan::FunctionCallbackInfo<v8::Value>& info) {
MyObject* obj = ObjectWrap::Unwrap<MyObject>(info.Holder());
Expand Down
6 changes: 6 additions & 0 deletions src/1-getting-started/6_object_wrap/nan/myobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ class MyObject : public Nan::ObjectWrap {
~MyObject();

static void New(const Nan::FunctionCallbackInfo<v8::Value>& info);
static void GetValueAcc(v8::Local<v8::String> property,
const Nan::PropertyCallbackInfo<v8::Value>& info);
static void SetValueAcc(v8::Local<v8::String> property,
v8::Local<v8::Value> value,
const Nan::PropertyCallbackInfo<void>& info);

static void GetValue(const Nan::FunctionCallbackInfo<v8::Value>& info);
static void PlusOne(const Nan::FunctionCallbackInfo<v8::Value>& info);
static void Multiply(const Nan::FunctionCallbackInfo<v8::Value>& info);
Expand Down
11 changes: 9 additions & 2 deletions src/1-getting-started/6_object_wrap/napi/addon.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@ console.log( obj.plusOne() ); // 12
console.log( obj.plusOne() ); // 13

console.log( obj.multiply().value ); // 13
console.log( obj.multiply(10).value ); // 130
console.log( obj.multiply().getValue() ); // 13
var multiplyed = obj.multiply(10);
console.log( multiplyed.value ); // 130
console.log( multiplyed.getValue() ); // 130

var newobj = obj.multiply(-1);
console.log( newobj.value ); // -13
console.log( newobj.getValue() ); // -13
console.log( obj === newobj ); // false

// use the setter
obj.value = 99;
console.log(obj.getValue()); // 99
3 changes: 2 additions & 1 deletion src/1-getting-started/6_object_wrap/napi/myobject.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ 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},
DECLARE_NAPI_METHOD("getValue", GetValue),
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);
env, "MyObject", NAPI_AUTO_LENGTH, New, nullptr, 4, properties, &cons);
assert(status == napi_ok);

// We will need the constructor `cons` later during the life cycle of the
Expand Down
13 changes: 10 additions & 3 deletions src/1-getting-started/6_object_wrap/node-addon-api/addon.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@ console.log( obj.plusOne() ); // 11
console.log( obj.plusOne() ); // 12
console.log( obj.plusOne() ); // 13

console.log( obj.multiply().value() ); // 13
console.log( obj.multiply(10).value() ); // 130
console.log( obj.multiply().value ); // 13
console.log( obj.multiply().getValue() ); // 13
var multiplyed = obj.multiply(10);
console.log( multiplyed.value ); // 130
console.log( multiplyed.getValue() ); // 130

var newobj = obj.multiply(-1);
console.log( newobj.value() ); // -13
console.log( newobj.getValue() ); // -13
console.log( obj === newobj ); // false

// use the setter
obj.value = 99;
console.log(obj.getValue()); // 99
26 changes: 20 additions & 6 deletions src/1-getting-started/6_object_wrap/node-addon-api/myobject.cc
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#include "myobject.h"

Napi::Object MyObject::Init(Napi::Env env, Napi::Object exports) {
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("getValue", &MyObject::GetValue),
InstanceAccessor("value", &MyObject::GetValue, &MyObject::SetValue),
InstanceMethod("multiply", &MyObject::Multiply)});

Napi::FunctionReference* constructor = new Napi::FunctionReference();
*constructor = Napi::Persistent(func);
Expand Down Expand Up @@ -37,6 +38,19 @@ Napi::Value MyObject::GetValue(const Napi::CallbackInfo& info) {
return Napi::Number::New(info.Env(), num);
}

void MyObject::SetValue(const Napi::CallbackInfo& info,
const Napi::Value& value) {
Napi::Env env = info.Env();

if (!value.IsNumber()) {
Napi::TypeError::New(env, "Number expected").ThrowAsJavaScriptException();
return;
}

Napi::Number num = value.As<Napi::Number>();
this->value_ = num.DoubleValue();
}

Napi::Value MyObject::PlusOne(const Napi::CallbackInfo& info) {
this->value_ = this->value_ + 1;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class MyObject : public Napi::ObjectWrap<MyObject> {

private:
Napi::Value GetValue(const Napi::CallbackInfo& info);
void SetValue(const Napi::CallbackInfo& info, const Napi::Value& value);
Napi::Value PlusOne(const Napi::CallbackInfo& info);
Napi::Value Multiply(const Napi::CallbackInfo& info);

Expand Down