Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
24b927a
build: allow clang 10+ in configure.py
krytarowski Sep 13, 2019
145dcc2
build: move doc versions JSON file out of out/doc
richardlau Apr 8, 2020
5436569
test: flaky test-stdout-close-catch on freebsd
sam-github Apr 14, 2020
9915774
build: log detected compilers in --verbose mode
richardlau Apr 8, 2020
89a306b
deps: fix V8 compiler error with clang++-11
sam-github Apr 27, 2020
3acc89f
deps: V8: backport cd21f71f9cb5
targos Jun 13, 2020
aaf2f82
inspector: more conservative minimum stack size
bnoordhuis May 24, 2019
ef9413b
deps: upgrade openssl sources to 1.1.1f
hassaanp Mar 31, 2020
94702c1
deps: upgrade openssl sources to 1.1.1g
hassaanp Apr 21, 2020
745b329
deps: update archs files for OpenSSL-1.1.1g
hassaanp Apr 21, 2020
74b00cc
tls: allow empty subject even with altNames defined
jasonmacgowan Sep 17, 2018
193d1d0
doc: document fs.watchFile() bigint option
cjihrig Mar 6, 2020
3dbd8cd
Revert "test: mark empty udp tests flaky on OS X"
lpinca Mar 25, 2020
961598b
n-api: add `napi_detach_arraybuffer`
legendecas Apr 25, 2020
b744ffd
n-api: implement napi_is_detached_arraybuffer
lundibundi Nov 23, 2019
5dab101
doc,n-api: mark napi_detach_arraybuffer as experimental
legendecas Nov 28, 2019
069b6e1
http: disable headersTimeout check when set to zero
ShogunPanda Apr 29, 2020
84fca3c
deps: upgrade npm to 6.14.5
ruyadorno May 4, 2020
97b5952
deps: upgrade npm to 6.14.6
claudiahdz Jul 7, 2020
7a109fe
test: remove timers-blocking-callback
Fishrock123 Apr 15, 2020
00f04e3
doc: fix quotes in tls.md
sparsh-99 May 29, 2020
c5215d0
2020-07-21, Version 10.22.0 'Dubnium' (LTS)
richardlau Jul 2, 2020
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
Prev Previous commit
Next Next commit
n-api: implement napi_is_detached_arraybuffer
This implements ArrayBuffer#IsDetachedBuffer operation as per ECMAScript
specification Section 24.1.1.2 https://tc39.es/ecma262/#sec-isdetachedbuffer

Closes: #29955

PR-URL: #30613
Backport-PR-URL: #33061
Fixes: #29955
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: David Carlier <devnexen@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
  • Loading branch information
lundibundi authored and richardlau committed Jul 1, 2020
commit b744ffd586ce956d0a6e23bb9ade7cd77fd7ea97
26 changes: 26 additions & 0 deletions doc/api/n-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2972,6 +2972,31 @@ that is, created with [`napi_create_external_arraybuffer`][].
This API represents the invocation of the `ArrayBuffer` detach operation as
defined in [Section 24.1.1.3][] of the ECMAScript Language Specification.

### napi_is_detached_arraybuffer
<!-- YAML
added: REPLACEME
-->

> Stability: 1 - Experimental

```C
napi_status napi_is_detached_arraybuffer(napi_env env,
napi_value arraybuffer,
bool* result)
```

* `[in] env`: The environment that the API is invoked under.
* `[in] arraybuffer`: The JavaScript `ArrayBuffer` to be checked.
* `[out] result`: Whether the `arraybuffer` is detached.

Returns `napi_ok` if the API succeeded.

The `ArrayBuffer` is considered detached if its internal data is `null`.

This API represents the invocation of the `ArrayBuffer` `IsDetachedBuffer`
operation as defined in [Section 24.1.1.2][] of the ECMAScript Language
Specification.

## Working with JavaScript Properties

N-API exposes a set of APIs to get and set properties on JavaScript
Expand Down Expand Up @@ -4924,6 +4949,7 @@ This API may only be called from the main thread.
[Section 8.7]: https://tc39.es/ecma262/#sec-agents
[Section 9.1.6]: https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-defineownproperty-p-desc
[Working with JavaScript Functions]: #n_api_working_with_javascript_functions
[Section 24.1.1.2]: https://tc39.es/ecma262/#sec-isdetachedbuffer
[Working with JavaScript Properties]: #n_api_working_with_javascript_properties
[Working with JavaScript Values - Abstract Operations]: #n_api_working_with_javascript_values_abstract_operations
[Working with JavaScript Values]: #n_api_working_with_javascript_values
Expand Down
15 changes: 15 additions & 0 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4403,3 +4403,18 @@ napi_status napi_detach_arraybuffer(napi_env env, napi_value arraybuffer) {

return napi_clear_last_error(env);
}

