emnapi 1.5.0 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -983,6 +983,10 @@ var emnapiExternalMemory = {
983
983
  emnapiExternalMemory.table = new WeakMap();
984
984
  emnapiExternalMemory.wasmMemoryViewTable = new WeakMap();
985
985
  },
986
+ isSharedArrayBuffer: function (value) {
987
+ return ((typeof SharedArrayBuffer === 'function' && value instanceof SharedArrayBuffer) ||
988
+ (Object.prototype.toString.call(value) === '[object SharedArrayBuffer]'));
989
+ },
986
990
  isDetachedArrayBuffer: function (arrayBuffer) {
987
991
  if (arrayBuffer.byteLength === 0) {
988
992
  try {
@@ -1124,7 +1128,7 @@ function _napi_get_arraybuffer_info(env, arraybuffer, data, byte_length) {
1124
1128
  if (!arraybuffer)
1125
1129
  return envObject.setLastError(1 /* napi_status.napi_invalid_arg */);
1126
1130
  var handle = emnapiCtx.handleStore.get(arraybuffer);
1127
- if (!handle.isArrayBuffer()) {
1131
+ if (!handle.isArrayBuffer() && !emnapiExternalMemory.isSharedArrayBuffer(handle.value)) {
1128
1132
  return envObject.setLastError(1 /* napi_status.napi_invalid_arg */);
1129
1133
  }
1130
1134
  if (data) {
@@ -2627,7 +2631,7 @@ function emnapiSyncMemory(js_to_wasm, arrayBufferOrView, offset, len) {
2627
2631
  offset = offset !== null && offset !== void 0 ? offset : 0;
2628
2632
  offset = offset >>> 0;
2629
2633
  var view;
2630
- if (arrayBufferOrView instanceof ArrayBuffer) {
2634
+ if (arrayBufferOrView instanceof ArrayBuffer || emnapiExternalMemory.isSharedArrayBuffer(arrayBufferOrView)) {
2631
2635
  var pointer = emnapiExternalMemory.getArrayBufferPointer(arrayBufferOrView, false).address;
2632
2636
  if (!pointer)
2633
2637
  throw new Error('Unknown ArrayBuffer address');
@@ -2695,7 +2699,7 @@ function _emnapi_sync_memory(env, js_to_wasm, arraybuffer_or_view, offset, len)
2695
2699
  {{{ from64('len') }}};
2696
2700
  var handleId = {{{ makeGetValue('arraybuffer_or_view', 0, '*') }}};
2697
2701
  var handle = envObject.ctx.handleStore.get(handleId);
2698
- if (!handle.isArrayBuffer() && !handle.isTypedArray() && !handle.isDataView()) {
2702
+ if (!handle.isArrayBuffer() && !handle.isTypedArray() && !handle.isDataView() && !emnapiExternalMemory.isSharedArrayBuffer(handle.value)) {
2699
2703
  return envObject.setLastError(1 /* napi_status.napi_invalid_arg */);
2700
2704
  }
2701
2705
  var ret = emnapiSyncMemory(Boolean(js_to_wasm), handle.value, offset, len);
@@ -2715,7 +2719,7 @@ function emnapiGetMemoryAddress(arrayBufferOrView) {
2715
2719
  var isArrayBuffer = arrayBufferOrView instanceof ArrayBuffer;
2716
2720
  var isDataView = arrayBufferOrView instanceof DataView;
2717
2721
  var isTypedArray = ArrayBuffer.isView(arrayBufferOrView) && !isDataView;
2718
- if (!isArrayBuffer && !isTypedArray && !isDataView) {
2722
+ if (!isArrayBuffer && !isTypedArray && !isDataView && !emnapiExternalMemory.isSharedArrayBuffer(arrayBufferOrView)) {
2719
2723
  throw new TypeError('emnapiGetMemoryAddress expect ArrayBuffer or ArrayBufferView as first parameter');
2720
2724
  }
2721
2725
  var info;
@@ -2841,10 +2845,10 @@ function _napi_create_array_with_length(env, length, result) {
2841
2845
  {{{ makeSetValue('result', 0, 'value', '*') }}};
2842
2846
  return envObject.clearLastError();
2843
2847
  }
2844
- function emnapiCreateArrayBuffer(byte_length, data) {
2848
+ function emnapiCreateArrayBuffer(byte_length, data, shared) {
2845
2849
  {{{ from64('byte_length') }}};
2846
2850
  byte_length = byte_length >>> 0;
2847
- var arrayBuffer = new ArrayBuffer(byte_length);
2851
+ var arrayBuffer = shared ? new SharedArrayBuffer(byte_length) : new ArrayBuffer(byte_length);
2848
2852
  if (data) {
2849
2853
  {{{ from64('data') }}};
2850
2854
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -2873,7 +2877,37 @@ function _napi_create_arraybuffer(env, byte_length, data, result) {
2873
2877
  if (!result)
2874
2878
  return envObject.setLastError(1 /* napi_status.napi_invalid_arg */);
2875
2879
  {{{ from64('result') }}};
2876
- var arrayBuffer = emnapiCreateArrayBuffer(byte_length, data);
2880
+ var arrayBuffer = emnapiCreateArrayBuffer(byte_length, data, false);
2881
+ value = emnapiCtx.addToCurrentScope(arrayBuffer).id;
2882
+ {{{ makeSetValue('result', 0, 'value', '*') }}};
2883
+ return envObject.getReturnStatus();
2884
+ }
2885
+ catch (err) {
2886
+ envObject.tryCatch.setError(err);
2887
+ return envObject.setLastError(10 /* napi_status.napi_pending_exception */);
2888
+ }
2889
+ }
2890
+ /**
2891
+ * @__sig ipppp
2892
+ */
2893
+ function _node_api_create_sharedarraybuffer(env, byte_length, data, result) {
2894
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
2895
+ var value;
2896
+ if (!env)
2897
+ return 1 /* napi_status.napi_invalid_arg */;
2898
+ // @ts-expect-error
2899
+ var envObject = emnapiCtx.envStore.get(env);
2900
+ envObject.checkGCAccess();
2901
+ if (!envObject.tryCatch.isEmpty())
2902
+ return envObject.setLastError(10 /* napi_status.napi_pending_exception */);
2903
+ if (!envObject.canCallIntoJs())
2904
+ return envObject.setLastError(envObject.moduleApiVersion >= 10 ? 23 /* napi_status.napi_cannot_run_js */ : 10 /* napi_status.napi_pending_exception */);
2905
+ envObject.clearLastError();
2906
+ try {
2907
+ if (!result)
2908
+ return envObject.setLastError(1 /* napi_status.napi_invalid_arg */);
2909
+ {{{ from64('result') }}};
2910
+ var arrayBuffer = emnapiCreateArrayBuffer(byte_length, data, true);
2877
2911
  value = emnapiCtx.addToCurrentScope(arrayBuffer).id;
2878
2912
  {{{ makeSetValue('result', 0, 'value', '*') }}};
2879
2913
  return envObject.getReturnStatus();
@@ -3037,6 +3071,55 @@ function _napi_create_object(env, result) {
3037
3071
  {{{ makeSetValue('result', 0, 'value', '*') }}};
3038
3072
  return envObject.clearLastError();
3039
3073
  }
3074
+ /**
3075
+ * @__sig ipppppp
3076
+ */
3077
+ function _napi_create_object_with_properties(env, prototype_or_null, property_names, property_values, property_count, result) {
3078
+ if (!env)
3079
+ return 1 /* napi_status.napi_invalid_arg */;
3080
+ // @ts-expect-error
3081
+ var envObject = emnapiCtx.envStore.get(env);
3082
+ envObject.checkGCAccess();
3083
+ if (!result)
3084
+ return envObject.setLastError(1 /* napi_status.napi_invalid_arg */);
3085
+ {{{ from64('property_count') }}};
3086
+ property_count = property_count >>> 0;
3087
+ if (property_count > 0) {
3088
+ if (!property_names)
3089
+ return envObject.setLastError(1 /* napi_status.napi_invalid_arg */);
3090
+ if (!property_values)
3091
+ return envObject.setLastError(1 /* napi_status.napi_invalid_arg */);
3092
+ }
3093
+ var v8_prototype_or_null = prototype_or_null
3094
+ ? emnapiCtx.handleStore.get(prototype_or_null).value
3095
+ : null;
3096
+ var properties = {};
3097
+ {{{ from64('property_names') }}};
3098
+ {{{ from64('property_values') }}};
3099
+ for (var i = 0; i < property_count; i++) {
3100
+ var name_value = emnapiCtx.handleStore.get({{{ makeGetValue('property_names', 'i * ' + POINTER_SIZE, '*') }}}).value;
3101
+ if (!(typeof name_value === "string" || typeof name_value === "symbol"))
3102
+ return envObject.setLastError(4 /* napi_status.napi_name_expected */);
3103
+ properties[name_value] = {
3104
+ value: emnapiCtx.handleStore.get({{{ makeGetValue('property_values', 'i * ' + POINTER_SIZE, '*') }}}).value,
3105
+ writable: true,
3106
+ enumerable: true,
3107
+ configurable: true
3108
+ };
3109
+ }
3110
+ var obj;
3111
+ try {
3112
+ obj = Object.defineProperties(Object.create(v8_prototype_or_null), properties);
3113
+ }
3114
+ catch (_) {
3115
+ return envObject.setLastError(9 /* napi_status.napi_generic_failure */);
3116
+ }
3117
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
3118
+ var value = emnapiCtx.addToCurrentScope(obj).id;
3119
+ {{{ from64('result') }}};
3120
+ {{{ makeSetValue('result', 0, 'value', '*') }}};
3121
+ return envObject.clearLastError();
3122
+ }
3040
3123
  /**
3041
3124
  * @__sig ippp
3042
3125
  */
@@ -3246,7 +3329,7 @@ function _napi_create_buffer_copy(env, length, data, result_data, result) {
3246
3329
  if (!Buffer) {
3247
3330
  throw emnapiCtx.createNotSupportBufferError('napi_create_buffer_copy', '');
3248
3331
  }
3249
- var arrayBuffer = emnapiCreateArrayBuffer(length, result_data);
3332
+ var arrayBuffer = emnapiCreateArrayBuffer(length, result_data, false);
3250
3333
  var buffer = Buffer.from(arrayBuffer);
3251
3334
  {{{ from64('data') }}};
3252
3335
  {{{ from64('length') }}};
@@ -6458,6 +6541,27 @@ function _napi_is_arraybuffer(env, value, result) {
6458
6541
  return envObject.clearLastError();
6459
6542
  }
6460
6543
  /** @__sig ippp */
6544
+ function _node_api_is_sharedarraybuffer(env, value, result) {
6545
+ if (!env)
6546
+ return 1 /* napi_status.napi_invalid_arg */;
6547
+ // @ts-expect-error
6548
+ var envObject = emnapiCtx.envStore.get(env);
6549
+ envObject.checkGCAccess();
6550
+ if (!value)
6551
+ return envObject.setLastError(1 /* napi_status.napi_invalid_arg */);
6552
+ if (!result)
6553
+ return envObject.setLastError(1 /* napi_status.napi_invalid_arg */);
6554
+ var h = emnapiCtx.handleStore.get(value);
6555
+ {{{ from64('result') }}};
6556
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
6557
+ var r = ((typeof SharedArrayBuffer === 'function' && h.value instanceof SharedArrayBuffer) ||
6558
+ (Object.prototype.toString.call(h.value) === '[object SharedArrayBuffer]'))
6559
+ ? 1
6560
+ : 0;
6561
+ {{{ makeSetValue('result', 0, 'r', 'i8') }}};
6562
+ return envObject.clearLastError();
6563
+ }
6564
+ /** @__sig ippp */
6461
6565
  function _napi_is_date(env, value, result) {
6462
6566
  if (!env)
6463
6567
  return 1 /* napi_status.napi_invalid_arg */;
@@ -6784,7 +6888,7 @@ function _napi_get_version(env, result) {
6784
6888
  emnapi_is_support_weakref__deps: ["$emnapiCtx"],
6785
6889
  emnapi_is_support_weakref__sig: "i",
6786
6890
  emnapi_sync_memory: _emnapi_sync_memory,
6787
- emnapi_sync_memory__deps: ["$emnapiCtx", "$emnapiSyncMemory"],
6891
+ emnapi_sync_memory__deps: ["$emnapiCtx", "$emnapiExternalMemory", "$emnapiSyncMemory"],
6788
6892
  emnapi_sync_memory__sig: "ipippp",
6789
6893
  $emnapiTSFN: emnapiTSFN,
6790
6894
  $emnapiTSFN__deps: ["$emnapiCtx", "_emnapi_runtime_keepalive_pop", "$emnapiNodeBinding", "_emnapi_node_emit_async_destroy", "malloc", "free"],
@@ -6900,6 +7004,9 @@ function _napi_get_version(env, result) {
6900
7004
  napi_create_object: _napi_create_object,
6901
7005
  napi_create_object__deps: ["$emnapiCtx"],
6902
7006
  napi_create_object__sig: "ipp",
7007
+ napi_create_object_with_properties: _napi_create_object_with_properties,
7008
+ napi_create_object_with_properties__deps: ["$emnapiCtx"],
7009
+ napi_create_object_with_properties__sig: "ipppppp",
6903
7010
  napi_create_promise: _napi_create_promise,
6904
7011
  napi_create_promise__deps: ["$emnapiCtx"],
6905
7012
  napi_create_promise__sig: "ippp",
@@ -7232,9 +7339,15 @@ function _napi_get_version(env, result) {
7232
7339
  node_api_create_property_key_utf8: _node_api_create_property_key_utf8,
7233
7340
  node_api_create_property_key_utf8__deps: ["napi_create_string_utf8"],
7234
7341
  node_api_create_property_key_utf8__sig: "ipppp",
7342
+ node_api_create_sharedarraybuffer: _node_api_create_sharedarraybuffer,
7343
+ node_api_create_sharedarraybuffer__deps: ["$emnapiCtx", "$emnapiCreateArrayBuffer"],
7344
+ node_api_create_sharedarraybuffer__sig: "ipppp",
7235
7345
  node_api_create_syntax_error: _node_api_create_syntax_error,
7236
7346
  node_api_create_syntax_error__deps: ["$emnapiCtx"],
7237
7347
  node_api_create_syntax_error__sig: "ipppp",
7348
+ node_api_is_sharedarraybuffer: _node_api_is_sharedarraybuffer,
7349
+ node_api_is_sharedarraybuffer__deps: ["$emnapiCtx"],
7350
+ node_api_is_sharedarraybuffer__sig: "ippp",
7238
7351
  node_api_post_finalizer: _node_api_post_finalizer,
7239
7352
  node_api_post_finalizer__deps: ["$emnapiCtx"],
7240
7353
  node_api_post_finalizer__sig: "ipppp",
@@ -6,7 +6,7 @@
6
6
  #include "emnapi_common.h"
7
7
 
8
8
  #define EMNAPI_MAJOR_VERSION 1
9
- #define EMNAPI_MINOR_VERSION 5
9
+ #define EMNAPI_MINOR_VERSION 7
10
10
  #define EMNAPI_PATCH_VERSION 0
11
11
 
12
12
  typedef enum {
@@ -72,6 +72,17 @@ NAPI_EXTERN napi_status NAPI_CDECL napi_get_boolean(napi_env env,
72
72
  // Methods to create Primitive types/Objects
73
73
  NAPI_EXTERN napi_status NAPI_CDECL napi_create_object(napi_env env,
74
74
  napi_value* result);
75
+ #ifdef NAPI_EXPERIMENTAL
76
+ #define NODE_API_EXPERIMENTAL_HAS_CREATE_OBJECT_WITH_PROPERTIES
77
+ NAPI_EXTERN napi_status NAPI_CDECL
78
+ napi_create_object_with_properties(napi_env env,
79
+ napi_value prototype_or_null,
80
+ napi_value* property_names,
81
+ napi_value* property_values,
82
+ size_t property_count,
83
+ napi_value* result);
84
+ #endif // NAPI_EXPERIMENTAL
85
+
75
86
  NAPI_EXTERN napi_status NAPI_CDECL napi_create_array(napi_env env,
76
87
  napi_value* result);
77
88
  NAPI_EXTERN napi_status NAPI_CDECL
@@ -358,7 +369,7 @@ napi_create_reference(napi_env env,
358
369
 
359
370
  // Deletes a reference. The referenced value is released, and may
360
371
  // be GC'd unless there are other references to it.
361
- NAPI_EXTERN napi_status NAPI_CDECL napi_delete_reference(napi_env env,
372
+ NAPI_EXTERN napi_status NAPI_CDECL napi_delete_reference(node_api_basic_env env,
362
373
  napi_ref ref);
363
374
 
364
375
  // Increments the reference count, optionally returning the resulting count.
@@ -480,6 +491,14 @@ napi_get_dataview_info(napi_env env,
480
491
  napi_value* arraybuffer,
481
492
  size_t* byte_offset);
482
493
 
494
+ #ifdef NAPI_EXPERIMENTAL
495
+ #define NODE_API_EXPERIMENTAL_HAS_SHAREDARRAYBUFFER
496
+ NAPI_EXTERN napi_status NAPI_CDECL
497
+ node_api_is_sharedarraybuffer(napi_env env, napi_value value, bool* result);
498
+ NAPI_EXTERN napi_status NAPI_CDECL node_api_create_sharedarraybuffer(
499
+ napi_env env, size_t byte_length, void** data, napi_value* result);
500
+ #endif // NAPI_EXPERIMENTAL
501
+
483
502
  // version management
484
503
  NAPI_EXTERN napi_status NAPI_CDECL napi_get_version(node_api_basic_env env,
485
504
  uint32_t* result);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "emnapi",
3
- "version": "1.5.0",
3
+ "version": "1.7.0",
4
4
  "description": "Node-API implementation for Emscripten",
5
5
  "main": "index.js",
6
6
  "gypfile": false,