jseption 0.0.4 → 0.0.6

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.
Files changed (90) hide show
  1. package/README.md +56 -4
  2. package/dist/atoms.d.ts +1 -0
  3. package/dist/atoms.js +2 -1
  4. package/dist/atoms.js.map +1 -1
  5. package/dist/builtins/base64.d.ts +14 -0
  6. package/dist/builtins/base64.js +219 -0
  7. package/dist/builtins/base64.js.map +1 -0
  8. package/dist/builtins/bigint.js +3 -3
  9. package/dist/builtins/bigint.js.map +1 -1
  10. package/dist/builtins/date.d.ts +1 -0
  11. package/dist/builtins/date.js +26 -12
  12. package/dist/builtins/date.js.map +1 -1
  13. package/dist/builtins/error.js +9 -0
  14. package/dist/builtins/error.js.map +1 -1
  15. package/dist/builtins/float16.d.ts +3 -0
  16. package/dist/builtins/float16.js +69 -0
  17. package/dist/builtins/float16.js.map +1 -0
  18. package/dist/builtins/function.js +2 -2
  19. package/dist/builtins/index.js +9 -6
  20. package/dist/builtins/index.js.map +1 -1
  21. package/dist/builtins/iterator.d.ts +2 -0
  22. package/dist/builtins/iterator.js +624 -0
  23. package/dist/builtins/iterator.js.map +1 -0
  24. package/dist/builtins/json.d.ts +13 -1
  25. package/dist/builtins/json.js +283 -211
  26. package/dist/builtins/json.js.map +1 -1
  27. package/dist/builtins/mapset.js +365 -6
  28. package/dist/builtins/mapset.js.map +1 -1
  29. package/dist/builtins/math.js +203 -3
  30. package/dist/builtins/math.js.map +1 -1
  31. package/dist/builtins/native.js +6 -6
  32. package/dist/builtins/object.js +9 -2
  33. package/dist/builtins/object.js.map +1 -1
  34. package/dist/builtins/promise.js +31 -11
  35. package/dist/builtins/promise.js.map +1 -1
  36. package/dist/builtins/proxy.js +2 -2
  37. package/dist/builtins/reflect.js +6 -2
  38. package/dist/builtins/reflect.js.map +1 -1
  39. package/dist/builtins/regexp.js +74 -3
  40. package/dist/builtins/regexp.js.map +1 -1
  41. package/dist/builtins/string.js +1 -1
  42. package/dist/builtins/symbol.js +1 -1
  43. package/dist/builtins/symbol.js.map +1 -1
  44. package/dist/builtins/typedarray.d.ts +1 -0
  45. package/dist/builtins/typedarray.js +302 -32
  46. package/dist/builtins/typedarray.js.map +1 -1
  47. package/dist/builtins/weakref.js +7 -7
  48. package/dist/embed.d.ts +5 -0
  49. package/dist/embed.js +47 -15
  50. package/dist/embed.js.map +1 -1
  51. package/dist/interpreter.js +15 -14
  52. package/dist/interpreter.js.map +1 -1
  53. package/dist/jit.d.ts +1 -0
  54. package/dist/jit.js +51 -13
  55. package/dist/jit.js.map +1 -1
  56. package/dist/jit_hook.d.ts +1 -0
  57. package/dist/jit_hook.js.map +1 -1
  58. package/dist/jit_runtime.d.ts +1 -1
  59. package/dist/jit_runtime.js +6 -6
  60. package/dist/jit_runtime.js.map +1 -1
  61. package/dist/jit_stack.d.ts +1 -0
  62. package/dist/jit_stack.js +2 -1
  63. package/dist/jit_stack.js.map +1 -1
  64. package/dist/jseption.d.ts +4 -0
  65. package/dist/jseption.js +2 -0
  66. package/dist/jseption.js.map +1 -1
  67. package/dist/membrane.d.ts +16 -0
  68. package/dist/membrane.js +576 -0
  69. package/dist/membrane.js.map +1 -0
  70. package/dist/module.js +5 -3
  71. package/dist/module.js.map +1 -1
  72. package/dist/parser.js +26 -9
  73. package/dist/parser.js.map +1 -1
  74. package/dist/print.d.ts +12 -0
  75. package/dist/print.js +494 -0
  76. package/dist/print.js.map +1 -0
  77. package/dist/qjs.js +13 -10
  78. package/dist/qjs.js.map +1 -1
  79. package/dist/quickjs.d.ts +8 -0
  80. package/dist/quickjs.js +132 -53
  81. package/dist/quickjs.js.map +1 -1
  82. package/dist/std.d.ts +4 -2
  83. package/dist/std.js +42 -8
  84. package/dist/std.js.map +1 -1
  85. package/dist/values.d.ts +31 -27
  86. package/dist/values.js.map +1 -1
  87. package/dist/version.d.ts +1 -1
  88. package/dist/version.js +1 -1
  89. package/dist/version.js.map +1 -1
  90. package/package.json +1 -1
@@ -8,27 +8,37 @@
8
8
  * access goes through host typed array / DataView views created over it.
9
9
  */
10
10
  import * as Q from "../quickjs.js";