napi_status napi_is_detached_arraybuffer(napi_env env,
napi_value arraybuffer,
bool* result) {
CHECK_ENV(env);
CHECK_ARG(env, arraybuffer);
CHECK_ARG(env, result);

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

*result = value->IsArrayBuffer() &&
value.As<v8::ArrayBuffer>()->GetContents().Data() == nullptr;

return napi_clear_last_error(env);
}
5 changes: 5 additions & 0 deletions src/node_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,11 @@ napi_get_all_property_names(napi_env env,
NAPI_EXTERN napi_status
napi_detach_arraybuffer(napi_env env,
napi_value arraybuffer);

NAPI_EXTERN napi_status
napi_is_detached_arraybuffer(napi_env env,
napi_value value,
bool* result);
#endif // NAPI_EXPERIMENTAL

EXTERN_C_END
Expand Down
13 changes: 13 additions & 0 deletions test/addons-napi/test_typedarray/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,21 @@ arrayTypes.forEach((currentType) => {
assert.ok(externalResult instanceof Int8Array);
assert.strictEqual(externalResult.length, 3);
assert.strictEqual(externalResult.byteLength, 3);
assert.ok(!test_typedarray.IsDetached(buffer.buffer));
test_typedarray.Detach(buffer);
assert.ok(test_typedarray.IsDetached(buffer.buffer));
assert.ok(externalResult instanceof Int8Array);
assert.strictEqual(buffer.length, 0);
assert.strictEqual(buffer.byteLength, 0);
}

{
const buffer = new ArrayBuffer(128);
assert.ok(!test_typedarray.IsDetached(buffer));
}

{
const buffer = test_typedarray.NullArrayBuffer();
assert.ok(buffer instanceof ArrayBuffer);
assert.ok(test_typedarray.IsDetached(buffer));
}
32 changes: 32 additions & 0 deletions test/addons-napi/test_typedarray/test_typedarray.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ static napi_value External(napi_env env, napi_callback_info info) {
return output_array;
}


static napi_value NullArrayBuffer(napi_env env, napi_callback_info info) {
static void* data = NULL;
napi_value arraybuffer;
NAPI_CALL(env,
napi_create_external_arraybuffer(env, data, 0, NULL, NULL, &arraybuffer));
return arraybuffer;
}

static napi_value CreateTypedArray(napi_env env, napi_callback_info info) {
size_t argc = 4;
napi_value args[4];
Expand Down Expand Up @@ -183,12 +192,35 @@ static napi_value Detach(napi_env env, napi_callback_info info) {
return NULL;
}

static napi_value IsDetached(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value args[1];
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
NAPI_ASSERT(env, argc == 1, "Wrong number of arguments.");

napi_value array_buffer = args[0];
bool is_arraybuffer;
NAPI_CALL(env, napi_is_arraybuffer(env, array_buffer, &is_arraybuffer));
NAPI_ASSERT(env, is_arraybuffer,
"Wrong type of arguments. Expects an array buffer as first argument.");

bool is_detached;
NAPI_CALL(env, napi_is_detached_arraybuffer(env, array_buffer, &is_detached));

napi_value result;
NAPI_CALL(env, napi_get_boolean(env, is_detached, &result));

return result;
}

static napi_value Init(napi_env env, napi_value exports) {
napi_property_descriptor descriptors[] = {
DECLARE_NAPI_PROPERTY("Multiply", Multiply),
DECLARE_NAPI_PROPERTY("External", External),
DECLARE_NAPI_PROPERTY("NullArrayBuffer", NullArrayBuffer),
DECLARE_NAPI_PROPERTY("CreateTypedArray", CreateTypedArray),
DECLARE_NAPI_PROPERTY("Detach", Detach),
DECLARE_NAPI_PROPERTY("IsDetached", IsDetached),
};

NAPI_CALL(env, napi_define_properties(
Expand Down