2012-02-27 20:06:31 +01:00
|
|
|
# Addons
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-12-08 03:50:25 +01:00
|
|
|
Addons are dynamically-linked shared objects. They can provide glue to C and
|
2010-10-28 14:18:16 +02:00
|
|
|
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
|
2015-08-13 18:14:34 +02:00
|
|
|
`v8.h` header file (`deps/v8/include/v8.h` in the Node.js source
|
2015-11-14 04:21:49 +01:00
|
|
|
tree), which is also available [online][].
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-14 04:21:49 +01:00
|
|
|
- [libuv][], C event loop library. Anytime one needs to wait for a file
|
|
|
|
descriptor to become readable, wait for a timer, or wait for a signal
|
2015-12-08 03:50:25 +01:00
|
|
|
to be received, one will need to interface with libuv. That is, if you
|
2015-11-14 04:21:49 +01:00
|
|
|
perform any I/O, libuv will need to be used.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-12-08 03:50:25 +01:00
|
|
|
- Internal Node.js libraries. The most important class is `node::ObjectWrap`
|
|
|
|
which you will likely want to derive from.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
- Others. Look in `deps/` for what else is available.
|
|
|
|
|
2015-08-13 18:14:34 +02:00
|
|
|
Node.js statically compiles all its dependencies into the executable.
|
2013-01-25 23:27:46 +01:00
|
|
|
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
|
|
|
|
2015-11-14 04:21:49 +01:00
|
|
|
All of the following examples are available for [download][] 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
|
|
|
|
2015-12-08 03:50:25 +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
|
|
|
|
2015-03-11 14:14:18 +01:00
|
|
|
namespace demo {
|
|
|
|
|
|
|
|
using v8::FunctionCallbackInfo;
|
|
|
|
using v8::Isolate;
|
|
|
|
using v8::Local;
|
|
|
|
using v8::Object;
|
|
|
|
using v8::String;
|
|
|
|
using v8::Value;
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
void Method(const FunctionCallbackInfo<Value>& args) {
|
2015-03-11 13:41:32 +01:00
|
|
|
Isolate* isolate = args.GetIsolate();
|
2014-01-18 23:49:18 +01:00
|
|
|
args.GetReturnValue().Set(String::NewFromUtf8(isolate, "world"));
|
2011-10-11 21:14:00 +02:00
|
|
|
}
|
|
|
|
|
2015-03-11 14:03:02 +01:00
|
|
|
void init(Local<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
|
|
|
|
2015-03-11 14:14:18 +01:00
|
|
|
} // namespace demo
|
|
|
|
|
2015-08-13 18:14:34 +02:00
|
|
|
Note that all Node.js addons must export an initialization function:
|
2011-11-09 03:13:59 +01:00
|
|
|
|
2015-03-11 14:03:02 +01:00
|
|
|
void Initialize(Local<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
|
|
|
|
2015-12-08 03:50:25 +01:00
|
|
|
The `module_name` needs to match the filename of the final binary (excluding
|
|
|
|
the .node suffix).
|
2011-11-28 20:44:23 +01:00
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
The source code needs to be built into `addon.node`, the binary Addon. To
|
2015-12-08 03:50:25 +01:00
|
|
|
do this, we create a file called `binding.gyp` which describes the configuration
|
2012-04-13 02:24:35 +02:00
|
|
|
to build your module in a JSON-like format. This file gets compiled by
|
2015-11-14 04:21:49 +01:00
|
|
|
[node-gyp][].
|
2012-04-13 02:24:35 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
"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
|
2015-12-08 03:50:25 +01:00
|
|
|
(on Windows) in the `build/` directory. Next, invoke the `node-gyp build`
|
2012-04-13 02:24:35 +02:00
|
|
|
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
|
|
|
|
2015-12-08 03:50:25 +01:00
|
|
|
You can now use the binary addon in a Node.js project `hello.js` by pointing
|
2013-06-13 07:05:33 +02:00
|
|
|
`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
|
2015-11-14 04:21:49 +01:00
|
|
|
[v8 reference][] for help with the various v8 calls, and v8's
|
|
|
|
[Embedder's Guide][] for an explanation of several concepts used such as
|
|
|
|
handles, scopes, function templates, etc.
|
2012-01-19 16:18:15 +01:00
|
|
|
|
2015-12-08 03:50:25 +01:00
|
|
|
In order to use these examples, you need to compile them using `node-gyp`.
|
2012-04-13 02:24:35 +02:00
|
|
|
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
|
2015-12-08 03:50:25 +01:00
|
|
|
the `sources` array. For example:
|
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>
|
|
|
|
|
2015-03-11 14:14:18 +01:00
|
|
|
namespace demo {
|
|
|
|
|
|
|
|
using v8::Exception;
|
|
|
|
using v8::FunctionCallbackInfo;
|
|
|
|
using v8::Isolate;
|
|
|
|
using v8::Local;
|
|
|
|
using v8::Number;
|
|
|
|
using v8::Object;
|
|
|
|
using v8::String;
|
|
|
|
using v8::Value;
|
2012-01-19 16:18:15 +01:00
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
void Add(const FunctionCallbackInfo<Value>& args) {
|
2015-03-11 13:41:32 +01:00
|
|
|
Isolate* isolate = args.GetIsolate();
|
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
|
|
|
}
|
|
|
|
|
2015-03-11 14:03:02 +01:00
|
|
|
void Init(Local<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)
|
|
|
|
|
2015-03-11 14:14:18 +01:00
|
|
|
} // namespace demo
|
|
|
|
|
2012-01-19 16:18:15 +01:00
|
|
|
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>
|
|
|
|
|
2015-03-11 14:14:18 +01:00
|
|
|
namespace demo {
|
|
|
|
|
|
|
|
using v8::Function;
|
|
|
|
using v8::FunctionCallbackInfo;
|
|
|
|
using v8::Isolate;
|
|
|
|
using v8::Local;
|
2015-03-11 14:19:21 +01:00
|
|
|
using v8::Null;
|
2015-03-11 14:14:18 +01:00
|
|
|
using v8::Object;
|
|
|
|
using v8::String;
|
|
|
|
using v8::Value;
|
2012-01-19 16:18:15 +01:00
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
void RunCallback(const FunctionCallbackInfo<Value>& args) {
|
2015-03-11 13:41:32 +01:00
|
|
|
Isolate* isolate = args.GetIsolate();
|
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") };
|
2015-03-11 14:19:21 +01:00
|
|
|
cb->Call(Null(isolate), argc, argv);
|
2012-01-19 16:18:15 +01:00
|
|
|
}
|
|
|
|
|
2015-03-11 14:03:02 +01:00
|
|
|
void Init(Local<Object> exports, Local<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)
|
|
|
|
|
2015-03-11 14:14:18 +01:00
|
|
|
} // namespace demo
|
|
|
|
|
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`.
|
|
|
|
|
2015-12-08 03:50:25 +01:00
|
|
|
To test it, run the following JavaScript snippet:
|
2012-01-19 16:18:15 +01:00
|
|
|
|
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>
|
|
|
|
|
2015-03-11 14:14:18 +01:00
|
|
|
namespace demo {
|
|
|
|
|
|
|
|
using v8::FunctionCallbackInfo;
|
|
|
|
using v8::Isolate;
|
|
|
|
using v8::Local;
|
|
|
|
using v8::Object;
|
|
|
|
using v8::String;
|
|
|
|
using v8::Value;
|
2012-01-19 16:18:15 +01:00
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
void CreateObject(const FunctionCallbackInfo<Value>& args) {
|
2015-03-11 13:41:32 +01:00
|
|
|
Isolate* isolate = args.GetIsolate();
|
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
|
|
|
}
|
|
|
|
|
2015-03-11 14:03:02 +01:00
|
|
|
void Init(Local<Object> exports, Local<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)
|
|
|
|
|
2015-03-11 14:14:18 +01:00
|
|
|
} // namespace demo
|
|
|
|
|
2012-01-19 16:18:15 +01:00
|
|
|
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>
|
|
|
|
|
2015-03-11 14:14:18 +01:00
|
|
|
namespace demo {
|
|
|
|
|
|
|
|
using v8::Function;
|
|
|
|
using v8::FunctionCallbackInfo;
|
|
|
|
using v8::FunctionTemplate;
|
|
|
|
using v8::Isolate;
|
|
|
|
using v8::Local;
|
|
|
|
using v8::Object;
|
|
|
|
using v8::String;
|
|
|
|
using v8::Value;
|
2012-01-19 16:18:15 +01:00
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
void MyFunction(const FunctionCallbackInfo<Value>& args) {
|
2015-03-11 13:41:32 +01:00
|
|
|
Isolate* isolate = args.GetIsolate();
|
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) {
|
2015-03-11 13:41:32 +01:00
|
|
|
Isolate* isolate = args.GetIsolate();
|
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
|
|
|
}
|
|
|
|
|
2015-03-11 14:03:02 +01:00
|
|
|
void Init(Local<Object> exports, Local<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)
|
|
|
|
|
2015-03-11 14:14:18 +01:00
|
|
|
} // namespace demo
|
|
|
|
|
2012-01-19 16:18:15 +01:00
|
|
|
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
|
|
|
|
2015-12-08 03:50:25 +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
|
2012-01-19 16:18:15 +01:00
|
|
|
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"
|
|
|
|
|
2015-03-11 14:14:18 +01:00
|
|
|
namespace demo {
|
|
|
|
|
|
|
|
using v8::Local;
|
|
|
|
using v8::Object;
|
2012-01-19 16:18:15 +01:00
|
|
|
|
2015-03-11 14:03:02 +01:00
|
|
|
void InitAll(Local<Object> exports) {
|
2013-01-21 04:20:43 +01:00
|
|
|
MyObject::Init(exports);
|
2012-01-19 16:18:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
NODE_MODULE(addon, InitAll)
|
|
|
|
|
2015-03-11 14:14:18 +01:00
|
|
|
} // namespace demo
|
|
|
|
|
2015-12-08 03:50:25 +01:00
|
|
|
Then, in `myobject.h`, make your wrapper inherit from `node::ObjectWrap`:
|
2012-01-19 16:18:15 +01:00
|
|
|
|
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
|
|
|
|
2015-03-11 14:14:18 +01:00
|
|
|
namespace demo {
|
|
|
|
|
2012-01-19 16:18:15 +01:00
|
|
|
class MyObject : public node::ObjectWrap {
|
|
|
|
public:
|
2015-03-11 14:03:02 +01:00
|
|
|
static void Init(v8::Local<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
|
|
|
};
|
|
|
|
|
2015-03-11 14:14:18 +01:00
|
|
|
} // namespace demo
|
|
|
|
|
2012-01-19 16:18:15 +01:00
|
|
|
#endif
|
|
|
|
|
2015-12-08 03:50:25 +01:00
|
|
|
And in `myobject.cc`, implement the various methods that you want to expose.
|
2012-01-19 16:18:15 +01:00
|
|
|
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"
|
|
|
|
|
2015-03-11 14:14:18 +01:00
|
|
|
namespace demo {
|
|
|
|
|
|
|
|
using v8::Function;
|
|
|
|
using v8::FunctionCallbackInfo;
|
|
|
|
using v8::FunctionTemplate;
|
|
|
|
using v8::Isolate;
|
|
|
|
using v8::Local;
|
|
|
|
using v8::Number;
|
|
|
|
using v8::Object;
|
|
|
|
using v8::Persistent;
|
|
|
|
using v8::String;
|
|
|
|
using v8::Value;
|
2012-01-19 16:18:15 +01:00
|
|
|
|
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
|
|
|
|
2015-03-11 14:03:02 +01:00
|
|
|
void MyObject::Init(Local<Object> exports) {
|
2015-03-11 13:41:32 +01:00
|
|
|
Isolate* isolate = exports->GetIsolate();
|
2013-06-16 09:30:50 +02:00
|
|
|
|
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) {
|
2015-03-11 13:41:32 +01:00
|
|
|
Isolate* isolate = args.GetIsolate();
|
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) {
|
2015-03-11 13:41:32 +01:00
|
|
|
Isolate* isolate = args.GetIsolate();
|
2012-01-19 16:18:15 +01:00
|
|
|
|
2014-03-06 21:44:18 +01:00
|
|
|
MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.Holder());
|
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
|
|
|
}
|
|
|
|
|
2015-03-11 14:14:18 +01:00
|
|
|
} // namespace demo
|
|
|
|
|
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
|
2015-12-08 03:50:25 +01:00
|
|
|
explicitly instantiating them with the `new` operator in JavaScript. For
|
|
|
|
example:
|
2012-01-19 16:18:15 +01:00
|
|
|
|
|
|
|
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"
|
|
|
|
|
2015-03-11 14:14:18 +01:00
|
|
|
namespace demo {
|
|
|
|
|
|
|
|
using v8::FunctionCallbackInfo;
|
|
|
|
using v8::Isolate;
|
|
|
|
using v8::Local;
|
|
|
|
using v8::Object;
|
|
|
|
using v8::String;
|
|
|
|
using v8::Value;
|
2012-01-19 16:18:15 +01:00
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
void CreateObject(const FunctionCallbackInfo<Value>& args) {
|
|
|
|
MyObject::NewInstance(args);
|
2012-01-19 16:18:15 +01:00
|
|
|
}
|
|
|
|
|
2015-03-11 14:03:02 +01:00
|
|
|
void InitAll(Local<Object> exports, Local<Object> module) {
|
2015-03-11 13:41:32 +01:00
|
|
|
MyObject::Init(exports->GetIsolate());
|
2012-01-19 16:18:15 +01:00
|
|
|
|
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)
|
|
|
|
|
2015-03-11 14:14:18 +01:00
|
|
|
} // namespace demo
|
|
|
|
|
2015-12-08 03:50:25 +01:00
|
|
|
In `myobject.h`, we now introduce the static method `NewInstance` that takes
|
|
|
|
care of instantiating the object. In other words, it does the job of `new` in
|
|
|
|
JavaScript:
|
2012-01-19 16:18:15 +01:00
|
|
|
|
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
|
|
|
|
2015-03-11 14:14:18 +01:00
|
|
|
namespace demo {
|
|
|
|
|
2012-01-19 16:18:15 +01:00
|
|
|
class MyObject : public node::ObjectWrap {
|
|
|
|
public:
|
2015-03-11 13:41:32 +01:00
|
|
|
static void Init(v8::Isolate* isolate);
|
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
|
|
|
};
|
|
|
|
|
2015-03-11 14:14:18 +01:00
|
|
|
} // namespace demo
|
|
|
|
|
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"
|
|
|
|
|
2015-03-11 14:14:18 +01:00
|
|
|
namespace demo {
|
|
|
|
|
|
|
|
using v8::Function;
|
|
|
|
using v8::FunctionCallbackInfo;
|
|
|
|
using v8::FunctionTemplate;
|
|
|
|
using v8::Isolate;
|
|
|
|
using v8::Local;
|
|
|
|
using v8::Number;
|
|
|
|
using v8::Object;
|
|
|
|
using v8::Persistent;
|
|
|
|
using v8::String;
|
|
|
|
using v8::Value;
|
2012-01-19 16:18:15 +01:00
|
|
|
|
|
|
|
Persistent<Function> MyObject::constructor;
|
|
|
|
|
2013-10-10 14:09:38 +02:00
|
|
|
MyObject::MyObject(double value) : value_(value) {
|
|
|
|
}
|
|
|
|
|
|
|
|
MyObject::~MyObject() {
|
|
|
|
}
|
|
|
|
|
2015-03-11 13:41:32 +01:00
|
|
|
void MyObject::Init(Isolate* isolate) {
|
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) {
|
2015-03-11 13:41:32 +01:00
|
|
|
Isolate* isolate = args.GetIsolate();
|
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) {
|
2015-03-11 13:41:32 +01:00
|
|
|
Isolate* isolate = args.GetIsolate();
|
2012-01-19 16:18:15 +01:00
|
|
|
|
|
|
|
const unsigned argc = 1;
|
2015-03-11 14:03:02 +01:00
|
|
|
Local<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) {
|
2015-03-11 13:41:32 +01:00
|
|
|
Isolate* isolate = args.GetIsolate();
|
2012-01-19 16:18:15 +01:00
|
|
|
|
2014-03-06 21:44:18 +01:00
|
|
|
MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.Holder());
|
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
|
|
|
}
|
|
|
|
|
2015-03-11 14:14:18 +01:00
|
|
|
} // namespace demo
|
|
|
|
|
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
|
2015-12-08 03:50:25 +01:00
|
|
|
by unwrapping them with the Node.js helper function `node::ObjectWrap::Unwrap`.
|
|
|
|
In the following `addon.cc`, we introduce a function `add()` that can take on
|
|
|
|
two `MyObject` objects:
|
2012-01-19 16:18:15 +01:00
|
|
|
|
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"
|
|
|
|
|
2015-03-11 14:14:18 +01:00
|
|
|
namespace demo {
|
|
|
|
|
|
|
|
using v8::FunctionCallbackInfo;
|
|
|
|
using v8::Isolate;
|
|
|
|
using v8::Local;
|
|
|
|
using v8::Number;
|
|
|
|
using v8::Object;
|
|
|
|
using v8::String;
|
|
|
|
using v8::Value;
|
2012-01-19 16:18:15 +01:00
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
void CreateObject(const FunctionCallbackInfo<Value>& args) {
|
|
|
|
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) {
|
2015-03-11 13:41:32 +01:00
|
|
|
Isolate* isolate = args.GetIsolate();
|
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
|
|
|
}
|
|
|
|
|
2015-03-11 14:03:02 +01:00
|
|
|
void InitAll(Local<Object> exports) {
|
2015-03-11 13:41:32 +01:00
|
|
|
MyObject::Init(exports->GetIsolate());
|
2012-01-19 16:18:15 +01:00
|
|
|
|
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)
|
|
|
|
|
2015-03-11 14:14:18 +01:00
|
|
|
} // namespace demo
|
|
|
|
|
2015-12-08 03:50:25 +01:00
|
|
|
To make things interesting, we introduce a public method in `myobject.h` so we
|
2012-01-19 16:18:15 +01:00
|
|
|
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
|
|
|
|
2015-03-11 14:14:18 +01:00
|
|
|
namespace demo {
|
|
|
|
|
2012-01-19 16:18:15 +01:00
|
|
|
class MyObject : public node::ObjectWrap {
|
|
|
|
public:
|
2015-03-11 13:41:32 +01:00
|
|
|
static void Init(v8::Isolate* isolate);
|
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
|
|
|
};
|
|
|
|
|
2015-03-11 14:14:18 +01:00
|
|
|
} // namespace demo
|
|
|
|
|
2012-01-19 16:18:15 +01:00
|
|
|
#endif
|
|
|
|
|
2015-12-08 03:50:25 +01:00
|
|
|
The implementation of `myobject.cc` is similar to before:
|
2012-01-19 16:18:15 +01:00
|
|
|
|
2014-01-18 23:49:18 +01:00
|
|
|
// myobject.cc
|
2012-01-19 16:18:15 +01:00
|
|
|
#include <node.h>
|
|
|
|
#include "myobject.h"
|
|
|
|
|
2015-03-11 14:14:18 +01:00
|
|
|
namespace demo {
|
|
|
|
|
|
|
|
using v8::Function;
|
|
|
|
using v8::FunctionCallbackInfo;
|
|
|
|
using v8::FunctionTemplate;
|
|
|
|
using v8::Isolate;
|
|
|
|
using v8::Local;
|
|
|
|
using v8::Object;
|
|
|
|
using v8::Persistent;
|
|
|
|
using v8::String;
|
|
|
|
using v8::Value;
|
2012-01-19 16:18:15 +01:00
|
|
|
|
|
|
|
Persistent<Function> MyObject::constructor;
|
|
|
|
|
2013-10-10 14:09:38 +02:00
|
|
|
MyObject::MyObject(double value) : value_(value) {
|
|
|
|
}
|
|
|
|
|
|
|
|
MyObject::~MyObject() {
|
|
|
|
}
|
|
|
|
|
2015-03-11 13:41:32 +01:00
|
|
|
void MyObject::Init(Isolate* isolate) {
|
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) {
|
2015-03-11 13:41:32 +01:00
|
|
|
Isolate* isolate = args.GetIsolate();
|
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) {
|
2015-03-11 13:41:32 +01:00
|
|
|
Isolate* isolate = args.GetIsolate();
|
2012-01-19 16:18:15 +01:00
|
|
|
|
|
|
|
const unsigned argc = 1;
|
2015-03-11 14:03:02 +01:00
|
|
|
Local<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
|
|
|
}
|
|
|
|
|
2015-03-11 14:14:18 +01:00
|
|
|
} // namespace demo
|
|
|
|
|
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
|
2014-09-29 00:36:31 +02:00
|
|
|
|
|
|
|
### AtExit hooks
|
|
|
|
#### void AtExit(callback, args)
|
|
|
|
|
|
|
|
* `callback`: `void (*)(void*)` - A pointer to the function to call at exit.
|
|
|
|
* `args`: `void*` - A pointer to pass to the callback at exit.
|
|
|
|
|
2015-12-08 03:50:25 +01:00
|
|
|
Registers exit hooks that run after the event loop has ended but before the VM
|
2014-09-29 00:36:31 +02:00
|
|
|
is killed.
|
|
|
|
|
2015-12-08 03:50:25 +01:00
|
|
|
Callbacks are run in last-in first-out order. AtExit takes two parameters:
|
2014-09-29 00:36:31 +02:00
|
|
|
a pointer to a callback function to run at exit, and a pointer to untyped
|
|
|
|
context data to be passed to that callback.
|
|
|
|
|
|
|
|
The file `addon.cc` implements AtExit below:
|
|
|
|
|
|
|
|
// addon.cc
|
|
|
|
#undef NDEBUG
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <node.h>
|
|
|
|
|
|
|
|
namespace demo {
|
|
|
|
|
|
|
|
using node::AtExit;
|
|
|
|
using v8::HandleScope;
|
|
|
|
using v8::Isolate;
|
|
|
|
using v8::Local;
|
|
|
|
using v8::Object;
|
|
|
|
|
|
|
|
static char cookie[] = "yum yum";
|
|
|
|
static int at_exit_cb1_called = 0;
|
|
|
|
static int at_exit_cb2_called = 0;
|
|
|
|
|
|
|
|
static void at_exit_cb1(void* arg) {
|
|
|
|
Isolate* isolate = static_cast<Isolate*>(arg);
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
Local<Object> obj = Object::New(isolate);
|
|
|
|
assert(!obj.IsEmpty()); // assert VM is still alive
|
|
|
|
assert(obj->IsObject());
|
|
|
|
at_exit_cb1_called++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void at_exit_cb2(void* arg) {
|
|
|
|
assert(arg == static_cast<void*>(cookie));
|
|
|
|
at_exit_cb2_called++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sanity_check(void*) {
|
|
|
|
assert(at_exit_cb1_called == 1);
|
|
|
|
assert(at_exit_cb2_called == 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void init(Local<Object> exports) {
|
|
|
|
AtExit(sanity_check);
|
|
|
|
AtExit(at_exit_cb2, cookie);
|
|
|
|
AtExit(at_exit_cb2, cookie);
|
|
|
|
AtExit(at_exit_cb1, exports->GetIsolate());
|
|
|
|
}
|
|
|
|
|
|
|
|
NODE_MODULE(addon, init);
|
|
|
|
|
|
|
|
} // namespace demo
|
|
|
|
|
|
|
|
Test in JavaScript by running:
|
|
|
|
|
|
|
|
// test.js
|
|
|
|
var addon = require('./build/Release/addon');
|
2015-11-14 04:21:49 +01:00
|
|
|
|
|
|
|
[online]: https://v8docs.nodesource.com/
|
|
|
|
[libuv]: https://github.com/libuv/libuv
|
|
|
|
[download]: https://github.com/rvagg/node-addon-examples
|
|
|
|
[node-gyp]: https://github.com/nodejs/node-gyp
|
|
|
|
[v8 reference]: http://izs.me/v8-docs/main.html
|
2015-12-02 17:17:19 +01:00
|
|
|
[Embedder's Guide]: https://code.google.com/apis/v8/embed.html
|