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

Added napi_is_detached_arraybuffer method #30317

Closed
wants to merge 1 commit 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
3 changes: 3 additions & 0 deletions src/js_native_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,9 @@ NAPI_EXTERN napi_status napi_create_arraybuffer(napi_env env,
size_t byte_length,
void** data,
napi_value* result);
NAPI_EXTERN napi_status napi_is_detached_arraybuffer(napi_env env,
napi_value value,
bool* result);
Copy link
Member

Choose a reason for hiding this comment

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

This needs to go in the #ifdef NAPI_EXPERIMENTAL section to start.

NAPI_EXTERN napi_status
napi_create_external_arraybuffer(napi_env env,
void* external_data,
Expand Down
32 changes: 32 additions & 0 deletions src/js_native_api_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2569,6 +2569,38 @@ napi_status napi_create_arraybuffer(napi_env env,
return GET_RETURN_STATUS(env);
}

napi_status napi_is_detached_arraybuffer(napi_env env,
napi_value value,
napi_value* result) {
NAPI_PREAMBLE(env);
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we need NAPI_PREAMBLE() here, because we don't have any Maybes.

CHECK_ARG(env, result);
v8::Isolate* isolate = env->isolate;

v8::Local<v8::Value> val = v8impl::V8LocalValueFromJsValue(value);

if (!val->IsObject()) {
napi_throw_type_error(env,
Copy link
Member

Choose a reason for hiding this comment

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

No JavaScript exceptions were expected on N-API checks. See discussion at #29849.

Copy link
Member

Choose a reason for hiding this comment

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

You could just remove this check and let the below one handle returning false.

"ERR_NAPI_CONS_FUNCTION",
"Value must be an Object");
GET_RETURN_STATUS(env);
}

if (!val->IsArrayBuffer()) {
*result = v8impl::JsValueFromV8LocalValue(v8::False(isolate));
Copy link
Contributor

@devnexen devnexen Nov 6, 2019

Choose a reason for hiding this comment

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

Overall result setting might be doable with less lines.

return GET_RETURN_STATUS(env);
}

v8::Local<v8::ArrayBuffer> buffer = v8::Local<v8::ArrayBuffer>::Cast(val);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
v8::Local<v8::ArrayBuffer> buffer = v8::Local<v8::ArrayBuffer>::Cast(val);
v8::Local<v8::ArrayBuffer> buffer = val.As<v8::ArrayBuffer>();

(It’s not important, just more common this way in our source tree)


if (buffer->GetContents().Data() != nullptr) {
Copy link
Member

Choose a reason for hiding this comment

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

Is this correct? We can create ArrayBuffers with zero length and nullptr data, and I guess we could consider them effectively detached, but maybe !buffer->IsDetachable() is a better choice?

Also, just fyi, GetContents() is scheduled to be deprecated in favour of GetBackingStore(), see e.g. e66a2ac

*result = v8impl::JsValueFromV8LocalValue(v8::True(isolate));
} else {
*result = v8impl::JsValueFromV8LocalValue(v8::False(isolate));
}

return GET_RETURN_STATUS(env);
}
Copy link
Member

@lundibundi lundibundi Nov 12, 2019

Choose a reason for hiding this comment

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

Perhaps IIUC

  bool is_detached = val->IsArrayBuffer() &&
                     val.As<v8::ArrayBuffer>()->GetContents().Data() != nullptr;
  auto ctor = is_detached ? v8::True : v8::False;
  *result = v8impl::JsValueFromV8LocalValue(ctor(isolate));
  return GET_RETURN_STATUS(env);

Though maybe simple if/else may be better instead of ternary.


napi_status napi_create_external_arraybuffer(napi_env env,
void* external_data,
size_t byte_length,
Expand Down