11
- import { JS_EXCEPTION, JS_IsException, JS_IsUndefined, JS_IsObject, JS_IsNull, JSArrayBuffer, JSTypedArray, JS_NewObject, JS_NewObjectProto, JS_NewArray, JS_NewCFunction2, JS_NewCFunction3, JS_DefinePropertyValue, JS_DefinePropertyValueStr, JS_DefinePropertyValueInt64, JS_PROP_WRITABLE, JS_PROP_CONFIGURABLE, JS_PROP_C_W_E, JS_PROP_THROW, JS_GetProperty, JS_GetPropertyInt64, JS_SetPropertyInt64, JS_GetPropertyUint32, JS_SetPropertyUint32, JS_GetPropertyValue, JS_SetPropertyValue, JS_ToString, JS_ToBoolFree, JS_ToFloat64, JS_ToUint32Free, JS_ToUint8ClampFree, JS_ToBigInt64, JS_ToIndex, JS_ToInt64Sat, JS_ToInt32Clamp, JS_ToInt64Clamp, JS_ToLengthFree, JS_ToPrimitive, HINT_NUMBER, JS_ToObject, js_get_length64, JS_ThrowTypeError, JS_ThrowRangeError, JS_ThrowTypeErrorInvalidClass, JS_IsFunction, JS_Call, JS_CallConstructor, JS_Invoke, js_create_from_ctor, js_same_value, JS_GetIterator2, JS_IteratorNext, JS_CreateDataPropertyUint32, JS_GetOpaque2, JS_AtomToString, JS_SetPropertyFunctionList, JS_NewGlobalCConstructorOnly, JS_NewGlobalCConstructor2, JS_SetConstructor, JS_CFUNC_DEF, JS_CFUNC_MAGIC_DEF, JS_NATIVE_DEF, JS_CGETSET_DEF, JS_CGETSET_MAGIC_DEF, JS_ALIAS_DEF, JS_PROP_STRING_DEF, JS_ATOM_length, JS_ATOM_toString, JS_ATOM_toLocaleString, JS_ATOM_join, JS_ATOM_next, JS_ATOM_set, JS_ATOM_Symbol_iterator, JS_ATOM_Symbol_species, JS_ATOM_Symbol_toStringTag, JS_ATOM_Uint8ClampedArray, } from "../quickjs.js";
11
+ import { JS_EXCEPTION, JS_IsException, JS_IsUndefined, JS_IsObject, JS_IsNull, JSArrayBuffer, JSTypedArray, JS_NewObject, JS_NewObjectProto, JS_NewArray, JS_NewCFunction2, JS_NewCFunction3, JS_DefinePropertyValue, JS_DefinePropertyValueStr, JS_DefinePropertyValueInt64, JS_PROP_WRITABLE, JS_PROP_CONFIGURABLE, JS_PROP_C_W_E, JS_PROP_THROW, JS_GetProperty, JS_GetPropertyInt64, JS_SetPropertyInt64, JS_GetPropertyUint32, JS_SetPropertyUint32, JS_GetPropertyValue, JS_SetPropertyValue, JS_ToString, JS_ToBoolFree, JS_ToFloat64, JS_ToUint32Free, JS_ToUint8ClampFree, JS_ToBigInt64, JS_ToIndex, JS_ToInt64Sat, JS_ToInt32Clamp, JS_ToInt64Clamp, JS_ToLengthFree, JS_ToPrimitive, HINT_NUMBER, JS_ToObject, js_get_length64, JS_ThrowTypeError, JS_ThrowRangeError, JS_ThrowSyntaxError, JS_GetPropertyStr, JS_ThrowTypeErrorInvalidClass, JS_IsFunction, JS_Call, JS_CallConstructor, JS_Invoke, js_create_from_ctor, js_same_value, JS_GetIterator2, JS_IteratorNext, JS_CreateDataPropertyUint32, JS_GetOpaque2, JS_AtomToString, JS_SetPropertyFunctionList, JS_NewGlobalCConstructorOnly, JS_NewGlobalCConstructor2, JS_SetConstructor, JS_CFUNC_DEF, JS_CFUNC_MAGIC_DEF, JS_NATIVE_DEF, JS_CGETSET_DEF, JS_CGETSET_MAGIC_DEF, JS_ALIAS_DEF, JS_PROP_STRING_DEF, JS_ATOM_length, JS_ATOM_toString, JS_ATOM_toLocaleString, JS_ATOM_join, JS_ATOM_next, JS_ATOM_set, JS_ATOM_Symbol_iterator, JS_ATOM_Symbol_species, JS_ATOM_Symbol_toStringTag, JS_ATOM_Uint8ClampedArray, } from "../quickjs.js";
12
12
  import { JS_SpeciesConstructor, check_function } from "./promise.js";
13
13
  import { fix_array_iterator } from "./native.js";
14
+ import { tofp16, fromfp16, f16round } from "./float16.js";
15
+ import { B64_ALPHABET_BASE64, B64_ALPHABET_BASE64URL, B64_LAST_LOOSE, B64_LAST_STRICT, B64_LAST_STOP_BEFORE_PARTIAL, b64_encode, from_base64, u8a_hex_encode, u8a_hex_decode, } from "./base64.js";
14
16
  const HOST_TYPED_ARRAY_PROTO = Object.getPrototypeOf(Uint8Array.prototype);
15
17
  /* JS_ITERATOR_KIND_xxx (same values as JSIteratorKind in index.ts) */
16
18
  const JS_ITERATOR_KIND_KEY = 0;
17
19
  const JS_ITERATOR_KIND_VALUE = 1;
18
20
  const JS_ITERATOR_KIND_KEY_AND_VALUE = 2;
19
- const JS_TYPED_ARRAY_COUNT = 31 /* JSClassID.FLOAT64_ARRAY */ - 21 /* JSClassID.UINT8C_ARRAY */ + 1;
21
+ const JS_TYPED_ARRAY_COUNT = 32 /* JSClassID.FLOAT64_ARRAY */ - 21 /* JSClassID.UINT8C_ARRAY */ + 1;
20
22
  const typed_array_size_log2_tab = [
21
23
  0, 0, 0, 1, 1, 2, 2,
22
24
  3, 3, /* BigInt64Array, BigUint64Array */
23
- 2, 3,
25
+ 1, 2, 3, /* Float16Array, Float32Array, Float64Array */
24
26
  ];
