Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions typed_array_to_native/node-addon-api/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"targets": [
{
"target_name": "typed_array_to_native",
"cflags!": [ "-fno-exceptions" ],
"cflags_cc!": [ "-fno-exceptions" ],
"sources": [ "typed_array_to_native.cc" ],
"include_dirs": [
"<!@(node -p \"require('node-addon-api').include\")"
],
'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ],
}
]
}
15 changes: 15 additions & 0 deletions typed_array_to_native/node-addon-api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const binding = require('bindings')('typed_array_to_native');

{
const byteArray = Uint8Array.from([10, 20, 30, 50, 25, 17]);
console.log("byteArray: ", byteArray);
binding.AcceptByteArray(byteArray);
}
console.log("---------");

{
const array = [7, 15, 26, 58, 64];
console.log("array: ", array);
const byteArray = binding.CreateByteArray(array);
console.log("byteArray: ", byteArray);
}
14 changes: 14 additions & 0 deletions typed_array_to_native/node-addon-api/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "typed_array_to_native",
"version": "0.0.0",
"description": "Shows how to read and create typed arrays",
"main": "index.js",
"private": true,
"dependencies": {
"bindings": "~1.2.1",
"node-addon-api": "^1.0.0"
},
"scripts": {
"test": "node index.js"
}
}
108 changes: 108 additions & 0 deletions typed_array_to_native/node-addon-api/typed_array_to_native.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#include <napi.h>
#include <cstdio>

static Napi::Value AcceptByteArray(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].IsTypedArray()) {
Napi::Error::New(info.Env(), "Expected a TypedArray")
.ThrowAsJavaScriptException();
return info.Env().Undefined();
}
Napi::TypedArray typedArray = info[0].As<Napi::TypedArray>();

if (typedArray.TypedArrayType() != napi_uint8_array) {
Napi::Error::New(info.Env(), "Expected an Uint8Array")
.ThrowAsJavaScriptException();
return info.Env().Undefined();
}
Napi::Uint8Array uint8Array = typedArray.As<Napi::Uint8Array>();

// Copy to std::vector<uint8_t>:
std::vector<uint8_t> bytes(uint8Array.Data(),
uint8Array.Data() + uint8Array.ElementLength());
printf("std::vector<uint8_t> from Uint8Array: [");
for (uint8_t byte : bytes) {
printf("%d, ", byte);
}
printf("\b\b]\n");

return info.Env().Undefined();
}

static Napi::Value CreateByteArray(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].IsArray()) {
Napi::Error::New(info.Env(), "Expected an Array")
.ThrowAsJavaScriptException();
return info.Env().Undefined();
}
Napi::Array array = info[0].As<Napi::Array>();
size_t arrayLength = array.Length();

// Create std::vector<uint8_t> out of array.
// We allocate it on the heap to allow wrapping it up into ArrayBuffer.
std::unique_ptr<std::vector<uint8_t>> nativeArray =
std::make_unique<std::vector<uint8_t>>(arrayLength, 0);
for (size_t i = 0; i < arrayLength; ++i) {
Napi::Value arrayItem = array[i];
if (!arrayItem.IsNumber()) {
Napi::Error::New(info.Env(), "Expected a Number as Array Item")
.ThrowAsJavaScriptException();
return info.Env().Undefined();
}
Napi::Number number = arrayItem.As<Napi::Number>();
double numberValue = number.DoubleValue();
if (numberValue < 0 || numberValue > 255) {
Napi::Error::New(info.Env(),
"Array Item Number value is out of range [0..255]")
.ThrowAsJavaScriptException();
return info.Env().Undefined();
}

(*nativeArray)[i] = static_cast<uint8_t>(numberValue);
}

printf("std::vector<uint8_t> from Array: [");
for (uint8_t byte : *nativeArray) {
printf("%d, ", byte);
}
printf("\b\b]\n");

// Wrap up the std::vector into the ArrayBuffer.
// Note: instead of wrapping the std::vector we could allow ArrayBuffer to
// create internal storage that copies the std::vector, but it is less
// efficient because it requires an extra memory allocation.
Napi::ArrayBuffer arrayBuffer = Napi::ArrayBuffer::New(
info.Env(),
nativeArray->data(),
arrayLength /* size in bytes */,
[](Napi::Env /*env*/, void* /*data*/, std::vector<uint8_t>* hint) {
std::unique_ptr<std::vector<uint8_t>> vectorPtrToDelete(hint);
},
nativeArray.get());
// The finalizer is responsible for deleting the vector: release the
// unique_ptr ownership.
nativeArray.release();

Napi::Uint8Array byteArray = Napi::Uint8Array::New(info.Env(), arrayLength, arrayBuffer, 0);

return byteArray;
}

static Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports["AcceptByteArray"] = Napi::Function::New(env, AcceptByteArray);
exports["CreateByteArray"] = Napi::Function::New(env, CreateByteArray);
return exports;
}

NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init)