Skip to content

Instantly share code, notes, and snippets.

@bellbind
Created July 18, 2012 08:57
Show Gist options
  • Save bellbind/3135139 to your computer and use it in GitHub Desktop.
Save bellbind/3135139 to your computer and use it in GitHub Desktop.
[nodejs]Making native package for node.js 0.8.x (with node-gyp)

Making native package for node.js 0.8.x (with node-gyp)

Example Package Files

  • package.json: npm package description (its name should be "package.json")
  • binding.gyp: gyp JSON (its name should be "binding.gyp")
  • hello.cc: native module source
  • run.js: example script for the module

Building and checking the native module.

Run npm install on the package root directory. A native module is built in "build/Release" directory.

You can check the module to run node run.js.

"binding.gyp"

GYP file format is (extended) JSON.

{"targets": [
    {"target_name": "hello", "sources": ["hello.cc"]}
]}

The target_name sets a DLL base name. For the example, it generates "./build/Release/hello.node" on linux systems. The sources lists source files for the DLL.

You can also set other gyp options, e.g. operating system dependent options. See: GYP User Documentation

"node-gyp" command (included in bundled "npm") use "binding.gyp" as a gyp file.

"package.json"

A "package.json" is a npm package description file.

If you set main to the DLL file, require(hello) returns the DLL as module.

    "main": "/build/Rerease/hello",

The current "npm" does not require additional descriptions for native modules. If "binding.gyp" exists in the directory, "script.install" automatically set as:

    "scripts": {
        "install": "node-gyp rebuild"
    },

Note on native module source.

NODE_SET_METHOD(target, "say", Say) sets the C++ Say function as JS function object. to target's say property.

NODE_MODULE(hello, init) makes global struct named hello and sets its initializer as init function pointer.

v8::HandleScope set newly created object handles as a local "scope". Each call frames make each local "scope" instances. The "scope"s form as a similar stack chain.

All handles, name and greet, are managed under the local "scope". The handles are garbage collected if exitted the "scope". scope.Close(handle) make the handle as a object in the parent "scope" (and also closes its local "scope").

Reference:

{"targets": [
{"target_name": "hello", "sources": ["hello.cc"]}
]}
#include <node.h>
#include <v8.h>
namespace {
v8::Handle<v8::Value> Say(const v8::Arguments& args) {
v8::HandleScope scope;
v8::Local<v8::String> name = args[0]->ToString();
v8::Local<v8::String> greet = v8::String::New("Hello ");
return scope.Close(v8::String::Concat(greet, name));
}
void init(v8::Handle<v8::Object> target) {
NODE_SET_METHOD(target, "say", Say);
}
}
NODE_MODULE(hello, init);
{
"name": "hello",
"main": "/build/Rerease/hello",
"version": "0.1.0",
"engines": {
"node": ">=0.8.2"
}
}
var hello = require("./build/Release/hello");
console.log(hello.say("Alice"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment