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 2 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
85 changes: 85 additions & 0 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove this one as it isn't something that we can expect other engines to be able to implement

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@devsnek Just for context, we also have napi_create_external_arraybuffer

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@addaleax then let's not make the same mistake twice?

Copy link
Author

@orange4glace orange4glace Oct 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the other engines do not support external arraybuffer and n-api has already some mis-design related to external-arraybuffer api things?

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 arraybuffer,
void** data,
size_t* byte_length) {
CHECK_ENV(env);
CHECK_ARG(env, arraybuffer);

v8::Local<v8::Value> value = v8impl::V8LocalValueFromJsValue(arraybuffer);
RETURN_STATUS_IF_FALSE(env, value->IsSharedArrayBuffer(), napi_invalid_arg);

v8::ArrayBuffer::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
18 changes: 18 additions & 0 deletions src/node_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down