25
27
  function typed_array_size_log2(class_id) {
26
28
  return typed_array_size_log2_tab[class_id - 21 /* JSClassID.UINT8C_ARRAY */];
27
29
  }
30
+ /* Guest typed arrays are host typed arrays, so Float16Array exists in the
31
+ guest only when the host has it (its class slot stays empty otherwise).
32
+ Math.f16round and DataView's float16 methods do not need it. */
33
+ const HostFloat16Array = globalThis.Float16Array;
28
34
  const host_ta_ctors = [
29
35
  Uint8ClampedArray, Int8Array, Uint8Array, Int16Array, Uint16Array, Int32Array, Uint32Array,
30
- BigInt64Array, BigUint64Array, Float32Array, Float64Array,
36
+ BigInt64Array, BigUint64Array, HostFloat16Array, Float32Array, Float64Array,
31
37
  ];
38
+ /* true if the host provides the guest typed array class 'class_id' */
39
+ export function typed_array_class_available(class_id) {
40
+ return host_ta_ctors[class_id - 21 /* JSClassID.UINT8C_ARRAY */] !== undefined;
41
+ }
32
42
  /* ---------------------------------------------------------------------
33
43
  ArrayBuffer
34
44
  --------------------------------------------------------------------- */
@@ -116,7 +126,7 @@ function js_array_buffer_constructor0(ctx, new_target, argv, class_id) {
116
126
  if (max_len < 0)
117
127
  return JS_EXCEPTION;
118
128
  if (len > max_len)
119
- return JS_ThrowRangeError(ctx, 'invalid array buffer length');
129
+ return JS_ThrowRangeError(ctx, 'invalid array buffer max length');
120
130
  }
121
131
  }
122
132
  return js_array_buffer_constructor3(ctx, new_target, len, max_len, class_id, null);
@@ -130,7 +140,7 @@ function js_shared_array_buffer_constructor(ctx, new_target, argv) {
130
140
  }
131
141
  function js_array_buffer_isView(ctx, this_val, argv) {
132
142
  const v = argv[0];
133
- return Q.JS_IsObject(v) && Q.JS_GetClassID(v) >= 21 /* JSClassID.UINT8C_ARRAY */ && Q.JS_GetClassID(v) <= 32 /* JSClassID.DATAVIEW */;
143
+ return Q.JS_IsObject(v) && Q.JS_GetClassID(v) >= 21 /* JSClassID.UINT8C_ARRAY */ && Q.JS_GetClassID(v) <= 33 /* JSClassID.DATAVIEW */;
134
144
  }
135
145
  function js_get_this(ctx, this_val) { return this_val; }
