Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8c72760

Browse files
committedFeb 22, 2016
Implement examples for node 4.x.
1 parent 6a51626 commit 8c72760

38 files changed

+765
-0
lines changed
 

‎1_hello_world/node_4.x/binding.gyp

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"targets": [
3+
{
4+
"target_name": "hello",
5+
"sources": [ "hello.cc" ]
6+
}
7+
]
8+
}

‎1_hello_world/node_4.x/hello.cc

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// hello.cc
2+
#include <node.h>
3+
4+
namespace demo {
5+
6+
using v8::FunctionCallbackInfo;
7+
using v8::Isolate;
8+
using v8::Local;
9+
using v8::Object;
10+
using v8::String;
11+
using v8::Value;
12+
13+
void Method(const v8::FunctionCallbackInfo<Value>& args) {
14+
Isolate* isolate = args.GetIsolate();
15+
args.GetReturnValue().Set(String::NewFromUtf8(isolate, "world"));
16+
}
17+
18+
void Init(Local<Object> exports) {
19+
NODE_SET_METHOD(exports, "hello", Method);
20+
}
21+
22+
NODE_MODULE(hello, Init)
23+
24+
} // namespace demo

‎1_hello_world/node_4.x/hello.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const addon = require('bindings')('hello');
2+
3+
console.log(addon.hello()); // 'world'

‎1_hello_world/node_4.x/package.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "hello_world",
3+
"version": "0.0.0",
4+
"description": "Node.js Addons Example #1",
5+
"main": "hello.js",
6+
"private": true,
7+
"scripts": {
8+
"test": "node hello.js"
9+
},
10+
"gypfile": true,
11+
"dependencies": {
12+
"bindings": "~1.2.1"
13+
}
14+
}
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// addon.cc
2+
#include <node.h>
3+
4+
namespace demo {
5+
6+
using v8::Exception;
7+
using v8::FunctionCallbackInfo;
8+
using v8::Isolate;
9+
using v8::Local;
10+
using v8::Number;
11+
using v8::Object;
12+
using v8::String;
13+
using v8::Value;
14+
15+
// This is the implementation of the "add" method
16+
// Input arguments are passed using the
17+
// const FunctionCallbackInfo<Value>& args struct
18+
void Add(const FunctionCallbackInfo<Value>& args) {
19+
Isolate* isolate = args.GetIsolate();
20+
21+
// Check the number of arguments passed.
22+
if (args.Length() < 2) {
23+
// Throw an Error that is passed back to JavaScript
24+
isolate->ThrowException(Exception::TypeError(
25+
String::NewFromUtf8(isolate, "Wrong number of arguments")));
26+
return;
27+
}
28+
29+
// Check the argument types
30+
if (!args[0]->IsNumber() || !args[1]->IsNumber()) {
31+
isolate->ThrowException(Exception::TypeError(
32+
String::NewFromUtf8(isolate, "Wrong arguments")));
33+
return;
34+
}
35+
36+
// Perform the operation
37+
double value = args[0]->NumberValue() + args[1]->NumberValue();
38+
Local<Number> num = Number::New(isolate, value);
39+
40+
// Set the return value (using the passed in
41+
// FunctionCallbackInfo<Value>&)
42+
args.GetReturnValue().Set(num);
43+
}
44+
45+
void Init(Local<Object> exports) {
46+
NODE_SET_METHOD(exports, "add", Add);
47+
}
48+
49+
NODE_MODULE(addon, Init)
50+
51+
} // namespace demo
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const addon = require('bindings')('addon.node');
2+
3+
console.log('This should be eight:', addon.add(3, 5));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"targets": [
3+
{
4+
"target_name": "addon",
5+
"sources": [ "addon.cc" ]
6+
}
7+
]
8+
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "function_arguments",
3+
"version": "0.0.0",
4+
"description": "Node.js Addons Example #2",
5+
"main": "addon.js",
6+
"private": true,
7+
"dependencies": {
8+
"bindings": "~1.2.1",
9+
"nan": "~1.3.0"
10+
},
11+
"scripts": {
12+
"test": "node addon.js"
13+
},
14+
"gypfile": true
15+
}

