Skip to content

Commit fd4bffb

Browse files
committed
initial import
0 parents  commit fd4bffb

39 files changed

+549
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build/

1_hello_world/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/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+
Handle<Value> Method(const Arguments& args) {
7+
HandleScope scope;
8+
return scope.Close(String::New("world"));
9+
}
10+
11+
void init(Handle<Object> target) {
12+
target->Get(String::NewSymbol("exports"))->ToObject()->Set(
13+
String::NewSymbol("hello"),
14+
FunctionTemplate::New(Method)->GetFunction());
15+
}
16+
17+
NODE_MODULE_INIT(hello, init)

1_hello_world/hello.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var addon = require('./build/Release/hello');
2+
3+
console.log(addon.hello()); // 'world'

1_hello_world/package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
"gypfile": true
8+
}

2_function_arguments/addon.cc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#define BUILDING_NODE_EXTENSION
2+
#include <node.h>
3+
4+
using namespace v8;
5+
6+
Handle<Value> Add(const Arguments& args) {
7+
HandleScope scope;
8+
9+
if (args.Length() < 2) {
10+
ThrowException(Exception::TypeError(String::New("Wrong number of arguments")));
11+
return scope.Close(Undefined());
12+
}
13+
14+
if (!args[0]->IsNumber() || !args[1]->IsNumber()) {
15+
ThrowException(Exception::TypeError(String::New("Wrong arguments")));
16+
return scope.Close(Undefined());
17+
}
18+
19+
Local<Number> num = Number::New(args[0]->NumberValue() +
20+
args[1]->NumberValue());
21+
return scope.Close(num);
22+
}
23+
24+
void Init(Handle<Object> target) {
25+
target->Get(String::NewSymbol("exports"))->ToObject()->Set(
26+
String::NewSymbol("add"),
27+
FunctionTemplate::New(Add)->GetFunction());
28+
}
29+
30+
NODE_MODULE_INIT(addon, Init)

2_function_arguments/addon.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var addon = require('./build/Release/addon');
2+
3+
console.log( 'This should be eight:', addon.add(3,5) );

2_function_arguments/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+
}

2_function_arguments/package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
"gypfile": true
8+
}

3_callbacks/addon.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#define BUILDING_NODE_EXTENSION
2+
#include <node.h>
3+
4+
using namespace v8;
5+
6+
Handle<Value> RunCallback(const Arguments& args) {
7+
HandleScope scope;
8+
9+
Local<Function> cb = Local<Function>::Cast(args[0]);
10+
const unsigned argc = 1;
11+
Local<Value> argv[argc] = { Local<Value>::New(String::New("hello world")) };
12+
cb->Call(Context::GetCurrent()->Global(), argc, argv);
13+
14+
return scope.Close(Undefined());
15+
}
16+
17+
void Init(Handle<Object> target) {
18+
target->Set(String::NewSymbol("exports"),
19+
FunctionTemplate::New(RunCallback)->GetFunction());
20+
}
21+
22+
NODE_MODULE_INIT(addon, Init)

0 commit comments

Comments
 (0)