-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaddon.cc
57 lines (47 loc) · 1.15 KB
/
addon.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// hello.cc
#include <node.h>
#include <v8.h>
using namespace v8;
/**
* function Hello() {
* var hello = 'hello ';
* var arg = arguments[0];
* return hello + arg;
* }
*/
Handle<Value> Hello(const Arguments& args) {
HandleScope scope;
Local<String> hello = String::New("hello ");
Local<String> arg = args[0]->ToString();
return scope.Close(String::Concat(hello, arg));
}
/**
* function Square() {
* var a = arguments[0]
* var sq = a * a;
* return sq;
* }
*/
Handle<Value> Square(const Arguments& args) {
HandleScope scope;
int a = args[0]->Int32Value();
int sq = a * a;
return scope.Close(Integer::New(sq));
}
/**
* function Init(exports) {
* var hello = Hello;
* exports.hello = hello;
* }
*/
void Init(Handle<Object> exports) {
Local<Function> hello = FunctionTemplate::New(Hello)->GetFunction();
exports->Set(String::NewSymbol("hello"), hello);
Local<Function> square = FunctionTemplate::New(Square)->GetFunction();
exports->Set(String::NewSymbol("square"), square);
}
/**
* NODE_MODULE is a macro defined at node.h
* first argument is module name (the file created at compile time)
*/
NODE_MODULE(addon, Init)