Skip to content

Commit 1db290d

Browse files
committed
Initial Commit
1 parent e784aed commit 1db290d

File tree

4 files changed

+150
-0
lines changed

4 files changed

+150
-0
lines changed

binding.gyp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
'targets': [
3+
{
4+
'target_name': 'sample',
5+
'sources': [
6+
'sample.cc',
7+
],
8+
"include_dirs" : [
9+
"<!(node -e \"require('node-arraybuffer')\")"
10+
]
11+
},
12+
],
13+
}

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"dependencies": {
3+
"node-arraybuffer": "^1.0.0"
4+
}
5+
}

sample.cc

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include <string>
2+
#include <node.h>
3+
#include "ArrayBuffer.h"
4+
5+
using namespace v8;
6+
7+
void StringArrayBuffer(const v8::FunctionCallbackInfo<v8::Value>& args) {
8+
Isolate *isolate = args.GetIsolate();
9+
10+
const char *str = "!!!EHLO WORLD!!!";
11+
node::ArrayBuffer *buffer = node::ArrayBuffer::New(isolate, str);
12+
13+
args.GetReturnValue().Set(buffer->ToArrayBuffer());
14+
}
15+
16+
void CreateArrayBuffer(const v8::FunctionCallbackInfo<v8::Value>& args) {
17+
Isolate *isolate = args.GetIsolate();
18+
19+
std::string str = "Hello World!";
20+
node::ArrayBuffer *buffer = node::ArrayBuffer::New(isolate, str);
21+
22+
args.GetReturnValue().Set(buffer->ToArrayBuffer());
23+
}
24+
25+
void PrintWrapped(const v8::FunctionCallbackInfo<v8::Value>& args) {
26+
Isolate *isolate = args.GetIsolate();
27+
28+
if (args[0]->IsArrayBuffer() || args[0]->IsTypedArray()) {
29+
Local<ArrayBuffer> arraybuffer;
30+
31+
if (args[0]->IsArrayBuffer()) {
32+
arraybuffer = Local<ArrayBuffer>::Cast(args[0]);
33+
} else {
34+
Local<ArrayBufferView> view = Local<ArrayBufferView>::Cast(args[0]);
35+
arraybuffer = view->Buffer();
36+
}
37+
38+
if (!arraybuffer.IsEmpty()) {
39+
node::ArrayBuffer *buffer = node::ArrayBuffer::New(isolate, arraybuffer);
40+
std::string str = buffer->Unwrap<std::string>();
41+
42+
printf("Wrapped: '%s', %zu\n", str.data(), str.length());
43+
}
44+
}
45+
}
46+
47+
void PrintArrayBuffer(const v8::FunctionCallbackInfo<v8::Value>& args) {
48+
Isolate *isolate = args.GetIsolate();
49+
50+
if (args[0]->IsArrayBuffer() || args[0]->IsTypedArray()) {
51+
Local<ArrayBuffer> arraybuffer;
52+
53+
if (args[0]->IsArrayBuffer()) {
54+
arraybuffer = Local<ArrayBuffer>::Cast(args[0]);
55+
} else {
56+
Local<ArrayBufferView> view = Local<ArrayBufferView>::Cast(args[0]);
57+
arraybuffer = view->Buffer();
58+
}
59+
60+
if (!arraybuffer.IsEmpty()) {
61+
node::ArrayBuffer *buffer = node::ArrayBuffer::New(isolate, arraybuffer);
62+
63+
printf("Buffer Size: '%s', %zu\n", buffer->String(), buffer->Length());
64+
}
65+
}
66+
}
67+
68+
void DisposeMemory(const v8::FunctionCallbackInfo<v8::Value>& args) {
69+
Isolate *isolate = args.GetIsolate();
70+
71+
while(!isolate->IdleNotification(1000));
72+
}
73+
74+
void SampleInit(Handle<Object> exports) {
75+
Isolate* isolate = Isolate::GetCurrent();
76+
HandleScope scope(isolate);
77+
78+
exports->Set(String::NewFromUtf8(isolate, "StringArrayBuffer"),
79+
FunctionTemplate::New(isolate, StringArrayBuffer)->GetFunction());
80+
81+
exports->Set(String::NewFromUtf8(isolate, "CreateArrayBuffer"),
82+
FunctionTemplate::New(isolate, CreateArrayBuffer)->GetFunction());
83+
84+
exports->Set(String::NewFromUtf8(isolate, "PrintWrapped"),
85+
FunctionTemplate::New(isolate, PrintWrapped)->GetFunction());
86+
87+
exports->Set(String::NewFromUtf8(isolate, "PrintArrayBuffer"),
88+
FunctionTemplate::New(isolate, PrintArrayBuffer)->GetFunction());
89+
90+
exports->Set(String::NewFromUtf8(isolate, "DisposeMemory"),
91+
FunctionTemplate::New(isolate, DisposeMemory)->GetFunction());
92+
}
93+
94+
NODE_MODULE(sample, SampleInit);

sample.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
var sample = require('./build/Release/sample.node');
2+
3+
function PrintJS(arrayBuffer) {
4+
var buffer = new Buffer(new Uint8Array(arrayBuffer));
5+
6+
console.log('JS:', "'" + buffer.toString('utf8') + "'", Buffer.byteLength(buffer, 'utf8'));
7+
}
8+
9+
function Print(arrayBuffer) {
10+
if (!arrayBuffer) {
11+
var buf = new Buffer(5);
12+
buf.write('HELLO', 0);
13+
14+
var arrayBuffer = new Uint8Array(buf).buffer;
15+
}
16+
17+
sample.PrintArrayBuffer(arrayBuffer);
18+
PrintJS(arrayBuffer);
19+
}
20+
21+
function Create() {
22+
var arrayBuffer = sample.CreateArrayBuffer();
23+
Print(arrayBuffer);
24+
sample.PrintWrapped(arrayBuffer);
25+
}
26+
27+
function String() {
28+
var arrayBuffer = sample.StringArrayBuffer();
29+
Print(arrayBuffer);
30+
}
31+
32+
for (var index = 0; index < 100; index++) {
33+
Print();
34+
Create();
35+
String();
36+
}
37+
38+
sample.DisposeMemory();

0 commit comments

Comments
 (0)