- 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
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
.
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.
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"
},
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: