From cbaf295a186b1c8dd6d9cf42e9407e00a80b5c16 Mon Sep 17 00:00:00 2001 From: orange4glace Date: Fri, 5 Oct 2018 22:49:52 +0900 Subject: [PATCH 1/6] N-API SharedArrayBuffer api --- src/node_api.cc | 85 +++++++++++++++++++++++++++++++++++++++++++++++++ src/node_api.h | 18 +++++++++++ 2 files changed, 103 insertions(+) diff --git a/src/node_api.cc b/src/node_api.cc index 83eef0ff2eea47..3fd738db1ff0f9 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -3571,6 +3571,91 @@ napi_status napi_get_arraybuffer_info(napi_env env, return napi_clear_last_error(env); } +napi_status napi_is_sharedarraybuffer(napi_env env, napi_value value, bool* result) { + CHECK_ENV(env); + CHECK_ARG(env, value); + CHECK_ARG(env, result); + + v8::Local val = v8impl::V8LocalValueFromJsValue(value); + *result = val->IsSharedArrayBuffer(); + + return napi_clear_last_error(env); +} + +napi_status napi_create_sharedarraybuffer(napi_env env, + size_t byte_length, + void** data, + napi_value* result) { + NAPI_PREAMBLE(env); + CHECK_ARG(env, result); + + v8::Isolate* isolate = env->isolate; + v8::Local buffer = + v8::SharedArrayBuffer::New(isolate, byte_length); + + // Optionally return a pointer to the buffer's data, to avoid another call to + // retrieve it. + if (data != nullptr) { + *data = buffer->GetContents().Data(); + } + + *result = v8impl::JsValueFromV8LocalValue(buffer); + return GET_RETURN_STATUS(env); +} + +napi_status napi_create_external_sharedarraybuffer(napi_env env, + void* external_data, + size_t byte_length, + napi_finalize finalize_cb, + void* finalize_hint, + napi_value* result) { + NAPI_PREAMBLE(env); + CHECK_ARG(env, result); + + v8::Isolate* isolate = env->isolate; + v8::Local buffer = + v8::SharedArrayBuffer::New(isolate, external_data, byte_length); + + if (finalize_cb != nullptr) { + // Create a self-deleting weak reference that invokes the finalizer + // callback. + v8impl::Reference::New(env, + buffer, + 0, + true, + finalize_cb, + external_data, + finalize_hint); + } + + *result = v8impl::JsValueFromV8LocalValue(buffer); + return GET_RETURN_STATUS(env); +} + +napi_status napi_get_sharedarraybuffer_info(napi_env env, + napi_value arraybuffer, + void** data, + size_t* byte_length) { + CHECK_ENV(env); + CHECK_ARG(env, arraybuffer); + + v8::Local value = v8impl::V8LocalValueFromJsValue(arraybuffer); + RETURN_STATUS_IF_FALSE(env, value->IsArrayBuffer(), napi_invalid_arg); + + v8::ArrayBuffer::Contents contents = + value.As()->GetContents(); + + if (data != nullptr) { + *data = contents.Data(); + } + + if (byte_length != nullptr) { + *byte_length = contents.ByteLength(); + } + + return napi_clear_last_error(env); +} + napi_status napi_is_typedarray(napi_env env, napi_value value, bool* result) { CHECK_ENV(env); CHECK_ARG(env, value); diff --git a/src/node_api.h b/src/node_api.h index 10a2c8ff3098be..8c8e938d7f756b 100644 --- a/src/node_api.h +++ b/src/node_api.h @@ -507,6 +507,24 @@ NAPI_EXTERN napi_status napi_get_arraybuffer_info(napi_env env, napi_value arraybuffer, void** data, size_t* byte_length); +NAPI_EXTERN napi_status napi_is_sharedarraybuffer(napi_env env, + napi_value value, + bool* result); +NAPI_EXTERN napi_status napi_create_sharedarraybuffer(napi_env env, + size_t byte_length, + void** data, + napi_value* result); +NAPI_EXTERN napi_status +napi_create_external_sharedarraybuffer(napi_env env, + void* external_data, + size_t byte_length, + napi_finalize finalize_cb, + void* finalize_hint, + napi_value* result); +NAPI_EXTERN napi_status napi_get_sharedarraybuffer_info(napi_env env, + napi_value arraybuffer, + void** data, + size_t* byte_length); NAPI_EXTERN napi_status napi_is_typedarray(napi_env env, napi_value value, bool* result); From 9bc56ed53ef97660c6f761ab53eccfcbbee5956b Mon Sep 17 00:00:00 2001 From: orange4glace Date: Fri, 5 Oct 2018 22:55:02 +0900 Subject: [PATCH 2/6] N-API SharedArrayBuffer api --- src/node_api.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node_api.cc b/src/node_api.cc index 3fd738db1ff0f9..23efe167ccf6ad 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -3640,7 +3640,7 @@ napi_status napi_get_sharedarraybuffer_info(napi_env env, CHECK_ARG(env, arraybuffer); v8::Local value = v8impl::V8LocalValueFromJsValue(arraybuffer); - RETURN_STATUS_IF_FALSE(env, value->IsArrayBuffer(), napi_invalid_arg); + RETURN_STATUS_IF_FALSE(env, value->IsSharedArrayBuffer(), napi_invalid_arg); v8::ArrayBuffer::Contents contents = value.As()->GetContents(); From c3081063193840aaedf53f2795cebf3c87735ea9 Mon Sep 17 00:00:00 2001 From: orange4glace Date: Sun, 18 Nov 2018 14:42:12 +0900 Subject: [PATCH 3/6] [api] Add : SharedArrayBuffer Experimental API --- src/js_native_api.h | 30 +++++++++++++++ src/js_native_api_v8.cc | 85 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) diff --git a/src/js_native_api.h b/src/js_native_api.h index 01707d78ccaa46..ca19503d48e2d9 100644 --- a/src/js_native_api.h +++ b/src/js_native_api.h @@ -478,6 +478,36 @@ NAPI_EXTERN napi_status napi_add_finalizer(napi_env env, napi_finalize finalize_cb, void* finalize_hint, napi_ref* result); + + +NAPI_EXTERN napi_status napi_is_arraybuffer(napi_env env, + napi_value value, + bool* result); +NAPI_EXTERN napi_status napi_create_arraybuffer(napi_env env, + size_t byte_length, + void** data, + napi_value* result); + +// Methods to work with shared array buffers +NAPI_EXTERN napi_status napi_is_sharedarraybuffer(napi_env env, + napi_value value, + bool* result); +NAPI_EXTERN napi_status napi_create_sharedarraybuffer(napi_env env, + size_t byte_length, + void** data, + napi_value* result); +NAPI_EXTERN napi_status +napi_create_external_sharedarraybuffer(napi_env env, + void* external_data, + size_t byte_length, + napi_finalize finalize_cb, + void* finalize_hint, + napi_value* result); +NAPI_EXTERN napi_status napi_get_sharedarraybuffer_info(napi_env env, + napi_value sharedarraybuffer, + void** data, + size_t* byte_length); + #endif // NAPI_EXPERIMENTAL EXTERN_C_END diff --git a/src/js_native_api_v8.cc b/src/js_native_api_v8.cc index b28376afb7cedd..8876b3fa31c8f5 100644 --- a/src/js_native_api_v8.cc +++ b/src/js_native_api_v8.cc @@ -2587,6 +2587,91 @@ napi_status napi_get_arraybuffer_info(napi_env env, return napi_clear_last_error(env); } +napi_status napi_is_sharedarraybuffer(napi_env env, napi_value value, bool* result) { + CHECK_ENV(env); + CHECK_ARG(env, value); + CHECK_ARG(env, result); + + v8::Local val = v8impl::V8LocalValueFromJsValue(value); + *result = val->IsSharedArrayBuffer(); + + return napi_clear_last_error(env); +} + +napi_status napi_create_sharedarraybuffer(napi_env env, + size_t byte_length, + void** data, + napi_value* result) { + NAPI_PREAMBLE(env); + CHECK_ARG(env, result); + + v8::Isolate* isolate = env->isolate; + v8::Local buffer = + v8::SharedArrayBuffer::New(isolate, byte_length); + + // Optionally return a pointer to the buffer's data, to avoid another call to + // retrieve it. + if (data != nullptr) { + *data = buffer->GetContents().Data(); + } + + *result = v8impl::JsValueFromV8LocalValue(buffer); + return GET_RETURN_STATUS(env); +} + +napi_status napi_create_external_arraybuffer(napi_env env, + void* external_data, + size_t byte_length, + napi_finalize finalize_cb, + void* finalize_hint, + napi_value* result) { + NAPI_PREAMBLE(env); + CHECK_ARG(env, result); + + v8::Isolate* isolate = env->isolate; + v8::Local buffer = + v8::SharedArrayBuffer::New(isolate, external_data, byte_length); + + if (finalize_cb != nullptr) { + // Create a self-deleting weak reference that invokes the finalizer + // callback. + v8impl::Reference::New(env, + buffer, + 0, + true, + finalize_cb, + external_data, + finalize_hint); + } + + *result = v8impl::JsValueFromV8LocalValue(buffer); + return GET_RETURN_STATUS(env); +} + +napi_status napi_get_sharedarraybuffer_info(napi_env env, + napi_value sharedarraybuffer, + void** data, + size_t* byte_length) { + CHECK_ENV(env); + CHECK_ARG(env, sharedarraybuffer); + + v8::Local value = v8impl::V8LocalValueFromJsValue(sharedarraybuffer); + RETURN_STATUS_IF_FALSE(env, value->IsSharedArrayBuffer(), napi_invalid_arg); + + v8::SharedArrayBuffer::Contents contents = + value.As()->GetContents(); + + if (data != nullptr) { + *data = contents.Data(); + } + + if (byte_length != nullptr) { + *byte_length = contents.ByteLength(); + } + + return napi_clear_last_error(env); +} + napi_status napi_is_typedarray(napi_env env, napi_value value, bool* result) { CHECK_ENV(env); CHECK_ARG(env, value); From a14869ab9f5966f8baf15ab2e9c214692d82f86c Mon Sep 17 00:00:00 2001 From: orange4glace Date: Sun, 18 Nov 2018 15:01:19 +0900 Subject: [PATCH 4/6] [api] deleted : duplicated declarations --- src/js_native_api.h | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/js_native_api.h b/src/js_native_api.h index ca19503d48e2d9..d4db381bac16c7 100644 --- a/src/js_native_api.h +++ b/src/js_native_api.h @@ -479,15 +479,6 @@ NAPI_EXTERN napi_status napi_add_finalizer(napi_env env, void* finalize_hint, napi_ref* result); - -NAPI_EXTERN napi_status napi_is_arraybuffer(napi_env env, - napi_value value, - bool* result); -NAPI_EXTERN napi_status napi_create_arraybuffer(napi_env env, - size_t byte_length, - void** data, - napi_value* result); - // Methods to work with shared array buffers NAPI_EXTERN napi_status napi_is_sharedarraybuffer(napi_env env, napi_value value, From 9965e786a83ad4e36ab6a72558f32e77249ed937 Mon Sep 17 00:00:00 2001 From: orange4glace Date: Sun, 18 Nov 2018 15:03:03 +0900 Subject: [PATCH 5/6] [api] fixed : type error --- src/js_native_api_v8.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/js_native_api_v8.cc b/src/js_native_api_v8.cc index 8876b3fa31c8f5..86379934e4619c 100644 --- a/src/js_native_api_v8.cc +++ b/src/js_native_api_v8.cc @@ -2606,7 +2606,7 @@ napi_status napi_create_sharedarraybuffer(napi_env env, CHECK_ARG(env, result); v8::Isolate* isolate = env->isolate; - v8::Local buffer = + v8::Local buffer = v8::SharedArrayBuffer::New(isolate, byte_length); // Optionally return a pointer to the buffer's data, to avoid another call to From 2d96cc28862bf6ea0625b54cb3c951a2fd08d9f1 Mon Sep 17 00:00:00 2001 From: orange4glace Date: Sun, 18 Nov 2018 15:30:30 +0900 Subject: [PATCH 6/6] [api] fixed: typo --- src/js_native_api_v8.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/js_native_api_v8.cc b/src/js_native_api_v8.cc index 86379934e4619c..ee8e0543d98f76 100644 --- a/src/js_native_api_v8.cc +++ b/src/js_native_api_v8.cc @@ -2619,7 +2619,7 @@ napi_status napi_create_sharedarraybuffer(napi_env env, return GET_RETURN_STATUS(env); } -napi_status napi_create_external_arraybuffer(napi_env env, +napi_status napi_create_external_sharedarraybuffer(napi_env env, void* external_data, size_t byte_length, napi_finalize finalize_cb,