Skip to content

Commit a757c3e

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

File tree

39 files changed

+637
-0
lines changed

39 files changed

+637
-0
lines changed

1_hello_world/a.js

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

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: 17 additions & 0 deletions
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

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

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

0 commit comments

Comments
 (0)