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

Conversation

alexahdp
Copy link
Contributor

@alexahdp alexahdp commented Nov 6, 2019

  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows [commit guidelines]

Fixes: #29955

@nodejs-github-bot nodejs-github-bot added the c++ Issues and PRs that require attention from people who are familiar with C++. label Nov 6, 2019
}

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.

@BridgeAR BridgeAR added the code-and-learn Issues related to the Code-and-Learn events and PRs submitted during the events. label Nov 6, 2019
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.

@legendecas legendecas added the node-api Issues and PRs related to the Node-API. label Nov 7, 2019
@@ -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.

Copy link
Contributor

@gabrielschulhof gabrielschulhof left a 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);
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.

}

return GET_RETURN_STATUS(env);
}
Copy link
Member

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);
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)


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

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

@Trott
Copy link
Member

Trott commented Nov 23, 2019

@alexahdp Are you able to add changes to address the comments?

@alexahdp
Copy link
Contributor Author

I don't think so. I'll close this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. code-and-learn Issues related to the Code-and-Learn events and PRs submitted during the events. node-api Issues and PRs related to the Node-API.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

n-api: add napi_is_detached_arraybuffer()