You can download this gist as a single repository.
build
node-gyp configure --debug
node-gyp build --debug
test
node test.js
{ | |
"targets": [ | |
{ | |
"target_name": "test_backstor", | |
'win_delay_load_hook': 'false', | |
"sources": [ "test_backstor.cc"], | |
'defines': [ "BUILDING_NODE_EXTENSION","BUILD_NODE_ADDON" ], | |
} | |
], | |
} | |
{ | |
"name": "testbackingstore", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1", | |
"install": "node-gyp rebuild" | |
}, | |
"author": "", | |
"license": "ISC", | |
"gypfile": true | |
} |
test = require( "./build/Debug/test_backstor.node" ); | |
var buf; | |
buf = test.getBuffer(); | |
console.log( "Buffer:", buf ); | |
#include <node.h> | |
#include <node_object_wrap.h> | |
#include <v8.h> | |
#include <uv.h> | |
using namespace v8; | |
using namespace v8; | |
#define ReadOnlyProperty (PropertyAttribute)((int)PropertyAttribute::ReadOnly | PropertyAttribute::DontDelete) | |
#define SET_READONLY_METHOD( object, name, method ) (object)->DefineOwnProperty( isolate->GetCurrentContext(), String::NewFromUtf8(isolate, name, v8::NewStringType::kNormal ).ToLocalChecked(), v8::Function::New(isolate->GetCurrentContext(), method ).ToLocalChecked(), ReadOnlyProperty ) | |
static void releaseBufferBackingStore( void* data, size_t length, void* deleter_data ) { | |
(void)length; | |
(void)deleter_data;; | |
(void)data; // it's actually a static buffer... | |
//Deallocate( void*, data ); | |
} | |
static void makeBuffer( const v8::FunctionCallbackInfo<Value>& args ){ | |
Isolate *isolate = args.GetIsolate(); | |
static char buf[] = {"This is a test buffer"}; | |
std::shared_ptr<BackingStore> bs = ArrayBuffer::NewBackingStore( buf, sizeof(buf), releaseBufferBackingStore, NULL ); | |
Local<Object> arrayBuffer = ArrayBuffer::New( isolate, bs); | |
args.GetReturnValue().Set( arrayBuffer ); | |
} | |
static void doInit( Local<Context> context, Local<Object> exports ) | |
{ | |
Isolate* isolate = Isolate::GetCurrent(); | |
SET_READONLY_METHOD( exports, "getBuffer", makeBuffer ); | |
} | |
NODE_MODULE_INIT( /*Local<Object> exports, | |
Local<Value>Module, | |
Local<Context> context*/ ) { | |
doInit(context,exports); | |
} | |