@@ -3299,6 +3299,10 @@ Specification. |
3299 | 3299 | <!-- YAML |
3300 | 3300 | added: v8.0.0 |
3301 | 3301 | napiVersion: 1 |
| 3302 | +changes: |
| 3303 | + - version: REPLACEME |
| 3304 | + pr-url: https://github.com/nodejs/node/pull/59071 |
| 3305 | + description: Added support for `SharedArrayBuffer`. |
3302 | 3306 | --> |
3303 | 3307 |
|
3304 | 3308 | ```c |
@@ -3309,21 +3313,20 @@ napi_status napi_get_arraybuffer_info(napi_env env, |
3309 | 3313 | ``` |
3310 | 3314 |
|
3311 | 3315 | * `[in] env`: The environment that the API is invoked under. |
3312 | | -* `[in] arraybuffer`: `napi_value` representing the `ArrayBuffer` being queried. |
3313 | | -* `[out] data`: The underlying data buffer of the `ArrayBuffer`. If byte\_length |
| 3316 | +* `[in] arraybuffer`: `napi_value` representing the `ArrayBuffer` or `SharedArrayBuffer` being queried. |
| 3317 | +* `[out] data`: The underlying data buffer of the `ArrayBuffer` or `SharedArrayBuffer` |
3314 | 3318 | is `0`, this may be `NULL` or any other pointer value. |
3315 | 3319 | * `[out] byte_length`: Length in bytes of the underlying data buffer. |
3316 | 3320 |
|
3317 | 3321 | Returns `napi_ok` if the API succeeded. |
3318 | 3322 |
|
3319 | | -This API is used to retrieve the underlying data buffer of an `ArrayBuffer` and |
3320 | | -its length. |
| 3323 | +This API is used to retrieve the underlying data buffer of an `ArrayBuffer` or `SharedArrayBuffer` and its length. |
3321 | 3324 |
|
3322 | 3325 | _WARNING_: Use caution while using this API. The lifetime of the underlying data |
3323 | | -buffer is managed by the `ArrayBuffer` even after it's returned. A |
| 3326 | +buffer is managed by the `ArrayBuffer` or `SharedArrayBuffer` even after it's returned. A |
3324 | 3327 | possible safe way to use this API is in conjunction with |
3325 | 3328 | [`napi_create_reference`][], which can be used to guarantee control over the |
3326 | | -lifetime of the `ArrayBuffer`. It's also safe to use the returned data buffer |
| 3329 | +lifetime of the `ArrayBuffer` or `SharedArrayBuffer`. It's also safe to use the returned data buffer |
3327 | 3330 | within the same callback as long as there are no calls to other APIs that might |
3328 | 3331 | trigger a GC. |
3329 | 3332 |
|
@@ -4278,6 +4281,63 @@ This API represents the invocation of the `ArrayBuffer` `IsDetachedBuffer` |
4278 | 4281 | operation as defined in [Section isDetachedBuffer][] of the ECMAScript Language |
4279 | 4282 | Specification. |
4280 | 4283 |
|
| 4284 | +### `node_api_is_sharedarraybuffer` |
| 4285 | + |
| 4286 | +<!-- YAML |
| 4287 | +added: REPLACEME |
| 4288 | +--> |
| 4289 | + |
| 4290 | +> Stability: 1 - Experimental |
| 4291 | + |
| 4292 | +```c |
| 4293 | +napi_status node_api_is_sharedarraybuffer(napi_env env, napi_value value, bool* result) |
| 4294 | +``` |
| 4295 | + |
| 4296 | +* `[in] env`: The environment that the API is invoked under. |
| 4297 | +* `[in] value`: The JavaScript value to check. |
| 4298 | +* `[out] result`: Whether the given `napi_value` represents a `SharedArrayBuffer`. |
| 4299 | + |
| 4300 | +Returns `napi_ok` if the API succeeded. |
| 4301 | + |
| 4302 | +This API checks if the Object passed in is a `SharedArrayBuffer`. |
| 4303 | + |
| 4304 | +### `node_api_create_sharedarraybuffer` |
| 4305 | + |
| 4306 | +<!-- YAML |
| 4307 | +added: REPLACEME |
| 4308 | +--> |
| 4309 | + |
| 4310 | +> Stability: 1 - Experimental |
| 4311 | + |
| 4312 | +```c |
| 4313 | +napi_status node_api_create_sharedarraybuffer(napi_env env, |
| 4314 | + size_t byte_length, |
| 4315 | + void** data, |
| 4316 | + napi_value* result) |
| 4317 | +``` |
| 4318 | + |
| 4319 | +* `[in] env`: The environment that the API is invoked under. |
| 4320 | +* `[in] byte_length`: The length in bytes of the shared array buffer to create. |
| 4321 | +* `[out] data`: Pointer to the underlying byte buffer of the `SharedArrayBuffer`. |
| 4322 | + `data` can optionally be ignored by passing `NULL`. |
| 4323 | +* `[out] result`: A `napi_value` representing a JavaScript `SharedArrayBuffer`. |
| 4324 | + |
| 4325 | +Returns `napi_ok` if the API succeeded. |
| 4326 | + |
| 4327 | +This API returns a Node-API value corresponding to a JavaScript `SharedArrayBuffer`. |
| 4328 | +`SharedArrayBuffer`s are used to represent fixed-length binary data buffers that |
| 4329 | +can be shared across multiple workers. |
| 4330 | + |
| 4331 | +The `SharedArrayBuffer` allocated will have an underlying byte buffer whose size is |
| 4332 | +determined by the `byte_length` parameter that's passed in. |
| 4333 | +The underlying buffer is optionally returned back to the caller in case the |
| 4334 | +caller wants to directly manipulate the buffer. This buffer can only be |
| 4335 | +written to directly from native code. To write to this buffer from JavaScript, |
| 4336 | +a typed array or `DataView` object would need to be created. |
| 4337 | + |
| 4338 | +JavaScript `SharedArrayBuffer` objects are described in |
| 4339 | +[Section SharedArrayBuffer objects][] of the ECMAScript Language Specification. |
| 4340 | + |
4281 | 4341 | ## Working with JavaScript properties |
4282 | 4342 |
|
4283 | 4343 | Node-API exposes a set of APIs to get and set properties on JavaScript |
@@ -6791,6 +6851,7 @@ the add-on's file name during loading. |
6791 | 6851 | [Section IsArray]: https://tc39.es/ecma262/#sec-isarray |
6792 | 6852 | [Section IsStrctEqual]: https://tc39.es/ecma262/#sec-strict-equality-comparison |
6793 | 6853 | [Section Promise objects]: https://tc39.es/ecma262/#sec-promise-objects |
| 6854 | +[Section SharedArrayBuffer objects]: https://tc39.es/ecma262/#sec-sharedarraybuffer-objects |
6794 | 6855 | [Section ToBoolean]: https://tc39.es/ecma262/#sec-toboolean |
6795 | 6856 | [Section ToNumber]: https://tc39.es/ecma262/#sec-tonumber |
6796 | 6857 | [Section ToObject]: https://tc39.es/ecma262/#sec-toobject |
|