Skip to content

Commit 4d6f2ec

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

33 files changed

+513
-0
lines changed

1_hello_world/a.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
let a = () => 'nom';
3+
4+
let b = function () {};
5+
6+
// a.prototype.x = () => {};
7+
b.prototype.x = () => {};
8+
a.bind(a)('hello');

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

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

1_hello_world/node_4.x/hello.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var 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+
}
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <node.h>
2+
3+
using namespace v8;
4+
5+
void Add(const FunctionCallbackInfo<Value>& args) {
6+
Isolate* isolate = args.GetIsolate();
7+
8+
if (args.Length() < 2) {
9+
isolate->ThrowException(Exception::TypeError(
10+
String::NewFromUtf8(isolate, "Wrong number of arguments")));
11+
return;
12+
}
13+
14+
if (!args[0]->IsNumber() || !args[1]->IsNumber()) {
15+
isolate->ThrowException(Exception::TypeError(
16+
String::NewFromUtf8(isolate, "Wrong arguments")));
17+
return;
18+
}
19+
20+
double value = args[0]->NumberValue() + args[1]->NumberValue();
21+
Local<Number> num = Number::New(isolate, value);
22+
23+
args.GetReturnValue().Set(num);
24+
}
25+
26+
void Init(Handle<Object> exports) {
27+
NODE_SET_METHOD(exports, "add", Add);
28+
}
29+
30+
NODE_MODULE(addon, Init)
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var 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

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

3_callbacks/node_4.x/addon.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var addon = require('bindings')('addon');
2+
3+
addon(function(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

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

4_object_factory/node_4.x/addon.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var addon = require('bindings')('addon');
2+
3+
var obj1 = addon('hello');
4+
var 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

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

5_function_factory/node_4.x/addon.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
var addon = require('bindings')('addon');
2+
3+
var 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

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

6_object_wrap/node_4.x/addon.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var addon = require('bindings')('addon');
2+
3+
var 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

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

0 commit comments

Comments
 (0)