136
146
  const js_array_buffer_funcs = [
@@ -316,12 +326,12 @@ const js_shared_array_buffer_proto_funcs = [
316
326
  Typed arrays
317
327
  --------------------------------------------------------------------- */
318
328
  export function is_typed_array_class(class_id) {
319
- return class_id >= 21 /* JSClassID.UINT8C_ARRAY */ && class_id <= 31 /* JSClassID.FLOAT64_ARRAY */;
329
+ return class_id >= 21 /* JSClassID.UINT8C_ARRAY */ && class_id <= 32 /* JSClassID.FLOAT64_ARRAY */;
320
330
  }
321
331
  function get_typed_array(ctx, this_val, is_dataview) {
322
332
  if (Q.JS_IsObject(this_val)) {
323
333
  if (is_dataview) {
324
- if (Q.JS_GetClassID(this_val) === 32 /* JSClassID.DATAVIEW */)
334
+ if (Q.JS_GetClassID(this_val) === 33 /* JSClassID.DATAVIEW */)
325
335
  return this_val;
326
336
  }
327
337
  else {
@@ -357,7 +367,7 @@ function typed_array_get_byte_length(ta, size_log2) {
357
367
  }
358
368
  /* byte length of a typed array or DataView object (-1 if detached/OOB) */
359
369
  function js_typed_array_byte_length(p) {
360
- const size_log2 = Q.JS_GetClassID(p) === 32 /* JSClassID.DATAVIEW */ ? 0 : typed_array_size_log2(Q.JS_GetClassID(p));
370
+ const size_log2 = Q.JS_GetClassID(p) === 33 /* JSClassID.DATAVIEW */ ? 0 : typed_array_size_log2(Q.JS_GetClassID(p));
361
371
  return typed_array_get_byte_length(Q.JS_GetAnyOpaque(p), size_log2);
362
372
  }
363
373
  /* return true if the typed array is detached or out of bounds (resized buffer) */
@@ -951,7 +961,25 @@ function js_typed_array_indexOf(ctx, this_val, argv, special) {
951
961
  scan(d);
952
962
  break;
953
963
  }
954
- case 30 /* JSClassID.FLOAT32_ARRAY */:
964
+ case 30 /* JSClassID.FLOAT16_ARRAY */:
965
+ if (is_bigint)
966
+ break;
967
+ if (Number.isNaN(d)) {
968
+ /* special case: indexOf returns -1, includes finds NaN */
969
+ if (special !== special_includes)
970
+ return done();
971
+ for (; k !== stop; k += inc) {
972
+ if (Number.isNaN(view[k])) {
973
+ res = k;
974
+ break;
975
+ }
976
+ }
977
+ }
978
+ else if (f16round(d) === d) {
979
+ scan(d);
980
+ }
981
+ break;
982
+ case 31 /* JSClassID.FLOAT32_ARRAY */:
955
983
  if (is_bigint)
956
984
  break;
957
985
  if (Number.isNaN(d)) {
@@ -969,7 +997,7 @@ function js_typed_array_indexOf(ctx, this_val, argv, special) {
969
997
  scan(d);
970
998
  }
971
999
  break;
972
- case 31 /* JSClassID.FLOAT64_ARRAY */:
1000
+ case 32 /* JSClassID.FLOAT64_ARRAY */:
973
1001
  if (is_bigint)
974
1002
  break;
975
1003
  if (Number.isNaN(d)) {
@@ -1559,10 +1587,16 @@ function js_typed_array_constructor(ctx, new_target, argv, classid) {
1559
1587
  let len, offset;
1560
1588
  let track_rab = false;
1561
1589
  const arg0 = argv[0];
1590
+ /* the object (its prototype read from new.target) is created before the
1591
+ offset and the length are converted (QuickJS 68caa5f) */
1592
+ let obj;
1562
1593
  if (!Q.JS_IsObject(arg0)) {
1563
1594
  len = JS_ToIndex(ctx, arg0);
1564
1595
  if (len < 0)
1565
1596
  return JS_EXCEPTION;
1597
+ obj = js_create_from_ctor(ctx, new_target, classid);
1598
+ if (JS_IsException(obj))
1599
+ return JS_EXCEPTION;
1566
1600
  buffer = js_array_buffer_constructor1(ctx, undefined, len << size_log2);
1567
1601
  if (JS_IsException(buffer))
1568
1602
  return JS_EXCEPTION;
@@ -1571,15 +1605,20 @@ function js_typed_array_constructor(ctx, new_target, argv, classid) {
1571
1605
  else {
1572
1606
  const p = arg0;
1573
1607
  if (Q.JS_GetClassID(p) === 19 /* JSClassID.ARRAY_BUFFER */ || Q.JS_GetClassID(p) === 20 /* JSClassID.SHARED_ARRAY_BUFFER */) {
1608
+ obj = js_create_from_ctor(ctx, new_target, classid);
1609
+ if (JS_IsException(obj))
1610
+ return JS_EXCEPTION;
1574
1611
  const abuf = Q.JS_GetAnyOpaque(p);
1575
1612
  offset = JS_ToIndex(ctx, argv[1]);
1576
1613
  if (offset < 0)
1577
1614
  return JS_EXCEPTION;
1578
- if (abuf.detached)
1579
- return JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
1580
- if ((offset & ((1 << size_log2) - 1)) !== 0 || offset > abuf.data.length)
1615
+ if ((offset & ((1 << size_log2) - 1)) !== 0)
1581
1616
  return JS_ThrowRangeError(ctx, 'invalid offset');
1582
1617
  if (JS_IsUndefined(argv[2])) {
1618
+ if (abuf.detached)
1619
+ return JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
1620
+ if (offset > abuf.data.length)
1621
+ return JS_ThrowRangeError(ctx, 'invalid offset');
1583
1622
  if (array_buffer_is_resizable(abuf)) {
1584
1623
  track_rab = true;
1585
1624
  len = (abuf.data.length - offset) >> size_log2;
@@ -1610,9 +1649,6 @@ function js_typed_array_constructor(ctx, new_target, argv, classid) {
1610
1649
  }
1611
1650
  }
1612
1651
  }
1613
- const obj = js_create_from_ctor(ctx, new_target, classid);
1614
- if (JS_IsException(obj))
1615
- return JS_EXCEPTION;
1616
1652
  return typed_array_init(ctx, obj, buffer, offset, len, track_rab);
1617
1653
  }
1618
1654
  /* ---------------------------------------------------------------------
@@ -1635,6 +1671,7 @@ function js_dataview_constructor(ctx, new_target, argv) {
1635
1671
  return JS_ThrowRangeError(ctx, 'invalid byteOffset');
1636
1672
  let len = abuf.data.length - offset;
1637
1673
  let track_rab = false;
1674
+ let recompute_len = false;
1638
1675
  if (argv.length > 2 && !JS_IsUndefined(argv[2])) {
1639
1676
  const l = JS_ToIndex(ctx, argv[2]);
1640
1677
  if (l < 0)
@@ -1644,18 +1681,24 @@ function js_dataview_constructor(ctx, new_target, argv) {
1644
1681
  len = l;
1645
1682
  }
1646
1683
  else {
1684
+ recompute_len = true;
1647
1685
  track_rab = array_buffer_is_resizable(abuf);
1648
1686
  }
1649
- const obj = js_create_from_ctor(ctx, new_target, 32 /* JSClassID.DATAVIEW */);
1687
+ const obj = js_create_from_ctor(ctx, new_target, 33 /* JSClassID.DATAVIEW */);
1650
1688
  if (JS_IsException(obj))
1651
1689
  return JS_EXCEPTION;
1652
1690
  if (abuf.detached) {
1653
1691
  /* could have been detached in js_create_from_ctor() */
1654
1692
  return JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
1655
1693
  }
1656
- /* the buffer could have been resized in js_create_from_ctor() */
1657
- if (offset > abuf.data.length || (!track_rab && offset + len > abuf.data.length))
1658
- return JS_ThrowTypeErrorArrayBufferOOB(ctx);
1694
+ /* the buffer could have been resized in js_create_from_ctor() (a RangeError, as in
1695
+ QuickJS 2026-06-04 and the specification) */
1696
+ if (offset > abuf.data.length)
1697
+ return JS_ThrowRangeError(ctx, 'invalid byteOffset or byteLength');
1698
+ if (recompute_len)
1699
+ len = abuf.data.length - offset;
1700
+ else if (offset + len > abuf.data.length)
1701
+ return JS_ThrowRangeError(ctx, 'invalid byteOffset or byteLength');
1659
1702
  const ta = new JSTypedArray(buffer, offset, len);
1660
1703
  ta.track_rab = track_rab;
1661
1704
  let view;
@@ -1668,7 +1711,7 @@ function js_dataview_constructor(ctx, new_target, argv) {
1668
1711
  return host_view_adopt(ctx, obj, view, ta);
1669
1712
  }
1670
1713
  function js_dataview_getValue(ctx, this_obj, argv, class_id) {
1671
- const ta = JS_GetOpaque2(ctx, this_obj, 32 /* JSClassID.DATAVIEW */);
1714
+ const ta = JS_GetOpaque2(ctx, this_obj, 33 /* JSClassID.DATAVIEW */);
1672
1715
  if (!ta)
1673
1716
  return JS_EXCEPTION;
1674
1717
  const size = 1 << typed_array_size_log2(class_id);
@@ -1695,13 +1738,14 @@ function js_dataview_getValue(ctx, this_obj, argv, class_id) {
1695
1738
  case 27 /* JSClassID.UINT32_ARRAY */: return dv.getUint32(ptr, littleEndian);
1696
1739
  case 28 /* JSClassID.BIG_INT64_ARRAY */: return dv.getBigInt64(ptr, littleEndian);
1697
1740
  case 29 /* JSClassID.BIG_UINT64_ARRAY */: return dv.getBigUint64(ptr, littleEndian);
1698
- case 30 /* JSClassID.FLOAT32_ARRAY */: return dv.getFloat32(ptr, littleEndian);
1699
- case 31 /* JSClassID.FLOAT64_ARRAY */: return dv.getFloat64(ptr, littleEndian);
1741
+ case 30 /* JSClassID.FLOAT16_ARRAY */: return fromfp16(dv.getUint16(ptr, littleEndian));
1742
+ case 31 /* JSClassID.FLOAT32_ARRAY */: return dv.getFloat32(ptr, littleEndian);
1743
+ case 32 /* JSClassID.FLOAT64_ARRAY */: return dv.getFloat64(ptr, littleEndian);
1700
1744
  default: return JS_ThrowTypeError(ctx, 'invalid DataView type');
1701
1745
  }
1702
1746
  }
1703
1747
  function js_dataview_setValue(ctx, this_obj, argv, class_id) {
1704
- const ta = JS_GetOpaque2(ctx, this_obj, 32 /* JSClassID.DATAVIEW */);
1748
+ const ta = JS_GetOpaque2(ctx, this_obj, 33 /* JSClassID.DATAVIEW */);
1705
1749
  if (!ta)
1706
1750
  return JS_EXCEPTION;
1707
1751
  const size = 1 << typed_array_size_log2(class_id);
@@ -1753,14 +1797,17 @@ function js_dataview_setValue(ctx, this_obj, argv, class_id) {
1753
1797
  case 27 /* JSClassID.UINT32_ARRAY */:
1754
1798
  dv.setUint32(ptr, v, littleEndian);
1755
1799
  break;
1756
- case 30 /* JSClassID.FLOAT32_ARRAY */:
1800
+ case 30 /* JSClassID.FLOAT16_ARRAY */:
1801
+ dv.setUint16(ptr, tofp16(v), littleEndian);
1802
+ break;
1803
+ case 31 /* JSClassID.FLOAT32_ARRAY */:
1757
1804
  dv.setFloat32(ptr, v, littleEndian);
1758
1805
  break;
1759
1806
  case 28 /* JSClassID.BIG_INT64_ARRAY */:
1760
1807
  case 29 /* JSClassID.BIG_UINT64_ARRAY */:
1761
1808
  dv.setBigInt64(ptr, v64, littleEndian);
1762
1809
  break;
1763
- case 31 /* JSClassID.FLOAT64_ARRAY */:
1810
+ case 32 /* JSClassID.FLOAT64_ARRAY */:
1764
1811
  dv.setFloat64(ptr, v, littleEndian);
1765
1812
  break;
1766
1813
  default:
@@ -1780,8 +1827,9 @@ const js_dataview_proto_funcs = [
1780
1827
  JS_CFUNC_MAGIC_DEF('getUint32', 1, js_dataview_getValue, 27 /* JSClassID.UINT32_ARRAY */),
1781
1828
  JS_CFUNC_MAGIC_DEF('getBigInt64', 1, js_dataview_getValue, 28 /* JSClassID.BIG_INT64_ARRAY */),
1782
1829
  JS_CFUNC_MAGIC_DEF('getBigUint64', 1, js_dataview_getValue, 29 /* JSClassID.BIG_UINT64_ARRAY */),
1783
- JS_CFUNC_MAGIC_DEF('getFloat32', 1, js_dataview_getValue, 30 /* JSClassID.FLOAT32_ARRAY */),
1784
- JS_CFUNC_MAGIC_DEF('getFloat64', 1, js_dataview_getValue, 31 /* JSClassID.FLOAT64_ARRAY */),
1830
+ JS_CFUNC_MAGIC_DEF('getFloat16', 1, js_dataview_getValue, 30 /* JSClassID.FLOAT16_ARRAY */),
1831
+ JS_CFUNC_MAGIC_DEF('getFloat32', 1, js_dataview_getValue, 31 /* JSClassID.FLOAT32_ARRAY */),
1832
+ JS_CFUNC_MAGIC_DEF('getFloat64', 1, js_dataview_getValue, 32 /* JSClassID.FLOAT64_ARRAY */),
1785
1833
  JS_CFUNC_MAGIC_DEF('setInt8', 2, js_dataview_setValue, 22 /* JSClassID.INT8_ARRAY */),
1786
1834
  JS_CFUNC_MAGIC_DEF('setUint8', 2, js_dataview_setValue, 23 /* JSClassID.UINT8_ARRAY */),
1787
1835
  JS_CFUNC_MAGIC_DEF('setInt16', 2, js_dataview_setValue, 24 /* JSClassID.INT16_ARRAY */),
@@ -1790,10 +1838,226 @@ const js_dataview_proto_funcs = [
1790
1838
  JS_CFUNC_MAGIC_DEF('setUint32', 2, js_dataview_setValue, 27 /* JSClassID.UINT32_ARRAY */),
1791
1839
  JS_CFUNC_MAGIC_DEF('setBigInt64', 2, js_dataview_setValue, 28 /* JSClassID.BIG_INT64_ARRAY */),
1792
1840
  JS_CFUNC_MAGIC_DEF('setBigUint64', 2, js_dataview_setValue, 29 /* JSClassID.BIG_UINT64_ARRAY */),
1793
- JS_CFUNC_MAGIC_DEF('setFloat32', 2, js_dataview_setValue, 30 /* JSClassID.FLOAT32_ARRAY */),
1794
- JS_CFUNC_MAGIC_DEF('setFloat64', 2, js_dataview_setValue, 31 /* JSClassID.FLOAT64_ARRAY */),
1841
+ JS_CFUNC_MAGIC_DEF('setFloat16', 2, js_dataview_setValue, 30 /* JSClassID.FLOAT16_ARRAY */),
1842
+ JS_CFUNC_MAGIC_DEF('setFloat32', 2, js_dataview_setValue, 31 /* JSClassID.FLOAT32_ARRAY */),
1843
+ JS_CFUNC_MAGIC_DEF('setFloat64', 2, js_dataview_setValue, 32 /* JSClassID.FLOAT64_ARRAY */),
1795
1844
  JS_PROP_STRING_DEF(JS_ATOM_Symbol_toStringTag, 'DataView', JS_PROP_CONFIGURABLE),
1796
1845
  ];
1846
+ /* ---------------------------------------------------------------------
1847
+ Uint8Array base64 / hex (tc39 proposal-arraybuffer-base64)
1848
+ --------------------------------------------------------------------- */
1849
+ /* JS_STRING_LEN_MAX */
1850
+ const JS_STRING_LEN_MAX = (1 << 30) - 1;
1851
+ /* a new Uint8Array holding a copy of 'buf' (JS_NewUint8ArrayCopy) */
1852
+ function JS_NewUint8ArrayCopy(ctx, buf) {
1853
+ const buffer = js_array_buffer_constructor3(ctx, undefined, buf.length, -1, 19 /* JSClassID.ARRAY_BUFFER */, buf);
1854
+ if (JS_IsException(buffer))
1855
+ return JS_EXCEPTION;
1856
+ const obj = js_create_from_ctor(ctx, undefined, 23 /* JSClassID.UINT8_ARRAY */);
1857
+ if (JS_IsException(obj))
1858
+ return JS_EXCEPTION;
1859
+ return typed_array_init(ctx, obj, buffer, 0, buf.length, false);
1860
+ }
1861
+ /* this_val must be a Uint8Array (type check only, no detach check) */
1862
+ function check_uint8array(ctx, this_val) {
1863
+ if (Q.JS_IsObject(this_val) && Q.JS_GetClassID(this_val) === 23 /* JSClassID.UINT8_ARRAY */)
1864
+ return this_val;
1865
+ JS_ThrowTypeError(ctx, 'not a Uint8Array');
1866
+ return null;
1867
+ }
1868
+ /* the bytes of a Uint8Array, checking for detached and out of bounds buffers.
1869
+ Must be called after the options are read (spec ordering). */
1870
+ function get_uint8array_bytes(ctx, p) {
1871
+ if (typed_array_is_oob(ctx, p)) {
1872
+ JS_ThrowTypeErrorArrayBufferOOB(ctx);
1873
+ return null;
1874
+ }
1875
+ return typed_array_get_view(p) ?? new Uint8Array(0);
1876
+ }
1877
+ /* GetOptionsObject: undefined or an object */
1878
+ function check_options_object(ctx, options) {
1879
+ if (JS_IsUndefined(options))
1880
+ return 0;
1881
+ if (!Q.JS_IsObject(options)) {
1882
+ JS_ThrowTypeError(ctx, 'options must be an object');
1883
+ return -1;
1884
+ }
1885
+ return 0;
1886
+ }
1887
+ /* the 'alphabet' option: B64_ALPHABET_xxx or -1 on exception */
1888
+ function parse_alphabet_option(ctx, options) {
1889
+ if (JS_IsUndefined(options))
1890
+ return B64_ALPHABET_BASE64;
1891
+ const val = JS_GetPropertyStr(ctx, options, 'alphabet');
1892
+ if (JS_IsException(val))
1893
+ return -1;
1894
+ if (JS_IsUndefined(val))
1895
+ return B64_ALPHABET_BASE64;
1896
+ if (typeof val !== 'string') {
1897
+ JS_ThrowTypeError(ctx, 'expected string for alphabet');
1898
+ return -1;
1899
+ }
1900
+ if (val === 'base64')
1901
+ return B64_ALPHABET_BASE64;
1902
+ if (val === 'base64url')
1903
+ return B64_ALPHABET_BASE64URL;
1904
+ JS_ThrowTypeError(ctx, 'invalid alphabet');
1905
+ return -1;
1906
+ }
1907
+ /* the 'lastChunkHandling' option: B64_LAST_xxx or -1 on exception */
1908
+ function parse_last_chunk_option(ctx, options) {
1909
+ if (JS_IsUndefined(options))
1910
+ return B64_LAST_LOOSE;
1911
+ const val = JS_GetPropertyStr(ctx, options, 'lastChunkHandling');
1912
+ if (JS_IsException(val))
1913
+ return -1;
1914
+ if (JS_IsUndefined(val))
1915
+ return B64_LAST_LOOSE;
1916
+ if (typeof val !== 'string') {
1917
+ JS_ThrowTypeError(ctx, 'expected string for lastChunkHandling');
1918
+ return -1;
1919
+ }
1920
+ if (val === 'loose')
1921
+ return B64_LAST_LOOSE;
1922
+ if (val === 'strict')
1923
+ return B64_LAST_STRICT;
1924
+ if (val === 'stop-before-partial')
1925
+ return B64_LAST_STOP_BEFORE_PARTIAL;
1926
+ JS_ThrowTypeError(ctx, 'invalid lastChunkHandling option');
1927
+ return -1;
1928
+ }
1929
+ /* Uint8Array.prototype.toBase64([options]) */
1930
+ function js_uint8array_to_base64(ctx, this_val, argv) {
1931
+ const p = check_uint8array(ctx, this_val);
1932
+ if (!p)
1933
+ return JS_EXCEPTION;
1934
+ const options = argv.length > 0 ? argv[0] : undefined;
1935
+ if (check_options_object(ctx, options))
1936
+ return JS_EXCEPTION;
1937
+ const alphabet = parse_alphabet_option(ctx, options);
1938
+ if (alphabet < 0)
1939
+ return JS_EXCEPTION;
1940
+ let omit_padding = false;
1941
+ if (!JS_IsUndefined(options)) {
1942
+ const op_val = JS_GetPropertyStr(ctx, options, 'omitPadding');
1943
+ if (JS_IsException(op_val))
1944
+ return JS_EXCEPTION;
1945
+ omit_padding = JS_ToBoolFree(ctx, op_val);
1946
+ }
1947
+ const data = get_uint8array_bytes(ctx, p);
1948
+ if (!data)
1949
+ return JS_EXCEPTION;
1950
+ if (4 * Math.floor((data.length + 2) / 3) > JS_STRING_LEN_MAX)
1951
+ return JS_ThrowRangeError(ctx, 'output too large');
1952
+ return b64_encode(data, alphabet, omit_padding);
1953
+ }
1954
+ /* Uint8Array.prototype.toHex() */
1955
+ function js_uint8array_to_hex(ctx, this_val, argv) {
1956
+ const p = check_uint8array(ctx, this_val);
1957
+ if (!p)
1958
+ return JS_EXCEPTION;
1959
+ const data = get_uint8array_bytes(ctx, p);
1960
+ if (!data)
1961
+ return JS_EXCEPTION;
1962
+ if (data.length * 2 > JS_STRING_LEN_MAX)
1963
+ return JS_ThrowRangeError(ctx, 'output too large');
1964
+ return u8a_hex_encode(data);
1965
+ }
1966
+ /* Uint8Array.fromBase64(string[, options]) */
1967
+ function js_uint8array_from_base64(ctx, this_val, argv) {
1968
+ const str = argv[0];
1969
+ if (typeof str !== 'string')
1970
+ return JS_ThrowTypeError(ctx, 'expected string');
1971
+ const options = argv.length > 1 ? argv[1] : undefined;
1972
+ if (check_options_object(ctx, options))
1973
+ return JS_EXCEPTION;
1974
+ const alphabet = parse_alphabet_option(ctx, options);
1975
+ if (alphabet < 0)
1976
+ return JS_EXCEPTION;
1977
+ const last_chunk = parse_last_chunk_option(ctx, options);
1978
+ if (last_chunk < 0)
1979
+ return JS_EXCEPTION;
1980
+ const out_cap = Math.floor(str.length / 4) * 3 + 3;
1981
+ const buf = new Uint8Array(out_cap);
1982
+ const r = from_base64(str, buf, out_cap, alphabet, last_chunk);
1983
+ if (r.err)
1984
+ return JS_ThrowSyntaxError(ctx, 'invalid base64 string');
1985
+ return JS_NewUint8ArrayCopy(ctx, buf.subarray(0, r.written));
1986
+ }
1987
+ /* Uint8Array.fromHex(string) */
1988
+ function js_uint8array_from_hex(ctx, this_val, argv) {
1989
+ const str = argv[0];
1990
+ if (typeof str !== 'string')
1991
+ return JS_ThrowTypeError(ctx, 'expected string');
1992
+ const out_cap = Math.floor(str.length / 2) + 1;
1993
+ const buf = new Uint8Array(out_cap);
1994
+ const r = u8a_hex_decode(str, buf, out_cap);
1995
+ if (r.err)
1996
+ return JS_ThrowSyntaxError(ctx, 'invalid hex string');
1997
+ return JS_NewUint8ArrayCopy(ctx, buf.subarray(0, r.written));
1998
+ }
1999
+ /* a { read, written } result object */
2000
+ function js_make_read_written(ctx, read, written) {
2001
+ const obj = JS_NewObject(ctx);
2002
+ if (JS_IsException(obj))
2003
+ return JS_EXCEPTION;
2004
+ if (JS_DefinePropertyValueStr(ctx, obj, 'read', read, JS_PROP_C_W_E) < 0)
2005
+ return JS_EXCEPTION;
2006
+ if (JS_DefinePropertyValueStr(ctx, obj, 'written', written, JS_PROP_C_W_E) < 0)
2007
+ return JS_EXCEPTION;
2008
+ return obj;
2009
+ }
2010
+ /* Uint8Array.prototype.setFromBase64(string[, options]) */
2011
+ function js_uint8array_set_from_base64(ctx, this_val, argv) {
2012
+ const p = check_uint8array(ctx, this_val);
2013
+ if (!p)
2014
+ return JS_EXCEPTION;
2015
+ const str = argv[0];
2016
+ if (typeof str !== 'string')
2017
+ return JS_ThrowTypeError(ctx, 'expected string');
2018
+ const options = argv.length > 1 ? argv[1] : undefined;
2019
+ if (check_options_object(ctx, options))
2020
+ return JS_EXCEPTION;
2021
+ const alphabet = parse_alphabet_option(ctx, options);
2022
+ if (alphabet < 0)
2023
+ return JS_EXCEPTION;
2024
+ const last_chunk = parse_last_chunk_option(ctx, options);
2025
+ if (last_chunk < 0)
2026
+ return JS_EXCEPTION;
2027
+ const data = get_uint8array_bytes(ctx, p);
2028
+ if (!data)
2029
+ return JS_EXCEPTION;
2030
+ const r = from_base64(str, data, data.length, alphabet, last_chunk);
2031
+ if (r.err)
2032
+ return JS_ThrowSyntaxError(ctx, 'invalid base64 string');
2033
+ return js_make_read_written(ctx, r.read, r.written);
2034
+ }
2035
+ /* Uint8Array.prototype.setFromHex(string) */
2036
+ function js_uint8array_set_from_hex(ctx, this_val, argv) {
2037
+ const p = check_uint8array(ctx, this_val);
2038
+ if (!p)
2039
+ return JS_EXCEPTION;
2040
+ const str = argv[0];
2041
+ if (typeof str !== 'string')
2042
+ return JS_ThrowTypeError(ctx, 'expected string');
2043
+ const data = get_uint8array_bytes(ctx, p);
2044
+ if (!data)
2045
+ return JS_EXCEPTION;
2046
+ const r = u8a_hex_decode(str, data, data.length);
2047
+ if (r.err)
2048
+ return JS_ThrowSyntaxError(ctx, 'invalid hex string');
2049
+ return js_make_read_written(ctx, r.read, r.written);
2050
+ }
2051
+ const js_uint8array_proto_funcs = [
2052
+ JS_CFUNC_DEF('toBase64', 0, js_uint8array_to_base64),
2053
+ JS_CFUNC_DEF('toHex', 0, js_uint8array_to_hex),
2054
+ JS_CFUNC_DEF('setFromBase64', 1, js_uint8array_set_from_base64),
2055
+ JS_CFUNC_DEF('setFromHex', 1, js_uint8array_set_from_hex),
2056
+ ];
2057
+ const js_uint8array_funcs = [
2058
+ JS_CFUNC_DEF('fromBase64', 1, js_uint8array_from_base64),
2059
+ JS_CFUNC_DEF('fromHex', 1, js_uint8array_from_hex),
2060
+ ];
1797
2061
  /* ---------------------------------------------------------------------
1798
2062
  Intrinsics
1799
2063
  --------------------------------------------------------------------- */
@@ -1820,6 +2084,8 @@ export function JS_AddIntrinsicTypedArrays(ctx) {
1820
2084
  JS_SetPropertyFunctionList(ctx, typed_array_base_func, js_typed_array_base_funcs);
1821
2085
  JS_SetConstructor(ctx, typed_array_base_func, typed_array_base_proto);
1822
2086
  for (let i = 21 /* JSClassID.UINT8C_ARRAY */; i < 21 /* JSClassID.UINT8C_ARRAY */ + JS_TYPED_ARRAY_COUNT; i++) {
2087
+ if (!typed_array_class_available(i))
2088
+ continue; /* Float16Array on hosts without it */
1823
2089
  const proto = JS_NewObjectProto(ctx, typed_array_base_proto);
1824
2090
  ctx.class_proto[i] = proto;
1825
2091
  JS_DefinePropertyValueStr(ctx, proto, 'BYTES_PER_ELEMENT', 1 << typed_array_size_log2(i), 0);
@@ -1827,10 +2093,14 @@ export function JS_AddIntrinsicTypedArrays(ctx) {
1827
2093
  const func_obj = JS_NewCFunction3(ctx, js_typed_array_constructor, name, 3, 3 /* JSCFunctionEnum.constructor_magic */, i, typed_array_base_func);
1828
2094
  JS_NewGlobalCConstructor2(ctx, func_obj, name, proto);
1829
2095
  JS_DefinePropertyValueStr(ctx, func_obj, 'BYTES_PER_ELEMENT', 1 << typed_array_size_log2(i), 0);
2096
+ if (i === 23 /* JSClassID.UINT8_ARRAY */) {
2097
+ JS_SetPropertyFunctionList(ctx, func_obj, js_uint8array_funcs);
2098
+ JS_SetPropertyFunctionList(ctx, proto, js_uint8array_proto_funcs);
2099
+ }
1830
2100
  }
1831
2101
  /* DataView */
1832
2102
  const dv_proto = JS_NewObject(ctx);
1833
- ctx.class_proto[32 /* JSClassID.DATAVIEW */] = dv_proto;
2103
+ ctx.class_proto[33 /* JSClassID.DATAVIEW */] = dv_proto;
1834
2104
  JS_SetPropertyFunctionList(ctx, dv_proto, js_dataview_proto_funcs);
1835
2105
  JS_NewGlobalCConstructorOnly(ctx, 'DataView', js_dataview_constructor, 1, dv_proto);
1836
2106
  }