visaljs 0.0.0-stage → 0.0.2

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 (520) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +45 -2
  3. package/cli.js +8084 -0
  4. package/node/include/js_node.h +46 -0
  5. package/node/prelude/async_hooks.js +88 -0
  6. package/node/prelude/buffer.js +31 -0
  7. package/node/prelude/crypto.js +114 -0
  8. package/node/prelude/events.js +227 -0
  9. package/node/prelude/fs.js +79 -0
  10. package/node/prelude/http.js +536 -0
  11. package/node/prelude/http2.js +30 -0
  12. package/node/prelude/internal/crypto/asym.js +333 -0
  13. package/node/prelude/internal/crypto/cipher.js +205 -0
  14. package/node/prelude/internal/crypto/hash.js +137 -0
  15. package/node/prelude/internal/crypto/kdf.js +182 -0
  16. package/node/prelude/internal/crypto/keys.js +603 -0
  17. package/node/prelude/internal/crypto/random.js +118 -0
  18. package/node/prelude/internal/crypto/util.js +76 -0
  19. package/node/prelude/internal/errors.js +88 -0
  20. package/node/prelude/internal/querystring.js +105 -0
  21. package/node/prelude/module.js +53 -0
  22. package/node/prelude/net.js +51 -0
  23. package/node/prelude/os.js +129 -0
  24. package/node/prelude/path.js +257 -0
  25. package/node/prelude/process.js +21 -0
  26. package/node/prelude/querystring.js +469 -0
  27. package/node/prelude/stream.js +1327 -0
  28. package/node/prelude/stream_promises.js +7 -0
  29. package/node/prelude/stream_web.js +6 -0
  30. package/node/prelude/string_decoder.js +236 -0
  31. package/node/prelude/tty.js +18 -0
  32. package/node/prelude/url.js +1156 -0
  33. package/node/prelude/util.js +64 -0
  34. package/node/prelude/zlib.js +81 -0
  35. package/node/src/host_fs.c +126 -0
  36. package/node/src/host_http.c +486 -0
  37. package/node/src/host_os.c +505 -0
  38. package/node/src/host_sys.c +78 -0
  39. package/node/third_party/libuv/LICENSE +19 -0
  40. package/node/third_party/libuv/include/uv/aix.h +32 -0
  41. package/node/third_party/libuv/include/uv/bsd.h +34 -0
  42. package/node/third_party/libuv/include/uv/darwin.h +61 -0
  43. package/node/third_party/libuv/include/uv/errno.h +483 -0
  44. package/node/third_party/libuv/include/uv/linux.h +34 -0
  45. package/node/third_party/libuv/include/uv/os390.h +33 -0
  46. package/node/third_party/libuv/include/uv/posix.h +31 -0
  47. package/node/third_party/libuv/include/uv/sunos.h +44 -0
  48. package/node/third_party/libuv/include/uv/threadpool.h +37 -0
  49. package/node/third_party/libuv/include/uv/tree.h +521 -0
  50. package/node/third_party/libuv/include/uv/unix.h +512 -0
  51. package/node/third_party/libuv/include/uv/version.h +43 -0
  52. package/node/third_party/libuv/include/uv/win.h +698 -0
  53. package/node/third_party/libuv/include/uv.h +1990 -0
  54. package/node/third_party/libuv/src/fs-poll.c +290 -0
  55. package/node/third_party/libuv/src/heap-inl.h +245 -0
  56. package/node/third_party/libuv/src/idna.c +560 -0
  57. package/node/third_party/libuv/src/idna.h +31 -0
  58. package/node/third_party/libuv/src/inet.c +298 -0
  59. package/node/third_party/libuv/src/queue.h +90 -0
  60. package/node/third_party/libuv/src/random.c +123 -0
  61. package/node/third_party/libuv/src/strscpy.c +38 -0
  62. package/node/third_party/libuv/src/strscpy.h +39 -0
  63. package/node/third_party/libuv/src/strtok.c +52 -0
  64. package/node/third_party/libuv/src/strtok.h +27 -0
  65. package/node/third_party/libuv/src/thread-common.c +175 -0
  66. package/node/third_party/libuv/src/threadpool.c +419 -0
  67. package/node/third_party/libuv/src/timer.c +200 -0
  68. package/node/third_party/libuv/src/unix/async.c +418 -0
  69. package/node/third_party/libuv/src/unix/bsd-ifaddrs.c +164 -0
  70. package/node/third_party/libuv/src/unix/core.c +2104 -0
  71. package/node/third_party/libuv/src/unix/darwin-proctitle.c +176 -0
  72. package/node/third_party/libuv/src/unix/darwin-stub.h +97 -0
  73. package/node/third_party/libuv/src/unix/darwin-syscalls.h +17 -0
  74. package/node/third_party/libuv/src/unix/darwin.c +237 -0
  75. package/node/third_party/libuv/src/unix/dl.c +80 -0
  76. package/node/third_party/libuv/src/unix/fs.c +2303 -0
  77. package/node/third_party/libuv/src/unix/fsevents.c +906 -0
  78. package/node/third_party/libuv/src/unix/getaddrinfo.c +252 -0
  79. package/node/third_party/libuv/src/unix/getnameinfo.c +121 -0
  80. package/node/third_party/libuv/src/unix/internal.h +530 -0
  81. package/node/third_party/libuv/src/unix/kqueue.c +667 -0
  82. package/node/third_party/libuv/src/unix/linux.c +2761 -0
  83. package/node/third_party/libuv/src/unix/loop-watcher.c +68 -0
  84. package/node/third_party/libuv/src/unix/loop.c +240 -0
  85. package/node/third_party/libuv/src/unix/pipe.c +555 -0
  86. package/node/third_party/libuv/src/unix/poll.c +159 -0
  87. package/node/third_party/libuv/src/unix/process.c +1123 -0
  88. package/node/third_party/libuv/src/unix/procfs-exepath.c +45 -0
  89. package/node/third_party/libuv/src/unix/proctitle.c +157 -0
  90. package/node/third_party/libuv/src/unix/random-devurandom.c +93 -0
  91. package/node/third_party/libuv/src/unix/random-getentropy.c +57 -0
  92. package/node/third_party/libuv/src/unix/random-getrandom.c +86 -0
  93. package/node/third_party/libuv/src/unix/random-sysctl-linux.c +99 -0
  94. package/node/third_party/libuv/src/unix/signal.c +581 -0
  95. package/node/third_party/libuv/src/unix/stream.c +1567 -0
  96. package/node/third_party/libuv/src/unix/tcp.c +673 -0
  97. package/node/third_party/libuv/src/unix/thread.c +975 -0
  98. package/node/third_party/libuv/src/unix/tty.c +510 -0
  99. package/node/third_party/libuv/src/unix/udp.c +1434 -0
  100. package/node/third_party/libuv/src/uv-common.c +1051 -0
  101. package/node/third_party/libuv/src/uv-common.h +474 -0
  102. package/node/third_party/libuv/src/uv-data-getter-setters.c +119 -0
  103. package/node/third_party/libuv/src/version.c +45 -0
  104. package/node/third_party/llhttp/LICENSE +22 -0
  105. package/node/third_party/llhttp/api.c +509 -0
  106. package/node/third_party/llhttp/http.c +170 -0
  107. package/node/third_party/llhttp/llhttp.c +10099 -0
  108. package/node/third_party/llhttp/llhttp.h +907 -0
  109. package/package.json +25 -4
  110. package/runtime/include/js_atoms.gen.h +68 -0
  111. package/runtime/include/js_atoms.h +54 -0
  112. package/runtime/include/js_bigint.h +93 -0
  113. package/runtime/include/js_buffer.h +79 -0
  114. package/runtime/include/js_builtins.h +168 -0
  115. package/runtime/include/js_console.h +58 -0
  116. package/runtime/include/js_convert.h +22 -0
  117. package/runtime/include/js_core.def +188 -0
  118. package/runtime/include/js_crypto.h +58 -0
  119. package/runtime/include/js_error.h +37 -0
  120. package/runtime/include/js_exception.h +65 -0
  121. package/runtime/include/js_function.h +167 -0
  122. package/runtime/include/js_gc.h +195 -0
  123. package/runtime/include/js_generator.h +96 -0
  124. package/runtime/include/js_host.h +19 -0
  125. package/runtime/include/js_jobs.h +84 -0
  126. package/runtime/include/js_module.h +16 -0
  127. package/runtime/include/js_object.h +384 -0
  128. package/runtime/include/js_ops.h +111 -0
  129. package/runtime/include/js_promise.h +45 -0
  130. package/runtime/include/js_regexp.h +25 -0
  131. package/runtime/include/js_runtime.h +30 -0
  132. package/runtime/include/js_symbol.h +62 -0
  133. package/runtime/include/js_web.h +110 -0
  134. package/runtime/include/jscell.h +48 -0
  135. package/runtime/include/jscontext.h +240 -0
  136. package/runtime/include/jsnum.h +42 -0
  137. package/runtime/include/jsstring.h +161 -0
  138. package/runtime/include/jsvalue.h +142 -0
  139. package/runtime/src/arguments.c +115 -0
  140. package/runtime/src/array.c +1145 -0
  141. package/runtime/src/arraybuffer.c +608 -0
  142. package/runtime/src/async_iter.c +479 -0
  143. package/runtime/src/atoms.c +157 -0
  144. package/runtime/src/base64.c +107 -0
  145. package/runtime/src/bigint.c +1189 -0
  146. package/runtime/src/blob.c +371 -0
  147. package/runtime/src/buffer.c +1357 -0
  148. package/runtime/src/builtin_table.c +100 -0
  149. package/runtime/src/builtins.c +987 -0
  150. package/runtime/src/builtins_util.h +53 -0
  151. package/runtime/src/collections.c +912 -0
  152. package/runtime/src/components.c +28 -0
  153. package/runtime/src/console.c +637 -0
  154. package/runtime/src/convert.c +82 -0
  155. package/runtime/src/core_all.c +21 -0
  156. package/runtime/src/crypto.c +116 -0
  157. package/runtime/src/crypto_asym.c +644 -0
  158. package/runtime/src/crypto_cipher.c +671 -0
  159. package/runtime/src/crypto_hash.c +260 -0
  160. package/runtime/src/crypto_host.c +414 -0
  161. package/runtime/src/crypto_internal.h +304 -0
  162. package/runtime/src/crypto_kdf.c +143 -0
  163. package/runtime/src/crypto_key.c +171 -0
  164. package/runtime/src/crypto_pkey.c +794 -0
  165. package/runtime/src/crypto_subtle.c +1675 -0
  166. package/runtime/src/date.c +940 -0
  167. package/runtime/src/domexception.c +169 -0
  168. package/runtime/src/elements.c +312 -0
  169. package/runtime/src/elements.h +45 -0
  170. package/runtime/src/encoding.c +485 -0
  171. package/runtime/src/error.c +297 -0
  172. package/runtime/src/events.c +793 -0
  173. package/runtime/src/exception.c +56 -0
  174. package/runtime/src/fetch.c +772 -0
  175. package/runtime/src/function.c +530 -0
  176. package/runtime/src/gc.c +803 -0
  177. package/runtime/src/generator.c +218 -0
  178. package/runtime/src/global.c +119 -0
  179. package/runtime/src/headers.c +557 -0
  180. package/runtime/src/inspect.c +1792 -0
  181. package/runtime/src/internal.h +80 -0
  182. package/runtime/src/iterate.c +94 -0
  183. package/runtime/src/iterator.c +409 -0
  184. package/runtime/src/jobs.c +337 -0
  185. package/runtime/src/jsnum.c +353 -0
  186. package/runtime/src/json.c +1028 -0
  187. package/runtime/src/math.c +243 -0
  188. package/runtime/src/module.c +336 -0
  189. package/runtime/src/number.c +176 -0
  190. package/runtime/src/object.c +1265 -0
  191. package/runtime/src/ops.c +322 -0
  192. package/runtime/src/os.c +196 -0
  193. package/runtime/src/os.h +29 -0
  194. package/runtime/src/process.c +432 -0
  195. package/runtime/src/promise.c +721 -0
  196. package/runtime/src/proxy.c +570 -0
  197. package/runtime/src/reflect.c +194 -0
  198. package/runtime/src/regexp.c +1141 -0
  199. package/runtime/src/regexp_anchor.h +34 -0
  200. package/runtime/src/regexp_message.h +32 -0
  201. package/runtime/src/shape.c +294 -0
  202. package/runtime/src/shape.h +46 -0
  203. package/runtime/src/streams.c +1046 -0
  204. package/runtime/src/streams.h +47 -0
  205. package/runtime/src/string.c +364 -0
  206. package/runtime/src/string_builtins.c +837 -0
  207. package/runtime/src/symbol.c +143 -0
  208. package/runtime/src/test_host.c +59 -0
  209. package/runtime/src/typedarray.c +1330 -0
  210. package/runtime/src/tznames.gen.h +712 -0
  211. package/runtime/src/uri.c +169 -0
  212. package/runtime/src/url.c +2116 -0
  213. package/runtime/src/weak.c +486 -0
  214. package/runtime/src/web.c +214 -0
  215. package/runtime/src/wrapper.c +59 -0
  216. package/runtime/src/writable.c +1486 -0
  217. package/runtime/third_party/ada-idna/LICENSE-MIT +18 -0
  218. package/runtime/third_party/ada-idna/README.md +27 -0
  219. package/runtime/third_party/ada-idna/convert.mjs +83 -0
  220. package/runtime/third_party/ada-idna/idna.c +416 -0
  221. package/runtime/third_party/ada-idna/idna.h +15 -0
  222. package/runtime/third_party/ada-idna/idna_tables.h +2657 -0
  223. package/runtime/third_party/bearssl/LICENSE.txt +21 -0
  224. package/runtime/third_party/bearssl/README.txt +136 -0
  225. package/runtime/third_party/bearssl/VERSION +1 -0
  226. package/runtime/third_party/bearssl/inc/bearssl.h +183 -0
  227. package/runtime/third_party/bearssl/inc/bearssl_aead.h +1059 -0
  228. package/runtime/third_party/bearssl/inc/bearssl_block.h +2618 -0
  229. package/runtime/third_party/bearssl/inc/bearssl_ec.h +967 -0
  230. package/runtime/third_party/bearssl/inc/bearssl_hash.h +1346 -0
  231. package/runtime/third_party/bearssl/inc/bearssl_hmac.h +241 -0
  232. package/runtime/third_party/bearssl/inc/bearssl_kdf.h +284 -0
  233. package/runtime/third_party/bearssl/inc/bearssl_pem.h +294 -0
  234. package/runtime/third_party/bearssl/inc/bearssl_prf.h +150 -0
  235. package/runtime/third_party/bearssl/inc/bearssl_rand.h +397 -0
  236. package/runtime/third_party/bearssl/inc/bearssl_rsa.h +1655 -0
  237. package/runtime/third_party/bearssl/inc/bearssl_ssl.h +4296 -0
  238. package/runtime/third_party/bearssl/inc/bearssl_x509.h +1474 -0
  239. package/runtime/third_party/bearssl/src/aead/ccm.c +346 -0
  240. package/runtime/third_party/bearssl/src/aead/eax.c +525 -0
  241. package/runtime/third_party/bearssl/src/aead/gcm.c +318 -0
  242. package/runtime/third_party/bearssl/src/codec/ccopy.c +44 -0
  243. package/runtime/third_party/bearssl/src/codec/dec16be.c +38 -0
  244. package/runtime/third_party/bearssl/src/codec/dec16le.c +38 -0
  245. package/runtime/third_party/bearssl/src/codec/dec32be.c +38 -0
  246. package/runtime/third_party/bearssl/src/codec/dec32le.c +38 -0
  247. package/runtime/third_party/bearssl/src/codec/dec64be.c +38 -0
  248. package/runtime/third_party/bearssl/src/codec/dec64le.c +38 -0
  249. package/runtime/third_party/bearssl/src/codec/enc16be.c +38 -0
  250. package/runtime/third_party/bearssl/src/codec/enc16le.c +38 -0
  251. package/runtime/third_party/bearssl/src/codec/enc32be.c +38 -0
  252. package/runtime/third_party/bearssl/src/codec/enc32le.c +38 -0
  253. package/runtime/third_party/bearssl/src/codec/enc64be.c +38 -0
  254. package/runtime/third_party/bearssl/src/codec/enc64le.c +38 -0
  255. package/runtime/third_party/bearssl/src/codec/pemdec.c +526 -0
  256. package/runtime/third_party/bearssl/src/codec/pemdec.t0 +314 -0
  257. package/runtime/third_party/bearssl/src/codec/pemenc.c +173 -0
  258. package/runtime/third_party/bearssl/src/config.h +247 -0
  259. package/runtime/third_party/bearssl/src/ec/ec_all_m15.c +121 -0
  260. package/runtime/third_party/bearssl/src/ec/ec_all_m31.c +171 -0
  261. package/runtime/third_party/bearssl/src/ec/ec_c25519_i15.c +398 -0
  262. package/runtime/third_party/bearssl/src/ec/ec_c25519_i31.c +390 -0
  263. package/runtime/third_party/bearssl/src/ec/ec_c25519_m15.c +1478 -0
  264. package/runtime/third_party/bearssl/src/ec/ec_c25519_m31.c +800 -0
  265. package/runtime/third_party/bearssl/src/ec/ec_c25519_m62.c +605 -0
  266. package/runtime/third_party/bearssl/src/ec/ec_c25519_m64.c +831 -0
  267. package/runtime/third_party/bearssl/src/ec/ec_curve25519.c +46 -0
  268. package/runtime/third_party/bearssl/src/ec/ec_default.c +36 -0
  269. package/runtime/third_party/bearssl/src/ec/ec_keygen.c +86 -0
  270. package/runtime/third_party/bearssl/src/ec/ec_p256_m15.c +2124 -0
  271. package/runtime/third_party/bearssl/src/ec/ec_p256_m31.c +1469 -0
  272. package/runtime/third_party/bearssl/src/ec/ec_p256_m62.c +1765 -0
  273. package/runtime/third_party/bearssl/src/ec/ec_p256_m64.c +1781 -0
  274. package/runtime/third_party/bearssl/src/ec/ec_prime_i15.c +824 -0
  275. package/runtime/third_party/bearssl/src/ec/ec_prime_i31.c +826 -0
  276. package/runtime/third_party/bearssl/src/ec/ec_pubkey.c +85 -0
  277. package/runtime/third_party/bearssl/src/ec/ec_secp256r1.c +51 -0
  278. package/runtime/third_party/bearssl/src/ec/ec_secp384r1.c +57 -0
  279. package/runtime/third_party/bearssl/src/ec/ec_secp521r1.c +64 -0
  280. package/runtime/third_party/bearssl/src/ec/ecdsa_atr.c +134 -0
  281. package/runtime/third_party/bearssl/src/ec/ecdsa_default_sign_asn1.c +36 -0
  282. package/runtime/third_party/bearssl/src/ec/ecdsa_default_sign_raw.c +36 -0
  283. package/runtime/third_party/bearssl/src/ec/ecdsa_default_vrfy_asn1.c +36 -0
  284. package/runtime/third_party/bearssl/src/ec/ecdsa_default_vrfy_raw.c +36 -0
  285. package/runtime/third_party/bearssl/src/ec/ecdsa_i15_bits.c +47 -0
  286. package/runtime/third_party/bearssl/src/ec/ecdsa_i15_sign_asn1.c +45 -0
  287. package/runtime/third_party/bearssl/src/ec/ecdsa_i15_sign_raw.c +174 -0
  288. package/runtime/third_party/bearssl/src/ec/ecdsa_i15_vrfy_asn1.c +48 -0
  289. package/runtime/third_party/bearssl/src/ec/ecdsa_i15_vrfy_raw.c +166 -0
  290. package/runtime/third_party/bearssl/src/ec/ecdsa_i31_bits.c +47 -0
  291. package/runtime/third_party/bearssl/src/ec/ecdsa_i31_sign_asn1.c +45 -0
  292. package/runtime/third_party/bearssl/src/ec/ecdsa_i31_sign_raw.c +173 -0
  293. package/runtime/third_party/bearssl/src/ec/ecdsa_i31_vrfy_asn1.c +48 -0
  294. package/runtime/third_party/bearssl/src/ec/ecdsa_i31_vrfy_raw.c +165 -0
  295. package/runtime/third_party/bearssl/src/ec/ecdsa_rta.c +121 -0
  296. package/runtime/third_party/bearssl/src/hash/dig_oid.c +84 -0
  297. package/runtime/third_party/bearssl/src/hash/dig_size.c +50 -0
  298. package/runtime/third_party/bearssl/src/hash/ghash_ctmul.c +345 -0
  299. package/runtime/third_party/bearssl/src/hash/ghash_ctmul32.c +251 -0
  300. package/runtime/third_party/bearssl/src/hash/ghash_ctmul64.c +154 -0
  301. package/runtime/third_party/bearssl/src/hash/ghash_pclmul.c +389 -0
  302. package/runtime/third_party/bearssl/src/hash/ghash_pwr8.c +411 -0
  303. package/runtime/third_party/bearssl/src/hash/md5.c +208 -0
  304. package/runtime/third_party/bearssl/src/hash/md5sha1.c +141 -0
  305. package/runtime/third_party/bearssl/src/hash/mgf1.c +56 -0
  306. package/runtime/third_party/bearssl/src/hash/multihash.c +166 -0
  307. package/runtime/third_party/bearssl/src/hash/sha1.c +191 -0
  308. package/runtime/third_party/bearssl/src/hash/sha2big.c +285 -0
  309. package/runtime/third_party/bearssl/src/hash/sha2small.c +341 -0
  310. package/runtime/third_party/bearssl/src/inner.h +2577 -0
  311. package/runtime/third_party/bearssl/src/int/i15_add.c +46 -0
  312. package/runtime/third_party/bearssl/src/int/i15_bitlen.c +44 -0
  313. package/runtime/third_party/bearssl/src/int/i15_decmod.c +124 -0
  314. package/runtime/third_party/bearssl/src/int/i15_decode.c +56 -0
  315. package/runtime/third_party/bearssl/src/int/i15_decred.c +100 -0
  316. package/runtime/third_party/bearssl/src/int/i15_encode.c +56 -0
  317. package/runtime/third_party/bearssl/src/int/i15_fmont.c +59 -0
  318. package/runtime/third_party/bearssl/src/int/i15_iszero.c +39 -0
  319. package/runtime/third_party/bearssl/src/int/i15_moddiv.c +465 -0
  320. package/runtime/third_party/bearssl/src/int/i15_modpow.c +50 -0
  321. package/runtime/third_party/bearssl/src/int/i15_modpow2.c +160 -0
  322. package/runtime/third_party/bearssl/src/int/i15_montmul.c +184 -0
  323. package/runtime/third_party/bearssl/src/int/i15_mulacc.c +61 -0
  324. package/runtime/third_party/bearssl/src/int/i15_muladd.c +173 -0
  325. package/runtime/third_party/bearssl/src/int/i15_ninv15.c +38 -0
  326. package/runtime/third_party/bearssl/src/int/i15_reduce.c +66 -0
  327. package/runtime/third_party/bearssl/src/int/i15_rshift.c +47 -0
  328. package/runtime/third_party/bearssl/src/int/i15_sub.c +46 -0
  329. package/runtime/third_party/bearssl/src/int/i15_tmont.c +36 -0
  330. package/runtime/third_party/bearssl/src/int/i31_add.c +46 -0
  331. package/runtime/third_party/bearssl/src/int/i31_bitlen.c +44 -0
  332. package/runtime/third_party/bearssl/src/int/i31_decmod.c +124 -0
  333. package/runtime/third_party/bearssl/src/int/i31_decode.c +57 -0
  334. package/runtime/third_party/bearssl/src/int/i31_decred.c +103 -0
  335. package/runtime/third_party/bearssl/src/int/i31_encode.c +79 -0
  336. package/runtime/third_party/bearssl/src/int/i31_fmont.c +60 -0
  337. package/runtime/third_party/bearssl/src/int/i31_iszero.c +39 -0
  338. package/runtime/third_party/bearssl/src/int/i31_moddiv.c +488 -0
  339. package/runtime/third_party/bearssl/src/int/i31_modpow.c +65 -0
  340. package/runtime/third_party/bearssl/src/int/i31_modpow2.c +160 -0
  341. package/runtime/third_party/bearssl/src/int/i31_montmul.c +127 -0
  342. package/runtime/third_party/bearssl/src/int/i31_mulacc.c +74 -0
  343. package/runtime/third_party/bearssl/src/int/i31_muladd.c +157 -0
  344. package/runtime/third_party/bearssl/src/int/i31_ninv31.c +39 -0
  345. package/runtime/third_party/bearssl/src/int/i31_reduce.c +66 -0
  346. package/runtime/third_party/bearssl/src/int/i31_rshift.c +47 -0
  347. package/runtime/third_party/bearssl/src/int/i31_sub.c +46 -0
  348. package/runtime/third_party/bearssl/src/int/i31_tmont.c +36 -0
  349. package/runtime/third_party/bearssl/src/int/i32_add.c +51 -0
  350. package/runtime/third_party/bearssl/src/int/i32_bitlen.c +44 -0
  351. package/runtime/third_party/bearssl/src/int/i32_decmod.c +77 -0
  352. package/runtime/third_party/bearssl/src/int/i32_decode.c +63 -0
  353. package/runtime/third_party/bearssl/src/int/i32_decred.c +107 -0
  354. package/runtime/third_party/bearssl/src/int/i32_div32.c +56 -0
  355. package/runtime/third_party/bearssl/src/int/i32_encode.c +72 -0
  356. package/runtime/third_party/bearssl/src/int/i32_fmont.c +60 -0
  357. package/runtime/third_party/bearssl/src/int/i32_iszero.c +39 -0
  358. package/runtime/third_party/bearssl/src/int/i32_modpow.c +65 -0
  359. package/runtime/third_party/bearssl/src/int/i32_montmul.c +69 -0
  360. package/runtime/third_party/bearssl/src/int/i32_mulacc.c +56 -0
  361. package/runtime/third_party/bearssl/src/int/i32_muladd.c +138 -0
  362. package/runtime/third_party/bearssl/src/int/i32_ninv32.c +39 -0
  363. package/runtime/third_party/bearssl/src/int/i32_reduce.c +66 -0
  364. package/runtime/third_party/bearssl/src/int/i32_sub.c +51 -0
  365. package/runtime/third_party/bearssl/src/int/i32_tmont.c +36 -0
  366. package/runtime/third_party/bearssl/src/int/i62_modpow2.c +493 -0
  367. package/runtime/third_party/bearssl/src/kdf/hkdf.c +107 -0
  368. package/runtime/third_party/bearssl/src/kdf/shake.c +590 -0
  369. package/runtime/third_party/bearssl/src/mac/hmac.c +122 -0
  370. package/runtime/third_party/bearssl/src/mac/hmac_ct.c +193 -0
  371. package/runtime/third_party/bearssl/src/rand/aesctr_drbg.c +206 -0
  372. package/runtime/third_party/bearssl/src/rand/hmac_drbg.c +157 -0
  373. package/runtime/third_party/bearssl/src/rand/sysrng.c +252 -0
  374. package/runtime/third_party/bearssl/src/rsa/rsa_default_keygen.c +38 -0
  375. package/runtime/third_party/bearssl/src/rsa/rsa_default_modulus.c +36 -0
  376. package/runtime/third_party/bearssl/src/rsa/rsa_default_oaep_decrypt.c +38 -0
  377. package/runtime/third_party/bearssl/src/rsa/rsa_default_oaep_encrypt.c +38 -0
  378. package/runtime/third_party/bearssl/src/rsa/rsa_default_pkcs1_sign.c +38 -0
  379. package/runtime/third_party/bearssl/src/rsa/rsa_default_pkcs1_vrfy.c +38 -0
  380. package/runtime/third_party/bearssl/src/rsa/rsa_default_priv.c +38 -0
  381. package/runtime/third_party/bearssl/src/rsa/rsa_default_privexp.c +36 -0
  382. package/runtime/third_party/bearssl/src/rsa/rsa_default_pss_sign.c +38 -0
  383. package/runtime/third_party/bearssl/src/rsa/rsa_default_pss_vrfy.c +38 -0
  384. package/runtime/third_party/bearssl/src/rsa/rsa_default_pub.c +38 -0
  385. package/runtime/third_party/bearssl/src/rsa/rsa_default_pubexp.c +36 -0
  386. package/runtime/third_party/bearssl/src/rsa/rsa_i15_keygen.c +583 -0
  387. package/runtime/third_party/bearssl/src/rsa/rsa_i15_modulus.c +99 -0
  388. package/runtime/third_party/bearssl/src/rsa/rsa_i15_oaep_decrypt.c +41 -0
  389. package/runtime/third_party/bearssl/src/rsa/rsa_i15_oaep_encrypt.c +44 -0
  390. package/runtime/third_party/bearssl/src/rsa/rsa_i15_pkcs1_sign.c +37 -0
  391. package/runtime/third_party/bearssl/src/rsa/rsa_i15_pkcs1_vrfy.c +43 -0
  392. package/runtime/third_party/bearssl/src/rsa/rsa_i15_priv.c +209 -0
  393. package/runtime/third_party/bearssl/src/rsa/rsa_i15_privexp.c +320 -0
  394. package/runtime/third_party/bearssl/src/rsa/rsa_i15_pss_sign.c +40 -0
  395. package/runtime/third_party/bearssl/src/rsa/rsa_i15_pss_vrfy.c +44 -0
  396. package/runtime/third_party/bearssl/src/rsa/rsa_i15_pub.c +113 -0
  397. package/runtime/third_party/bearssl/src/rsa/rsa_i15_pubexp.c +152 -0
  398. package/runtime/third_party/bearssl/src/rsa/rsa_i31_keygen.c +37 -0
  399. package/runtime/third_party/bearssl/src/rsa/rsa_i31_keygen_inner.c +608 -0
  400. package/runtime/third_party/bearssl/src/rsa/rsa_i31_modulus.c +99 -0
  401. package/runtime/third_party/bearssl/src/rsa/rsa_i31_oaep_decrypt.c +41 -0
  402. package/runtime/third_party/bearssl/src/rsa/rsa_i31_oaep_encrypt.c +44 -0
  403. package/runtime/third_party/bearssl/src/rsa/rsa_i31_pkcs1_sign.c +37 -0
  404. package/runtime/third_party/bearssl/src/rsa/rsa_i31_pkcs1_vrfy.c +43 -0
  405. package/runtime/third_party/bearssl/src/rsa/rsa_i31_priv.c +203 -0
  406. package/runtime/third_party/bearssl/src/rsa/rsa_i31_privexp.c +318 -0
  407. package/runtime/third_party/bearssl/src/rsa/rsa_i31_pss_sign.c +40 -0
  408. package/runtime/third_party/bearssl/src/rsa/rsa_i31_pss_vrfy.c +44 -0
  409. package/runtime/third_party/bearssl/src/rsa/rsa_i31_pub.c +106 -0
  410. package/runtime/third_party/bearssl/src/rsa/rsa_i31_pubexp.c +152 -0
  411. package/runtime/third_party/bearssl/src/rsa/rsa_i32_oaep_decrypt.c +41 -0
  412. package/runtime/third_party/bearssl/src/rsa/rsa_i32_oaep_encrypt.c +44 -0
  413. package/runtime/third_party/bearssl/src/rsa/rsa_i32_pkcs1_sign.c +37 -0
  414. package/runtime/third_party/bearssl/src/rsa/rsa_i32_pkcs1_vrfy.c +43 -0
  415. package/runtime/third_party/bearssl/src/rsa/rsa_i32_priv.c +160 -0
  416. package/runtime/third_party/bearssl/src/rsa/rsa_i32_pss_sign.c +40 -0
  417. package/runtime/third_party/bearssl/src/rsa/rsa_i32_pss_vrfy.c +44 -0
  418. package/runtime/third_party/bearssl/src/rsa/rsa_i32_pub.c +77 -0
  419. package/runtime/third_party/bearssl/src/rsa/rsa_i62_keygen.c +57 -0
  420. package/runtime/third_party/bearssl/src/rsa/rsa_i62_oaep_decrypt.c +61 -0
  421. package/runtime/third_party/bearssl/src/rsa/rsa_i62_oaep_encrypt.c +64 -0
  422. package/runtime/third_party/bearssl/src/rsa/rsa_i62_pkcs1_sign.c +57 -0
  423. package/runtime/third_party/bearssl/src/rsa/rsa_i62_pkcs1_vrfy.c +63 -0
  424. package/runtime/third_party/bearssl/src/rsa/rsa_i62_priv.c +223 -0
  425. package/runtime/third_party/bearssl/src/rsa/rsa_i62_pss_sign.c +60 -0
  426. package/runtime/third_party/bearssl/src/rsa/rsa_i62_pss_vrfy.c +64 -0
  427. package/runtime/third_party/bearssl/src/rsa/rsa_i62_pub.c +125 -0
  428. package/runtime/third_party/bearssl/src/rsa/rsa_oaep_pad.c +112 -0
  429. package/runtime/third_party/bearssl/src/rsa/rsa_oaep_unpad.c +145 -0
  430. package/runtime/third_party/bearssl/src/rsa/rsa_pkcs1_sig_pad.c +100 -0
  431. package/runtime/third_party/bearssl/src/rsa/rsa_pkcs1_sig_unpad.c +121 -0
  432. package/runtime/third_party/bearssl/src/rsa/rsa_pss_sig_pad.c +106 -0
  433. package/runtime/third_party/bearssl/src/rsa/rsa_pss_sig_unpad.c +121 -0
  434. package/runtime/third_party/bearssl/src/rsa/rsa_ssl_decrypt.c +52 -0
  435. package/runtime/third_party/bearssl/src/settings.c +306 -0
  436. package/runtime/third_party/bearssl/src/symcipher/aes_big_cbcdec.c +69 -0
  437. package/runtime/third_party/bearssl/src/symcipher/aes_big_cbcenc.c +67 -0
  438. package/runtime/third_party/bearssl/src/symcipher/aes_big_ctr.c +84 -0
  439. package/runtime/third_party/bearssl/src/symcipher/aes_big_ctrcbc.c +142 -0
  440. package/runtime/third_party/bearssl/src/symcipher/aes_big_dec.c +254 -0
  441. package/runtime/third_party/bearssl/src/symcipher/aes_big_enc.c +157 -0
  442. package/runtime/third_party/bearssl/src/symcipher/aes_common.c +112 -0
  443. package/runtime/third_party/bearssl/src/symcipher/aes_ct.c +328 -0
  444. package/runtime/third_party/bearssl/src/symcipher/aes_ct64.c +398 -0
  445. package/runtime/third_party/bearssl/src/symcipher/aes_ct64_cbcdec.c +104 -0
  446. package/runtime/third_party/bearssl/src/symcipher/aes_ct64_cbcenc.c +81 -0
  447. package/runtime/third_party/bearssl/src/symcipher/aes_ct64_ctr.c +114 -0
  448. package/runtime/third_party/bearssl/src/symcipher/aes_ct64_ctrcbc.c +433 -0
  449. package/runtime/third_party/bearssl/src/symcipher/aes_ct64_dec.c +159 -0
  450. package/runtime/third_party/bearssl/src/symcipher/aes_ct64_enc.c +115 -0
  451. package/runtime/third_party/bearssl/src/symcipher/aes_ct_cbcdec.c +111 -0
  452. package/runtime/third_party/bearssl/src/symcipher/aes_ct_cbcenc.c +91 -0
  453. package/runtime/third_party/bearssl/src/symcipher/aes_ct_ctr.c +116 -0
  454. package/runtime/third_party/bearssl/src/symcipher/aes_ct_ctrcbc.c +422 -0
  455. package/runtime/third_party/bearssl/src/symcipher/aes_ct_dec.c +170 -0
  456. package/runtime/third_party/bearssl/src/symcipher/aes_ct_enc.c +112 -0
  457. package/runtime/third_party/bearssl/src/symcipher/aes_pwr8.c +445 -0
  458. package/runtime/third_party/bearssl/src/symcipher/aes_pwr8_cbcdec.c +670 -0
  459. package/runtime/third_party/bearssl/src/symcipher/aes_pwr8_cbcenc.c +417 -0
  460. package/runtime/third_party/bearssl/src/symcipher/aes_pwr8_ctr.c +717 -0
  461. package/runtime/third_party/bearssl/src/symcipher/aes_pwr8_ctrcbc.c +946 -0
  462. package/runtime/third_party/bearssl/src/symcipher/aes_small_cbcdec.c +69 -0
  463. package/runtime/third_party/bearssl/src/symcipher/aes_small_cbcenc.c +67 -0
  464. package/runtime/third_party/bearssl/src/symcipher/aes_small_ctr.c +84 -0
  465. package/runtime/third_party/bearssl/src/symcipher/aes_small_ctrcbc.c +142 -0
  466. package/runtime/third_party/bearssl/src/symcipher/aes_small_dec.c +176 -0
  467. package/runtime/third_party/bearssl/src/symcipher/aes_small_enc.c +129 -0
  468. package/runtime/third_party/bearssl/src/symcipher/aes_x86ni.c +240 -0
  469. package/runtime/third_party/bearssl/src/symcipher/aes_x86ni_cbcdec.c +223 -0
  470. package/runtime/third_party/bearssl/src/symcipher/aes_x86ni_cbcenc.c +122 -0
  471. package/runtime/third_party/bearssl/src/symcipher/aes_x86ni_ctr.c +211 -0
  472. package/runtime/third_party/bearssl/src/symcipher/aes_x86ni_ctrcbc.c +596 -0
  473. package/runtime/third_party/bearssl/src/symcipher/chacha20_ct.c +106 -0
  474. package/runtime/third_party/bearssl/src/symcipher/chacha20_sse2.c +237 -0
  475. package/runtime/third_party/bearssl/src/symcipher/des_ct.c +411 -0
  476. package/runtime/third_party/bearssl/src/symcipher/des_ct_cbcdec.c +87 -0
  477. package/runtime/third_party/bearssl/src/symcipher/des_ct_cbcenc.c +69 -0
  478. package/runtime/third_party/bearssl/src/symcipher/des_support.c +166 -0
  479. package/runtime/third_party/bearssl/src/symcipher/des_tab.c +310 -0
  480. package/runtime/third_party/bearssl/src/symcipher/des_tab_cbcdec.c +85 -0
  481. package/runtime/third_party/bearssl/src/symcipher/des_tab_cbcenc.c +67 -0
  482. package/runtime/third_party/bearssl/src/symcipher/poly1305_ctmul.c +260 -0
  483. package/runtime/third_party/bearssl/src/symcipher/poly1305_ctmul32.c +297 -0
  484. package/runtime/third_party/bearssl/src/symcipher/poly1305_ctmulq.c +475 -0
  485. package/runtime/third_party/bearssl/src/symcipher/poly1305_i15.c +221 -0
  486. package/runtime/third_party/bearssl/src/x509/asn1.t0 +757 -0
  487. package/runtime/third_party/bearssl/src/x509/asn1enc.c +93 -0
  488. package/runtime/third_party/bearssl/src/x509/encode_ec_pk8der.c +110 -0
  489. package/runtime/third_party/bearssl/src/x509/encode_ec_rawder.c +161 -0
  490. package/runtime/third_party/bearssl/src/x509/encode_rsa_pk8der.c +97 -0
  491. package/runtime/third_party/bearssl/src/x509/encode_rsa_rawder.c +96 -0
  492. package/runtime/third_party/bearssl/src/x509/skey_decoder.c +653 -0
  493. package/runtime/third_party/bearssl/src/x509/skey_decoder.t0 +376 -0
  494. package/runtime/third_party/bearssl/src/x509/x509_decoder.c +776 -0
  495. package/runtime/third_party/bearssl/src/x509/x509_decoder.t0 +324 -0
  496. package/runtime/third_party/bearssl/src/x509/x509_knownkey.c +105 -0
  497. package/runtime/third_party/bearssl/src/x509/x509_minimal.c +1722 -0
  498. package/runtime/third_party/bearssl/src/x509/x509_minimal.t0 +1526 -0
  499. package/runtime/third_party/bearssl/src/x509/x509_minimal_full.c +59 -0
  500. package/runtime/third_party/quickjs-dtoa/LICENSE +22 -0
  501. package/runtime/third_party/quickjs-dtoa/README.md +23 -0
  502. package/runtime/third_party/quickjs-dtoa/dtoa.c +1640 -0
  503. package/runtime/third_party/quickjs-dtoa/dtoa.h +83 -0
  504. package/runtime/third_party/quickjs-dtoa/dtoa_compat.h +89 -0
  505. package/runtime/third_party/quickjs-regexp/LICENSE +22 -0
  506. package/runtime/third_party/quickjs-regexp/README.md +22 -0
  507. package/runtime/third_party/quickjs-regexp/cutils.c +641 -0
  508. package/runtime/third_party/quickjs-regexp/cutils.h +457 -0
  509. package/runtime/third_party/quickjs-regexp/libregexp-opcode.h +73 -0
  510. package/runtime/third_party/quickjs-regexp/libregexp.c +3448 -0
  511. package/runtime/third_party/quickjs-regexp/libregexp.h +65 -0
  512. package/runtime/third_party/quickjs-regexp/libunicode-table.h +5206 -0
  513. package/runtime/third_party/quickjs-regexp/libunicode.c +2124 -0
  514. package/runtime/third_party/quickjs-regexp/libunicode.h +196 -0
  515. package/runtime/third_party/v8-ieee754/LICENSE +61 -0
  516. package/runtime/third_party/v8-ieee754/README.md +22 -0
  517. package/runtime/third_party/v8-ieee754/convert.py +84 -0
  518. package/runtime/third_party/v8-ieee754/ieee754.c +3030 -0
  519. package/runtime/third_party/v8-ieee754/ieee754.h +27 -0
  520. package/runtime/tools/regexp_compile.c +90 -0