‎3_callbacks/node_4.x/addon.cc

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// addon.cc
2+
#include <node.h>
3+
4+
namespace demo {
5+
6+
using v8::Function;
7+
using v8::FunctionCallbackInfo;
8+
using v8::Isolate;
9+
using v8::Local;
10+
using v8::Null;
11+
using v8::Object;
12+
using v8::String;
13+
using v8::Value;
14+
15+
void RunCallback(const FunctionCallbackInfo<Value>& args) {
16+
Isolate* isolate = args.GetIsolate();
17+
if (!args[0]->IsFunction()) {
18+
return;
19+
}
20+
Local<Function> cb = Local<Function>::Cast(args[0]);
21+
const unsigned argc = 1;
22+
Local<Value> argv[argc] = { String::NewFromUtf8(isolate, "hello, world") };
23+
cb->Call(Null(isolate), argc, argv);
24+
}
25+
26+
void Init(Local<Object> exports, Local<Object> module) {
27+
NODE_SET_METHOD(module, "exports", RunCallback);
28+
}
29+
30+
NODE_MODULE(addon, Init)
31+
32+
} // namespace demo

‎3_callbacks/node_4.x/addon.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const addon = require('bindings')('addon');
2+
3+
addon((msg) => {
4+
console.log(msg); // 'hello world'
5+
});
6+
addon(); // nothing

‎3_callbacks/node_4.x/binding.gyp

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"targets": [
3+
{
4+
"target_name": "addon",
5+
"sources": [ "addon.cc" ]
6+
}
7+
]
8+
}

‎3_callbacks/node_4.x/package.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "callbacks",
3+
"version": "0.0.0",
4+
"description": "Node.js Addons Example #3",
5+
"main": "addon.js",
6+
"private": true,
7+
"gypfile": true,
8+
"dependencies": {
9+
"bindings": "~1.2.1"
10+
}
11+
}

‎4_object_factory/node_4.x/addon.cc

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// addon.cc
2+
#include <node.h>
3+
4+
namespace demo {
5+
6+
using v8::Exception;
7+
using v8::FunctionCallbackInfo;
8+
using v8::Isolate;
9+
using v8::Local;
10+
using v8::Null;
11+
using v8::Object;
12+
using v8::String;
13+
using v8::Value;
14+
15+
void CreateObject(const FunctionCallbackInfo<Value>& args) {
16+
Isolate* isolate = args.GetIsolate();
17+
18+
if (!args[0]->IsString()) {
19+
isolate->ThrowException(Exception::TypeError(
20+
String::NewFromUtf8(isolate, "Argument must be a string"))
21+
);
22+
return;
23+
}
24+
25+
Local<Object> obj = Object::New(isolate);
26+
obj->Set(String::NewFromUtf8(isolate, "msg"), args[0]->ToString());
27+
28+
args.GetReturnValue().Set(obj);
29+
}
30+
31+
void Init(Local<Object> exports, Local<Object> module) {
32+
NODE_SET_METHOD(module, "exports", CreateObject);
33+
}
34+
35+
NODE_MODULE(addon, Init)
36+
37+
} // namespace demo

‎4_object_factory/node_4.x/addon.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const addon = require('bindings')('addon');
2+
3+
const obj1 = addon('hello');
4+
const obj2 = addon('world');
5+
console.log(obj1.msg + ', ' + obj2.msg); // 'hello, world'

‎4_object_factory/node_4.x/binding.gyp

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"targets": [
3+
{
4+
"target_name": "addon",
5+
"sources": [ "addon.cc" ]
6+
}
7+
]
8+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "object_factory",
3+
"version": "0.0.0",
4+
"description": "Node.js Addons Example #4",
5+
"main": "addon.js",
6+
"private": true,
7+
"gypfile": true,
8+
"dependencies": {
9+
"bindings": "~1.2.1"
10+
}
11+
}

