Skip to content

Commit 8c72760

Browse files
committed
Implement examples for node 4.x.
1 parent 6a51626 commit 8c72760

File tree

38 files changed

+765
-0
lines changed

38 files changed

+765
-0
lines changed

1_hello_world/node_4.x/binding.gyp

Lines changed: 8 additions & 0 deletions
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

Lines changed: 24 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 14 additions & 0 deletions
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+
}
Lines changed: 51 additions & 0 deletions
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
Lines changed: 3 additions & 0 deletions
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));
Lines changed: 8 additions & 0 deletions
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+
}
Lines changed: 15 additions & 0 deletions
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

Lines changed: 32 additions & 0 deletions
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 8 additions & 0 deletions
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

Lines changed: 11 additions & 0 deletions
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

Lines changed: 37 additions & 0 deletions
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

Lines changed: 5 additions & 0 deletions
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

Lines changed: 8 additions & 0 deletions
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+
}
Lines changed: 11 additions & 0 deletions
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

Lines changed: 37 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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'
Lines changed: 8 additions & 0 deletions
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+
}
Lines changed: 11 additions & 0 deletions
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

Lines changed: 15 additions & 0 deletions
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 8 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)