-
Notifications
You must be signed in to change notification settings - Fork 598
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
src: example passing an ArrayBuffer to native func
PR-URL: #93 Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: NickNaso <nicoladelgobbo@gmail.com>
- Loading branch information
Showing
4 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
array_buffer_to_native/node-addon-api/array_buffer_to_native.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,35 @@ | ||
#include <stdio.h> | ||
#include <napi.h> | ||
|
||
static void ArrayConsumer(const int32_t* array, size_t length) { | ||
for (size_t index = 0; index < length; index++) { | ||
fprintf(stderr, "array[%lu] = %d\n", index, array[index]); | ||
} | ||
} | ||
|
||
static Napi::Value AcceptArrayBuffer(const Napi::CallbackInfo& info) { | ||
if (info.Length() != 1) { | ||
Napi::Error::New(info.Env(), "Expected exactly one argument") | ||
.ThrowAsJavaScriptException(); | ||
return info.Env().Undefined(); | ||
} | ||
if (!info[0].IsArrayBuffer()) { | ||
Napi::Error::New(info.Env(), "Expected an ArrayBuffer") | ||
.ThrowAsJavaScriptException(); | ||
return info.Env().Undefined(); | ||
} | ||
|
||
Napi::ArrayBuffer buf = info[0].As<Napi::ArrayBuffer>(); | ||
|
||
ArrayConsumer(reinterpret_cast<int32_t*>(buf.Data()), | ||
buf.ByteLength()/sizeof(int32_t)); | ||
|
||
return info.Env().Undefined(); | ||
} | ||
|
||
static Napi::Object Init(Napi::Env env, Napi::Object exports) { | ||
exports["AcceptArrayBuffer"] = Napi::Function::New(env, AcceptArrayBuffer); | ||
return exports; | ||
} | ||
|
||
NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init) |
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,14 @@ | ||
{ | ||
"targets": [ | ||
{ | ||
"target_name": "array_buffer_to_native", | ||
"cflags!": [ "-fno-exceptions" ], | ||
"cflags_cc!": [ "-fno-exceptions" ], | ||
"sources": [ "array_buffer_to_native.cc" ], | ||
"include_dirs": [ | ||
"<!@(node -p \"require('node-addon-api').include\")" | ||
], | ||
'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ], | ||
} | ||
] | ||
} |
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,15 @@ | ||
const binding = require('bindings')('array_buffer_to_native'); | ||
const array = new Int32Array(10); | ||
|
||
array[0] = 19; | ||
array[1] = -41; | ||
array[2] = 98; | ||
array[3] = -922; | ||
array[4] = 587; | ||
array[5] = 12; | ||
array[6] = 221; | ||
array[7] = 49; | ||
array[8] = -96; | ||
array[9] = -1; | ||
|
||
binding.AcceptArrayBuffer(array.buffer); |
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,14 @@ | ||
{ | ||
"name": "array_buffer_to_native", | ||
"version": "0.0.0", | ||
"description": "Fill Out a Native Array in JavaScript", | ||
"main": "index.js", | ||
"private": true, | ||
"dependencies": { | ||
"bindings": "~1.2.1", | ||
"node-addon-api": "^1.0.0" | ||
}, | ||
"scripts": { | ||
"test": "node index.js" | ||
} | ||
} |