‎5_function_factory/node_4.x/addon.cc

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <node.h>
2+
3+
namespace demo {
4+
5+
using v8::Function;
6+
using v8::FunctionCallbackInfo;
7+
using v8::FunctionTemplate;
8+
using v8::Isolate;
9+
using v8::Local;
10+
using v8::Object;
11+
using v8::String;
12+
using v8::Value;
13+
14+
void MyFunction(const FunctionCallbackInfo<Value>& args) {
15+
Isolate* isolate = args.GetIsolate();
16+
args.GetReturnValue().Set(String::NewFromUtf8(isolate, "hello, world"));
17+
}
18+
19+
void CreateFunction(const FunctionCallbackInfo<Value>& args) {
20+
Isolate* isolate = args.GetIsolate();
21+
22+
Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, MyFunction);
23+
Local<Function> fn = tpl->GetFunction();
24+
25+
// omit this to make it anonymous
26+
fn->SetName(String::NewFromUtf8(isolate, "theFunction"));
27+
28+
args.GetReturnValue().Set(fn);
29+
}
30+
31+
void Init(Local<Object> exports, Local<Object> module) {
32+
NODE_SET_METHOD(module, "exports", CreateFunction);
33+
}
34+
35+
NODE_MODULE(addon, Init)
36+
37+
} // namespace demo

‎5_function_factory/node_4.x/addon.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const addon = require('bindings')('addon');
2+
3+
const fn = addon();
4+
console.log(fn()); // 'hello, world'
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"targets": [
3+
{
4+
"target_name": "addon",
5+
"sources": [ "addon.cc" ]
6+
}
7+
]
8+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "function_factory",
3+
"version": "0.0.0",
4+
"description": "Node.js Addons Example #5",
5+
"main": "addon.js",
6+
"private": true,
7+
"gypfile": true,
8+
"dependencies": {
9+
"bindings": "~1.2.1"
10+
}
11+
}

‎6_object_wrap/node_4.x/addon.cc

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// addon.cc
2+
#include <node.h>
3+
#include "myobject.h"
4+
5+
namespace demo {
6+
7+
using v8::Local;
8+
9+
void InitAll(Local<Object> exports) {
10+
MyObject::Init(exports);
11+
}
12+
13+
NODE_MODULE(addon, InitAll)
14+
15+
} // namespace demo

‎6_object_wrap/node_4.x/addon.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const addon = require('bindings')('addon');
2+
3+
const obj = new addon.MyObject(10);
4+
console.log( obj.plusOne() ); // 11
5+
console.log( obj.plusOne() ); // 12
6+
console.log( obj.plusOne() ); // 13

‎6_object_wrap/node_4.x/binding.gyp

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"targets": [
3+
{
4+
"target_name": "addon",
5+
"sources": [ "addon.cc", "myobject.cc" ]
6+
}
7+
]
8+
}

‎6_object_wrap/node_4.x/myobject.cc

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include "myobject.h"
2+
3+
namespace demo {
4+
5+
using v8::Function;
6+
using v8::FunctionCallbackInfo;
7+
using v8::FunctionTemplate;
8+
using v8::Isolate;
9+
using v8::Local;
10+
using v8::Number;
11+
using v8::Object;
12+
using v8::Persistent;
13+
using v8::String;
14+
using v8::Value;
15+
16+
Persistent<Function> MyObject::constructor;
17+
18+
MyObject::MyObject(double value) : value_(value) {
19+
}
20+
21+
MyObject::~MyObject() {
22+
}
23+
24+
void MyObject::Init(Local<Object> exports) {
25+
Isolate* isolate = exports->GetIsolate();
26+
27+
// Prepare constructor template
28+
Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New);
29+
tpl->SetClassName(String::NewFromUtf8(isolate, "MyObject"));
30+
tpl->InstanceTemplate()->SetInternalFieldCount(1);
31+
32+
// Prototype
33+
NODE_SET_PROTOTYPE_METHOD(tpl, "plusOne", PlusOne);
34+
35+
constructor.Reset(isolate, tpl->GetFunction());
36+
exports->Set(String::NewFromUtf8(isolate, "MyObject"),
37+
tpl->GetFunction());
38+
}
39+
40+
void MyObject::New(const FunctionCallbackInfo<Value>& args) {
41+
Isolate* isolate = args.GetIsolate();
42+
43+
if (args.IsConstructCall()) {
44+
// Invoked as constructor: `new MyObject(...)`
45+
double value = args[0]->IsUndefined() ? 0 : args[0]->NumberValue();
46+
MyObject* obj = new MyObject(value);
47+
obj->Wrap(args.This());
48+
return;
49+
} else {
50+
// Invoked as plain function `MyObject(...)`, turn into construct call.
51+
const int argc = 1;
52+
Local<Value> argv[argc] = { args[0] };
53+
Local<Function> cons = Local<Function>::New(isolate, constructor);
54+
args.GetReturnValue().Set(cons->NewInstance(argc, argv));
55+
}
56+
}
57+
58+
void MyObject::PlusOne(const FunctionCallbackInfo<Value>& args) {
59+
Isolate* isolate = args.GetIsolate();
60+
61+
MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.Holder());
62+
obj->value_ += 1;
63+
64+
args.GetReturnValue().Set(Number::New(isolate, obj->value_));
65+
}
66+
67+
} // namespace demo

