2012-02-27 20:06:31 +01:00
|
|
|
# Addons
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
Addons are dynamically linked shared objects. They can provide glue to C and
|
|
|
|
C++ libraries. The API (at the moment) is rather complex, involving
|
|
|
|
knowledge of several libraries:
|
|
|
|
|
|
|
|
- V8 JavaScript, a C++ library. Used for interfacing with JavaScript:
|
|
|
|
creating objects, calling functions, etc. Documented mostly in the
|
2013-01-25 23:27:46 +01:00
|
|
|
`v8.h` header file (`deps/v8/include/v8.h` in the Node source
|
|
|
|
tree), which is also available
|
|
|
|
[online](http://izs.me/v8-docs/main.html).
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2013-01-25 23:27:46 +01:00
|
|
|
- [libuv](https://github.com/joyent/libuv), C event loop library.
|
|
|
|
Anytime one needs to wait for a file descriptor to become readable,
|
2013-03-27 11:09:37 +01:00
|
|
|
wait for a timer, or wait for a signal to be received one will need
|
|
|
|
to interface with libuv. That is, if you perform any I/O, libuv will
|
2013-01-25 23:27:46 +01:00
|
|
|
need to be used.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
- Internal Node libraries. Most importantly is the `node::ObjectWrap`
|
|
|
|
class which you will likely want to derive from.
|
|
|
|
|
|
|
|
- Others. Look in `deps/` for what else is available.
|
|
|
|
|
2013-01-25 23:27:46 +01:00
|
|
|
Node statically compiles all its dependencies into the executable.
|
|
|
|
When compiling your module, you don't need to worry about linking to
|
|
|
|
any of these libraries.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2013-01-25 23:27:46 +01:00
|
|
|
All of the following examples are available for
|
|
|
|
[download](https://github.com/rvagg/node-addon-examples) and may be
|
|
|
|
used as a starting-point for your own Addon.
|
2012-01-19 16:18:15 +01:00
|
|
|
|
2012-02-27 20:06:31 +01:00
|
|
|
## Hello world
|
2012-01-19 16:18:15 +01:00
|
|
|
|
2011-11-09 03:13:59 +01:00
|
|
|
To get started let's make a small Addon which is the C++ equivalent of
|
2012-05-26 12:25:44 +02:00
|
|
|
the following JavaScript code:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2013-01-21 04:20:43 +01:00
|
|
|
module.exports.hello = function() { return 'world'; };
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-01-19 16:18:15 +01:00
|
|
|
First we create a file `hello.cc`:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
// hello.cc
|
2011-10-30 17:15:10 +01:00
|
|
|
#include <node.h>
|
2013-03-19 11:28:17 +01:00
|
|
|
|
2010-10-28 14:18:16 +02:00
|
|
|
using namespace v8;
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
void Method(const FunctionCallbackInfo<Value>& args) {
|
2013-06-16 09:30:50 +02:00
|
|
|
Isolate* isolate = Isolate::GetCurrent();
|
2013-03-19 11:28:17 +01:00
|
|
|
HandleScope scope(isolate);
|
2014-01-18 23:49:18 +01:00
|
|
|
args.GetReturnValue().Set(String::NewFromUtf8(isolate, "world"));
|
2011-10-11 21:14:00 +02:00
|
|
|
}
|
|
|
|
|
2013-01-21 04:20:43 +01:00
|
|
|
void init(Handle<Object> exports) {
|
2014-01-18 23:49:18 +01:00
|
|
|
NODE_SET_METHOD(exports, "hello", Method);
|
2010-10-28 14:18:16 +02:00
|
|
|
}
|
2013-01-21 04:20:43 +01:00
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
NODE_MODULE(addon, init)
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2011-11-09 03:13:59 +01:00
|
|
|
Note that all Node addons must export an initialization function:
|
|
|
|
|
2013-01-21 04:20:43 +01:00
|
|
|
void Initialize (Handle<Object> exports);
|
2011-11-09 03:13:59 +01:00
|
|
|
NODE_MODULE(module_name, Initialize)
|
|
|
|
|
2013-06-13 07:05:33 +02:00
|
|
|
There is no semi-colon after `NODE_MODULE` as it's not a function (see
|
|
|
|
`node.h`).
|
2011-11-09 03:13:59 +01:00
|
|
|
|
2011-11-28 20:44:23 +01:00
|
|
|
The `module_name` needs to match the filename of the final binary (minus the
|
|
|
|
.node suffix).
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
The source code needs to be built into `addon.node`, the binary Addon. To
|
2012-04-13 02:24:35 +02:00
|
|
|
do this we create a file called `binding.gyp` which describes the configuration
|
|
|
|
to build your module in a JSON-like format. This file gets compiled by
|
|
|
|
[node-gyp](https://github.com/TooTallNate/node-gyp).
|
|
|
|
|
|
|
|
{
|
|
|
|
"targets": [
|
|
|
|
{
|
2014-01-18 23:49:18 +01:00
|
|
|
"target_name": "addon",
|
2012-04-13 02:24:35 +02:00
|
|
|
"sources": [ "hello.cc" ]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-04-13 02:24:35 +02:00
|
|
|
The next step is to generate the appropriate project build files for the
|
|
|
|
current platform. Use `node-gyp configure` for that.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-04-13 02:24:35 +02:00
|
|
|
Now you will have either a `Makefile` (on Unix platforms) or a `vcxproj` file
|
|
|
|
(on Windows) in the `build/` directory. Next invoke the `node-gyp build`
|
|
|
|
command.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-04-13 02:24:35 +02:00
|
|
|
Now you have your compiled `.node` bindings file! The compiled bindings end up
|
|
|
|
in `build/Release/`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2013-06-13 07:05:33 +02:00
|
|
|
You can now use the binary addon in a Node project `hello.js` by pointing
|
|
|
|
`require` to the recently built `hello.node` module:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
// hello.js
|
|
|
|
var addon = require('./build/Release/addon');
|
2011-11-09 03:13:59 +01:00
|
|
|
|
|
|
|
console.log(addon.hello()); // 'world'
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-01-19 16:18:15 +01:00
|
|
|
Please see patterns below for further information or
|
2012-04-13 02:24:35 +02:00
|
|
|
<https://github.com/arturadib/node-qt> for an example in production.
|
2012-01-19 16:18:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
## Addon patterns
|
|
|
|
|
|
|
|
Below are some addon patterns to help you get started. Consult the online
|
|
|
|
[v8 reference](http://izs.me/v8-docs/main.html) for help with the various v8
|
|
|
|
calls, and v8's [Embedder's Guide](http://code.google.com/apis/v8/embed.html)
|
|
|
|
for an explanation of several concepts used such as handles, scopes,
|
|
|
|
function templates, etc.
|
|
|
|
|
2012-04-13 02:24:35 +02:00
|
|
|
In order to use these examples you need to compile them using `node-gyp`.
|
|
|
|
Create the following `binding.gyp` file:
|
2012-01-19 16:18:15 +01:00
|
|
|
|
2012-04-13 02:24:35 +02:00
|
|
|
{
|
|
|
|
"targets": [
|
|
|
|
{
|
|
|
|
"target_name": "addon",
|
|
|
|
"sources": [ "addon.cc" ]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2012-01-19 16:18:15 +01:00
|
|
|
|
2013-06-13 07:05:33 +02:00
|
|
|
In cases where there is more than one `.cc` file, simply add the file name to
|
|
|
|
the `sources` array, e.g.:
|
2012-01-19 16:18:15 +01:00
|
|
|
|
2012-04-13 02:24:35 +02:00
|
|
|
"sources": ["addon.cc", "myexample.cc"]
|
2012-01-19 16:18:15 +01:00
|
|
|
|
2012-04-13 02:24:35 +02:00
|
|
|
Now that you have your `binding.gyp` ready, you can configure and build the
|
|
|
|
addon:
|
2012-01-19 16:18:15 +01:00
|
|
|
|
2012-04-13 02:24:35 +02:00
|
|
|
$ node-gyp configure build
|
2012-01-19 16:18:15 +01:00
|
|
|
|
|
|
|
|
2012-02-27 20:06:31 +01:00
|
|
|
### Function arguments
|
2012-01-19 16:18:15 +01:00
|
|
|
|
|
|
|
The following pattern illustrates how to read arguments from JavaScript
|
|
|
|
function calls and return a result. This is the main and only needed source
|
|
|
|
`addon.cc`:
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
// addon.cc
|
2012-01-19 16:18:15 +01:00
|
|
|
#include <node.h>
|
|
|
|
|
|
|
|
using namespace v8;
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
void Add(const FunctionCallbackInfo<Value>& args) {
|
2013-06-16 09:30:50 +02:00
|
|
|
Isolate* isolate = Isolate::GetCurrent();
|
2013-03-19 11:28:17 +01:00
|
|
|
HandleScope scope(isolate);
|
2012-01-19 16:18:15 +01:00
|
|
|
|
|
|
|
if (args.Length() < 2) {
|
2014-01-18 23:49:18 +01:00
|
|
|
isolate->ThrowException(Exception::TypeError(
|
|
|
|
String::NewFromUtf8(isolate, "Wrong number of arguments")));
|
|
|
|
return;
|
2012-01-19 16:18:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!args[0]->IsNumber() || !args[1]->IsNumber()) {
|
2014-01-18 23:49:18 +01:00
|
|
|
isolate->ThrowException(Exception::TypeError(
|
|
|
|
String::NewFromUtf8(isolate, "Wrong arguments")));
|
|
|
|
return;
|
2012-01-19 16:18:15 +01:00
|
|
|
}
|
|
|
|
|
2014-03-13 18:53:48 +01:00
|
|
|
double value = args[0]->NumberValue() + args[1]->NumberValue();
|
|
|
|
Local<Number> num = Number::New(isolate, value);
|
2014-01-18 23:49:18 +01:00
|
|
|
|
|
|
|
args.GetReturnValue().Set(num);
|
2012-01-19 16:18:15 +01:00
|
|
|
}
|
|
|
|
|
2013-01-21 04:20:43 +01:00
|
|
|
void Init(Handle<Object> exports) {
|
2014-01-18 23:49:18 +01:00
|
|
|
NODE_SET_METHOD(exports, "add", Add);
|
2012-01-19 16:18:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
NODE_MODULE(addon, Init)
|
|
|
|
|
|
|
|
You can test it with the following JavaScript snippet:
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
// test.js
|
2012-01-19 16:18:15 +01:00
|
|
|
var addon = require('./build/Release/addon');
|
|
|
|
|
|
|
|
console.log( 'This should be eight:', addon.add(3,5) );
|
|
|
|
|
|
|
|
|
2012-02-27 20:06:31 +01:00
|
|
|
### Callbacks
|
2012-01-19 16:18:15 +01:00
|
|
|
|
|
|
|
You can pass JavaScript functions to a C++ function and execute them from
|
|
|
|
there. Here's `addon.cc`:
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
// addon.cc
|
2012-01-19 16:18:15 +01:00
|
|
|
#include <node.h>
|
|
|
|
|
|
|
|
using namespace v8;
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
void RunCallback(const FunctionCallbackInfo<Value>& args) {
|
2013-06-16 09:30:50 +02:00
|
|
|
Isolate* isolate = Isolate::GetCurrent();
|
2013-03-19 11:28:17 +01:00
|
|
|
HandleScope scope(isolate);
|
2012-01-19 16:18:15 +01:00
|
|
|
|
|
|
|
Local<Function> cb = Local<Function>::Cast(args[0]);
|
|
|
|
const unsigned argc = 1;
|
2014-01-18 23:49:18 +01:00
|
|
|
Local<Value> argv[argc] = { String::NewFromUtf8(isolate, "hello world") };
|
2014-03-13 18:53:48 +01:00
|
|
|
cb->Call(isolate->GetCurrentContext()->Global(), argc, argv);
|
2012-01-19 16:18:15 +01:00
|
|
|
}
|
|
|
|
|
2013-01-21 04:20:43 +01:00
|
|
|
void Init(Handle<Object> exports, Handle<Object> module) {
|
2014-01-18 23:49:18 +01:00
|
|
|
NODE_SET_METHOD(module, "exports", RunCallback);
|
2012-01-19 16:18:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
NODE_MODULE(addon, Init)
|
|
|
|
|
2013-01-21 04:20:43 +01:00
|
|
|
Note that this example uses a two-argument form of `Init()` that receives
|
|
|
|
the full `module` object as the second argument. This allows the addon
|
|
|
|
to completely overwrite `exports` with a single function instead of
|
|
|
|
adding the function as a property of `exports`.
|
|
|
|
|
2012-01-19 16:18:15 +01:00
|
|
|
To test it run the following JavaScript snippet:
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
// test.js
|
2012-01-19 16:18:15 +01:00
|
|
|
var addon = require('./build/Release/addon');
|
|
|
|
|
2013-01-21 04:20:43 +01:00
|
|
|
addon(function(msg){
|
2012-01-19 16:18:15 +01:00
|
|
|
console.log(msg); // 'hello world'
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2012-02-27 20:06:31 +01:00
|
|
|
### Object factory
|
2012-01-19 16:18:15 +01:00
|
|
|
|
|
|
|
You can create and return new objects from within a C++ function with this
|
|
|
|
`addon.cc` pattern, which returns an object with property `msg` that echoes
|
|
|
|
the string passed to `createObject()`:
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
// addon.cc
|
2012-01-19 16:18:15 +01:00
|
|
|
#include <node.h>
|
|
|
|
|
|
|
|
using namespace v8;
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
void CreateObject(const FunctionCallbackInfo<Value>& args) {
|
2013-06-16 09:30:50 +02:00
|
|
|
Isolate* isolate = Isolate::GetCurrent();
|
2013-03-19 11:28:17 +01:00
|
|
|
HandleScope scope(isolate);
|
2012-01-19 16:18:15 +01:00
|
|
|
|
2014-03-13 18:53:48 +01:00
|
|
|
Local<Object> obj = Object::New(isolate);
|
2014-01-18 23:49:18 +01:00
|
|
|
obj->Set(String::NewFromUtf8(isolate, "msg"), args[0]->ToString());
|
2012-01-19 16:18:15 +01:00
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
args.GetReturnValue().Set(obj);
|
2012-01-19 16:18:15 +01:00
|
|
|
}
|
|
|
|
|
2013-01-21 04:20:43 +01:00
|
|
|
void Init(Handle<Object> exports, Handle<Object> module) {
|
2014-01-18 23:49:18 +01:00
|
|
|
NODE_SET_METHOD(module, "exports", CreateObject);
|
2012-01-19 16:18:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
NODE_MODULE(addon, Init)
|
|
|
|
|
|
|
|
To test it in JavaScript:
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
// test.js
|
2012-01-19 16:18:15 +01:00
|
|
|
var addon = require('./build/Release/addon');
|
|
|
|
|
2013-01-21 04:20:43 +01:00
|
|
|
var obj1 = addon('hello');
|
|
|
|
var obj2 = addon('world');
|
2012-01-19 16:18:15 +01:00
|
|
|
console.log(obj1.msg+' '+obj2.msg); // 'hello world'
|
|
|
|
|
|
|
|
|
2012-02-27 20:06:31 +01:00
|
|
|
### Function factory
|
2012-01-19 16:18:15 +01:00
|
|
|
|
|
|
|
This pattern illustrates how to create and return a JavaScript function that
|
|
|
|
wraps a C++ function:
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
// addon.cc
|
2012-01-19 16:18:15 +01:00
|
|
|
#include <node.h>
|
|
|
|
|
|
|
|
using namespace v8;
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
void MyFunction(const FunctionCallbackInfo<Value>& args) {
|
2013-06-16 09:30:50 +02:00
|
|
|
Isolate* isolate = Isolate::GetCurrent();
|
2013-03-19 11:28:17 +01:00
|
|
|
HandleScope scope(isolate);
|
2014-01-18 23:49:18 +01:00
|
|
|
args.GetReturnValue().Set(String::NewFromUtf8(isolate, "hello world"));
|
2012-01-19 16:18:15 +01:00
|
|
|
}
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
void CreateFunction(const FunctionCallbackInfo<Value>& args) {
|
2013-06-16 09:30:50 +02:00
|
|
|
Isolate* isolate = Isolate::GetCurrent();
|
2013-03-19 11:28:17 +01:00
|
|
|
HandleScope scope(isolate);
|
2012-01-19 16:18:15 +01:00
|
|
|
|
2014-03-13 18:53:48 +01:00
|
|
|
Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, MyFunction);
|
2012-01-19 16:18:15 +01:00
|
|
|
Local<Function> fn = tpl->GetFunction();
|
2013-06-16 09:30:50 +02:00
|
|
|
|
2013-06-13 07:05:33 +02:00
|
|
|
// omit this to make it anonymous
|
2014-01-18 23:49:18 +01:00
|
|
|
fn->SetName(String::NewFromUtf8(isolate, "theFunction"));
|
2012-01-19 16:18:15 +01:00
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
args.GetReturnValue().Set(fn);
|
2012-01-19 16:18:15 +01:00
|
|
|
}
|
|
|
|
|
2013-01-21 04:20:43 +01:00
|
|
|
void Init(Handle<Object> exports, Handle<Object> module) {
|
2014-01-18 23:49:18 +01:00
|
|
|
NODE_SET_METHOD(module, "exports", CreateFunction);
|
2012-01-19 16:18:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
NODE_MODULE(addon, Init)
|
|
|
|
|
|
|
|
To test:
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
// test.js
|
2012-01-19 16:18:15 +01:00
|
|
|
var addon = require('./build/Release/addon');
|
|
|
|
|
2013-01-21 04:20:43 +01:00
|
|
|
var fn = addon();
|
2012-01-19 16:18:15 +01:00
|
|
|
console.log(fn()); // 'hello world'
|
|
|
|
|
|
|
|
|
2012-02-27 20:06:31 +01:00
|
|
|
### Wrapping C++ objects
|
2012-01-19 16:18:15 +01:00
|
|
|
|
|
|
|
Here we will create a wrapper for a C++ object/class `MyObject` that can be
|
|
|
|
instantiated in JavaScript through the `new` operator. First prepare the main
|
|
|
|
module `addon.cc`:
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
// addon.cc
|
2012-01-19 16:18:15 +01:00
|
|
|
#include <node.h>
|
|
|
|
#include "myobject.h"
|
|
|
|
|
|
|
|
using namespace v8;
|
|
|
|
|
2013-01-21 04:20:43 +01:00
|
|
|
void InitAll(Handle<Object> exports) {
|
|
|
|
MyObject::Init(exports);
|
2012-01-19 16:18:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
NODE_MODULE(addon, InitAll)
|
|
|
|
|
|
|
|
Then in `myobject.h` make your wrapper inherit from `node::ObjectWrap`:
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
// myobject.h
|
2012-01-19 16:18:15 +01:00
|
|
|
#ifndef MYOBJECT_H
|
|
|
|
#define MYOBJECT_H
|
|
|
|
|
|
|
|
#include <node.h>
|
2014-01-18 23:49:18 +01:00
|
|
|
#include <node_object_wrap.h>
|
2012-01-19 16:18:15 +01:00
|
|
|
|
|
|
|
class MyObject : public node::ObjectWrap {
|
|
|
|
public:
|
2013-01-21 04:20:43 +01:00
|
|
|
static void Init(v8::Handle<v8::Object> exports);
|
2012-01-19 16:18:15 +01:00
|
|
|
|
|
|
|
private:
|
2013-10-10 14:09:38 +02:00
|
|
|
explicit MyObject(double value = 0);
|
2012-01-19 16:18:15 +01:00
|
|
|
~MyObject();
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
|
|
|
|
static void PlusOne(const v8::FunctionCallbackInfo<v8::Value>& args);
|
2013-10-10 14:09:38 +02:00
|
|
|
static v8::Persistent<v8::Function> constructor;
|
|
|
|
double value_;
|
2012-01-19 16:18:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
And in `myobject.cc` implement the various methods that you want to expose.
|
|
|
|
Here we expose the method `plusOne` by adding it to the constructor's
|
|
|
|
prototype:
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
// myobject.cc
|
2012-01-19 16:18:15 +01:00
|
|
|
#include "myobject.h"
|
|
|
|
|
|
|
|
using namespace v8;
|
|
|
|
|
2013-10-10 14:09:38 +02:00
|
|
|
Persistent<Function> MyObject::constructor;
|
|
|
|
|
|
|
|
MyObject::MyObject(double value) : value_(value) {
|
|
|
|
}
|
|
|
|
|
|
|
|
MyObject::~MyObject() {
|
|
|
|
}
|
2012-01-19 16:18:15 +01:00
|
|
|
|
2013-01-21 04:20:43 +01:00
|
|
|
void MyObject::Init(Handle<Object> exports) {
|
2013-06-16 09:30:50 +02:00
|
|
|
Isolate* isolate = Isolate::GetCurrent();
|
|
|
|
|
2012-01-19 16:18:15 +01:00
|
|
|
// Prepare constructor template
|
2014-03-13 18:53:48 +01:00
|
|
|
Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New);
|
2014-01-18 23:49:18 +01:00
|
|
|
tpl->SetClassName(String::NewFromUtf8(isolate, "MyObject"));
|
2012-01-19 16:18:15 +01:00
|
|
|
tpl->InstanceTemplate()->SetInternalFieldCount(1);
|
2013-06-16 09:30:50 +02:00
|
|
|
|
2012-01-19 16:18:15 +01:00
|
|
|
// Prototype
|
2014-01-18 23:49:18 +01:00
|
|
|
NODE_SET_PROTOTYPE_METHOD(tpl, "plusOne", PlusOne);
|
2013-06-16 09:30:50 +02:00
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
constructor.Reset(isolate, tpl->GetFunction());
|
|
|
|
exports->Set(String::NewFromUtf8(isolate, "MyObject"),
|
|
|
|
tpl->GetFunction());
|
2012-01-19 16:18:15 +01:00
|
|
|
}
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
void MyObject::New(const FunctionCallbackInfo<Value>& args) {
|
2013-06-16 09:30:50 +02:00
|
|
|
Isolate* isolate = Isolate::GetCurrent();
|
2013-03-19 11:28:17 +01:00
|
|
|
HandleScope scope(isolate);
|
2012-01-19 16:18:15 +01:00
|
|
|
|
2013-10-10 14:09:38 +02:00
|
|
|
if (args.IsConstructCall()) {
|
|
|
|
// Invoked as constructor: `new MyObject(...)`
|
|
|
|
double value = args[0]->IsUndefined() ? 0 : args[0]->NumberValue();
|
|
|
|
MyObject* obj = new MyObject(value);
|
|
|
|
obj->Wrap(args.This());
|
2014-01-18 23:49:18 +01:00
|
|
|
args.GetReturnValue().Set(args.This());
|
2013-10-10 14:09:38 +02:00
|
|
|
} else {
|
|
|
|
// Invoked as plain function `MyObject(...)`, turn into construct call.
|
|
|
|
const int argc = 1;
|
|
|
|
Local<Value> argv[argc] = { args[0] };
|
2014-01-18 23:49:18 +01:00
|
|
|
Local<Function> cons = Local<Function>::New(isolate, constructor);
|
|
|
|
args.GetReturnValue().Set(cons->NewInstance(argc, argv));
|
2013-10-10 14:09:38 +02:00
|
|
|
}
|
2012-01-19 16:18:15 +01:00
|
|
|
}
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
void MyObject::PlusOne(const FunctionCallbackInfo<Value>& args) {
|
2013-06-16 09:30:50 +02:00
|
|
|
Isolate* isolate = Isolate::GetCurrent();
|
2013-03-19 11:28:17 +01:00
|
|
|
HandleScope scope(isolate);
|
2012-01-19 16:18:15 +01:00
|
|
|
|
|
|
|
MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.This());
|
2013-10-10 14:09:38 +02:00
|
|
|
obj->value_ += 1;
|
2012-01-19 16:18:15 +01:00
|
|
|
|
2014-03-13 18:53:48 +01:00
|
|
|
args.GetReturnValue().Set(Number::New(isolate, obj->value_));
|
2012-01-19 16:18:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Test it with:
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
// test.js
|
2012-01-19 16:18:15 +01:00
|
|
|
var addon = require('./build/Release/addon');
|
|
|
|
|
|
|
|
var obj = new addon.MyObject(10);
|
|
|
|
console.log( obj.plusOne() ); // 11
|
|
|
|
console.log( obj.plusOne() ); // 12
|
|
|
|
console.log( obj.plusOne() ); // 13
|
|
|
|
|
2012-02-27 20:06:31 +01:00
|
|
|
### Factory of wrapped objects
|
2012-01-19 16:18:15 +01:00
|
|
|
|
|
|
|
This is useful when you want to be able to create native objects without
|
|
|
|
explicitly instantiating them with the `new` operator in JavaScript, e.g.
|
|
|
|
|
|
|
|
var obj = addon.createObject();
|
|
|
|
// instead of:
|
|
|
|
// var obj = new addon.Object();
|
|
|
|
|
|
|
|
Let's register our `createObject` method in `addon.cc`:
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
// addon.cc
|
2012-01-19 16:18:15 +01:00
|
|
|
#include <node.h>
|
|
|
|
#include "myobject.h"
|
|
|
|
|
|
|
|
using namespace v8;
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
void CreateObject(const FunctionCallbackInfo<Value>& args) {
|
2013-06-16 09:30:50 +02:00
|
|
|
Isolate* isolate = Isolate::GetCurrent();
|
2013-03-19 11:28:17 +01:00
|
|
|
HandleScope scope(isolate);
|
2014-01-18 23:49:18 +01:00
|
|
|
MyObject::NewInstance(args);
|
2012-01-19 16:18:15 +01:00
|
|
|
}
|
|
|
|
|
2013-01-21 04:20:43 +01:00
|
|
|
void InitAll(Handle<Object> exports, Handle<Object> module) {
|
2012-01-19 16:18:15 +01:00
|
|
|
MyObject::Init();
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
NODE_SET_METHOD(module, "exports", CreateObject);
|
2012-01-19 16:18:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
NODE_MODULE(addon, InitAll)
|
|
|
|
|
|
|
|
In `myobject.h` we now introduce the static method `NewInstance` that takes
|
|
|
|
care of instantiating the object (i.e. it does the job of `new` in JavaScript):
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
// myobject.h
|
2012-01-19 16:18:15 +01:00
|
|
|
#ifndef MYOBJECT_H
|
|
|
|
#define MYOBJECT_H
|
|
|
|
|
|
|
|
#include <node.h>
|
2014-01-18 23:49:18 +01:00
|
|
|
#include <node_object_wrap.h>
|
2012-01-19 16:18:15 +01:00
|
|
|
|
|
|
|
class MyObject : public node::ObjectWrap {
|
|
|
|
public:
|
|
|
|
static void Init();
|
2014-01-18 23:49:18 +01:00
|
|
|
static void NewInstance(const v8::FunctionCallbackInfo<v8::Value>& args);
|
2012-01-19 16:18:15 +01:00
|
|
|
|
|
|
|
private:
|
2013-10-10 14:09:38 +02:00
|
|
|
explicit MyObject(double value = 0);
|
2012-01-19 16:18:15 +01:00
|
|
|
~MyObject();
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
|
|
|
|
static void PlusOne(const v8::FunctionCallbackInfo<v8::Value>& args);
|
2013-10-10 14:09:38 +02:00
|
|
|
static v8::Persistent<v8::Function> constructor;
|
|
|
|
double value_;
|
2012-01-19 16:18:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
The implementation is similar to the above in `myobject.cc`:
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
// myobject.cc
|
2012-01-19 16:18:15 +01:00
|
|
|
#include <node.h>
|
|
|
|
#include "myobject.h"
|
|
|
|
|
|
|
|
using namespace v8;
|
|
|
|
|
|
|
|
Persistent<Function> MyObject::constructor;
|
|
|
|
|
2013-10-10 14:09:38 +02:00
|
|
|
MyObject::MyObject(double value) : value_(value) {
|
|
|
|
}
|
|
|
|
|
|
|
|
MyObject::~MyObject() {
|
|
|
|
}
|
|
|
|
|
2012-01-19 16:18:15 +01:00
|
|
|
void MyObject::Init() {
|
2013-06-16 09:30:50 +02:00
|
|
|
Isolate* isolate = Isolate::GetCurrent();
|
2012-01-19 16:18:15 +01:00
|
|
|
// Prepare constructor template
|
2014-03-13 18:53:48 +01:00
|
|
|
Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New);
|
2014-01-18 23:49:18 +01:00
|
|
|
tpl->SetClassName(String::NewFromUtf8(isolate, "MyObject"));
|
2012-01-19 16:18:15 +01:00
|
|
|
tpl->InstanceTemplate()->SetInternalFieldCount(1);
|
2013-06-16 09:30:50 +02:00
|
|
|
|
2012-01-19 16:18:15 +01:00
|
|
|
// Prototype
|
2014-01-18 23:49:18 +01:00
|
|
|
NODE_SET_PROTOTYPE_METHOD(tpl, "plusOne", PlusOne);
|
2012-01-19 16:18:15 +01:00
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
constructor.Reset(isolate, tpl->GetFunction());
|
2012-01-19 16:18:15 +01:00
|
|
|
}
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
void MyObject::New(const FunctionCallbackInfo<Value>& args) {
|
2013-06-16 09:30:50 +02:00
|
|
|
Isolate* isolate = Isolate::GetCurrent();
|
2013-03-19 11:28:17 +01:00
|
|
|
HandleScope scope(isolate);
|
2012-01-19 16:18:15 +01:00
|
|
|
|
2013-10-10 14:09:38 +02:00
|
|
|
if (args.IsConstructCall()) {
|
|
|
|
// Invoked as constructor: `new MyObject(...)`
|
|
|
|
double value = args[0]->IsUndefined() ? 0 : args[0]->NumberValue();
|
|
|
|
MyObject* obj = new MyObject(value);
|
|
|
|
obj->Wrap(args.This());
|
2014-01-18 23:49:18 +01:00
|
|
|
args.GetReturnValue().Set(args.This());
|
2013-10-10 14:09:38 +02:00
|
|
|
} else {
|
|
|
|
// Invoked as plain function `MyObject(...)`, turn into construct call.
|
|
|
|
const int argc = 1;
|
|
|
|
Local<Value> argv[argc] = { args[0] };
|
2014-01-18 23:49:18 +01:00
|
|
|
Local<Function> cons = Local<Function>::New(isolate, constructor);
|
|
|
|
args.GetReturnValue().Set(cons->NewInstance(argc, argv));
|
2013-10-10 14:09:38 +02:00
|
|
|
}
|
2012-01-19 16:18:15 +01:00
|
|
|
}
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
void MyObject::NewInstance(const FunctionCallbackInfo<Value>& args) {
|
2013-06-16 09:30:50 +02:00
|
|
|
Isolate* isolate = Isolate::GetCurrent();
|
2013-03-19 11:28:17 +01:00
|
|
|
HandleScope scope(isolate);
|
2012-01-19 16:18:15 +01:00
|
|
|
|
|
|
|
const unsigned argc = 1;
|
|
|
|
Handle<Value> argv[argc] = { args[0] };
|
2014-01-18 23:49:18 +01:00
|
|
|
Local<Function> cons = Local<Function>::New(isolate, constructor);
|
|
|
|
Local<Object> instance = cons->NewInstance(argc, argv);
|
2012-01-19 16:18:15 +01:00
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
args.GetReturnValue().Set(instance);
|
2012-01-19 16:18:15 +01:00
|
|
|
}
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
void MyObject::PlusOne(const FunctionCallbackInfo<Value>& args) {
|
2013-06-16 09:30:50 +02:00
|
|
|
Isolate* isolate = Isolate::GetCurrent();
|
2013-03-19 11:28:17 +01:00
|
|
|
HandleScope scope(isolate);
|
2012-01-19 16:18:15 +01:00
|
|
|
|
|
|
|
MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.This());
|
2013-10-10 14:09:38 +02:00
|
|
|
obj->value_ += 1;
|
2012-01-19 16:18:15 +01:00
|
|
|
|
2014-03-13 18:53:48 +01:00
|
|
|
args.GetReturnValue().Set(Number::New(isolate, obj->value_));
|
2012-01-19 16:18:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Test it with:
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
// test.js
|
2013-01-21 04:20:43 +01:00
|
|
|
var createObject = require('./build/Release/addon');
|
2012-01-19 16:18:15 +01:00
|
|
|
|
2013-01-21 04:20:43 +01:00
|
|
|
var obj = createObject(10);
|
2012-01-19 16:18:15 +01:00
|
|
|
console.log( obj.plusOne() ); // 11
|
|
|
|
console.log( obj.plusOne() ); // 12
|
|
|
|
console.log( obj.plusOne() ); // 13
|
|
|
|
|
2013-01-21 04:20:43 +01:00
|
|
|
var obj2 = createObject(20);
|
2012-01-19 16:18:15 +01:00
|
|
|
console.log( obj2.plusOne() ); // 21
|
|
|
|
console.log( obj2.plusOne() ); // 22
|
|
|
|
console.log( obj2.plusOne() ); // 23
|
|
|
|
|
|
|
|
|
2012-02-27 20:06:31 +01:00
|
|
|
### Passing wrapped objects around
|
2012-01-19 16:18:15 +01:00
|
|
|
|
|
|
|
In addition to wrapping and returning C++ objects, you can pass them around
|
|
|
|
by unwrapping them with Node's `node::ObjectWrap::Unwrap` helper function.
|
|
|
|
In the following `addon.cc` we introduce a function `add()` that can take on two
|
|
|
|
`MyObject` objects:
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
// addon.cc
|
2012-01-19 16:18:15 +01:00
|
|
|
#include <node.h>
|
2013-10-29 20:49:41 +01:00
|
|
|
#include <node_object_wrap.h>
|
2012-01-19 16:18:15 +01:00
|
|
|
#include "myobject.h"
|
|
|
|
|
|
|
|
using namespace v8;
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
void CreateObject(const FunctionCallbackInfo<Value>& args) {
|
2013-06-16 09:30:50 +02:00
|
|
|
Isolate* isolate = Isolate::GetCurrent();
|
2013-03-19 11:28:17 +01:00
|
|
|
HandleScope scope(isolate);
|
2014-01-18 23:49:18 +01:00
|
|
|
MyObject::NewInstance(args);
|
2012-01-19 16:18:15 +01:00
|
|
|
}
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
void Add(const FunctionCallbackInfo<Value>& args) {
|
2013-06-16 09:30:50 +02:00
|
|
|
Isolate* isolate = Isolate::GetCurrent();
|
2013-03-19 11:28:17 +01:00
|
|
|
HandleScope scope(isolate);
|
2012-01-19 16:18:15 +01:00
|
|
|
|
|
|
|
MyObject* obj1 = node::ObjectWrap::Unwrap<MyObject>(
|
|
|
|
args[0]->ToObject());
|
|
|
|
MyObject* obj2 = node::ObjectWrap::Unwrap<MyObject>(
|
|
|
|
args[1]->ToObject());
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
double sum = obj1->value() + obj2->value();
|
2014-03-13 18:53:48 +01:00
|
|
|
args.GetReturnValue().Set(Number::New(isolate, sum));
|
2012-01-19 16:18:15 +01:00
|
|
|
}
|
|
|
|
|
2013-01-21 04:20:43 +01:00
|
|
|
void InitAll(Handle<Object> exports) {
|
2012-01-19 16:18:15 +01:00
|
|
|
MyObject::Init();
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
NODE_SET_METHOD(exports, "createObject", CreateObject);
|
|
|
|
NODE_SET_METHOD(exports, "add", Add);
|
2012-01-19 16:18:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
NODE_MODULE(addon, InitAll)
|
|
|
|
|
|
|
|
To make things interesting we introduce a public method in `myobject.h` so we
|
|
|
|
can probe private values after unwrapping the object:
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
// myobject.h
|
2012-01-19 16:18:15 +01:00
|
|
|
#ifndef MYOBJECT_H
|
|
|
|
#define MYOBJECT_H
|
|
|
|
|
|
|
|
#include <node.h>
|
2013-10-29 20:49:41 +01:00
|
|
|
#include <node_object_wrap.h>
|
2012-01-19 16:18:15 +01:00
|
|
|
|
|
|
|
class MyObject : public node::ObjectWrap {
|
|
|
|
public:
|
|
|
|
static void Init();
|
2014-01-18 23:49:18 +01:00
|
|
|
static void NewInstance(const v8::FunctionCallbackInfo<v8::Value>& args);
|
|
|
|
inline double value() const { return value_; }
|
2012-01-19 16:18:15 +01:00
|
|
|
|
|
|
|
private:
|
2013-10-10 14:09:38 +02:00
|
|
|
explicit MyObject(double value = 0);
|
2012-01-19 16:18:15 +01:00
|
|
|
~MyObject();
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
|
2013-10-10 14:09:38 +02:00
|
|
|
static v8::Persistent<v8::Function> constructor;
|
|
|
|
double value_;
|
2012-01-19 16:18:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
The implementation of `myobject.cc` is similar as before:
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
// myobject.cc
|
2012-01-19 16:18:15 +01:00
|
|
|
#include <node.h>
|
|
|
|
#include "myobject.h"
|
|
|
|
|
|
|
|
using namespace v8;
|
|
|
|
|
|
|
|
Persistent<Function> MyObject::constructor;
|
|
|
|
|
2013-10-10 14:09:38 +02:00
|
|
|
MyObject::MyObject(double value) : value_(value) {
|
|
|
|
}
|
|
|
|
|
|
|
|
MyObject::~MyObject() {
|
|
|
|
}
|
|
|
|
|
2012-01-19 16:18:15 +01:00
|
|
|
void MyObject::Init() {
|
2013-06-16 09:30:50 +02:00
|
|
|
Isolate* isolate = Isolate::GetCurrent();
|
|
|
|
|
2012-01-19 16:18:15 +01:00
|
|
|
// Prepare constructor template
|
2014-03-13 18:53:48 +01:00
|
|
|
Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New);
|
2014-01-18 23:49:18 +01:00
|
|
|
tpl->SetClassName(String::NewFromUtf8(isolate, "MyObject"));
|
2012-01-19 16:18:15 +01:00
|
|
|
tpl->InstanceTemplate()->SetInternalFieldCount(1);
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
constructor.Reset(isolate, tpl->GetFunction());
|
2012-01-19 16:18:15 +01:00
|
|
|
}
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
void MyObject::New(const FunctionCallbackInfo<Value>& args) {
|
2013-06-16 09:30:50 +02:00
|
|
|
Isolate* isolate = Isolate::GetCurrent();
|
2013-03-19 11:28:17 +01:00
|
|
|
HandleScope scope(isolate);
|
2012-01-19 16:18:15 +01:00
|
|
|
|
2013-10-10 14:09:38 +02:00
|
|
|
if (args.IsConstructCall()) {
|
|
|
|
// Invoked as constructor: `new MyObject(...)`
|
|
|
|
double value = args[0]->IsUndefined() ? 0 : args[0]->NumberValue();
|
|
|
|
MyObject* obj = new MyObject(value);
|
|
|
|
obj->Wrap(args.This());
|
2014-01-18 23:49:18 +01:00
|
|
|
args.GetReturnValue().Set(args.This());
|
2013-10-10 14:09:38 +02:00
|
|
|
} else {
|
|
|
|
// Invoked as plain function `MyObject(...)`, turn into construct call.
|
|
|
|
const int argc = 1;
|
|
|
|
Local<Value> argv[argc] = { args[0] };
|
2014-01-18 23:49:18 +01:00
|
|
|
Local<Function> cons = Local<Function>::New(isolate, constructor);
|
|
|
|
args.GetReturnValue().Set(cons->NewInstance(argc, argv));
|
2013-10-10 14:09:38 +02:00
|
|
|
}
|
2012-01-19 16:18:15 +01:00
|
|
|
}
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
void MyObject::NewInstance(const FunctionCallbackInfo<Value>& args) {
|
2013-06-16 09:30:50 +02:00
|
|
|
Isolate* isolate = Isolate::GetCurrent();
|
2013-03-19 11:28:17 +01:00
|
|
|
HandleScope scope(isolate);
|
2012-01-19 16:18:15 +01:00
|
|
|
|
|
|
|
const unsigned argc = 1;
|
|
|
|
Handle<Value> argv[argc] = { args[0] };
|
2014-01-18 23:49:18 +01:00
|
|
|
Local<Function> cons = Local<Function>::New(isolate, constructor);
|
|
|
|
Local<Object> instance = cons->NewInstance(argc, argv);
|
2012-01-19 16:18:15 +01:00
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
args.GetReturnValue().Set(instance);
|
2012-01-19 16:18:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Test it with:
|
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
// test.js
|
2012-01-19 16:18:15 +01:00
|
|
|
var addon = require('./build/Release/addon');
|
|
|
|
|
|
|
|
var obj1 = addon.createObject(10);
|
|
|
|
var obj2 = addon.createObject(20);
|
|
|
|
var result = addon.add(obj1, obj2);
|
|
|
|
|
|
|
|
console.log(result); // 30
|