@@ -0,0 +1,912 @@
1
+ /* Map and Set (ECMAScript §24.1–24.2), an optional component (ADR 0014): with Map.groupBy,
2
+ * Map.prototype.getOrInsert / getOrInsertComputed, and the Set methods (union, …).
3
+ *
4
+ * Each Map or Set holds an ordered hash table (V8's OrderedHashTable): the entries in insertion
5
+ * order, a deleted one left as a hole, and buckets chaining entry indices by hash. When the table
6
+ * grows, shrinks or is cleared, a new table replaces it and the old one points to the new one. An
7
+ * iterator still on an old table moves over lazily: its index becomes the number of live entries
8
+ * before it (0 after a clear). So iterators see entries added while they run and skip deleted
9
+ * ones, as the spec's list with empty slots does. */
10
+ #include <math.h>
11
+ #include <stdio.h>
12
+ #include <string.h>
13
+
14
+ #include "internal.h"
15
+ #include "builtins_util.h"
16
+ #include "js_atoms.h"
17
+ #include "js_convert.h"
18
+ #include "js_error.h"
19
+ #include "js_function.h"
20
+ #include "js_ops.h"
21
+ #include "js_symbol.h"
22
+ #include "jscontext.h"
23
+ #include "jsstring.h"
24
+
25
+ #define NIL UINT32_MAX
26
+ #define MIN_CAP 4
27
+
28
+ /* ---- Ordered hash tables ------------------------------------------------------------------ */
29
+
30
+ typedef struct Entry {
31
+ JSValue key; /* EMPTY: deleted */
32
+ JSValue value; /* Set: unused */
33
+ uint32_t chain; /* the next entry in the bucket, or NIL */
34
+ uint32_t hash;
35
+ } Entry;
36
+
37
+ typedef struct Table {
38
+ JSHostCell host;
39
+ JSValue next; /* the table that replaced this one; undefined while current */
40
+ uint32_t cap; /* entries */
41
+ uint32_t used; /* entries filled, holes included */
42
+ uint32_t count; /* live entries */
43
+ bool cleared; /* replaced by clear(): iterators restart at 0 */
44
+ Entry entries[]; /* then uint32_t buckets[cap / 2] */
45
+ } Table;
46
+
47
+ static uint32_t *buckets_of(Table *t) { return (uint32_t *)(t->entries + t->cap); }
48
+
49
+ static void table_visit(JSCell *c, JSTracer *trc) {
50
+ Table *t = (Table *)c;
51
+ js_trace_value(trc, t->next);
52
+ for (uint32_t i = 0; i < t->used; i++) {
53
+ js_trace_value(trc, t->entries[i].key);
54
+ js_trace_value(trc, t->entries[i].value);
55
+ }
56
+ }
57
+
58
+ static const JSCellClass table_class = { "OrderedHashTable", table_visit, NULL };
59
+
60
+ static Table *new_table(JSContext *ctx, uint32_t cap) {
61
+ size_t size = sizeof(Table) + cap * sizeof(Entry) + (cap / 2) * sizeof(uint32_t);
62
+ Table *t = (Table *)js_alloc_host_cell(ctx, &table_class, size); /* zeroed: keys are EMPTY */
63
+ t->next = JS_UNDEFINED;
64
+ t->cap = cap;
65
+ memset(buckets_of(t), 0xFF, (cap / 2) * sizeof(uint32_t));
66
+ return t;
67
+ }
68
+
69
+ /* CanonicalizeKeyedCollectionKey (§24.5.1), and one encoding per number: -0 is +0, an
70
+ * integral double is an int32. Then SameValueZero is: same bits, or equal strings. */
71
+ static JSValue canonical_key(JSValue k) {
72
+ if (!js_is_double(k)) return k;
73
+ double d = js_as_double(k);
74
+ return d == 0 ? js_int32(0) : js_number(d);
75
+ }
76
+
77
+ static uint32_t hash_key(JSContext *ctx, JSValue k) {
78
+ if (js_is_string(k)) return js_string_hash(ctx, k);
79
+ if (js_is_bigint(k)) return js_bigint_hash(k); /* by value, as SameValueZero compares them */
80
+ uint64_t x = k.bits ^ (k.bits >> 29);
81
+ return (uint32_t)((x * UINT64_C(0x9E3779B97F4A7C15)) >> 32);
82
+ }
83
+
84
+ static bool same_key(JSContext *ctx, JSValue a, JSValue b) {
85
+ return js_same_bits(a, b) || (js_is_string(a) && js_is_string(b) && js_string_equals(ctx, a, b)) ||
86
+ (js_is_bigint(a) && js_is_bigint(b) && js_bigint_equals(a, b));
87
+ }
88
+
89
+ /* The entry index of a canonical key, or NIL. */
90
+ static uint32_t table_find(JSContext *ctx, Table *t, JSValue key, uint32_t hash) {
91
+ for (uint32_t i = buckets_of(t)[hash & (t->cap / 2 - 1)]; i != NIL; i = t->entries[i].chain) {
92
+ Entry *e = &t->entries[i];
93
+ if (e->hash == hash && same_key(ctx, e->key, key)) return i;
94
+ }
95
+ return NIL;
96
+ }
97
+
98
+ static void table_append(Table *t, JSValue key, JSValue value, uint32_t hash) {
99
+ uint32_t *b = &buckets_of(t)[hash & (t->cap / 2 - 1)];
100
+ t->entries[t->used] = (Entry){ key, value, *b, hash };
101
+ *b = t->used++;
102
+ t->count++;
103
+ }
104
+
105
+ /* A Map or Set: its current table. */
106
+ typedef struct Collection {
107
+ JSObject obj;
108
+ JSValue table;
109
+ } Collection;
110
+
111
+ static Table *table_of(Collection *c) { return (Table *)js_as_cell(c->table); }
112
+
113
+ static void collection_visit(JSCell *c, JSTracer *trc) { js_trace_value(trc, ((Collection *)c)->table); }
114
+
115
+ static const JSClass map_class = { "Map", sizeof(Collection), NULL, NULL, NULL, NULL, collection_visit, NULL, NULL };
116
+ static const JSClass set_class = { "Set", sizeof(Collection), NULL, NULL, NULL, NULL, collection_visit, NULL, NULL };
117
+
118
+ /* Replaces c's table by one of `cap` entries holding the live ones, in order. */
119
+ static void rehash(JSContext *ctx, Collection *c, uint32_t cap) {
120
+ Table *old = table_of(c);
121
+ Table *t = new_table(ctx, cap);
122
+ for (uint32_t i = 0; i < old->used; i++) {
123
+ Entry *e = &old->entries[i];
124
+ if (!js_is_empty(e->key)) table_append(t, e->key, e->value, e->hash);
125
+ }
126
+ js_store(ctx, &old->host.cell, &old->next, js_cell(&t->host.cell));
127
+ js_store(ctx, &c->obj.cell, &c->table, js_cell(&t->host.cell));
128
+ }
129
+
130
+ /* Adds a canonical key that isn't there. */
131
+ static void collection_add(JSContext *ctx, Collection *c, JSValue key, JSValue value, uint32_t hash) {
132
+ Table *t = table_of(c);
133
+ if (t->used == t->cap) {
134
+ rehash(ctx, c, t->count >= t->cap / 2 ? t->cap * 2 : t->cap);
135
+ t = table_of(c);
136
+ }
137
+ table_append(t, key, value, hash);
138
+ }
139
+
140
+ /* Sets key's value (adding it at the end if new). */
141
+ static void collection_set(JSContext *ctx, Collection *c, JSValue key, JSValue value) {
142
+ key = canonical_key(key);
143
+ uint32_t hash = hash_key(ctx, key);
144
+ Table *t = table_of(c);
145
+ uint32_t i = table_find(ctx, t, key, hash);
146
+ if (i != NIL) js_store(ctx, &t->host.cell, &t->entries[i].value, value);
147
+ else collection_add(ctx, c, key, value, hash);
148
+ }
149
+
150
+ static Entry *collection_find(JSContext *ctx, Collection *c, JSValue key) {
151
+ key = canonical_key(key);
152
+ Table *t = table_of(c);
153
+ uint32_t i = table_find(ctx, t, key, hash_key(ctx, key));
154
+ return i == NIL ? NULL : &t->entries[i];
155
+ }
156
+
157
+ static bool collection_delete(JSContext *ctx, Collection *c, JSValue key) {
158
+ Entry *e = collection_find(ctx, c, key);
159
+ if (!e) return false;
160
+ e->key = JS_EMPTY;
161
+ e->value = JS_UNDEFINED;
162
+ Table *t = table_of(c);
163
+ t->count--;
164
+ if (t->cap > MIN_CAP && t->count < t->cap / 4) rehash(ctx, c, t->cap / 2);
165
+ return true;
166
+ }
167
+
168
+ static void collection_clear(JSContext *ctx, Collection *c) {
169
+ Table *old = table_of(c);
170
+ Table *t = new_table(ctx, MIN_CAP);
171
+ old->cleared = true;
172
+ old->used = 0; /* its iterators restart at 0 of the new table: the entries are dead */
173
+ js_store(ctx, &old->host.cell, &old->next, js_cell(&t->host.cell));
174
+ js_store(ctx, &c->obj.cell, &c->table, js_cell(&t->host.cell));
175
+ }
176
+
177
+ /* ---- Cursors: a position in a table, which follows it when it's replaced -------------------- */
178
+
179
+ typedef struct Cursor {
180
+ Table *table;
181
+ uint32_t index;
182
+ } Cursor;
183
+
184
+ static void cursor_follow(Cursor *k) {
185
+ while (!js_is_undefined(k->table->next)) {
186
+ Table *t = k->table;
187
+ uint32_t live = 0;
188
+ if (!t->cleared)
189
+ for (uint32_t j = 0; j < k->index && j < t->used; j++) live += !js_is_empty(t->entries[j].key);
190
+ k->index = live;
191
+ k->table = (Table *)js_as_cell(t->next);
192
+ }
193
+ }
194
+
195
+ /* The next live entry, or NULL at the end. */
196
+ static Entry *cursor_next(Cursor *k) {
197
+ cursor_follow(k);
198
+ while (k->index < k->table->used) {
199
+ Entry *e = &k->table->entries[k->index++];
200
+ if (!js_is_empty(e->key)) return e;
201
+ }
202
+ return NULL;
203
+ }
204
+
205
+ static Cursor cursor_start(Collection *c) { return (Cursor){ table_of(c), 0 }; }
206
+
207
+ /* ---- Receivers and errors ----------------------------------------------------------------- */
208
+
209
+ static JSValue throw_incompatible(JSContext *ctx, const char *method, JSValue receiver) {
210
+ char what[128];
211
+ js_describe_value(ctx, receiver, what, sizeof what);
212
+ return js_throw_type_error(ctx, "Method %s called on incompatible receiver %s", method, what);
213
+ }
214
+
215
+ static Collection *this_collection(JSContext *ctx, JSValue v, const JSClass *cls, const char *method) {
216
+ if (js_is_object(v) && js_as_object(v)->shape->cls == cls) return (Collection *)js_as_object(v);
217
+ js_discard(throw_incompatible(ctx, method, v));
218
+ return NULL;
219
+ }
220
+
221
+ #define THIS_MAP(method) \
222
+ Collection *c = this_collection(ctx, this_val, &map_class, method); \
223
+ if (!c) return JS_EXCEPTION
224
+ #define THIS_SET(method) \
225
+ Collection *c = this_collection(ctx, this_val, &set_class, method); \
226
+ if (!c) return JS_EXCEPTION
227
+
228
+ static JSValue throw_not_function(JSContext *ctx, JSValue v) {
229
+ char what[128];
230
+ js_describe_value(ctx, v, what, sizeof what);
231
+ return js_throw_type_error(ctx, "%s is not a function", what);
232
+ }
233
+
234
+ /* The same, as V8 words it for a callback: "number 1 is not a function", "object is not a function". */
235
+ static JSValue throw_not_callback(JSContext *ctx, JSValue v) {
236
+ if (js_is_undefined(v)) return js_throw_type_error(ctx, "undefined is not a function");
237
+ if (js_is_null(v)) return js_throw_type_error(ctx, "object null is not a function");
238
+ if (js_is_symbol(v)) return js_throw_type_error(ctx, "symbol is not a function");
239
+ if (js_is_object(v)) return js_throw_type_error(ctx, "object is not a function");
240
+ char what[128];
241
+ js_describe_value(ctx, v, what, sizeof what);
242
+ if (js_is_string(v)) return js_throw_type_error(ctx, "string \"%s\" is not a function", what);
243
+ return js_throw_type_error(ctx, "%s %s is not a function", js_is_bool(v) ? "boolean" : "number", what);
244
+ }
245
+
246
+ /* ---- Constructors -------------------------------------------------------------------------- */
247
+
248
+ static Collection *new_collection(JSContext *ctx, const JSClass *cls, JSValue proto) {
249
+ Collection *c = (Collection *)js_alloc_object(ctx, JS_CELL_OBJECT, js_shape_root(ctx, cls, proto, 0));
250
+ Table *t = new_table(ctx, MIN_CAP);
251
+ js_store(ctx, &c->obj.cell, &c->table, js_cell(&t->host.cell));
252
+ return c;
253
+ }
254
+
255
+ /* new Map(iterable) / new Set(iterable) (§24.1.1.1, §24.2.2.1): each entry through this.set / this.add. */
256
+ static JSValue construct(JSContext *ctx, JSValue new_target, JSValue iterable, const JSClass *cls, JSIntrinsic proto_slot, bool pairs) {
257
+ if (js_is_undefined(new_target)) return js_throw_type_error(ctx, "Constructor %s requires 'new'", cls->name);
258
+ JSValue proto = js_get_prototype_from_constructor(ctx, new_target, proto_slot);
259
+ if (js_is_exception(proto)) return JS_EXCEPTION;
260
+ Collection *c = new_collection(ctx, cls, proto);
261
+ JSValue self = js_object_value(&c->obj);
262
+ if (js_is_nullish(iterable)) return self;
263
+ const char *adder_name = pairs ? "set" : "add";
264
+ JSValue adder = js_get_value(ctx, self, js_atom_from_cstr(ctx, adder_name));
265
+ if (js_is_exception(adder)) return JS_EXCEPTION;
266
+ if (!js_is_callable(adder)) {
267
+ char what[128];
268
+ js_describe_value(ctx, adder, what, sizeof what);
269
+ return js_throw_type_error(ctx, "'%s' returned for property '%s' of object '#<%s>' is not a function", what, adder_name, cls->name);
270
+ }
271
+ JSValue rec = js_iter_start(ctx, iterable);
272
+ if (js_is_exception(rec)) return JS_EXCEPTION;
273
+ for (;;) {
274
+ JSValue item = js_iter_next(ctx, rec);
275
+ if (js_is_exception(item)) return JS_EXCEPTION;
276
+ if (js_is_empty(item)) return self;
277
+ JSValue args[2] = { item, JS_UNDEFINED };
278
+ JSValue r = JS_UNDEFINED;
279
+ if (pairs) {
280
+ if (!js_is_object(item)) {
281
+ char what[128];
282
+ js_describe_value(ctx, item, what, sizeof what);
283
+ r = js_throw_type_error(ctx, "Iterator value %s is not an entry object", what);
284
+ }
285
+ if (!js_is_exception(r)) r = args[0] = js_get_value(ctx, item, js_int32(0));
286
+ if (!js_is_exception(r)) r = args[1] = js_get_value(ctx, item, js_int32(1));
287
+ }
288
+ if (!js_is_exception(r)) r = js_call(ctx, adder, self, pairs ? 2 : 1, args);
289
+ if (js_is_exception(r)) { /* close the iterator; the original error wins */
290
+ JSValue e = js_take_exception(ctx);
291
+ js_iter_close_throw(ctx, rec);
292
+ return js_throw(ctx, e);
293
+ }
294
+ }
295
+ }
296
+
297
+ BUILTIN(map_ctor) {
298
+ (void)callee, (void)this_val;
299
+ return construct(ctx, new_target, ARG(0), &map_class, JS_INTRINSIC_MAP_PROTOTYPE, true);
300
+ }
301
+
302
+ BUILTIN(set_ctor) {
303
+ (void)callee, (void)this_val;
304
+ return construct(ctx, new_target, ARG(0), &set_class, JS_INTRINSIC_SET_PROTOTYPE, false);
305
+ }
306
+
307
+ /* get Map[@@species], get Set[@@species] */
308
+ BUILTIN(get_species) {
309
+ UNUSED_ARGS;
310
+ return this_val;
311
+ }
312
+
313
+ /* Map.groupBy(items, callback) (§24.1.2.1): a Map from each key to the array of its items. */
314
+ BUILTIN(map_group_by) {
315
+ (void)callee, (void)this_val, (void)new_target;
316
+ JSValue items = ARG(0), callback = ARG(1);
317
+ if (js_is_nullish(items)) return js_throw_type_error(ctx, "Map.groupBy requires an iterable");
318
+ if (!js_is_callable(callback)) return throw_not_function(ctx, callback);
319
+ Collection *c = new_collection(ctx, &map_class, ctx->intrinsics[JS_INTRINSIC_MAP_PROTOTYPE]);
320
+ JSValue map = js_object_value(&c->obj);
321
+ JSValue rec = js_iter_start(ctx, items);
322
+ if (js_is_exception(rec)) return rec;
323
+ for (double k = 0;; k++) {
324
+ JSValue v = js_iter_next(ctx, rec);
325
+ if (js_is_exception(v)) return v;
326
+ if (js_is_empty(v)) return map;
327
+ JSValue args[2] = { v, js_number(k) };
328
+ JSValue key = js_call(ctx, callback, JS_UNDEFINED, 2, args);
329
+ if (js_is_exception(key)) {
330
+ js_iter_close_throw(ctx, rec);
331
+ return JS_EXCEPTION;
332
+ }
333
+ Entry *e = collection_find(ctx, c, key);
334
+ JSValue group = e ? e->value : js_new_array(ctx, 0);
335
+ if (!e) collection_set(ctx, c, key, group);
336
+ if (js_array_push(ctx, js_as_object(group), v) < 0) return JS_EXCEPTION;
337
+ }
338
+ }
339
+
340
+ /* ---- Map.prototype (§24.1.3) ---------------------------------------------------------------- */
341
+
342
+ BUILTIN(map_get) {
343
+ (void)callee, (void)new_target;
344
+ THIS_MAP("Map.prototype.get");
345
+ Entry *e = collection_find(ctx, c, ARG(0));
346
+ return e ? e->value : JS_UNDEFINED;
347
+ }
348
+
349
+ BUILTIN(map_set) {
350
+ (void)callee, (void)new_target;
351
+ THIS_MAP("Map.prototype.set");
352
+ collection_set(ctx, c, ARG(0), ARG(1));
353
+ return this_val;
354
+ }
355
+
356
+ BUILTIN(map_has) {
357
+ (void)callee, (void)new_target;
358
+ THIS_MAP("Map.prototype.has");
359
+ return js_bool(collection_find(ctx, c, ARG(0)) != NULL);
360
+ }
361
+
362
+ BUILTIN(map_delete) {
363
+ (void)callee, (void)new_target;
364
+ THIS_MAP("Map.prototype.delete");
365
+ return js_bool(collection_delete(ctx, c, ARG(0)));
366
+ }
367
+
368
+ BUILTIN(map_clear) {
369
+ (void)callee, (void)argc, (void)argv, (void)new_target;
370
+ THIS_MAP("Map.prototype.clear");
371
+ collection_clear(ctx, c);
372
+ return JS_UNDEFINED;
373
+ }
374
+
375
+ /* Map.prototype.getOrInsert(key, value): key's value, after setting it to value if absent. */
376
+ BUILTIN(map_get_or_insert) {
377
+ (void)callee, (void)new_target;
378
+ THIS_MAP("Map.prototype.getOrInsert");
379
+ Entry *e = collection_find(ctx, c, ARG(0));
380
+ if (e) return e->value;
381
+ collection_set(ctx, c, ARG(0), ARG(1));
382
+ return ARG(1);
383
+ }
384
+
385
+ /* Map.prototype.getOrInsertComputed(key, callback): the callback's result is stored (even if
386
+ * the callback added the key meanwhile). */
387
+ BUILTIN(map_get_or_insert_computed) {
388
+ (void)callee, (void)new_target;
389
+ THIS_MAP("Map.prototype.getOrInsertComputed");
390
+ JSValue callback = ARG(1);
391
+ if (!js_is_callable(callback)) return throw_not_function(ctx, callback);
392
+ JSValue key = canonical_key(ARG(0));
393
+ Entry *e = collection_find(ctx, c, key);
394
+ if (e) return e->value;
395
+ JSValue value = js_call(ctx, callback, JS_UNDEFINED, 1, &key);
396
+ if (js_is_exception(value)) return value;
397
+ collection_set(ctx, c, key, value);
398
+ return value;
399
+ }
400
+
401
+ /* forEach (§24.1.3.5, §24.2.4.7): callback(value, key, collection) over the live entries. */
402
+ static JSValue for_each(JSContext *ctx, Collection *c, JSValue this_val, uint32_t argc, const JSValue *argv, bool pairs) {
403
+ JSValue callback = ARG(0);
404
+ if (!js_is_callable(callback)) return throw_not_callback(ctx, callback);
405
+ Cursor k = cursor_start(c);
406
+ for (Entry *e; (e = cursor_next(&k));) {
407
+ JSValue args[3] = { pairs ? e->value : e->key, e->key, this_val };
408
+ JSValue r = js_call(ctx, callback, ARG(1), 3, args);
409
+ if (js_is_exception(r)) return r;
410
+ }
411
+ return JS_UNDEFINED;
412
+ }
413
+
414
+ BUILTIN(map_for_each) {
415
+ (void)callee, (void)new_target;
416
+ THIS_MAP("Map.prototype.forEach");
417
+ return for_each(ctx, c, this_val, argc, argv, true);
418
+ }
419
+
420
+ BUILTIN(map_size) {
421
+ (void)callee, (void)argc, (void)argv, (void)new_target;
422
+ THIS_MAP("get Map.prototype.size");
423
+ return js_number(table_of(c)->count);
424
+ }
425
+
426
+ /* ---- Map and Set iterators (§24.1.5, §24.2.6) ------------------------------------------------ */
427
+
428
+ enum { KIND_KEYS, KIND_VALUES, KIND_ENTRIES };
429
+
430
+ typedef struct CollectionIterator {
431
+ JSObject obj;
432
+ JSValue table; /* undefined once exhausted */
433
+ uint32_t index;
434
+ uint8_t kind;
435
+ } CollectionIterator;
436
+
437
+ static void iterator_visit(JSCell *c, JSTracer *trc) { js_trace_value(trc, ((CollectionIterator *)c)->table); }
438
+
439
+ static const JSClass map_iterator_class = { "Map Iterator", sizeof(CollectionIterator), NULL, NULL, NULL, NULL, iterator_visit, NULL, NULL };
440
+ static const JSClass set_iterator_class = { "Set Iterator", sizeof(CollectionIterator), NULL, NULL, NULL, NULL, iterator_visit, NULL, NULL };
441
+
442
+ static JSValue new_iterator(JSContext *ctx, Collection *c, const JSClass *cls, JSIntrinsic proto, int kind) {
443
+ JSShape *shape = js_shape_root(ctx, cls, ctx->intrinsics[proto], 0);
444
+ CollectionIterator *it = (CollectionIterator *)js_alloc_object(ctx, JS_CELL_OBJECT, shape);
445
+ js_store(ctx, &it->obj.cell, &it->table, c->table);
446
+ it->index = 0;
447
+ it->kind = (uint8_t)kind;
448
+ return js_object_value(&it->obj);
449
+ }
450
+
451
+ static JSValue iterator_next(JSContext *ctx, JSValue this_val, const JSClass *cls, const char *method) {
452
+ if (!js_is_object(this_val) || js_as_object(this_val)->shape->cls != cls) return throw_incompatible(ctx, method, this_val);
453
+ CollectionIterator *it = (CollectionIterator *)js_as_object(this_val);
454
+ if (js_is_undefined(it->table)) return js_iter_result(ctx, JS_UNDEFINED, true);
455
+ Cursor k = { (Table *)js_as_cell(it->table), it->index };
456
+ Entry *e = cursor_next(&k);
457
+ if (!e) {
458
+ js_store(ctx, &it->obj.cell, &it->table, JS_UNDEFINED);
459
+ return js_iter_result(ctx, JS_UNDEFINED, true);
460
+ }
461
+ js_store(ctx, &it->obj.cell, &it->table, js_cell(&k.table->host.cell));
462
+ it->index = k.index;
463
+ JSValue value = cls == &set_iterator_class ? e->key : e->value; /* a Set's value is its key */
464
+ if (it->kind == KIND_KEYS) value = e->key;
465
+ if (it->kind == KIND_ENTRIES) {
466
+ JSValue pair[2] = { e->key, value };
467
+ value = js_new_array_from(ctx, pair, 2);
468
+ }
469
+ return js_iter_result(ctx, value, false);
470
+ }
471
+
472
+ BUILTIN(map_iterator_next) {
473
+ (void)callee, (void)argc, (void)argv, (void)new_target;
474
+ return iterator_next(ctx, this_val, &map_iterator_class, "Map Iterator.prototype.next");
475
+ }
476
+
477
+ BUILTIN(set_iterator_next) {
478
+ (void)callee, (void)argc, (void)argv, (void)new_target;
479
+ return iterator_next(ctx, this_val, &set_iterator_class, "Set Iterator.prototype.next");
480
+ }
481
+
482
+ #define MAP_ITERATOR_METHOD(fn, kind, method) \
483
+ BUILTIN(fn) { \
484
+ (void)callee, (void)argc, (void)argv, (void)new_target; \
485
+ THIS_MAP(method); \
486
+ return new_iterator(ctx, c, &map_iterator_class, JS_INTRINSIC_MAP_ITERATOR_PROTOTYPE, kind); \
487
+ }
488
+ MAP_ITERATOR_METHOD(map_keys, KIND_KEYS, "Map.prototype.keys")
489
+ MAP_ITERATOR_METHOD(map_values, KIND_VALUES, "Map.prototype.values")
490
+ MAP_ITERATOR_METHOD(map_entries, KIND_ENTRIES, "Map.prototype.entries")
491
+
492
+ BUILTIN(set_values) {
493
+ (void)callee, (void)argc, (void)argv, (void)new_target;
494
+ THIS_SET("Set.prototype.values");
495
+ return new_iterator(ctx, c, &set_iterator_class, JS_INTRINSIC_SET_ITERATOR_PROTOTYPE, KIND_VALUES);
496
+ }
497
+
498
+ BUILTIN(set_entries) {
499
+ (void)callee, (void)argc, (void)argv, (void)new_target;
500
+ THIS_SET("Set.prototype.entries");
501
+ return new_iterator(ctx, c, &set_iterator_class, JS_INTRINSIC_SET_ITERATOR_PROTOTYPE, KIND_ENTRIES);
502
+ }
503
+
504
+ /* ---- Set.prototype (§24.2.4) ------------------------------------------------------------------ */
505
+
506
+ static void set_add(JSContext *ctx, Collection *c, JSValue v) {
507
+ if (!collection_find(ctx, c, v)) collection_set(ctx, c, v, JS_UNDEFINED);
508
+ }
509
+
510
+ BUILTIN(set_proto_add) {
511
+ (void)callee, (void)new_target;
512
+ THIS_SET("Set.prototype.add");
513
+ set_add(ctx, c, ARG(0));
514
+ return this_val;
515
+ }
516
+
517
+ BUILTIN(set_has) {
518
+ (void)callee, (void)new_target;
519
+ THIS_SET("Set.prototype.has");
520
+ return js_bool(collection_find(ctx, c, ARG(0)) != NULL);
521
+ }
522
+
523
+ BUILTIN(set_delete) {
524
+ (void)callee, (void)new_target;
525
+ THIS_SET("Set.prototype.delete");
526
+ return js_bool(collection_delete(ctx, c, ARG(0)));
527
+ }
528
+
529
+ BUILTIN(set_clear) {
530
+ (void)callee, (void)argc, (void)argv, (void)new_target;
531
+ THIS_SET("Set.prototype.clear");
532
+ collection_clear(ctx, c);
533
+ return JS_UNDEFINED;
534
+ }
535
+
536
+ BUILTIN(set_for_each) {
537
+ (void)callee, (void)new_target;
538
+ THIS_SET("Set.prototype.forEach");
539
+ return for_each(ctx, c, this_val, argc, argv, false);
540
+ }
541
+
542
+ BUILTIN(set_size) {
543
+ (void)callee, (void)argc, (void)argv, (void)new_target;
544
+ THIS_SET("get Set.prototype.size");
545
+ return js_number(table_of(c)->count);
546
+ }
547
+
548
+ /* ---- The Set methods (§24.2.4: union, intersection, …) ----------------------------------------- */
549
+
550
+ /* GetSetRecord (§24.2.1.2): the other set's size, has and keys. */
551
+ typedef struct SetRecord {
552
+ JSValue set, has, keys;
553
+ double size;
554
+ } SetRecord;
555
+
556
+ static int get_set_record(JSContext *ctx, JSValue other, const char *method, SetRecord *r) {
557
+ if (!js_is_object(other)) {
558
+ js_discard(js_throw_type_error(ctx, "%s argument must be an object", method));
559
+ return -1;
560
+ }
561
+ JSValue raw = js_get_value(ctx, other, js_atom_from_cstr(ctx, "size"));
562
+ if (js_is_exception(raw)) return -1;
563
+ double n;
564
+ if (js_to_number(ctx, &n, raw) < 0) return -1;
565
+ if (isnan(n)) {
566
+ js_discard(js_throw_type_error(ctx, "The .size property is NaN"));
567
+ return -1;
568
+ }
569
+ if (js_to_integer_or_infinity(ctx, &n, js_number(n)) < 0) return -1;
570
+ if (n < 0) {
571
+ JSValue s = js_to_string(ctx, js_number(n));
572
+ char t[64];
573
+ t[js_view_to_utf8(js_string_view(ctx, s), t, sizeof t - 1)] = '\0';
574
+ js_discard(js_throw_range_error(ctx, "'%s' is an invalid size", t));
575
+ return -1;
576
+ }
577
+ JSValue has = js_get_value(ctx, other, js_atom_from_cstr(ctx, "has"));
578
+ if (js_is_exception(has)) return -1;
579
+ if (!js_is_callable(has)) {
580
+ js_discard(js_throw_type_error(ctx, "string \"has\" is not a function"));
581
+ return -1;
582
+ }
583
+ JSValue keys = js_get_value(ctx, other, js_atom_from_cstr(ctx, "keys"));
584
+ if (js_is_exception(keys)) return -1;
585
+ if (!js_is_callable(keys)) {
586
+ js_discard(js_throw_type_error(ctx, "string \"keys\" is not a function"));
587
+ return -1;
588
+ }
589
+ *r = (SetRecord){ other, has, keys, n };
590
+ return 0;
591
+ }
592
+
593
+ /* GetIteratorFromMethod(other, keys), then IteratorStepValue and IteratorClose on it. */
594
+ typedef struct KeysIter {
595
+ JSValue iterator, next;
596
+ } KeysIter;
597
+
598
+ static int keys_start(JSContext *ctx, const SetRecord *r, KeysIter *k) {
599
+ JSValue it = js_call(ctx, r->keys, r->set, 0, NULL);
600
+ if (js_is_exception(it)) return -1;
601
+ if (!js_is_object(it)) {
602
+ js_discard(js_throw_type_error(ctx, "Result of the keys method is not an object"));
603
+ return -1;
604
+ }
605
+ JSValue next = js_get_value(ctx, it, JS_ATOM(next));
606
+ if (js_is_exception(next)) return -1;
607
+ *k = (KeysIter){ it, next };
608
+ return 0;
609
+ }
610
+
611
+ /* The next key (canonical), EMPTY when done, or EXCEPTION. */
612
+ static JSValue keys_step(JSContext *ctx, KeysIter *k) {
613
+ if (!js_is_callable(k->next)) return js_throw_type_error(ctx, "string \"next\" is not a function");
614
+ JSValue result = js_call(ctx, k->next, k->iterator, 0, NULL);
615
+ if (js_is_exception(result)) return result;
616
+ if (!js_is_object(result)) {
617
+ char what[128];
618
+ js_describe_value(ctx, result, what, sizeof what);
619
+ return js_throw_type_error(ctx, "Iterator result %s is not an object", what);
620
+ }
621
+ JSValue done = js_get_value(ctx, result, JS_ATOM(done));
622
+ if (js_is_exception(done)) return done;
623
+ if (js_truthy(done)) return JS_EMPTY;
624
+ JSValue v = js_get_value(ctx, result, JS_ATOM(value));
625
+ return js_is_exception(v) ? v : canonical_key(v);
626
+ }
627
+
628
+ /* IteratorClose after a normal completion: 0, or -1. */
629
+ static int keys_close(JSContext *ctx, KeysIter *k) {
630
+ JSValue ret = js_get_value(ctx, k->iterator, JS_ATOM(return));
631
+ if (js_is_exception(ret)) return -1;
632
+ if (js_is_nullish(ret)) return 0;
633
+ if (!js_is_callable(ret)) {
634
+ js_discard(throw_not_callback(ctx, ret));
635
+ return -1;
636
+ }
637
+ JSValue r = js_call(ctx, ret, k->iterator, 0, NULL);
638
+ if (js_is_exception(r)) return -1;
639
+ if (!js_is_object(r)) {
640
+ js_discard(js_throw_type_error(ctx, "The iterator's 'return' method returned a non-object"));
641
+ return -1;
642
+ }
643
+ return 0;
644
+ }
645
+
646
+ /* ToBoolean(Call(other.has, other, « e »)): 1, 0, or -1. */
647
+ static int other_has(JSContext *ctx, const SetRecord *r, JSValue e) {
648
+ JSValue v = js_call(ctx, r->has, r->set, 1, &e);
649
+ return js_is_exception(v) ? -1 : js_truthy(v);
650
+ }
651
+
652
+ /* A new Set (of %Set.prototype%) holding c's elements. */
653
+ static Collection *copy_set(JSContext *ctx, Collection *c) {
654
+ Collection *copy = new_collection(ctx, &set_class, ctx->intrinsics[JS_INTRINSIC_SET_PROTOTYPE]);
655
+ Cursor k = cursor_start(c);
656
+ for (Entry *e; (e = cursor_next(&k));) collection_add(ctx, copy, e->key, JS_UNDEFINED, e->hash);
657
+ return copy;
658
+ }
659
+
660
+ #define SET_METHOD_PROLOGUE(method) \
661
+ (void)callee, (void)new_target; \
662
+ THIS_SET(method); \
663
+ SetRecord rec; \
664
+ if (get_set_record(ctx, ARG(0), method, &rec) < 0) return JS_EXCEPTION
665
+
666
+ BUILTIN(set_union) {
667
+ SET_METHOD_PROLOGUE("Set.prototype.union");
668
+ KeysIter k;
669
+ if (keys_start(ctx, &rec, &k) < 0) return JS_EXCEPTION;
670
+ Collection *result = copy_set(ctx, c);
671
+ for (;;) {
672
+ JSValue v = keys_step(ctx, &k);
673
+ if (js_is_exception(v)) return v;
674
+ if (js_is_empty(v)) return js_object_value(&result->obj);
675
+ set_add(ctx, result, v);
676
+ }
677
+ }
678
+
679
+ BUILTIN(set_intersection) {
680
+ SET_METHOD_PROLOGUE("Set.prototype.intersection");
681
+ Collection *result = new_collection(ctx, &set_class, ctx->intrinsics[JS_INTRINSIC_SET_PROTOTYPE]);
682
+ if (table_of(c)->count <= rec.size) {
683
+ Cursor cur = cursor_start(c);
684
+ for (Entry *e; (e = cursor_next(&cur));) {
685
+ JSValue v = e->key;
686
+ int in = other_has(ctx, &rec, v);
687
+ if (in < 0) return JS_EXCEPTION;
688
+ if (in) set_add(ctx, result, v);
689
+ }
690
+ } else {
691
+ KeysIter k;
692
+ if (keys_start(ctx, &rec, &k) < 0) return JS_EXCEPTION;
693
+ for (;;) {
694
+ JSValue v = keys_step(ctx, &k);
695
+ if (js_is_exception(v)) return v;
696
+ if (js_is_empty(v)) break;
697
+ if (collection_find(ctx, c, v)) set_add(ctx, result, v);
698
+ }
699
+ }
700
+ return js_object_value(&result->obj);
701
+ }
702
+
703
+ BUILTIN(set_difference) {
704
+ SET_METHOD_PROLOGUE("Set.prototype.difference");
705
+ Collection *result = copy_set(ctx, c);
706
+ if (table_of(c)->count <= rec.size) {
707
+ /* the copy's elements, by index: has() may change this set, not the copy */
708
+ uint32_t n = table_of(result)->used;
709
+ for (uint32_t i = 0; i < n; i++) {
710
+ JSValue v = table_of(result)->entries[i].key;
711
+ if (js_is_empty(v)) continue;
712
+ int in = other_has(ctx, &rec, v);
713
+ if (in < 0) return JS_EXCEPTION;
714
+ if (in) { /* a hole, never a rehash: the indices must stay */
715
+ Table *t = table_of(result);
716
+ t->entries[i].key = JS_EMPTY;
717
+ t->count--;
718
+ }
719
+ }
720
+ } else {
721
+ KeysIter k;
722
+ if (keys_start(ctx, &rec, &k) < 0) return JS_EXCEPTION;
723
+ for (;;) {
724
+ JSValue v = keys_step(ctx, &k);
725
+ if (js_is_exception(v)) return v;
726
+ if (js_is_empty(v)) break;
727
+ collection_delete(ctx, result, v);
728
+ }
729
+ }
730
+ return js_object_value(&result->obj);
731
+ }
732
+
733
+ BUILTIN(set_symmetric_difference) {
734
+ SET_METHOD_PROLOGUE("Set.prototype.symmetricDifference");
735
+ KeysIter k;
736
+ if (keys_start(ctx, &rec, &k) < 0) return JS_EXCEPTION;
737
+ Collection *result = copy_set(ctx, c);
738
+ for (;;) {
739
+ JSValue v = keys_step(ctx, &k);
740
+ if (js_is_exception(v)) return v;
741
+ if (js_is_empty(v)) return js_object_value(&result->obj);
742
+ bool in_result = collection_find(ctx, result, v) != NULL;
743
+ if (collection_find(ctx, c, v)) {
744
+ if (in_result) collection_delete(ctx, result, v);
745
+ } else if (!in_result) {
746
+ collection_set(ctx, result, v, JS_UNDEFINED);
747
+ }
748
+ }
749
+ }
750
+
751
+ BUILTIN(set_is_subset_of) {
752
+ SET_METHOD_PROLOGUE("Set.prototype.isSubsetOf");
753
+ if (table_of(c)->count > rec.size) return JS_FALSE;
754
+ Cursor cur = cursor_start(c);
755
+ for (Entry *e; (e = cursor_next(&cur));) {
756
+ int in = other_has(ctx, &rec, e->key);
757
+ if (in <= 0) return in < 0 ? JS_EXCEPTION : JS_FALSE;
758
+ }
759
+ return JS_TRUE;
760
+ }
761
+
762
+ BUILTIN(set_is_superset_of) {
763
+ SET_METHOD_PROLOGUE("Set.prototype.isSupersetOf");
764
+ if (table_of(c)->count < rec.size) return JS_FALSE;
765
+ KeysIter k;
766
+ if (keys_start(ctx, &rec, &k) < 0) return JS_EXCEPTION;
767
+ for (;;) {
768
+ JSValue v = keys_step(ctx, &k);
769
+ if (js_is_exception(v)) return v;
770
+ if (js_is_empty(v)) return JS_TRUE;
771
+ if (!collection_find(ctx, c, v)) return keys_close(ctx, &k) < 0 ? JS_EXCEPTION : JS_FALSE;
772
+ }
773
+ }
774
+
775
+ BUILTIN(set_is_disjoint_from) {
776
+ SET_METHOD_PROLOGUE("Set.prototype.isDisjointFrom");
777
+ if (table_of(c)->count <= rec.size) {
778
+ Cursor cur = cursor_start(c);
779
+ for (Entry *e; (e = cursor_next(&cur));) {
780
+ int in = other_has(ctx, &rec, e->key);
781
+ if (in != 0) return in < 0 ? JS_EXCEPTION : JS_FALSE;
782
+ }
783
+ return JS_TRUE;
784
+ }
785
+ KeysIter k;
786
+ if (keys_start(ctx, &rec, &k) < 0) return JS_EXCEPTION;
787
+ for (;;) {
788
+ JSValue v = keys_step(ctx, &k);
789
+ if (js_is_exception(v)) return v;
790
+ if (js_is_empty(v)) return JS_TRUE;
791
+ if (collection_find(ctx, c, v)) return keys_close(ctx, &k) < 0 ? JS_EXCEPTION : JS_FALSE;
792
+ }
793
+ }
794
+
795
+ /* ---- util.inspect's view (inspect.c, through rt->collection_preview) ------------------------- */
796
+
797
+ /* A Map's or Set's entries, or what an iterator has left: an array of [k1, v1, k2, v2, …] when
798
+ * *pairs (a Map, an entries iterator), else of values. EMPTY for any other value. */
799
+ static JSValue collection_preview(JSContext *ctx, JSValue v, JSCollectionPreview *what) {
800
+ if (!js_is_object(v)) return JS_EMPTY;
801
+ const JSClass *cls = js_as_object(v)->shape->cls;
802
+ Cursor k;
803
+ bool pairs, values;
804
+ if (cls == &map_class || cls == &set_class) {
805
+ k = cursor_start((Collection *)js_as_object(v));
806
+ pairs = cls == &map_class;
807
+ values = false;
808
+ *what = pairs ? JS_PREVIEW_MAP : JS_PREVIEW_SET;
809
+ } else if (cls == &map_iterator_class || cls == &set_iterator_class) {
810
+ CollectionIterator *it = (CollectionIterator *)js_as_object(v);
811
+ *what = cls == &map_iterator_class ? JS_PREVIEW_MAP_ITERATOR : JS_PREVIEW_SET_ITERATOR;
812
+ pairs = it->kind == KIND_ENTRIES;
813
+ values = it->kind == KIND_VALUES && cls == &map_iterator_class;
814
+ if (pairs) *what = cls == &map_iterator_class ? JS_PREVIEW_MAP_ENTRIES : JS_PREVIEW_SET_ENTRIES;
815
+ if (js_is_undefined(it->table)) return js_new_array(ctx, 0);
816
+ k = (Cursor){ (Table *)js_as_cell(it->table), it->index };
817
+ } else {
818
+ return JS_EMPTY;
819
+ }
820
+ JSValue out = js_new_array(ctx, 0);
821
+ for (Entry *e; (e = cursor_next(&k));) {
822
+ bool is_set = cls == &set_class || cls == &set_iterator_class;
823
+ if (js_array_push(ctx, js_as_object(out), values ? e->value : e->key) < 0) return JS_EXCEPTION;
824
+ if (pairs && js_array_push(ctx, js_as_object(out), is_set ? e->key : e->value) < 0) return JS_EXCEPTION;
825
+ }
826
+ return out;
827
+ }
828
+
829
+ /* ---- Realm ------------------------------------------------------------------------------------- */
830
+
831
+ static const JSBuiltin map_statics[] = {
832
+ JS_BUILTIN_FN("groupBy", map_group_by, 2),
833
+ JS_BUILTIN_SYMBOL_GETTER(species, get_species),
834
+ };
835
+
836
+ static const JSBuiltin map_proto[] = {
837
+ JS_BUILTIN_FN("get", map_get, 1),
838
+ JS_BUILTIN_FN("set", map_set, 2),
839
+ JS_BUILTIN_FN("has", map_has, 1),
840
+ JS_BUILTIN_FN("delete", map_delete, 1),
841
+ JS_BUILTIN_FN("clear", map_clear, 0),
842
+ JS_BUILTIN_FN("entries", map_entries, 0),
843
+ JS_BUILTIN_FN("forEach", map_for_each, 1),
844
+ JS_BUILTIN_FN("keys", map_keys, 0),
845
+ JS_BUILTIN_GETTER("size", map_size),
846
+ JS_BUILTIN_FN("values", map_values, 0),
847
+ JS_BUILTIN_FN("getOrInsert", map_get_or_insert, 2),
848
+ JS_BUILTIN_FN("getOrInsertComputed", map_get_or_insert_computed, 2),
849
+ JS_BUILTIN_SYMBOL_STRING(toStringTag, "Map", JS_PROP_CONFIGURABLE),
850
+ JS_BUILTIN_SYMBOL_ALIAS(iterator, "entries"),
851
+ };
852
+
853
+ static const JSBuiltin set_statics[] = {
854
+ JS_BUILTIN_SYMBOL_GETTER(species, get_species),
855
+ };
856
+
857
+ static const JSBuiltin set_proto[] = {
858
+ JS_BUILTIN_FN("has", set_has, 1),
859
+ JS_BUILTIN_FN("add", set_proto_add, 1),
860
+ JS_BUILTIN_FN("delete", set_delete, 1),
861
+ JS_BUILTIN_FN("clear", set_clear, 0),
862
+ JS_BUILTIN_FN("entries", set_entries, 0),
863
+ JS_BUILTIN_FN("forEach", set_for_each, 1),
864
+ JS_BUILTIN_GETTER("size", set_size),
865
+ JS_BUILTIN_FN("values", set_values, 0),
866
+ JS_BUILTIN_ALIAS("keys", "values"),
867
+ JS_BUILTIN_FN("union", set_union, 1),
868
+ JS_BUILTIN_FN("intersection", set_intersection, 1),
869
+ JS_BUILTIN_FN("difference", set_difference, 1),
870
+ JS_BUILTIN_FN("symmetricDifference", set_symmetric_difference, 1),
871
+ JS_BUILTIN_FN("isSubsetOf", set_is_subset_of, 1),
872
+ JS_BUILTIN_FN("isSupersetOf", set_is_superset_of, 1),
873
+ JS_BUILTIN_FN("isDisjointFrom", set_is_disjoint_from, 1),
874
+ JS_BUILTIN_SYMBOL_STRING(toStringTag, "Set", JS_PROP_CONFIGURABLE),
875
+ JS_BUILTIN_SYMBOL_ALIAS(iterator, "values"),
876
+ };
877
+
878
+ static const JSBuiltin map_iterator_proto[] = {
879
+ JS_BUILTIN_FN("next", map_iterator_next, 0),
880
+ JS_BUILTIN_SYMBOL_STRING(toStringTag, "Map Iterator", JS_PROP_CONFIGURABLE),
881
+ };
882
+
883
+ static const JSBuiltin set_iterator_proto[] = {
884
+ JS_BUILTIN_FN("next", set_iterator_next, 0),
885
+ JS_BUILTIN_SYMBOL_STRING(toStringTag, "Set Iterator", JS_PROP_CONFIGURABLE),
886
+ };
887
+
888
+ static void define_class(JSContext *ctx, JSObject *global, const char *name, JSNativeFn ctor, JSIntrinsic proto_slot,
889
+ const JSBuiltin *statics, size_t nstatics, const JSBuiltin *methods, size_t n) {
890
+ JSValue proto = js_new_object_proto(ctx, ctx->intrinsics[JS_INTRINSIC_OBJECT_PROTOTYPE], 8);
891
+ ctx->intrinsics[proto_slot] = proto;
892
+ JSValue key = js_atom_from_cstr(ctx, name);
893
+ JSValue c = js_new_native(ctx, ctor, key, 0, JS_FN_STRICT | JS_FN_CONSTRUCTOR);
894
+ js_define_data(ctx, js_as_object(c), JS_ATOM(prototype), proto, 0);
895
+ js_define_builtins(ctx, js_as_object(c), statics, nstatics);
896
+ js_define_data(ctx, js_as_object(proto), JS_ATOM(constructor), c, JS_BUILTIN_METHOD_ATTRS);
897
+ js_define_builtins(ctx, js_as_object(proto), methods, n);
898
+ js_define_data(ctx, global, key, c, JS_BUILTIN_METHOD_ATTRS);
899
+ }
900
+
901
+ static void install_collections(JSContext *ctx, JSObject *global) {
902
+ define_class(ctx, global, "Map", map_ctor, JS_INTRINSIC_MAP_PROTOTYPE, map_statics, JS_COUNTOF(map_statics), map_proto, JS_COUNTOF(map_proto));
903
+ define_class(ctx, global, "Set", set_ctor, JS_INTRINSIC_SET_PROTOTYPE, set_statics, JS_COUNTOF(set_statics), set_proto, JS_COUNTOF(set_proto));
904
+ JSValue iterator_proto = ctx->intrinsics[JS_INTRINSIC_ITERATOR_PROTOTYPE];
905
+ ctx->intrinsics[JS_INTRINSIC_MAP_ITERATOR_PROTOTYPE] = js_new_object_proto(ctx, iterator_proto, 2);
906
+ js_define_builtins(ctx, js_as_object(ctx->intrinsics[JS_INTRINSIC_MAP_ITERATOR_PROTOTYPE]), map_iterator_proto, JS_COUNTOF(map_iterator_proto));
907
+ ctx->intrinsics[JS_INTRINSIC_SET_ITERATOR_PROTOTYPE] = js_new_object_proto(ctx, iterator_proto, 2);
908
+ js_define_builtins(ctx, js_as_object(ctx->intrinsics[JS_INTRINSIC_SET_ITERATOR_PROTOTYPE]), set_iterator_proto, JS_COUNTOF(set_iterator_proto));
909
+ ctx->rt->collection_preview = collection_preview;
910
+ }
911
+
912
+ const JSComponent js_component_collections = { "collections", install_collections };