node-addon-layer
C API For writing Node modules
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
Addon Layer

This documentation discusses the best practices and the API for the C API layer for node.

Getting Started

In test.c

#include "shim.h"
foobar(shim_ctx_t* ctx, shim_args_t* args)
{
/* create a new string with contents "Hello World" */
shim_val_t* ret = shim_string_new_copy(ctx, "Hello World");
/* set that string as the return value */
shim_args_set_rval(ctx, args, ret);
/* TRUE because this function didn't fail */
return TRUE;
/* If this were false, there probably would be an exception pending */
/* shim_exception_pending() */
}
myinit(shim_ctx_t* ctx, shim_val_t* exports, shim_val_t* module)
{
/* The list of C functions we want to wrap as JavaScript functions */
shim_fspec_t funcs[] = {
SHIM_FS(foobar), /* wrap the c function foobar, and export it as "foobar" */
SHIM_FS_END, /* no more functions to wrap */
}
/* Add the list of functions to the `exports` object */
shim_obj_set_funcs(ctx, exports, funcs);
/* The module initialized successfully */
return TRUE;
}
/*
* Define a module `mymodule` whose initialization function is `myinit` which
* will be called the first time the module is `require()`d.
*/
SHIM_MODULE(mymodule, myinit)

And then in index.js

var mymodule = require('bindings')('mymodule');
console.log('module return', mymodule.foobar());

Further Reading