Skip to content

Node.js native module programming

bellbind edited this page Oct 13, 2013 · 11 revisions

Separate application algorithm codes and Node.js plug codes

  • Application codes should be removed any node.js and v8 related definitions.
    • it can use another languages for application code. e.g. C, Objective-C
  • Node.js plug codes are almost for convert arguments and result.

About v8 Handles

3 Handle (smart pointer) type exists

  • Handle: as smart pointer of RAII (release the holding resource by its destructor)
  • Local: automatically managed by HandleScope
  • Persistent: explicitly call Dispose() to release

HandleScope instance is stacked memory manager for Local handle.

  • HandleScope scope: make scope upon top of HandleScope stack.
  • Local<...> v = ...: managed by stack top scope. the scope destructor release all managed handle
  • return scope.Close(localHandle): unmanage a localHandle object on the scope, then return localHandle as Handle

Convert C type to v8::Value

  • char* => String: String::New(s) or String::NewSymbol(s)
  • String => char*: String::Utf8Value utf8(str); char* s = *utf8;
  • int32_t => Integer: Integer::New(i)
  • Integer => int32_t: v->Int32Value()
  • uint32_t => Integer: Integer::NewFromUnsigned(i)
  • Integer => uint32_t: v->Uint32Value()
Clone this wiki locally