‎6_object_wrap/node_4.x/myobject.h

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef MYOBJECT_H
2+
#define MYOBJECT_H
3+
4+
#include <node.h>
5+
#include <node_object_wrap.h>
6+
7+
namespace demo {
8+
9+
using v8::Function;
10+
using v8::FunctionCallbackInfo;
11+
using v8::Local;
12+
using v8::Object;
13+
using v8::Persistent;
14+
using v8::Value;
15+
16+
class MyObject : public node::ObjectWrap {
17+
public:
18+
static void Init(Local<Object> exports);
19+
20+
private:
21+
explicit MyObject(double value = 0);
22+
~MyObject();
23+
24+
static void New(const FunctionCallbackInfo<Value>& args);
25+
static void PlusOne(const FunctionCallbackInfo<Value>& args);
26+
static Persistent<Function> constructor;
27+
double value_;
28+
};
29+
30+
} // namespace demo
31+
32+
#endif

‎6_object_wrap/node_4.x/package.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "object_wrap",
3+
"version": "0.0.0",
4+
"description": "Node.js Addons Example #6",
5+
"main": "addon.js",
6+
"private": true,
7+
"gypfile": true,
8+
"dependencies": {
9+
"bindings": "~1.2.1"
10+
}
11+
}

‎7_factory_wrap/node_4.x/addon.cc

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <node.h>
2+
#include "myobject.h"
3+
4+
namespace demo {
5+
6+
using v8::FunctionCallbackInfo;
7+
using v8::Isolate;
8+
using v8::Local;
9+
using v8::Object;
10+
using v8::String;
11+
using v8::Value;
12+
13+
void CreateObject(const FunctionCallbackInfo<Value>& args) {
14+
MyObject::NewInstance(args);
15+
}
16+
17+
void InitAll(Local<Object> exports, Local<Object> module) {
18+
MyObject::Init(exports->GetIsolate());
19+
20+
NODE_SET_METHOD(module, "exports", CreateObject);
21+
}
22+
23+
NODE_MODULE(addon, InitAll)
24+
25+
} // namespace demo

‎7_factory_wrap/node_4.x/addon.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const createObject = require('bindings')('addon');
2+
3+
const obj = createObject(10);
4+
console.log( obj.plusOne() ); // 11
5+
console.log( obj.plusOne() ); // 12
6+
console.log( obj.plusOne() ); // 13
7+
8+
const obj2 = createObject(20);
9+
console.log( obj2.plusOne() ); // 21
10+
console.log( obj2.plusOne() ); // 22
11+
console.log( obj2.plusOne() ); // 23

‎7_factory_wrap/node_4.x/binding.gyp

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"targets": [
3+
{
4+
"target_name": "addon",
5+
"sources": [ "addon.cc", "myobject.cc" ]
6+
}
7+
]
8+
}

