Skip to content

Commit 7065a2f

Browse files
committed
add object instantiation example
1 parent 0ea5de3 commit 7065a2f

File tree

5 files changed

+117
-0
lines changed

5 files changed

+117
-0
lines changed

06_objects/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
To compile, run
2+
3+
```
4+
node-waf configure
5+
node-waf build
6+
```

06_objects/modulename.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include <node/v8.h>
2+
#include <node/node.h>
3+
4+
#include "modulename.hpp"
5+
6+
using namespace v8;
7+
8+
9+
Persistent<FunctionTemplate> MyObject::constructor;
10+
11+
void MyObject::Init(Handle<Object> target) {
12+
HandleScope scope;
13+
14+
Local<FunctionTemplate> tpl = FunctionTemplate::New(New);
15+
Local<String> name = String::NewSymbol("MyObject");
16+
17+
constructor = Persistent<FunctionTemplate>::New(tpl);
18+
// ObjectWrap uses the first internal field to store the wrapped pointer.
19+
constructor->InstanceTemplate()->SetInternalFieldCount(1);
20+
constructor->SetClassName(name);
21+
22+
// Add all prototype methods, getters and setters here.
23+
NODE_SET_PROTOTYPE_METHOD(constructor, "value", Value);
24+
25+
// This has to be last, otherwise the properties won't show up on the
26+
// object in JavaScript.
27+
target->Set(name, constructor->GetFunction());
28+
}
29+
30+
MyObject::MyObject(int val)
31+
: ObjectWrap(),
32+
value_(val) {}
33+
34+
35+
Handle<Value> MyObject::New(const Arguments& args) {
36+
HandleScope scope;
37+
38+
if (args.Length() < 1) {
39+
return ThrowException(Exception::TypeError(
40+
String::New("First argument must be a number")));
41+
}
42+
43+
// Creates a new instance object of this type and wraps it.
44+
MyObject* obj = new MyObject(args[0]->ToInteger()->Value());
45+
obj->Wrap(args.This());
46+
47+
return args.This();
48+
}
49+
50+
51+
Handle<Value> MyObject::Value(const Arguments& args) {
52+
HandleScope scope;
53+
54+
// Retrieves the pointer to the wrapped object instance.
55+
MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.This());
56+
57+
return scope.Close(Integer::New(obj->value_));
58+
}
59+
60+
61+
62+
63+
64+
65+
66+
67+
void RegisterModule(Handle<Object> target) {
68+
MyObject::Init(target);
69+
}
70+
71+
NODE_MODULE(modulename, RegisterModule);

06_objects/modulename.hpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef MODULENAME_HPP
2+
#define MODULENAME_HPP
3+
4+
#include <v8.h>
5+
#include <node.h>
6+
7+
class MyObject : public node::ObjectWrap {
8+
public:
9+
static v8::Persistent<v8::FunctionTemplate> constructor;
10+
static void Init(v8::Handle<v8::Object> target);
11+
12+
protected:
13+
MyObject(int val);
14+
15+
static v8::Handle<v8::Value> New(const v8::Arguments& args);
16+
static v8::Handle<v8::Value> Value(const v8::Arguments& args);
17+
18+
// Your own object variables here
19+
int value_;
20+
};
21+
22+
#endif

06_objects/run.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var modulename = require('./build/Release/modulename');
2+
3+
var obj = new modulename.MyObject(42);
4+
console.warn(obj);
5+
console.warn(obj.value());

06_objects/wscript

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env python
2+
3+
def set_options(ctx):
4+
ctx.tool_options('compiler_cxx')
5+
6+
def configure(ctx):
7+
ctx.check_tool('compiler_cxx')
8+
ctx.check_tool('node_addon')
9+
10+
def build(ctx):
11+
t = ctx.new_task_gen('cxx', 'shlib', 'node_addon')
12+
t.source = ['modulename.cpp']
13+
t.target = 'modulename'

0 commit comments

Comments
 (0)