-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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
Conversation
} | ||
|
||
if (!val->IsArrayBuffer()) { | ||
*result = v8impl::JsValueFromV8LocalValue(v8::False(isolate)); |
There was a problem hiding this comment.
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.
v8::Local<v8::Value> val = v8impl::V8LocalValueFromJsValue(value); | ||
|
||
if (!val->IsObject()) { | ||
napi_throw_type_error(env, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
@@ -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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aside from the existing comments, the PR also needs tests and documentation.
napi_status napi_is_detached_arraybuffer(napi_env env, | ||
napi_value value, | ||
napi_value* result) { | ||
NAPI_PREAMBLE(env); |
There was a problem hiding this comment.
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 Maybe
s.
} | ||
|
||
return GET_RETURN_STATUS(env); | ||
} |
There was a problem hiding this comment.
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.
return GET_RETURN_STATUS(env); | ||
} | ||
|
||
v8::Local<v8::ArrayBuffer> buffer = v8::Local<v8::ArrayBuffer>::Cast(val); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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)
|
||
v8::Local<v8::ArrayBuffer> buffer = v8::Local<v8::ArrayBuffer>::Cast(val); | ||
|
||
if (buffer->GetContents().Data() != nullptr) { |
There was a problem hiding this comment.
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 ArrayBuffer
s 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
@alexahdp Are you able to add changes to address the comments? |
I don't think so. I'll close this PR. |
make -j4 test
(UNIX), orvcbuild test
(Windows) passesFixes: #29955