‎7_factory_wrap/node_4.x/myobject.cc

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include <node.h>
2+
#include "myobject.h"
3+
4+
namespace demo {
5+
6+
using v8::Function;
7+
using v8::FunctionCallbackInfo;
8+
using v8::FunctionTemplate;
9+
using v8::Isolate;
10+
using v8::Local;
11+
using v8::Number;
12+
using v8::Object;
13+
using v8::Persistent;
14+
using v8::String;
15+
using v8::Value;
16+
17+
Persistent<Function> MyObject::constructor;
18+
19+
MyObject::MyObject(double value) : value_(value) {
20+
}
21+
22+
MyObject::~MyObject() {
23+
}
24+
25+
void MyObject::Init(Isolate* isolate) {
26+
// Prepare constructor template
27+
Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New);
28+
tpl->SetClassName(String::NewFromUtf8(isolate, "MyObject"));
29+
tpl->InstanceTemplate()->SetInternalFieldCount(1);
30+
31+
// Prototype
32+
NODE_SET_PROTOTYPE_METHOD(tpl, "plusOne", PlusOne);
33+
34+
constructor.Reset(isolate, tpl->GetFunction());
35+
}
36+
37+
void MyObject::New(const FunctionCallbackInfo<Value>& args) {
38+
Isolate* isolate = args.GetIsolate();
39+
40+
if (args.IsConstructCall()) {
41+
// Invoked as constructor: `new MyObject(...)`
42+
double value = args[0]->IsUndefined() ? 0 : args[0]->NumberValue();
43+
MyObject* obj = new MyObject(value);
44+
obj->Wrap(args.This());
45+
return;
46+
} else {
47+
// Invoked as plain function `MyObject(...)`, turn into construct call.
48+
const int argc = 1;
49+
Local<Value> argv[argc] = { args[0] };
50+
Local<Function> cons = Local<Function>::New(isolate, constructor);
51+
args.GetReturnValue().Set(cons->NewInstance(argc, argv));
52+
}
53+
}
54+
55+
void MyObject::NewInstance(const FunctionCallbackInfo<Value>& args) {
56+
Isolate* isolate = args.GetIsolate();
57+
58+
const unsigned argc = 1;
59+
Local<Value> argv[argc] = { args[0] };
60+
Local<Function> cons = Local<Function>::New(isolate, constructor);
61+
Local<Object> instance = cons->NewInstance(argc, argv);
62+
63+
args.GetReturnValue().Set(instance);
64+
}
65+
66+
void MyObject::PlusOne(const FunctionCallbackInfo<Value>& args) {
67+
Isolate* isolate = args.GetIsolate();
68+
69+
MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.Holder());
70+
obj->value_ += 1;
71+
72+
args.GetReturnValue().Set(Number::New(isolate, obj->value_));
73+
}
74+
75+
} // namespace demo

‎7_factory_wrap/node_4.x/myobject.h

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef MYOBJECT_H
2+
#define MYOBJECT_H
3+
4+
#include <node.h>
5+
#include <node_object_wrap.h>
6+
7+
namespace demo {
8+
9+
using v8::Function;
10+
using v8::FunctionCallbackInfo;
11+
using v8::Isolate;
12+
using v8::Persistent;
13+
using v8::Value;
14+
15+
class MyObject : public node::ObjectWrap {
16+
public:
17+
static void Init(Isolate* isolate);
18+
static void NewInstance(const FunctionCallbackInfo<Value>& args);
19+
20+
private:
21+
explicit MyObject(double value = 0);
22+
~MyObject();
23+
24+
static void New(const FunctionCallbackInfo<Value>& args);
25+
static void PlusOne(const FunctionCallbackInfo<Value>& args);
26+
static Persistent<Function> constructor;
27+
double value_;
28+
};
29+
30+
} // namespace demo
31+
32+
#endif

‎7_factory_wrap/node_4.x/package.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "factory_wrap",
3+
"version": "0.0.0",
4+
"description": "Node.js Addons Example #7",
5+
"main": "addon.js",
6+
"private": true,
7+
"gypfile": true,
8+
"dependencies": {
9+
"bindings": "~1.2.1"
10+
}
11+
}

‎8_passing_wrapped/node_4.x/addon.cc

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <node.h>
2+
#include <node_object_wrap.h>
3+
#include "myobject.h"
4+
5+
namespace demo {
6+
7+
using v8::FunctionCallbackInfo;
8+
using v8::Isolate;
9+
using v8::Local;
10+
using v8::Number;
11+
using v8::Object;
12+
using v8::String;
13+
using v8::Value;
14+
15+
void CreateObject(const FunctionCallbackInfo<Value>& args) {
16+
MyObject::NewInstance(args);
17+
}
18+
19+
void Add(const FunctionCallbackInfo<Value>& args) {
20+
Isolate* isolate = args.GetIsolate();
21+
22+
MyObject* obj1 = node::ObjectWrap::Unwrap<MyObject>(
23+
args[0]->ToObject());
24+
MyObject* obj2 = node::ObjectWrap::Unwrap<MyObject>(
25+
args[1]->ToObject());
26+
27+
double sum = obj1->value() + obj2->value();
28+
args.GetReturnValue().Set(Number::New(isolate, sum));
29+
}
30+
31+
void InitAll(Local<Object> exports) {
32+
MyObject::Init(exports->GetIsolate());
33+
34+
NODE_SET_METHOD(exports, "createObject", CreateObject);
35+
NODE_SET_METHOD(exports, "add", Add);
36+
}
37+
38+
NODE_MODULE(addon, InitAll)
39+
40+
} // namespace demo

