Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

N-API SharedArrayBuffer api #23279

Closed
wants to merge 7 commits into from
Closed
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
21 changes: 21 additions & 0 deletions src/js_native_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,27 @@ NAPI_EXTERN napi_status napi_add_finalizer(napi_env env,
napi_finalize finalize_cb,
void* finalize_hint,
napi_ref* 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
Expand Down
85 changes: 85 additions & 0 deletions src/js_native_api_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<v8::Value> 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<v8::SharedArrayBuffer> 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<v8::SharedArrayBuffer> 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<v8::Value> value = v8impl::V8LocalValueFromJsValue(sharedarraybuffer);
RETURN_STATUS_IF_FALSE(env, value->IsSharedArrayBuffer(), napi_invalid_arg);

v8::SharedArrayBuffer::Contents contents =
value.As<v8::SharedArrayBuffer>()->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);
Expand Down