This repository has been archived by the owner on Apr 22, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add shared-buffer isolate addon test
- Loading branch information
Showing
3 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include <node.h> | ||
#include <v8.h> | ||
#include <uv.h> | ||
|
||
using namespace v8; | ||
|
||
extern "C" { | ||
void init(Handle<Object> target); | ||
} | ||
|
||
|
||
#define BUFSIZE 1024 | ||
static uint8_t buf[BUFSIZE]; | ||
static uv_mutex_t lock; | ||
|
||
|
||
Handle<Value> Get(const Arguments& args) { | ||
HandleScope scope; | ||
|
||
int index = args[0]->Uint32Value(); | ||
|
||
if (index < 0 || BUFSIZE <= index) { | ||
return ThrowException(Exception::Error(String::New("out of bounds"))); | ||
} | ||
|
||
return scope.Close(Integer::New(buf[index])); | ||
} | ||
|
||
|
||
Handle<Value> Set(const Arguments& args) { | ||
uv_mutex_lock(&lock); | ||
HandleScope scope; | ||
|
||
int index = args[0]->Uint32Value(); | ||
|
||
if (index < 0 || BUFSIZE <= index) { | ||
return ThrowException(Exception::Error(String::New("out of bounds"))); | ||
} | ||
|
||
buf[index] = args[1]->Uint32Value(); | ||
|
||
Local<Integer> val = Integer::New(buf[index]); | ||
|
||
uv_mutex_unlock(&lock); | ||
|
||
return scope.Close(val); | ||
} | ||
|
||
|
||
void init(Handle<Object> target) { | ||
NODE_SET_METHOD(target, "get", Get); | ||
NODE_SET_METHOD(target, "set", Set); | ||
target->Set(String::New("length"), Integer::New(BUFSIZE)); | ||
uv_mutex_init(&lock); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
'targets': [ | ||
{ | ||
'target_name': 'binding', | ||
'sources': [ 'binding.cc' ] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
var assert = require('assert'); | ||
var binding = require('./out/Release/binding'); | ||
|
||
console.log("binding.length =", binding.length); | ||
|
||
if (process.tid === 1) { | ||
var isolate = process._newIsolate(process.argv); | ||
for (var i = 0; i < binding.length; i++) { | ||
console.log('parent', | ||
'binding.set(' + i + ', ' + i + ')', | ||
binding.set(i, i)); | ||
} | ||
} else { | ||
for (var i = 0; i < binding.length; i++) { | ||
console.log('child', 'binding.get(' + i + ')', binding.get(i)); | ||
} | ||
} | ||
|