emnapi 0.36.2 → 0.37.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.
@@ -212,9 +212,13 @@ function _emnapi_create_memory_view(env, typedarray_type, external_data, byte_le
212
212
  case -1 /* emnapi_memory_view_type.emnapi_data_view */:
213
213
  viewDescriptor_1 = { Ctor: DataView, address: external_data, length: byte_length, ownership: 1 /* Ownership.kUserland */, runtimeAllocated: 0 };
214
214
  break;
215
- case -2 /* emnapi_memory_view_type.emnapi_buffer */:
215
+ case -2 /* emnapi_memory_view_type.emnapi_buffer */: {
216
+ if (!emnapiCtx.feature.Buffer) {
217
+ throw emnapiCtx.createNotSupportBufferError("emnapi_create_memory_view", "");
218
+ }
216
219
  viewDescriptor_1 = { Ctor: emnapiCtx.feature.Buffer, address: external_data, length: byte_length, ownership: 1 /* Ownership.kUserland */, runtimeAllocated: 0 };
217
220
  break;
221
+ }
218
222
  default: return envObject_1.setLastError(1 /* napi_status.napi_invalid_arg */);
219
223
  }
220
224
  var Ctor_1 = viewDescriptor_1.Ctor;
@@ -474,9 +478,9 @@ function napi_throw_error(env, code, msg) {
474
478
  return envObject_5.setLastError(1 /* napi_status.napi_invalid_arg */);
475
479
  {{{ from64("code") }}};
476
480
  {{{ from64("msg") }}};
477
- var error_1 = new Error(UTF8ToString(msg));
481
+ var error_1 = new Error(emnapiString.UTF8ToString(msg, -1));
478
482
  if (code) {
479
- error_1.code = UTF8ToString(code);
483
+ error_1.code = emnapiString.UTF8ToString(code, -1);
480
484
  }
481
485
  envObject_5.tryCatch.setError(error_1);
482
486
  return envObject_5.clearLastError();
@@ -499,9 +503,9 @@ function napi_throw_type_error(env, code, msg) {
499
503
  return envObject_6.setLastError(1 /* napi_status.napi_invalid_arg */);
500
504
  {{{ from64("code") }}};
501
505
  {{{ from64("msg") }}};
502
- var error_2 = new TypeError(UTF8ToString(msg));
506
+ var error_2 = new TypeError(emnapiString.UTF8ToString(msg, -1));
503
507
  if (code) {
504
- error_2.code = UTF8ToString(code);
508
+ error_2.code = emnapiString.UTF8ToString(code, -1);
505
509
  }
506
510
  envObject_6.tryCatch.setError(error_2);
507
511
  return envObject_6.clearLastError();
@@ -524,9 +528,9 @@ function napi_throw_range_error(env, code, msg) {
524
528
  return envObject_7.setLastError(1 /* napi_status.napi_invalid_arg */);
525
529
  {{{ from64("code") }}};
526
530
  {{{ from64("msg") }}};
527
- var error_3 = new RangeError(UTF8ToString(msg));
531
+ var error_3 = new RangeError(emnapiString.UTF8ToString(msg, -1));
528
532
  if (code) {
529
- error_3.code = UTF8ToString(code);
533
+ error_3.code = emnapiString.UTF8ToString(code, -1);
530
534
  }
531
535
  envObject_7.tryCatch.setError(error_3);
532
536
  return envObject_7.clearLastError();
@@ -549,9 +553,9 @@ function node_api_throw_syntax_error(env, code, msg) {
549
553
  return envObject_8.setLastError(1 /* napi_status.napi_invalid_arg */);
550
554
  {{{ from64("code") }}};
551
555
  {{{ from64("msg") }}};
552
- var error_4 = new SyntaxError(UTF8ToString(msg));
556
+ var error_4 = new SyntaxError(emnapiString.UTF8ToString(msg, -1));
553
557
  if (code) {
554
- error_4.code = UTF8ToString(code);
558
+ error_4.code = emnapiString.UTF8ToString(code, -1);
555
559
  }
556
560
  envObject_8.tryCatch.setError(error_4);
557
561
  return envObject_8.clearLastError();
@@ -703,9 +707,9 @@ function napi_fatal_error(location, location_len, message, message_len) {
703
707
  {{{ from64('message') }}};
704
708
  {{{ from64('message_len') }}};
705
709
  abort('FATAL ERROR: ' +
706
- emnapiUtf8ToString(location, location_len) +
710
+ emnapiString.UTF8ToString(location, location_len) +
707
711
  ' ' +
708
- emnapiUtf8ToString(message, message_len));
712
+ emnapiString.UTF8ToString(message, message_len));
709
713
  }
710
714
  // @ts-expect-error
711
715
  function napi_fatal_exception(env, err) {
@@ -733,16 +737,16 @@ function napi_fatal_exception(env, err) {
733
737
  emnapiImplementInternal('_emnapi_get_last_error_info', 'vpppp', __emnapi_get_last_error_info);
734
738
  emnapiImplement('napi_get_and_clear_last_exception', 'ipp', napi_get_and_clear_last_exception);
735
739
  emnapiImplement('napi_throw', 'ipp', napi_throw);
736
- emnapiImplement('napi_throw_error', 'ippp', napi_throw_error);
737
- emnapiImplement('napi_throw_type_error', 'ippp', napi_throw_type_error);
738
- emnapiImplement('napi_throw_range_error', 'ippp', napi_throw_range_error);
739
- emnapiImplement('node_api_throw_syntax_error', 'ippp', node_api_throw_syntax_error);
740
- emnapiImplement('napi_create_error', 'ipppp', napi_create_error);
741
- emnapiImplement('napi_create_type_error', 'ipppp', napi_create_type_error);
742
- emnapiImplement('napi_create_range_error', 'ipppp', napi_create_range_error);
743
- emnapiImplement('node_api_create_syntax_error', 'ipppp', node_api_create_syntax_error);
740
+ emnapiImplement('napi_throw_error', 'ippp', napi_throw_error, ['$emnapiString']);
741
+ emnapiImplement('napi_throw_type_error', 'ippp', napi_throw_type_error, ['$emnapiString']);
742
+ emnapiImplement('napi_throw_range_error', 'ippp', napi_throw_range_error, ['$emnapiString']);
743
+ emnapiImplement('node_api_throw_syntax_error', 'ippp', node_api_throw_syntax_error, ['$emnapiString']);
744
+ emnapiImplement('napi_create_error', 'ipppp', napi_create_error, ['$emnapiString']);
745
+ emnapiImplement('napi_create_type_error', 'ipppp', napi_create_type_error, ['$emnapiString']);
746
+ emnapiImplement('napi_create_range_error', 'ipppp', napi_create_range_error, ['$emnapiString']);
747
+ emnapiImplement('node_api_create_syntax_error', 'ipppp', node_api_create_syntax_error, ['$emnapiString']);
744
748
  emnapiImplement('napi_is_exception_pending', 'ipp', napi_is_exception_pending);
745
- emnapiImplement('napi_fatal_error', 'vpppp', napi_fatal_error, ['$emnapiUtf8ToString']);
749
+ emnapiImplement('napi_fatal_error', 'vpppp', napi_fatal_error, ['$emnapiString']);
746
750
  emnapiImplement('napi_fatal_exception', 'ipp', napi_fatal_exception);
747
751
  // @ts-expect-error
748
752
  function napi_create_function(env, utf8name, length, cb, data, result) {
@@ -946,7 +950,7 @@ emnapiImplement('napi_get_new_target', 'ippp', napi_get_new_target);
946
950
  /* eslint-disable @typescript-eslint/no-implied-eval */
947
951
  function emnapiCreateFunction(envObject, utf8name, length, cb, data) {
948
952
  {{{ from64('utf8name') }}};
949
- var functionName = (!utf8name || !length) ? '' : (emnapiUtf8ToString(utf8name, length));
953
+ var functionName = (!utf8name || !length) ? '' : (emnapiString.UTF8ToString(utf8name, length));
950
954
  var f;
951
955
  var makeFunction = function () { return function () {
952
956
  'use strict';
@@ -1137,7 +1141,7 @@ function emnapiUnwrap(env, js_object, result, action) {
1137
1141
  return envObject_14.setLastError(10 /* napi_status.napi_pending_exception */);
1138
1142
  }
1139
1143
  }
1140
- emnapiImplementHelper('$emnapiCreateFunction', undefined, emnapiCreateFunction, ['$emnapiUtf8ToString']);
1144
+ emnapiImplementHelper('$emnapiCreateFunction', undefined, emnapiCreateFunction, ['$emnapiString']);
1141
1145
  emnapiImplementHelper('$emnapiDefineProperty', undefined, emnapiDefineProperty, ['$emnapiCreateFunction']);
1142
1146
  emnapiImplementHelper('$emnapiGetHandle', undefined, emnapiGetHandle);
1143
1147
  emnapiImplementHelper('$emnapiWrap', undefined, emnapiWrap, ['$emnapiGetHandle']);
@@ -1380,7 +1384,7 @@ var emnapiExternalMemory = {
1380
1384
  if (!shouldCopy) {
1381
1385
  return info;
1382
1386
  }
1383
- var pointer = {{{ makeMalloc('$emnapiExternalMemory.getArrayBufferPointer', 'arrayBuffer.byteLength') }}};
1387
+ var pointer = _malloc({{{ to64('arrayBuffer.byteLength') }}});
1384
1388
  if (!pointer)
1385
1389
  throw new Error('Out of memory');
1386
1390
  new Uint8Array(wasmMemory.buffer).set(new Uint8Array(arrayBuffer), pointer);
@@ -2070,7 +2074,7 @@ function napi_set_named_property(env, object, cname, value) {
2070
2074
  return envObject_25.setLastError(1 /* napi_status.napi_invalid_arg */);
2071
2075
  }
2072
2076
  {{{ from64("cname") }}};
2073
- emnapiCtx.handleStore.get(object).value[UTF8ToString(cname)] = emnapiCtx.handleStore.get(value).value;
2077
+ emnapiCtx.handleStore.get(object).value[emnapiString.UTF8ToString(cname, -1)] = emnapiCtx.handleStore.get(value).value;
2074
2078
  return 0 /* napi_status.napi_ok */;
2075
2079
  }
2076
2080
  catch (err_26) {
@@ -2109,7 +2113,7 @@ function napi_has_named_property(env, object, utf8name, result) {
2109
2113
  }
2110
2114
  {{{ from64("utf8name") }}};
2111
2115
  {{{ from64("result") }}};
2112
- r = UTF8ToString(utf8name) in v_4;
2116
+ r = emnapiString.UTF8ToString(utf8name, -1) in v_4;
2113
2117
  {{{ makeSetValue("result", 0, "r ? 1 : 0", "i8") }}};
2114
2118
  return envObject_26.getReturnStatus();
2115
2119
  }
@@ -2150,7 +2154,7 @@ function napi_get_named_property(env, object, utf8name, result) {
2150
2154
  {{{ from64("utf8name") }}};
2151
2155
  {{{ from64("result") }}};
2152
2156
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
2153
- value = envObject_27.ensureHandleId(v_5[UTF8ToString(utf8name)]);
2157
+ value = envObject_27.ensureHandleId(v_5[emnapiString.UTF8ToString(utf8name, -1)]);
2154
2158
  {{{ makeSetValue("result", 0, "value", "*") }}};
2155
2159
  return envObject_27.getReturnStatus();
2156
2160
  }
@@ -2336,7 +2340,7 @@ function napi_define_properties(env, object, property_count, properties
2336
2340
  {{{ from64("attributes") }}};
2337
2341
  var data_2 = {{{ makeGetValue("propPtr", POINTER_SIZE * 7, "*") }}};
2338
2342
  if (utf8Name_1) {
2339
- propertyName_1 = UTF8ToString(utf8Name_1);
2343
+ propertyName_1 = emnapiString.UTF8ToString(utf8Name_1, -1);
2340
2344
  }
2341
2345
  else {
2342
2346
  if (!name_2) {
@@ -2411,14 +2415,14 @@ emnapiImplement('napi_has_property', 'ipppp', napi_has_property);
2411
2415
  emnapiImplement('napi_get_property', 'ipppp', napi_get_property);
2412
2416
  emnapiImplement('napi_delete_property', 'ipppp', napi_delete_property);
2413
2417
  emnapiImplement('napi_has_own_property', 'ipppp', napi_has_own_property);
2414
- emnapiImplement('napi_set_named_property', 'ipppp', napi_set_named_property);
2415
- emnapiImplement('napi_has_named_property', 'ipppp', napi_has_named_property);
2416
- emnapiImplement('napi_get_named_property', 'ipppp', napi_get_named_property);
2418
+ emnapiImplement('napi_set_named_property', 'ipppp', napi_set_named_property, ['$emnapiString']);
2419
+ emnapiImplement('napi_has_named_property', 'ipppp', napi_has_named_property, ['$emnapiString']);
2420
+ emnapiImplement('napi_get_named_property', 'ipppp', napi_get_named_property, ['$emnapiString']);
2417
2421
  emnapiImplement('napi_set_element', 'ippip', napi_set_element);
2418
2422
  emnapiImplement('napi_has_element', 'ippip', napi_has_element);
2419
2423
  emnapiImplement('napi_get_element', 'ippip', napi_get_element);
2420
2424
  emnapiImplement('napi_delete_element', 'ippip', napi_delete_element);
2421
- emnapiImplement('napi_define_properties', 'ipppp', napi_define_properties, ['$emnapiDefineProperty']);
2425
+ emnapiImplement('napi_define_properties', 'ipppp', napi_define_properties, ['$emnapiDefineProperty', '$emnapiString']);
2422
2426
  emnapiImplement('napi_object_freeze', 'ipp', napi_object_freeze);
2423
2427
  emnapiImplement('napi_object_seal', 'ipp', napi_object_seal);
2424
2428
  /* eslint-disable @typescript-eslint/indent */
@@ -2462,6 +2466,225 @@ function napi_run_script(env, script, result) {
2462
2466
  }
2463
2467
  emnapiImplement('napi_run_script', 'ippp', napi_run_script, ['napi_set_last_error']);
2464
2468
  /* eslint-disable @typescript-eslint/indent */
2469
+ var emnapiString = {
2470
+ utf8Decoder: undefined,
2471
+ utf16Decoder: undefined,
2472
+ init: function () {
2473
+ #if !TEXTDECODER || TEXTDECODER == 1
2474
+ var fallbackDecoder = {
2475
+ decode: function (input) {
2476
+ var isArrayBuffer = input instanceof ArrayBuffer;
2477
+ var isView = ArrayBuffer.isView(input);
2478
+ if (!isArrayBuffer && !isView) {
2479
+ throw new TypeError('The "input" argument must be an instance of ArrayBuffer or ArrayBufferView');
2480
+ }
2481
+ var bytes = isArrayBuffer ? new Uint8Array(input) : new Uint8Array(input.buffer, input.byteOffset, input.byteLength);
2482
+ var inputIndex = 0;
2483
+ var pendingSize = Math.min(256 * 256, bytes.length + 1);
2484
+ var pending = new Uint16Array(pendingSize);
2485
+ var chunks = [];
2486
+ var pendingIndex = 0;
2487
+ for (;;) {
2488
+ var more = inputIndex < bytes.length;
2489
+ if (!more || (pendingIndex >= pendingSize - 1)) {
2490
+ var subarray = pending.subarray(0, pendingIndex);
2491
+ var arraylike = subarray;
2492
+ chunks.push(String.fromCharCode.apply(null, arraylike));
2493
+ if (!more) {
2494
+ return chunks.join('');
2495
+ }
2496
+ bytes = bytes.subarray(inputIndex);
2497
+ inputIndex = 0;
2498
+ pendingIndex = 0;
2499
+ }
2500
+ var byte1 = bytes[inputIndex++];
2501
+ if ((byte1 & 0x80) === 0) {
2502
+ pending[pendingIndex++] = byte1;
2503
+ }
2504
+ else if ((byte1 & 0xe0) === 0xc0) {
2505
+ var byte2 = bytes[inputIndex++] & 0x3f;
2506
+ pending[pendingIndex++] = ((byte1 & 0x1f) << 6) | byte2;
2507
+ }
2508
+ else if ((byte1 & 0xf0) === 0xe0) {
2509
+ var byte2 = bytes[inputIndex++] & 0x3f;
2510
+ var byte3 = bytes[inputIndex++] & 0x3f;
2511
+ pending[pendingIndex++] = ((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3;
2512
+ }
2513
+ else if ((byte1 & 0xf8) === 0xf0) {
2514
+ var byte2 = bytes[inputIndex++] & 0x3f;
2515
+ var byte3 = bytes[inputIndex++] & 0x3f;
2516
+ var byte4 = bytes[inputIndex++] & 0x3f;
2517
+ var codepoint = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
2518
+ if (codepoint > 0xffff) {
2519
+ codepoint -= 0x10000;
2520
+ pending[pendingIndex++] = (codepoint >>> 10) & 0x3ff | 0xd800;
2521
+ codepoint = 0xdc00 | codepoint & 0x3ff;
2522
+ }
2523
+ pending[pendingIndex++] = codepoint;
2524
+ }
2525
+ else {
2526
+ }
2527
+ }
2528
+ }
2529
+ };
2530
+ #endif
2531
+ var utf8Decoder;
2532
+ #if !TEXTDECODER
2533
+ utf8Decoder = fallbackDecoder;
2534
+ #elif TEXTDECODER == 1
2535
+ utf8Decoder = typeof TextDecoder === 'function' ? new TextDecoder() : fallbackDecoder;
2536
+ #elif TEXTDECODER == 2
2537
+ utf8Decoder = new TextDecoder();
2538
+ #endif
2539
+ emnapiString.utf8Decoder = utf8Decoder;
2540
+ #if !TEXTDECODER || TEXTDECODER == 1
2541
+ var fallbackDecoder2 = {
2542
+ decode: function (input) {
2543
+ var isArrayBuffer = input instanceof ArrayBuffer;
2544
+ var isView = ArrayBuffer.isView(input);
2545
+ if (!isArrayBuffer && !isView) {
2546
+ throw new TypeError('The "input" argument must be an instance of ArrayBuffer or ArrayBufferView');
2547
+ }
2548
+ var bytes = isArrayBuffer ? new Uint16Array(input) : new Uint16Array(input.buffer, input.byteOffset, input.byteLength / 2);
2549
+ var wcharArray = Array(bytes.length);
2550
+ for (var i = 0; i < bytes.length; ++i) {
2551
+ wcharArray[i] = String.fromCharCode(bytes[i]);
2552
+ }
2553
+ return wcharArray.join('');
2554
+ }
2555
+ };
2556
+ #endif
2557
+ var utf16Decoder;
2558
+ #if !TEXTDECODER
2559
+ utf16Decoder = fallbackDecoder2;
2560
+ #elif TEXTDECODER == 1
2561
+ utf16Decoder = typeof TextDecoder === 'function' ? new TextDecoder('utf-16le') : fallbackDecoder2;
2562
+ #elif TEXTDECODER == 2
2563
+ utf16Decoder = new TextDecoder('utf-16le');
2564
+ #endif
2565
+ emnapiString.utf16Decoder = utf16Decoder;
2566
+ },
2567
+ lengthBytesUTF8: function (str) {
2568
+ var c;
2569
+ var len = 0;
2570
+ for (var i = 0; i < str.length; ++i) {
2571
+ c = str.charCodeAt(i);
2572
+ if (c <= 0x7F) {
2573
+ len++;
2574
+ }
2575
+ else if (c <= 0x7FF) {
2576
+ len += 2;
2577
+ }
2578
+ else if (c >= 0xD800 && c <= 0xDFFF) {
2579
+ len += 4;
2580
+ ++i;
2581
+ }
2582
+ else {
2583
+ len += 3;
2584
+ }
2585
+ }
2586
+ return len;
2587
+ },
2588
+ UTF8ToString: function (ptr, length) {
2589
+ ptr >>>= 0;
2590
+ if (!ptr || !length)
2591
+ return '';
2592
+ var HEAPU8 = new Uint8Array(wasmMemory.buffer);
2593
+ var end = ptr;
2594
+ if (length === -1) {
2595
+ for (; HEAPU8[end];)
2596
+ ++end;
2597
+ }
2598
+ else {
2599
+ end = ptr + (length >>> 0);
2600
+ }
2601
+ return emnapiString.utf8Decoder.decode({{{ getUnsharedTextDecoderView('HEAPU8', 'ptr', 'end') }}});
2602
+ },
2603
+ stringToUTF8: function (str, outPtr, maxBytesToWrite) {
2604
+ var HEAPU8 = new Uint8Array(wasmMemory.buffer);
2605
+ var outIdx = outPtr;
2606
+ outIdx >>>= 0;
2607
+ if (!(maxBytesToWrite > 0)) {
2608
+ return 0;
2609
+ }
2610
+ var startIdx = outIdx;
2611
+ var endIdx = outIdx + maxBytesToWrite - 1;
2612
+ for (var i = 0; i < str.length; ++i) {
2613
+ var u = str.charCodeAt(i);
2614
+ if (u >= 0xD800 && u <= 0xDFFF) {
2615
+ var u1 = str.charCodeAt(++i);
2616
+ u = 0x10000 + ((u & 0x3FF) << 10) | (u1 & 0x3FF);
2617
+ }
2618
+ if (u <= 0x7F) {
2619
+ if (outIdx >= endIdx)
2620
+ break;
2621
+ HEAPU8[outIdx++] = u;
2622
+ }
2623
+ else if (u <= 0x7FF) {
2624
+ if (outIdx + 1 >= endIdx)
2625
+ break;
2626
+ HEAPU8[outIdx++] = 0xC0 | (u >> 6);
2627
+ HEAPU8[outIdx++] = 0x80 | (u & 63);
2628
+ }
2629
+ else if (u <= 0xFFFF) {
2630
+ if (outIdx + 2 >= endIdx)
2631
+ break;
2632
+ HEAPU8[outIdx++] = 0xE0 | (u >> 12);
2633
+ HEAPU8[outIdx++] = 0x80 | ((u >> 6) & 63);
2634
+ HEAPU8[outIdx++] = 0x80 | (u & 63);
2635
+ }
2636
+ else {
2637
+ if (outIdx + 3 >= endIdx)
2638
+ break;
2639
+ HEAPU8[outIdx++] = 0xF0 | (u >> 18);
2640
+ HEAPU8[outIdx++] = 0x80 | ((u >> 12) & 63);
2641
+ HEAPU8[outIdx++] = 0x80 | ((u >> 6) & 63);
2642
+ HEAPU8[outIdx++] = 0x80 | (u & 63);
2643
+ }
2644
+ }
2645
+ HEAPU8[outIdx] = 0;
2646
+ return outIdx - startIdx;
2647
+ },
2648
+ UTF16ToString: function (ptr, length) {
2649
+ ptr >>>= 0;
2650
+ if (!ptr || !length)
2651
+ return '';
2652
+ var end = ptr;
2653
+ if (length === -1) {
2654
+ var idx = end >> 1;
2655
+ var HEAPU16 = new Uint16Array(wasmMemory.buffer);
2656
+ while (HEAPU16[idx])
2657
+ ++idx;
2658
+ end = idx << 1;
2659
+ }
2660
+ else {
2661
+ end = ptr + (length >>> 0) * 2;
2662
+ }
2663
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
2664
+ var HEAPU8 = new Uint8Array(wasmMemory.buffer);
2665
+ return emnapiString.utf16Decoder.decode({{{ getUnsharedTextDecoderView('HEAPU8', 'ptr', 'end') }}});
2666
+ },
2667
+ stringToUTF16: function (str, outPtr, maxBytesToWrite) {
2668
+ if (maxBytesToWrite === undefined) {
2669
+ maxBytesToWrite = 0x7FFFFFFF;
2670
+ }
2671
+ if (maxBytesToWrite < 2)
2672
+ return 0;
2673
+ maxBytesToWrite -= 2;
2674
+ var startPtr = outPtr;
2675
+ var numCharsToWrite = (maxBytesToWrite < str.length * 2) ? (maxBytesToWrite / 2) : str.length;
2676
+ for (var i = 0; i < numCharsToWrite; ++i) {
2677
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
2678
+ var codeUnit = str.charCodeAt(i);
2679
+ {{{ makeSetValue('outPtr', 0, 'codeUnit', 'i16') }}};
2680
+ outPtr += 2;
2681
+ }
2682
+ {{{ makeSetValue('outPtr', 0, '0', 'i16') }}};
2683
+ return outPtr - startPtr;
2684
+ }
2685
+ };
2686
+ emnapiDefineVar('$emnapiString', emnapiString, [], 'emnapiString.init();');
2687
+ /* eslint-disable @typescript-eslint/indent */
2465
2688
  var emnapiTSFN = {
2466
2689
  offset: {
2467
2690
  /* napi_ref */ resource: 0,
@@ -2533,7 +2756,7 @@ var emnapiTSFN = {
2533
2756
  },
2534
2757
  initQueue: function (func) {
2535
2758
  var size = 2 * {{{ POINTER_SIZE }}};
2536
- var queue = {{{ makeMalloc('emnapiTSFN.initQueue', 'size') }}};
2759
+ var queue = _malloc({{{ to64('size') }}});
2537
2760
  if (!queue)
2538
2761
  return false;
2539
2762
  new Uint8Array(wasmMemory.buffer, queue, size).fill(0);
@@ -2552,7 +2775,7 @@ var emnapiTSFN = {
2552
2775
  var tail = emnapiTSFN.loadSizeTypeValue(queue + {{{ POINTER_SIZE }}}, false);
2553
2776
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
2554
2777
  var size = 2 * {{{ POINTER_SIZE }}};
2555
- var node = {{{ makeMalloc('emnapiTSFN.pushQueue', 'size') }}};
2778
+ var node = _malloc({{{ to64('size') }}});
2556
2779
  if (!node)
2557
2780
  throw new Error('OOM');
2558
2781
  emnapiTSFN.storeSizeTypeValue(node, data, false);
@@ -3142,7 +3365,7 @@ function _napi_create_threadsafe_function(env, func, async_resource, async_resou
3142
3365
  var resource_name = envObject.ensureHandleId(asyncResourceName);
3143
3366
  // tsfn create
3144
3367
  var sizeofTSFN = emnapiTSFN.offset.end;
3145
- var tsfn = {{{ makeMalloc('napi_create_threadsafe_function', 'sizeofTSFN') }}};
3368
+ var tsfn = _malloc({{{ to64('sizeofTSFN') }}});
3146
3369
  if (!tsfn)
3147
3370
  return envObject.setLastError(9 /* napi_status.napi_generic_failure */);
3148
3371
  new Uint8Array(wasmMemory.buffer).subarray(tsfn, tsfn + sizeofTSFN).fill(0);
@@ -3269,6 +3492,8 @@ function _napi_ref_threadsafe_function(env, func) {
3269
3492
  emnapiDefineVar('$emnapiTSFN', emnapiTSFN, [
3270
3493
  '$emnapiInit',
3271
3494
  '$PThread',
3495
+ 'malloc',
3496
+ 'free',
3272
3497
  '_emnapi_node_emit_async_init',
3273
3498
  '_emnapi_node_emit_async_destroy',
3274
3499
  '_emnapi_runtime_keepalive_pop',
@@ -3816,7 +4041,7 @@ function napi_define_class(env, utf8name, length, constructor, callback_data, pr
3816
4041
  {{{ from64("attributes") }}};
3817
4042
  var data_3 = {{{ makeGetValue("propPtr", POINTER_SIZE * 7, "*") }}};
3818
4043
  if (utf8Name_2) {
3819
- propertyName_2 = UTF8ToString(utf8Name_2);
4044
+ propertyName_2 = emnapiString.UTF8ToString(utf8Name_2, -1);
3820
4045
  }
3821
4046
  else {
3822
4047
  if (!name_3) {
@@ -4002,7 +4227,7 @@ function _napi_add_finalizer(env, js_object, finalize_data, finalize_cb, finaliz
4002
4227
  }
4003
4228
  return envObject.clearLastError();
4004
4229
  }
4005
- emnapiImplement('napi_define_class', 'ipppppppp', napi_define_class, ['$emnapiCreateFunction', '$emnapiDefineProperty']);
4230
+ emnapiImplement('napi_define_class', 'ipppppppp', napi_define_class, ['$emnapiCreateFunction', '$emnapiDefineProperty', '$emnapiString']);
4006
4231
  emnapiImplement('napi_wrap', 'ipppppp', napi_wrap, ['$emnapiWrap']);
4007
4232
  emnapiImplement('napi_unwrap', 'ippp', napi_unwrap, ['$emnapiUnwrap']);
4008
4233
  emnapiImplement('napi_remove_wrap', 'ippp', napi_remove_wrap, ['$emnapiUnwrap']);
@@ -4531,12 +4756,12 @@ function napi_get_value_string_utf8(env, value, buf, buf_size, result) {
4531
4756
  if (!result)
4532
4757
  return envObject.setLastError(1 /* napi_status.napi_invalid_arg */);
4533
4758
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
4534
- var strLength = lengthBytesUTF8(handle.value);
4759
+ var strLength = emnapiString.lengthBytesUTF8(handle.value);
4535
4760
  {{{ makeSetValue('result', 0, 'strLength', SIZE_TYPE) }}};
4536
4761
  }
4537
4762
  else if (buf_size !== 0) {
4538
4763
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
4539
- var copied = stringToUTF8(handle.value, buf, buf_size);
4764
+ var copied = emnapiString.stringToUTF8(handle.value, buf, buf_size);
4540
4765
  if (result) {
4541
4766
  {{{ makeSetValue('result', 0, 'copied', SIZE_TYPE) }}};
4542
4767
  }
@@ -4567,7 +4792,7 @@ function napi_get_value_string_utf16(env, value, buf, buf_size, result) {
4567
4792
  }
4568
4793
  else if (buf_size !== 0) {
4569
4794
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
4570
- var copied = stringToUTF16(handle.value, buf, buf_size * 2);
4795
+ var copied = emnapiString.stringToUTF16(handle.value, buf, buf_size * 2);
4571
4796
  if (result) {
4572
4797
  {{{ makeSetValue('result', 0, 'copied / 2', SIZE_TYPE) }}};
4573
4798
  }
@@ -4609,12 +4834,8 @@ emnapiImplement('napi_get_value_external', 'ippp', napi_get_value_external);
4609
4834
  emnapiImplement('napi_get_value_int32', 'ippp', napi_get_value_int32);
4610
4835
  emnapiImplement('napi_get_value_int64', 'ippp', napi_get_value_int64);
4611
4836
  emnapiImplement('napi_get_value_string_latin1', 'ippppp', napi_get_value_string_latin1);
4612
- emnapiImplement('napi_get_value_string_utf8', 'ippppp', napi_get_value_string_utf8);
4613
- #if typeof LEGACY_RUNTIME !== 'undefined' && !LEGACY_RUNTIME
4614
- emnapiImplement('napi_get_value_string_utf16', 'ippppp', napi_get_value_string_utf16, ['$stringToUTF16']);
4615
- #else
4616
- emnapiImplement('napi_get_value_string_utf16', 'ippppp', napi_get_value_string_utf16);
4617
- #endif
4837
+ emnapiImplement('napi_get_value_string_utf8', 'ippppp', napi_get_value_string_utf8, ['$emnapiString']);
4838
+ emnapiImplement('napi_get_value_string_utf16', 'ippppp', napi_get_value_string_utf16, ['$emnapiString']);
4618
4839
  emnapiImplement('napi_get_value_uint32', 'ippp', napi_get_value_uint32);
4619
4840
  /* eslint-disable @typescript-eslint/indent */
4620
4841
  function napi_create_int32(env, value, result) {
@@ -4737,7 +4958,7 @@ function napi_create_string_utf16(env, str, length, result) {
4737
4958
  if (!(autoLength || (sizelength <= 2147483647))) {
4738
4959
  return envObject.setLastError(1 /* napi_status.napi_invalid_arg */);
4739
4960
  }
4740
- var utf16String = emnapiUtf16ToString(str, length);
4961
+ var utf16String = emnapiString.UTF16ToString(str, length);
4741
4962
  {{{ from64('result') }}};
4742
4963
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
4743
4964
  var value = emnapiCtx.addToCurrentScope(utf16String).id;
@@ -4761,7 +4982,7 @@ function napi_create_string_utf8(env, str, length, result) {
4761
4982
  if (!(autoLength || (sizelength <= 2147483647))) {
4762
4983
  return envObject.setLastError(1 /* napi_status.napi_invalid_arg */);
4763
4984
  }
4764
- var utf8String = emnapiUtf8ToString(str, length);
4985
+ var utf8String = emnapiString.UTF8ToString(str, length);
4765
4986
  {{{ from64('result') }}};
4766
4987
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
4767
4988
  var value = emnapiCtx.addToCurrentScope(utf8String).id;
@@ -4873,8 +5094,8 @@ emnapiImplement('napi_create_bigint_int64', 'ipjp', napi_create_bigint_int64);
4873
5094
  emnapiImplement('napi_create_bigint_uint64', 'ipjp', napi_create_bigint_uint64);
4874
5095
  emnapiImplement('napi_create_bigint_words', 'ipippp', napi_create_bigint_words);
4875
5096
  emnapiImplement('napi_create_string_latin1', 'ipppp', napi_create_string_latin1);
4876
- emnapiImplement('napi_create_string_utf16', 'ipppp', napi_create_string_utf16, ['$emnapiUtf16ToString']);
4877
- emnapiImplement('napi_create_string_utf8', 'ipppp', napi_create_string_utf8, ['$emnapiUtf8ToString']);
5097
+ emnapiImplement('napi_create_string_utf16', 'ipppp', napi_create_string_utf16, ['$emnapiString']);
5098
+ emnapiImplement('napi_create_string_utf8', 'ipppp', napi_create_string_utf8, ['$emnapiString']);
4878
5099
  function napi_create_array(env, result) {
4879
5100
  if (env == 0)
4880
5101
  return 1 /* napi_status.napi_invalid_arg */;
@@ -5214,8 +5435,11 @@ function napi_create_buffer(env, size, data, result
5214
5435
  try {
5215
5436
  if (result == 0)
5216
5437
  return envObject_56.setLastError(1 /* napi_status.napi_invalid_arg */);
5217
- {{{ from64("result") }}};
5218
5438
  var Buffer_1 = emnapiCtx.feature.Buffer;
5439
+ if (!Buffer_1) {
5440
+ throw emnapiCtx.createNotSupportBufferError("napi_create_buffer", "");
5441
+ }
5442
+ {{{ from64("result") }}};
5219
5443
  var buffer_2 = void 0;
5220
5444
  {{{ from64("size") }}};
5221
5445
  size = size >>> 0;
@@ -5225,7 +5449,7 @@ function napi_create_buffer(env, size, data, result
5225
5449
  {{{ makeSetValue("result", 0, "value", "*") }}};
5226
5450
  }
5227
5451
  else {
5228
- pointer = {{{ makeMalloc("napi_create_buffer", "size") }}};
5452
+ pointer = _malloc({{{ to64("size") }}});
5229
5453
  if (!pointer)
5230
5454
  throw new Error("Out of memory");
5231
5455
  new Uint8Array(wasmMemory.buffer).subarray(pointer, pointer + size).fill(0);
@@ -5268,8 +5492,8 @@ function napi_create_buffer_copy(env, length, data, result_data, result
5268
5492
  try {
5269
5493
  if (result == 0)
5270
5494
  return envObject_57.setLastError(1 /* napi_status.napi_invalid_arg */);
5271
- var arrayBuffer_3 = emnapiCreateArrayBuffer(length, result_data);
5272
5495
  var Buffer_2 = emnapiCtx.feature.Buffer;
5496
+ var arrayBuffer_3 = emnapiCreateArrayBuffer(length, result_data);
5273
5497
  var buffer_4 = Buffer_2.from(arrayBuffer_3);
5274
5498
  {{{ from64("data") }}};
5275
5499
  {{{ from64("length") }}};
@@ -5362,7 +5586,7 @@ function node_api_symbol_for(env, utf8description, length, result) {
5362
5586
  if (!(autoLength || (sizelength <= 2147483647))) {
5363
5587
  return envObject.setLastError(1 /* napi_status.napi_invalid_arg */);
5364
5588
  }
5365
- var descriptionString = emnapiUtf8ToString(utf8description, length);
5589
+ var descriptionString = emnapiString.UTF8ToString(utf8description, length);
5366
5590
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
5367
5591
  var value = emnapiCtx.addToCurrentScope(Symbol.for(descriptionString)).id;
5368
5592
  {{{ makeSetValue('result', 0, 'value', '*') }}};
@@ -5382,7 +5606,7 @@ emnapiImplement('napi_create_object', 'ipp', napi_create_object);
5382
5606
  emnapiImplement('napi_create_symbol', 'ippp', napi_create_symbol);
5383
5607
  emnapiImplement('napi_create_typedarray', 'ipipppp', napi_create_typedarray, ['$emnapiExternalMemory']);
5384
5608
  emnapiImplement('napi_create_dataview', 'ippppp', napi_create_dataview, ['$emnapiExternalMemory']);
5385
- emnapiImplement('node_api_symbol_for', 'ipppp', node_api_symbol_for, ['$emnapiUtf8ToString']);
5609
+ emnapiImplement('node_api_symbol_for', 'ipppp', node_api_symbol_for, ['$emnapiString']);
5386
5610
  function napi_get_boolean(env, value, result) {
5387
5611
  if (env == 0)
5388
5612
  return 1 /* napi_status.napi_invalid_arg */;
@@ -5623,7 +5847,7 @@ function emnapi_get_module_property(env, utf8name, result) {
5623
5847
  {{{ from64("utf8name") }}};
5624
5848
  {{{ from64("result") }}};
5625
5849
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
5626
- value = envObject_60.ensureHandleId(Module[UTF8ToString(utf8name)]);
5850
+ value = envObject_60.ensureHandleId(Module[emnapiString.UTF8ToString(utf8name, -1)]);
5627
5851
  {{{ makeSetValue("result", 0, "value", "*") }}};
5628
5852
  return envObject_60.getReturnStatus();
5629
5853
  }
@@ -5633,7 +5857,7 @@ function emnapi_get_module_property(env, utf8name, result) {
5633
5857
  }
5634
5858
  }
5635
5859
  emnapiImplement2('emnapi_get_module_object', 'ipp', emnapi_get_module_object);
5636
- emnapiImplement2('emnapi_get_module_property', 'ippp', emnapi_get_module_property);
5860
+ emnapiImplement2('emnapi_get_module_property', 'ippp', emnapi_get_module_property, ['$emnapiString']);
5637
5861
  emnapiDefineVar('$emnapiCtx', undefined);
5638
5862
  emnapiDefineVar('$emnapiNodeBinding', undefined);
5639
5863
  emnapiDefineVar('$emnapiModule', {
@@ -5703,139 +5927,11 @@ function __emnapi_async_work_pool_size() {
5703
5927
  emnapiImplementInternal('_emnapi_async_work_pool_size', 'i', __emnapi_async_work_pool_size, ['$emnapiAsyncWorkPoolSize']);
5704
5928
  function __emnapi_get_filename(buf, len) {
5705
5929
  if (!buf) {
5706
- return lengthBytesUTF8(emnapiModule.filename);
5930
+ return emnapiString.lengthBytesUTF8(emnapiModule.filename);
5707
5931
  }
5708
- return stringToUTF8(emnapiModule.filename, buf, len);
5932
+ return emnapiString.stringToUTF8(emnapiModule.filename, buf, len);
5709
5933
  }
5710
- emnapiImplementInternal('_emnapi_get_filename', 'ipi', __emnapi_get_filename);
5711
- mergeInto(LibraryManager.library, {
5712
- $emnapiUtf8Decoder__postset: 'emnapiUtf8Decoder();',
5713
- $emnapiUtf8Decoder: function () {
5714
- #if !TEXTDECODER || TEXTDECODER == 1
5715
- var fallbackDecoder = {
5716
- decode: function (input) {
5717
- var isArrayBuffer = input instanceof ArrayBuffer;
5718
- var isView = ArrayBuffer.isView(input);
5719
- if (!isArrayBuffer && !isView) {
5720
- throw new TypeError('The "input" argument must be an instance of ArrayBuffer or ArrayBufferView');
5721
- }
5722
- var bytes = isArrayBuffer ? new Uint8Array(input) : new Uint8Array(input.buffer, input.byteOffset, input.byteLength);
5723
- var inputIndex = 0;
5724
- var pendingSize = Math.min(256 * 256, bytes.length + 1);
5725
- var pending = new Uint16Array(pendingSize);
5726
- var chunks = [];
5727
- var pendingIndex = 0;
5728
- for (;;) {
5729
- var more = inputIndex < bytes.length;
5730
- if (!more || (pendingIndex >= pendingSize - 1)) {
5731
- var subarray = pending.subarray(0, pendingIndex);
5732
- var arraylike = subarray;
5733
- chunks.push(String.fromCharCode.apply(null, arraylike));
5734
- if (!more) {
5735
- return chunks.join('');
5736
- }
5737
- bytes = bytes.subarray(inputIndex);
5738
- inputIndex = 0;
5739
- pendingIndex = 0;
5740
- }
5741
- var byte1 = bytes[inputIndex++];
5742
- if ((byte1 & 0x80) === 0) {
5743
- pending[pendingIndex++] = byte1;
5744
- }
5745
- else if ((byte1 & 0xe0) === 0xc0) {
5746
- var byte2 = bytes[inputIndex++] & 0x3f;
5747
- pending[pendingIndex++] = ((byte1 & 0x1f) << 6) | byte2;
5748
- }
5749
- else if ((byte1 & 0xf0) === 0xe0) {
5750
- var byte2 = bytes[inputIndex++] & 0x3f;
5751
- var byte3 = bytes[inputIndex++] & 0x3f;
5752
- pending[pendingIndex++] = ((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3;
5753
- }
5754
- else if ((byte1 & 0xf8) === 0xf0) {
5755
- var byte2 = bytes[inputIndex++] & 0x3f;
5756
- var byte3 = bytes[inputIndex++] & 0x3f;
5757
- var byte4 = bytes[inputIndex++] & 0x3f;
5758
- var codepoint = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
5759
- if (codepoint > 0xffff) {
5760
- codepoint -= 0x10000;
5761
- pending[pendingIndex++] = (codepoint >>> 10) & 0x3ff | 0xd800;
5762
- codepoint = 0xdc00 | codepoint & 0x3ff;
5763
- }
5764
- pending[pendingIndex++] = codepoint;
5765
- }
5766
- else {
5767
- }
5768
- }
5769
- }
5770
- };
5771
- #endif
5772
- var tmp; // typescript bug
5773
- #if !TEXTDECODER
5774
- tmp = fallbackDecoder;
5775
- #elif TEXTDECODER == 1
5776
- tmp = typeof TextDecoder === 'function' ? new TextDecoder() : fallbackDecoder;
5777
- #elif TEXTDECODER == 2
5778
- tmp = new TextDecoder();
5779
- #endif
5780
- emnapiUtf8Decoder = tmp;
5781
- },
5782
- $emnapiUtf8ToString__deps: ['$emnapiUtf8Decoder'],
5783
- $emnapiUtf8ToString: function (ptr, length) {
5784
- // eslint-disable-next-line eqeqeq
5785
- if (length == -1) {
5786
- return UTF8ToString(ptr);
5787
- }
5788
- length = length >>> 0;
5789
- if (!length)
5790
- return '';
5791
- return emnapiUtf8Decoder.decode({{{ getUnsharedTextDecoderView('HEAPU8', 'ptr', 'ptr + length') }}});
5792
- },
5793
- $emnapiUtf16leDecoder__postset: 'emnapiUtf16leDecoder();',
5794
- $emnapiUtf16leDecoder: function () {
5795
- #if !TEXTDECODER || TEXTDECODER == 1
5796
- var fallbackDecoder = {
5797
- decode: function (input) {
5798
- var isArrayBuffer = input instanceof ArrayBuffer;
5799
- var isView = ArrayBuffer.isView(input);
5800
- if (!isArrayBuffer && !isView) {
5801
- throw new TypeError('The "input" argument must be an instance of ArrayBuffer or ArrayBufferView');
5802
- }
5803
- var bytes = isArrayBuffer ? new Uint16Array(input) : new Uint16Array(input.buffer, input.byteOffset, input.byteLength / 2);
5804
- var wcharArray = Array(bytes.length);
5805
- for (var i = 0; i < bytes.length; ++i) {
5806
- wcharArray[i] = String.fromCharCode(bytes[i]);
5807
- }
5808
- return wcharArray.join('');
5809
- }
5810
- };
5811
- #endif
5812
- var tmp; // typescript bug
5813
- #if !TEXTDECODER
5814
- tmp = fallbackDecoder;
5815
- #elif TEXTDECODER == 1
5816
- tmp = typeof TextDecoder === 'function' ? new TextDecoder('utf-16le') : fallbackDecoder;
5817
- #elif TEXTDECODER == 2
5818
- tmp = new TextDecoder('utf-16le');
5819
- #endif
5820
- emnapiUtf16leDecoder = tmp;
5821
- },
5822
- $emnapiUtf16ToString__deps: [
5823
- #if typeof LEGACY_RUNTIME !== 'undefined' && !LEGACY_RUNTIME
5824
- '$UTF16ToString',
5825
- #endif
5826
- '$emnapiUtf16leDecoder'
5827
- ],
5828
- $emnapiUtf16ToString: function (ptr, length) {
5829
- // eslint-disable-next-line eqeqeq
5830
- if (length == -1) {
5831
- return UTF16ToString(ptr);
5832
- }
5833
- length = length >>> 0;
5834
- if (!length)
5835
- return '';
5836
- return emnapiUtf16leDecoder.decode({{{ getUnsharedTextDecoderView('HEAPU8', 'ptr', 'ptr + length * 2') }}});
5837
- }
5838
- });
5934
+ emnapiImplementInternal('_emnapi_get_filename', 'ipi', __emnapi_get_filename, ['$emnapiString']);
5839
5935
  /* eslint-disable @typescript-eslint/no-unused-vars */
5840
5936
  function emnapiImplement(name, sig, compilerTimeFunction, deps) {
5841
5937
  var _a;