‎8_passing_wrapped/node_4.x/addon.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const addon = require('bindings')('addon');
2+
3+
const obj1 = addon.createObject(10);
4+
const obj2 = addon.createObject(20);
5+
const result = addon.add(obj1, obj2);
6+
7+
console.log(result); // 30
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"targets": [
3+
{
4+
"target_name": "addon",
5+
"sources": [ "addon.cc", "myobject.cc" ]
6+
}
7+
]
8+
}
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include <node.h>
2+
#include "myobject.h"
3+
4+
namespace demo {
5+
6+
using v8::Function;
7+
using v8::FunctionCallbackInfo;
8+
using v8::FunctionTemplate;
9+
using v8::Isolate;
10+
using v8::Local;
11+
using v8::Object;
12+
using v8::Persistent;
13+
using v8::String;
14+
using v8::Value;
15+
16+
Persistent<Function> MyObject::constructor;
17+
18+
MyObject::MyObject(double value) : value_(value) {
19+
}
20+
21+
MyObject::~MyObject() {
22+
}
23+
24+
void MyObject::Init(Isolate* isolate) {
25+
// Prepare constructor template
26+
Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New);
27+
tpl->SetClassName(String::NewFromUtf8(isolate, "MyObject"));
28+
tpl->InstanceTemplate()->SetInternalFieldCount(1);
29+
30+
constructor.Reset(isolate, tpl->GetFunction());
31+
}
32+
33+
void MyObject::New(const FunctionCallbackInfo<Value>& args) {
34+
Isolate* isolate = args.GetIsolate();
35+
36+
if (args.IsConstructCall()) {
37+
// Invoked as constructor: `new MyObject(...)`
38+
double value = args[0]->IsUndefined() ? 0 : args[0]->NumberValue();
39+
MyObject* obj = new MyObject(value);
40+
obj->Wrap(args.This());
41+
return;
42+
} else {
43+
// Invoked as plain function `MyObject(...)`, turn into construct call.
44+
const int argc = 1;
45+
Local<Value> argv[argc] = { args[0] };
46+
Local<Function> cons = Local<Function>::New(isolate, constructor);
47+
args.GetReturnValue().Set(cons->NewInstance(argc, argv));
48+
}
49+
}
50+
51+
void MyObject::NewInstance(const FunctionCallbackInfo<Value>& args) {
52+
Isolate* isolate = args.GetIsolate();
53+
54+
const unsigned argc = 1;
55+
Local<Value> argv[argc] = { args[0] };
56+
Local<Function> cons = Local<Function>::New(isolate, constructor);
57+
Local<Object> instance = cons->NewInstance(argc, argv);
58+
59+
args.GetReturnValue().Set(instance);
60+
}
61+
62+
} // namespace demo

‎8_passing_wrapped/node_4.x/myobject.h

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef MYOBJECT_H
2+
#define MYOBJECT_H
3+
4+
#include <node.h>
5+
#include <node_object_wrap.h>
6+
7+
namespace demo {
8+
9+
using v8::Function;
10+
using v8::FunctionCallbackInfo;
11+
using v8::Isolate;
12+
using v8::Persistent;
13+
using v8::Value;
14+
15+
class MyObject : public node::ObjectWrap {
16+
public:
17+
static void Init(v8::Isolate* Isolate);
18+
static void NewInstance(const v8::FunctionCallbackInfo<v8::Value>& args);
19+
inline double value() const { return value_; }
20+
21+
private:
22+
explicit MyObject(double value = 0);
23+
~MyObject();
24+
25+
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
26+
static v8::Persistent<v8::Function> constructor;
27+
double value_;
28+
};
29+
30+
} // namespace demo
31+
32+
#endif
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "passing_wrapped",
3+
"version": "0.0.0",
4+
"description": "Node.js Addons Example #8",
5+
"main": "addon.js",
6+
"private": true,
7+
"gypfile": true,
8+
"dependencies": {
9+
"bindings": "~1.2.1"
10+
}
11+
}

0 commit comments

Comments
 (0)
Please sign in to comment.