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.
- package/LICENSE +21 -0
- package/README.md +45 -2
- package/cli.js +8084 -0
- package/node/include/js_node.h +46 -0
- package/node/prelude/async_hooks.js +88 -0
- package/node/prelude/buffer.js +31 -0
- package/node/prelude/crypto.js +114 -0
- package/node/prelude/events.js +227 -0
- package/node/prelude/fs.js +79 -0
- package/node/prelude/http.js +536 -0
- package/node/prelude/http2.js +30 -0
- package/node/prelude/internal/crypto/asym.js +333 -0
- package/node/prelude/internal/crypto/cipher.js +205 -0
- package/node/prelude/internal/crypto/hash.js +137 -0
- package/node/prelude/internal/crypto/kdf.js +182 -0
- package/node/prelude/internal/crypto/keys.js +603 -0
- package/node/prelude/internal/crypto/random.js +118 -0
- package/node/prelude/internal/crypto/util.js +76 -0
- package/node/prelude/internal/errors.js +88 -0
- package/node/prelude/internal/querystring.js +105 -0
- package/node/prelude/module.js +53 -0
- package/node/prelude/net.js +51 -0
- package/node/prelude/os.js +129 -0
- package/node/prelude/path.js +257 -0
- package/node/prelude/process.js +21 -0
- package/node/prelude/querystring.js +469 -0
- package/node/prelude/stream.js +1327 -0
- package/node/prelude/stream_promises.js +7 -0
- package/node/prelude/stream_web.js +6 -0
- package/node/prelude/string_decoder.js +236 -0
- package/node/prelude/tty.js +18 -0
- package/node/prelude/url.js +1156 -0
- package/node/prelude/util.js +64 -0
- package/node/prelude/zlib.js +81 -0
- package/node/src/host_fs.c +126 -0
- package/node/src/host_http.c +486 -0
- package/node/src/host_os.c +505 -0
- package/node/src/host_sys.c +78 -0
- package/node/third_party/libuv/LICENSE +19 -0
- package/node/third_party/libuv/include/uv/aix.h +32 -0
- package/node/third_party/libuv/include/uv/bsd.h +34 -0
- package/node/third_party/libuv/include/uv/darwin.h +61 -0
- package/node/third_party/libuv/include/uv/errno.h +483 -0
- package/node/third_party/libuv/include/uv/linux.h +34 -0
- package/node/third_party/libuv/include/uv/os390.h +33 -0
- package/node/third_party/libuv/include/uv/posix.h +31 -0
- package/node/third_party/libuv/include/uv/sunos.h +44 -0
- package/node/third_party/libuv/include/uv/threadpool.h +37 -0
- package/node/third_party/libuv/include/uv/tree.h +521 -0
- package/node/third_party/libuv/include/uv/unix.h +512 -0
- package/node/third_party/libuv/include/uv/version.h +43 -0
- package/node/third_party/libuv/include/uv/win.h +698 -0
- package/node/third_party/libuv/include/uv.h +1990 -0
- package/node/third_party/libuv/src/fs-poll.c +290 -0
- package/node/third_party/libuv/src/heap-inl.h +245 -0
- package/node/third_party/libuv/src/idna.c +560 -0
- package/node/third_party/libuv/src/idna.h +31 -0
- package/node/third_party/libuv/src/inet.c +298 -0
- package/node/third_party/libuv/src/queue.h +90 -0
- package/node/third_party/libuv/src/random.c +123 -0
- package/node/third_party/libuv/src/strscpy.c +38 -0
- package/node/third_party/libuv/src/strscpy.h +39 -0
- package/node/third_party/libuv/src/strtok.c +52 -0
- package/node/third_party/libuv/src/strtok.h +27 -0
- package/node/third_party/libuv/src/thread-common.c +175 -0
- package/node/third_party/libuv/src/threadpool.c +419 -0
- package/node/third_party/libuv/src/timer.c +200 -0
- package/node/third_party/libuv/src/unix/async.c +418 -0
- package/node/third_party/libuv/src/unix/bsd-ifaddrs.c +164 -0
- package/node/third_party/libuv/src/unix/core.c +2104 -0
- package/node/third_party/libuv/src/unix/darwin-proctitle.c +176 -0
- package/node/third_party/libuv/src/unix/darwin-stub.h +97 -0
- package/node/third_party/libuv/src/unix/darwin-syscalls.h +17 -0
- package/node/third_party/libuv/src/unix/darwin.c +237 -0
- package/node/third_party/libuv/src/unix/dl.c +80 -0
- package/node/third_party/libuv/src/unix/fs.c +2303 -0
- package/node/third_party/libuv/src/unix/fsevents.c +906 -0
- package/node/third_party/libuv/src/unix/getaddrinfo.c +252 -0
- package/node/third_party/libuv/src/unix/getnameinfo.c +121 -0
- package/node/third_party/libuv/src/unix/internal.h +530 -0
- package/node/third_party/libuv/src/unix/kqueue.c +667 -0
- package/node/third_party/libuv/src/unix/linux.c +2761 -0
- package/node/third_party/libuv/src/unix/loop-watcher.c +68 -0
- package/node/third_party/libuv/src/unix/loop.c +240 -0
- package/node/third_party/libuv/src/unix/pipe.c +555 -0
- package/node/third_party/libuv/src/unix/poll.c +159 -0
- package/node/third_party/libuv/src/unix/process.c +1123 -0
- package/node/third_party/libuv/src/unix/procfs-exepath.c +45 -0
- package/node/third_party/libuv/src/unix/proctitle.c +157 -0
- package/node/third_party/libuv/src/unix/random-devurandom.c +93 -0
- package/node/third_party/libuv/src/unix/random-getentropy.c +57 -0
- package/node/third_party/libuv/src/unix/random-getrandom.c +86 -0
- package/node/third_party/libuv/src/unix/random-sysctl-linux.c +99 -0
- package/node/third_party/libuv/src/unix/signal.c +581 -0
- package/node/third_party/libuv/src/unix/stream.c +1567 -0
- package/node/third_party/libuv/src/unix/tcp.c +673 -0
- package/node/third_party/libuv/src/unix/thread.c +975 -0
- package/node/third_party/libuv/src/unix/tty.c +510 -0
- package/node/third_party/libuv/src/unix/udp.c +1434 -0
- package/node/third_party/libuv/src/uv-common.c +1051 -0
- package/node/third_party/libuv/src/uv-common.h +474 -0
- package/node/third_party/libuv/src/uv-data-getter-setters.c +119 -0
- package/node/third_party/libuv/src/version.c +45 -0
- package/node/third_party/llhttp/LICENSE +22 -0
- package/node/third_party/llhttp/api.c +509 -0
- package/node/third_party/llhttp/http.c +170 -0
- package/node/third_party/llhttp/llhttp.c +10099 -0
- package/node/third_party/llhttp/llhttp.h +907 -0
- package/package.json +25 -4
- package/runtime/include/js_atoms.gen.h +68 -0
- package/runtime/include/js_atoms.h +54 -0
- package/runtime/include/js_bigint.h +93 -0
- package/runtime/include/js_buffer.h +79 -0
- package/runtime/include/js_builtins.h +168 -0
- package/runtime/include/js_console.h +58 -0
- package/runtime/include/js_convert.h +22 -0
- package/runtime/include/js_core.def +188 -0
- package/runtime/include/js_crypto.h +58 -0
- package/runtime/include/js_error.h +37 -0
- package/runtime/include/js_exception.h +65 -0
- package/runtime/include/js_function.h +167 -0
- package/runtime/include/js_gc.h +195 -0
- package/runtime/include/js_generator.h +96 -0
- package/runtime/include/js_host.h +19 -0
- package/runtime/include/js_jobs.h +84 -0
- package/runtime/include/js_module.h +16 -0
- package/runtime/include/js_object.h +384 -0
- package/runtime/include/js_ops.h +111 -0
- package/runtime/include/js_promise.h +45 -0
- package/runtime/include/js_regexp.h +25 -0
- package/runtime/include/js_runtime.h +30 -0
- package/runtime/include/js_symbol.h +62 -0
- package/runtime/include/js_web.h +110 -0
- package/runtime/include/jscell.h +48 -0
- package/runtime/include/jscontext.h +240 -0
- package/runtime/include/jsnum.h +42 -0
- package/runtime/include/jsstring.h +161 -0
- package/runtime/include/jsvalue.h +142 -0
- package/runtime/src/arguments.c +115 -0
- package/runtime/src/array.c +1145 -0
- package/runtime/src/arraybuffer.c +608 -0
- package/runtime/src/async_iter.c +479 -0
- package/runtime/src/atoms.c +157 -0
- package/runtime/src/base64.c +107 -0
- package/runtime/src/bigint.c +1189 -0
- package/runtime/src/blob.c +371 -0
- package/runtime/src/buffer.c +1357 -0
- package/runtime/src/builtin_table.c +100 -0
- package/runtime/src/builtins.c +987 -0
- package/runtime/src/builtins_util.h +53 -0
- package/runtime/src/collections.c +912 -0
- package/runtime/src/components.c +28 -0
- package/runtime/src/console.c +637 -0
- package/runtime/src/convert.c +82 -0
- package/runtime/src/core_all.c +21 -0
- package/runtime/src/crypto.c +116 -0
- package/runtime/src/crypto_asym.c +644 -0
- package/runtime/src/crypto_cipher.c +671 -0
- package/runtime/src/crypto_hash.c +260 -0
- package/runtime/src/crypto_host.c +414 -0
- package/runtime/src/crypto_internal.h +304 -0
- package/runtime/src/crypto_kdf.c +143 -0
- package/runtime/src/crypto_key.c +171 -0
- package/runtime/src/crypto_pkey.c +794 -0
- package/runtime/src/crypto_subtle.c +1675 -0
- package/runtime/src/date.c +940 -0
- package/runtime/src/domexception.c +169 -0
- package/runtime/src/elements.c +312 -0
- package/runtime/src/elements.h +45 -0
- package/runtime/src/encoding.c +485 -0
- package/runtime/src/error.c +297 -0
- package/runtime/src/events.c +793 -0
- package/runtime/src/exception.c +56 -0
- package/runtime/src/fetch.c +772 -0
- package/runtime/src/function.c +530 -0
- package/runtime/src/gc.c +803 -0
- package/runtime/src/generator.c +218 -0
- package/runtime/src/global.c +119 -0
- package/runtime/src/headers.c +557 -0
- package/runtime/src/inspect.c +1792 -0
- package/runtime/src/internal.h +80 -0
- package/runtime/src/iterate.c +94 -0
- package/runtime/src/iterator.c +409 -0
- package/runtime/src/jobs.c +337 -0
- package/runtime/src/jsnum.c +353 -0
- package/runtime/src/json.c +1028 -0
- package/runtime/src/math.c +243 -0
- package/runtime/src/module.c +336 -0
- package/runtime/src/number.c +176 -0
- package/runtime/src/object.c +1265 -0
- package/runtime/src/ops.c +322 -0
- package/runtime/src/os.c +196 -0
- package/runtime/src/os.h +29 -0
- package/runtime/src/process.c +432 -0
- package/runtime/src/promise.c +721 -0
- package/runtime/src/proxy.c +570 -0
- package/runtime/src/reflect.c +194 -0
- package/runtime/src/regexp.c +1141 -0
- package/runtime/src/regexp_anchor.h +34 -0
- package/runtime/src/regexp_message.h +32 -0
- package/runtime/src/shape.c +294 -0
- package/runtime/src/shape.h +46 -0
- package/runtime/src/streams.c +1046 -0
- package/runtime/src/streams.h +47 -0
- package/runtime/src/string.c +364 -0
- package/runtime/src/string_builtins.c +837 -0
- package/runtime/src/symbol.c +143 -0
- package/runtime/src/test_host.c +59 -0
- package/runtime/src/typedarray.c +1330 -0
- package/runtime/src/tznames.gen.h +712 -0
- package/runtime/src/uri.c +169 -0
- package/runtime/src/url.c +2116 -0
- package/runtime/src/weak.c +486 -0
- package/runtime/src/web.c +214 -0
- package/runtime/src/wrapper.c +59 -0
- package/runtime/src/writable.c +1486 -0
- package/runtime/third_party/ada-idna/LICENSE-MIT +18 -0
- package/runtime/third_party/ada-idna/README.md +27 -0
- package/runtime/third_party/ada-idna/convert.mjs +83 -0
- package/runtime/third_party/ada-idna/idna.c +416 -0
- package/runtime/third_party/ada-idna/idna.h +15 -0
- package/runtime/third_party/ada-idna/idna_tables.h +2657 -0
- package/runtime/third_party/bearssl/LICENSE.txt +21 -0
- package/runtime/third_party/bearssl/README.txt +136 -0
- package/runtime/third_party/bearssl/VERSION +1 -0
- package/runtime/third_party/bearssl/inc/bearssl.h +183 -0
- package/runtime/third_party/bearssl/inc/bearssl_aead.h +1059 -0
- package/runtime/third_party/bearssl/inc/bearssl_block.h +2618 -0
- package/runtime/third_party/bearssl/inc/bearssl_ec.h +967 -0
- package/runtime/third_party/bearssl/inc/bearssl_hash.h +1346 -0
- package/runtime/third_party/bearssl/inc/bearssl_hmac.h +241 -0
- package/runtime/third_party/bearssl/inc/bearssl_kdf.h +284 -0
- package/runtime/third_party/bearssl/inc/bearssl_pem.h +294 -0
- package/runtime/third_party/bearssl/inc/bearssl_prf.h +150 -0
- package/runtime/third_party/bearssl/inc/bearssl_rand.h +397 -0
- package/runtime/third_party/bearssl/inc/bearssl_rsa.h +1655 -0
- package/runtime/third_party/bearssl/inc/bearssl_ssl.h +4296 -0
- package/runtime/third_party/bearssl/inc/bearssl_x509.h +1474 -0
- package/runtime/third_party/bearssl/src/aead/ccm.c +346 -0
- package/runtime/third_party/bearssl/src/aead/eax.c +525 -0
- package/runtime/third_party/bearssl/src/aead/gcm.c +318 -0
- package/runtime/third_party/bearssl/src/codec/ccopy.c +44 -0
- package/runtime/third_party/bearssl/src/codec/dec16be.c +38 -0
- package/runtime/third_party/bearssl/src/codec/dec16le.c +38 -0
- package/runtime/third_party/bearssl/src/codec/dec32be.c +38 -0
- package/runtime/third_party/bearssl/src/codec/dec32le.c +38 -0
- package/runtime/third_party/bearssl/src/codec/dec64be.c +38 -0
- package/runtime/third_party/bearssl/src/codec/dec64le.c +38 -0
- package/runtime/third_party/bearssl/src/codec/enc16be.c +38 -0
- package/runtime/third_party/bearssl/src/codec/enc16le.c +38 -0
- package/runtime/third_party/bearssl/src/codec/enc32be.c +38 -0
- package/runtime/third_party/bearssl/src/codec/enc32le.c +38 -0
- package/runtime/third_party/bearssl/src/codec/enc64be.c +38 -0
- package/runtime/third_party/bearssl/src/codec/enc64le.c +38 -0
- package/runtime/third_party/bearssl/src/codec/pemdec.c +526 -0
- package/runtime/third_party/bearssl/src/codec/pemdec.t0 +314 -0
- package/runtime/third_party/bearssl/src/codec/pemenc.c +173 -0
- package/runtime/third_party/bearssl/src/config.h +247 -0
- package/runtime/third_party/bearssl/src/ec/ec_all_m15.c +121 -0
- package/runtime/third_party/bearssl/src/ec/ec_all_m31.c +171 -0
- package/runtime/third_party/bearssl/src/ec/ec_c25519_i15.c +398 -0
- package/runtime/third_party/bearssl/src/ec/ec_c25519_i31.c +390 -0
- package/runtime/third_party/bearssl/src/ec/ec_c25519_m15.c +1478 -0
- package/runtime/third_party/bearssl/src/ec/ec_c25519_m31.c +800 -0
- package/runtime/third_party/bearssl/src/ec/ec_c25519_m62.c +605 -0
- package/runtime/third_party/bearssl/src/ec/ec_c25519_m64.c +831 -0
- package/runtime/third_party/bearssl/src/ec/ec_curve25519.c +46 -0
- package/runtime/third_party/bearssl/src/ec/ec_default.c +36 -0
- package/runtime/third_party/bearssl/src/ec/ec_keygen.c +86 -0
- package/runtime/third_party/bearssl/src/ec/ec_p256_m15.c +2124 -0
- package/runtime/third_party/bearssl/src/ec/ec_p256_m31.c +1469 -0
- package/runtime/third_party/bearssl/src/ec/ec_p256_m62.c +1765 -0
- package/runtime/third_party/bearssl/src/ec/ec_p256_m64.c +1781 -0
- package/runtime/third_party/bearssl/src/ec/ec_prime_i15.c +824 -0
- package/runtime/third_party/bearssl/src/ec/ec_prime_i31.c +826 -0
- package/runtime/third_party/bearssl/src/ec/ec_pubkey.c +85 -0
- package/runtime/third_party/bearssl/src/ec/ec_secp256r1.c +51 -0
- package/runtime/third_party/bearssl/src/ec/ec_secp384r1.c +57 -0
- package/runtime/third_party/bearssl/src/ec/ec_secp521r1.c +64 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_atr.c +134 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_default_sign_asn1.c +36 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_default_sign_raw.c +36 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_default_vrfy_asn1.c +36 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_default_vrfy_raw.c +36 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_i15_bits.c +47 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_i15_sign_asn1.c +45 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_i15_sign_raw.c +174 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_i15_vrfy_asn1.c +48 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_i15_vrfy_raw.c +166 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_i31_bits.c +47 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_i31_sign_asn1.c +45 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_i31_sign_raw.c +173 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_i31_vrfy_asn1.c +48 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_i31_vrfy_raw.c +165 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_rta.c +121 -0
- package/runtime/third_party/bearssl/src/hash/dig_oid.c +84 -0
- package/runtime/third_party/bearssl/src/hash/dig_size.c +50 -0
- package/runtime/third_party/bearssl/src/hash/ghash_ctmul.c +345 -0
- package/runtime/third_party/bearssl/src/hash/ghash_ctmul32.c +251 -0
- package/runtime/third_party/bearssl/src/hash/ghash_ctmul64.c +154 -0
- package/runtime/third_party/bearssl/src/hash/ghash_pclmul.c +389 -0
- package/runtime/third_party/bearssl/src/hash/ghash_pwr8.c +411 -0
- package/runtime/third_party/bearssl/src/hash/md5.c +208 -0
- package/runtime/third_party/bearssl/src/hash/md5sha1.c +141 -0
- package/runtime/third_party/bearssl/src/hash/mgf1.c +56 -0
- package/runtime/third_party/bearssl/src/hash/multihash.c +166 -0
- package/runtime/third_party/bearssl/src/hash/sha1.c +191 -0
- package/runtime/third_party/bearssl/src/hash/sha2big.c +285 -0
- package/runtime/third_party/bearssl/src/hash/sha2small.c +341 -0
- package/runtime/third_party/bearssl/src/inner.h +2577 -0
- package/runtime/third_party/bearssl/src/int/i15_add.c +46 -0
- package/runtime/third_party/bearssl/src/int/i15_bitlen.c +44 -0
- package/runtime/third_party/bearssl/src/int/i15_decmod.c +124 -0
- package/runtime/third_party/bearssl/src/int/i15_decode.c +56 -0
- package/runtime/third_party/bearssl/src/int/i15_decred.c +100 -0
- package/runtime/third_party/bearssl/src/int/i15_encode.c +56 -0
- package/runtime/third_party/bearssl/src/int/i15_fmont.c +59 -0
- package/runtime/third_party/bearssl/src/int/i15_iszero.c +39 -0
- package/runtime/third_party/bearssl/src/int/i15_moddiv.c +465 -0
- package/runtime/third_party/bearssl/src/int/i15_modpow.c +50 -0
- package/runtime/third_party/bearssl/src/int/i15_modpow2.c +160 -0
- package/runtime/third_party/bearssl/src/int/i15_montmul.c +184 -0
- package/runtime/third_party/bearssl/src/int/i15_mulacc.c +61 -0
- package/runtime/third_party/bearssl/src/int/i15_muladd.c +173 -0
- package/runtime/third_party/bearssl/src/int/i15_ninv15.c +38 -0
- package/runtime/third_party/bearssl/src/int/i15_reduce.c +66 -0
- package/runtime/third_party/bearssl/src/int/i15_rshift.c +47 -0
- package/runtime/third_party/bearssl/src/int/i15_sub.c +46 -0
- package/runtime/third_party/bearssl/src/int/i15_tmont.c +36 -0
- package/runtime/third_party/bearssl/src/int/i31_add.c +46 -0
- package/runtime/third_party/bearssl/src/int/i31_bitlen.c +44 -0
- package/runtime/third_party/bearssl/src/int/i31_decmod.c +124 -0
- package/runtime/third_party/bearssl/src/int/i31_decode.c +57 -0
- package/runtime/third_party/bearssl/src/int/i31_decred.c +103 -0
- package/runtime/third_party/bearssl/src/int/i31_encode.c +79 -0
- package/runtime/third_party/bearssl/src/int/i31_fmont.c +60 -0
- package/runtime/third_party/bearssl/src/int/i31_iszero.c +39 -0
- package/runtime/third_party/bearssl/src/int/i31_moddiv.c +488 -0
- package/runtime/third_party/bearssl/src/int/i31_modpow.c +65 -0
- package/runtime/third_party/bearssl/src/int/i31_modpow2.c +160 -0
- package/runtime/third_party/bearssl/src/int/i31_montmul.c +127 -0
- package/runtime/third_party/bearssl/src/int/i31_mulacc.c +74 -0
- package/runtime/third_party/bearssl/src/int/i31_muladd.c +157 -0
- package/runtime/third_party/bearssl/src/int/i31_ninv31.c +39 -0
- package/runtime/third_party/bearssl/src/int/i31_reduce.c +66 -0
- package/runtime/third_party/bearssl/src/int/i31_rshift.c +47 -0
- package/runtime/third_party/bearssl/src/int/i31_sub.c +46 -0
- package/runtime/third_party/bearssl/src/int/i31_tmont.c +36 -0
- package/runtime/third_party/bearssl/src/int/i32_add.c +51 -0
- package/runtime/third_party/bearssl/src/int/i32_bitlen.c +44 -0
- package/runtime/third_party/bearssl/src/int/i32_decmod.c +77 -0
- package/runtime/third_party/bearssl/src/int/i32_decode.c +63 -0
- package/runtime/third_party/bearssl/src/int/i32_decred.c +107 -0
- package/runtime/third_party/bearssl/src/int/i32_div32.c +56 -0
- package/runtime/third_party/bearssl/src/int/i32_encode.c +72 -0
- package/runtime/third_party/bearssl/src/int/i32_fmont.c +60 -0
- package/runtime/third_party/bearssl/src/int/i32_iszero.c +39 -0
- package/runtime/third_party/bearssl/src/int/i32_modpow.c +65 -0
- package/runtime/third_party/bearssl/src/int/i32_montmul.c +69 -0
- package/runtime/third_party/bearssl/src/int/i32_mulacc.c +56 -0
- package/runtime/third_party/bearssl/src/int/i32_muladd.c +138 -0
- package/runtime/third_party/bearssl/src/int/i32_ninv32.c +39 -0
- package/runtime/third_party/bearssl/src/int/i32_reduce.c +66 -0
- package/runtime/third_party/bearssl/src/int/i32_sub.c +51 -0
- package/runtime/third_party/bearssl/src/int/i32_tmont.c +36 -0
- package/runtime/third_party/bearssl/src/int/i62_modpow2.c +493 -0
- package/runtime/third_party/bearssl/src/kdf/hkdf.c +107 -0
- package/runtime/third_party/bearssl/src/kdf/shake.c +590 -0
- package/runtime/third_party/bearssl/src/mac/hmac.c +122 -0
- package/runtime/third_party/bearssl/src/mac/hmac_ct.c +193 -0
- package/runtime/third_party/bearssl/src/rand/aesctr_drbg.c +206 -0
- package/runtime/third_party/bearssl/src/rand/hmac_drbg.c +157 -0
- package/runtime/third_party/bearssl/src/rand/sysrng.c +252 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_default_keygen.c +38 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_default_modulus.c +36 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_default_oaep_decrypt.c +38 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_default_oaep_encrypt.c +38 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_default_pkcs1_sign.c +38 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_default_pkcs1_vrfy.c +38 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_default_priv.c +38 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_default_privexp.c +36 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_default_pss_sign.c +38 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_default_pss_vrfy.c +38 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_default_pub.c +38 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_default_pubexp.c +36 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i15_keygen.c +583 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i15_modulus.c +99 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i15_oaep_decrypt.c +41 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i15_oaep_encrypt.c +44 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i15_pkcs1_sign.c +37 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i15_pkcs1_vrfy.c +43 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i15_priv.c +209 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i15_privexp.c +320 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i15_pss_sign.c +40 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i15_pss_vrfy.c +44 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i15_pub.c +113 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i15_pubexp.c +152 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i31_keygen.c +37 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i31_keygen_inner.c +608 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i31_modulus.c +99 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i31_oaep_decrypt.c +41 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i31_oaep_encrypt.c +44 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i31_pkcs1_sign.c +37 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i31_pkcs1_vrfy.c +43 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i31_priv.c +203 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i31_privexp.c +318 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i31_pss_sign.c +40 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i31_pss_vrfy.c +44 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i31_pub.c +106 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i31_pubexp.c +152 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i32_oaep_decrypt.c +41 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i32_oaep_encrypt.c +44 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i32_pkcs1_sign.c +37 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i32_pkcs1_vrfy.c +43 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i32_priv.c +160 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i32_pss_sign.c +40 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i32_pss_vrfy.c +44 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i32_pub.c +77 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i62_keygen.c +57 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i62_oaep_decrypt.c +61 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i62_oaep_encrypt.c +64 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i62_pkcs1_sign.c +57 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i62_pkcs1_vrfy.c +63 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i62_priv.c +223 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i62_pss_sign.c +60 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i62_pss_vrfy.c +64 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i62_pub.c +125 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_oaep_pad.c +112 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_oaep_unpad.c +145 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_pkcs1_sig_pad.c +100 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_pkcs1_sig_unpad.c +121 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_pss_sig_pad.c +106 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_pss_sig_unpad.c +121 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_ssl_decrypt.c +52 -0
- package/runtime/third_party/bearssl/src/settings.c +306 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_big_cbcdec.c +69 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_big_cbcenc.c +67 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_big_ctr.c +84 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_big_ctrcbc.c +142 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_big_dec.c +254 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_big_enc.c +157 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_common.c +112 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_ct.c +328 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_ct64.c +398 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_ct64_cbcdec.c +104 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_ct64_cbcenc.c +81 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_ct64_ctr.c +114 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_ct64_ctrcbc.c +433 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_ct64_dec.c +159 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_ct64_enc.c +115 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_ct_cbcdec.c +111 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_ct_cbcenc.c +91 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_ct_ctr.c +116 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_ct_ctrcbc.c +422 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_ct_dec.c +170 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_ct_enc.c +112 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_pwr8.c +445 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_pwr8_cbcdec.c +670 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_pwr8_cbcenc.c +417 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_pwr8_ctr.c +717 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_pwr8_ctrcbc.c +946 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_small_cbcdec.c +69 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_small_cbcenc.c +67 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_small_ctr.c +84 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_small_ctrcbc.c +142 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_small_dec.c +176 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_small_enc.c +129 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_x86ni.c +240 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_x86ni_cbcdec.c +223 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_x86ni_cbcenc.c +122 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_x86ni_ctr.c +211 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_x86ni_ctrcbc.c +596 -0
- package/runtime/third_party/bearssl/src/symcipher/chacha20_ct.c +106 -0
- package/runtime/third_party/bearssl/src/symcipher/chacha20_sse2.c +237 -0
- package/runtime/third_party/bearssl/src/symcipher/des_ct.c +411 -0
- package/runtime/third_party/bearssl/src/symcipher/des_ct_cbcdec.c +87 -0
- package/runtime/third_party/bearssl/src/symcipher/des_ct_cbcenc.c +69 -0
- package/runtime/third_party/bearssl/src/symcipher/des_support.c +166 -0
- package/runtime/third_party/bearssl/src/symcipher/des_tab.c +310 -0
- package/runtime/third_party/bearssl/src/symcipher/des_tab_cbcdec.c +85 -0
- package/runtime/third_party/bearssl/src/symcipher/des_tab_cbcenc.c +67 -0
- package/runtime/third_party/bearssl/src/symcipher/poly1305_ctmul.c +260 -0
- package/runtime/third_party/bearssl/src/symcipher/poly1305_ctmul32.c +297 -0
- package/runtime/third_party/bearssl/src/symcipher/poly1305_ctmulq.c +475 -0
- package/runtime/third_party/bearssl/src/symcipher/poly1305_i15.c +221 -0
- package/runtime/third_party/bearssl/src/x509/asn1.t0 +757 -0
- package/runtime/third_party/bearssl/src/x509/asn1enc.c +93 -0
- package/runtime/third_party/bearssl/src/x509/encode_ec_pk8der.c +110 -0
- package/runtime/third_party/bearssl/src/x509/encode_ec_rawder.c +161 -0
- package/runtime/third_party/bearssl/src/x509/encode_rsa_pk8der.c +97 -0
- package/runtime/third_party/bearssl/src/x509/encode_rsa_rawder.c +96 -0
- package/runtime/third_party/bearssl/src/x509/skey_decoder.c +653 -0
- package/runtime/third_party/bearssl/src/x509/skey_decoder.t0 +376 -0
- package/runtime/third_party/bearssl/src/x509/x509_decoder.c +776 -0
- package/runtime/third_party/bearssl/src/x509/x509_decoder.t0 +324 -0
- package/runtime/third_party/bearssl/src/x509/x509_knownkey.c +105 -0
- package/runtime/third_party/bearssl/src/x509/x509_minimal.c +1722 -0
- package/runtime/third_party/bearssl/src/x509/x509_minimal.t0 +1526 -0
- package/runtime/third_party/bearssl/src/x509/x509_minimal_full.c +59 -0
- package/runtime/third_party/quickjs-dtoa/LICENSE +22 -0
- package/runtime/third_party/quickjs-dtoa/README.md +23 -0
- package/runtime/third_party/quickjs-dtoa/dtoa.c +1640 -0
- package/runtime/third_party/quickjs-dtoa/dtoa.h +83 -0
- package/runtime/third_party/quickjs-dtoa/dtoa_compat.h +89 -0
- package/runtime/third_party/quickjs-regexp/LICENSE +22 -0
- package/runtime/third_party/quickjs-regexp/README.md +22 -0
- package/runtime/third_party/quickjs-regexp/cutils.c +641 -0
- package/runtime/third_party/quickjs-regexp/cutils.h +457 -0
- package/runtime/third_party/quickjs-regexp/libregexp-opcode.h +73 -0
- package/runtime/third_party/quickjs-regexp/libregexp.c +3448 -0
- package/runtime/third_party/quickjs-regexp/libregexp.h +65 -0
- package/runtime/third_party/quickjs-regexp/libunicode-table.h +5206 -0
- package/runtime/third_party/quickjs-regexp/libunicode.c +2124 -0
- package/runtime/third_party/quickjs-regexp/libunicode.h +196 -0
- package/runtime/third_party/v8-ieee754/LICENSE +61 -0
- package/runtime/third_party/v8-ieee754/README.md +22 -0
- package/runtime/third_party/v8-ieee754/convert.py +84 -0
- package/runtime/third_party/v8-ieee754/ieee754.c +3030 -0
- package/runtime/third_party/v8-ieee754/ieee754.h +27 -0
- package/runtime/tools/regexp_compile.c +90 -0
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
/* The job queue (ADR 0008, layer 1: microtasks, unhandled rejections) and the event
|
|
2
|
+
* loop (layer 2: timers; the v1 backend has no I/O, so waiting is sleeping). */
|
|
3
|
+
#include "js_jobs.h"
|
|
4
|
+
|
|
5
|
+
#include <stdio.h>
|
|
6
|
+
#include <stdlib.h>
|
|
7
|
+
#include <string.h>
|
|
8
|
+
|
|
9
|
+
#include "internal.h"
|
|
10
|
+
#include "js_console.h"
|
|
11
|
+
#include "js_convert.h"
|
|
12
|
+
#include "js_function.h"
|
|
13
|
+
#include "js_promise.h"
|
|
14
|
+
#include "jscontext.h"
|
|
15
|
+
#include "os.h"
|
|
16
|
+
|
|
17
|
+
/* ---- The queue -------------------------------------------------------------------- */
|
|
18
|
+
|
|
19
|
+
void js_enqueue_microtask(JSContext *ctx, const JSJob *job) {
|
|
20
|
+
JSJobQueue *q = &ctx->rt->jobs;
|
|
21
|
+
if (q->count == q->cap) { /* grow, unwrapping the ring: no allocation of cells, so no GC */
|
|
22
|
+
size_t cap = q->cap ? q->cap * 2 : 64;
|
|
23
|
+
JSJob *items = malloc(cap * sizeof *items);
|
|
24
|
+
if (!items) js_fatal("out of memory queueing a job");
|
|
25
|
+
for (size_t i = 0; i < q->count; i++) items[i] = q->items[(q->head + i) % q->cap];
|
|
26
|
+
free(q->items);
|
|
27
|
+
q->items = items;
|
|
28
|
+
q->head = 0;
|
|
29
|
+
q->cap = cap;
|
|
30
|
+
}
|
|
31
|
+
JSJob *slot = &q->items[(q->head + q->count) % q->cap];
|
|
32
|
+
*slot = *job;
|
|
33
|
+
slot->realm = ctx;
|
|
34
|
+
q->count++;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
static int run_job(const JSJob *job) {
|
|
38
|
+
JSContext *ctx = job->realm;
|
|
39
|
+
switch (job->kind) {
|
|
40
|
+
case JS_JOB_PROMISE_REACTION: return js_promise_reaction_job(ctx, job->a, job->b, job->c);
|
|
41
|
+
case JS_JOB_RESOLVE_THENABLE: return js_promise_thenable_job(ctx, job->a, job->b, job->c);
|
|
42
|
+
default: {
|
|
43
|
+
uint32_t argc = js_is_array(job->b) ? js_array_length(js_as_object(job->b)) : 0;
|
|
44
|
+
JSValue small[8], *argv = argc <= 8 ? small : malloc(argc * sizeof *argv);
|
|
45
|
+
if (!argv) js_fatal("out of memory running a job");
|
|
46
|
+
for (uint32_t i = 0; i < argc; i++) {
|
|
47
|
+
argv[i] = js_get_elem(ctx, job->b, js_index_key(i));
|
|
48
|
+
if (js_is_exception(argv[i])) {
|
|
49
|
+
if (argv != small) free(argv);
|
|
50
|
+
return -1;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
JSValue r = js_call(ctx, job->a, JS_UNDEFINED, argc, argv);
|
|
54
|
+
if (argv != small) free(argv);
|
|
55
|
+
return js_is_exception(r) ? -1 : 0;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/* ---- Unhandled rejections ------------------------------------------------------- */
|
|
61
|
+
|
|
62
|
+
void js_set_rejection_mode(JSRuntime *rt, JSRejectionMode mode) { rt->rejection_mode = (uint8_t)mode; }
|
|
63
|
+
|
|
64
|
+
void js_track_rejection(JSContext *ctx, JSValue promise, bool rejected) {
|
|
65
|
+
JSRuntime *rt = ctx->rt;
|
|
66
|
+
struct JSRejections *l = &rt->rejections;
|
|
67
|
+
if (rt->rejection_mode == JS_REJECTIONS_NONE) return;
|
|
68
|
+
if (rejected) {
|
|
69
|
+
if (l->count == l->cap) {
|
|
70
|
+
size_t cap = l->cap ? l->cap * 2 : 16;
|
|
71
|
+
JSValue *items = realloc(l->items, cap * sizeof *items);
|
|
72
|
+
if (!items) js_fatal("out of memory tracking a rejection");
|
|
73
|
+
l->items = items;
|
|
74
|
+
l->cap = cap;
|
|
75
|
+
}
|
|
76
|
+
l->items[l->count++] = promise;
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
for (size_t i = 0; i < l->count; i++) { /* handled before the checkpoint ended: not reported */
|
|
80
|
+
if (js_same_bits(l->items[i], promise)) {
|
|
81
|
+
memmove(&l->items[i], &l->items[i + 1], (l->count - i - 1) * sizeof *l->items);
|
|
82
|
+
l->count--;
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/* Reports what is still unhandled. Strict: the first one ends the program (like Node). */
|
|
89
|
+
static int process_rejections(JSContext *ctx) {
|
|
90
|
+
JSRuntime *rt = ctx->rt;
|
|
91
|
+
struct JSRejections *l = &rt->rejections;
|
|
92
|
+
while (l->count) {
|
|
93
|
+
JSValue promise = l->items[0];
|
|
94
|
+
memmove(&l->items[0], &l->items[1], (l->count - 1) * sizeof *l->items);
|
|
95
|
+
l->count--;
|
|
96
|
+
js_report_error(ctx, "Uncaught (in promise) ", js_as_promise(promise)->result);
|
|
97
|
+
if (rt->rejection_mode == JS_REJECTIONS_STRICT) {
|
|
98
|
+
l->count = 0;
|
|
99
|
+
return -1;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return 0;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/* ---- The checkpoint -------------------------------------------------------------- */
|
|
106
|
+
|
|
107
|
+
int js_run_microtasks(JSContext *ctx) {
|
|
108
|
+
JSRuntime *rt = ctx->rt;
|
|
109
|
+
if (rt->in_checkpoint) return 0;
|
|
110
|
+
rt->in_checkpoint = 1;
|
|
111
|
+
JSJobQueue *q = &rt->jobs;
|
|
112
|
+
int status = 0;
|
|
113
|
+
while (q->count) {
|
|
114
|
+
JSJob job = q->items[q->head]; /* on the C stack: the conservative scan keeps it alive */
|
|
115
|
+
q->head = (q->head + 1) % q->cap;
|
|
116
|
+
q->count--;
|
|
117
|
+
if (run_job(&job) < 0) {
|
|
118
|
+
js_report_uncaught(job.realm);
|
|
119
|
+
status = -1;
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
js_clear_kept_objects(rt); /* the end of the job: WeakRef targets may go now */
|
|
124
|
+
if (status == 0) status = process_rejections(ctx);
|
|
125
|
+
rt->in_checkpoint = 0;
|
|
126
|
+
return status;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/* ---- GC and teardown (gc.c) ----------------------------------------------------- */
|
|
130
|
+
|
|
131
|
+
void js_jobs_trace(JSRuntime *rt, JSTracer *trc) {
|
|
132
|
+
JSJobQueue *q = &rt->jobs;
|
|
133
|
+
for (size_t i = 0; i < q->count; i++) {
|
|
134
|
+
JSJob *j = &q->items[(q->head + i) % q->cap];
|
|
135
|
+
js_trace_value(trc, j->a);
|
|
136
|
+
js_trace_value(trc, j->b);
|
|
137
|
+
js_trace_value(trc, j->c);
|
|
138
|
+
}
|
|
139
|
+
for (size_t i = 0; i < rt->rejections.count; i++) js_trace_value(trc, rt->rejections.items[i]);
|
|
140
|
+
for (size_t i = 0; i < rt->timers.count; i++) {
|
|
141
|
+
js_trace_value(trc, rt->timers.heap[i].fn);
|
|
142
|
+
js_trace_value(trc, rt->timers.heap[i].args);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
void js_jobs_free(JSRuntime *rt) {
|
|
147
|
+
free(rt->jobs.items);
|
|
148
|
+
free(rt->rejections.items);
|
|
149
|
+
free(rt->timers.heap);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/* ---- Timers: a min-heap on (deadline, creation order) ------------------------------ */
|
|
153
|
+
|
|
154
|
+
static bool before(const JSTimer *a, const JSTimer *b) {
|
|
155
|
+
return a->deadline < b->deadline || (a->deadline == b->deadline && a->seq < b->seq);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
static void swap(JSTimer *a, JSTimer *b) {
|
|
159
|
+
JSTimer x = *a;
|
|
160
|
+
*a = *b;
|
|
161
|
+
*b = x;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
static void sift_up(JSTimers *t, size_t i) {
|
|
165
|
+
for (; i > 0 && before(&t->heap[i], &t->heap[(i - 1) / 2]); i = (i - 1) / 2) swap(&t->heap[i], &t->heap[(i - 1) / 2]);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
static void sift_down(JSTimers *t, size_t i) {
|
|
169
|
+
for (;;) {
|
|
170
|
+
size_t l = 2 * i + 1, r = l + 1, m = i;
|
|
171
|
+
if (l < t->count && before(&t->heap[l], &t->heap[m])) m = l;
|
|
172
|
+
if (r < t->count && before(&t->heap[r], &t->heap[m])) m = r;
|
|
173
|
+
if (m == i) return;
|
|
174
|
+
swap(&t->heap[i], &t->heap[m]);
|
|
175
|
+
i = m;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
static void timer_push(JSTimers *t, const JSTimer *timer) {
|
|
180
|
+
if (t->count == t->cap) {
|
|
181
|
+
size_t cap = t->cap ? t->cap * 2 : 16;
|
|
182
|
+
JSTimer *heap = realloc(t->heap, cap * sizeof *heap);
|
|
183
|
+
if (!heap) js_fatal("out of memory adding a timer");
|
|
184
|
+
t->heap = heap;
|
|
185
|
+
t->cap = cap;
|
|
186
|
+
}
|
|
187
|
+
t->heap[t->count++] = *timer;
|
|
188
|
+
if (!timer->unref) t->refed++;
|
|
189
|
+
sift_up(t, t->count - 1);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
static void timer_remove(JSTimers *t, size_t i) {
|
|
193
|
+
if (!t->heap[i].unref) t->refed--;
|
|
194
|
+
t->heap[i] = t->heap[--t->count];
|
|
195
|
+
if (i < t->count) {
|
|
196
|
+
sift_down(t, i);
|
|
197
|
+
sift_up(t, i);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
#define TIMEOUT_MAX 2147483647.0 /* Node's: longer delays, like shorter than 1 ms, become 1 */
|
|
202
|
+
|
|
203
|
+
/* setTimeout(fn, delay, ...args) / setInterval: the delay as Node takes it. */
|
|
204
|
+
static JSValue add_timer(JSContext *ctx, uint32_t argc, const JSValue *argv, bool repeat) {
|
|
205
|
+
JSValue fn = argc ? argv[0] : JS_UNDEFINED;
|
|
206
|
+
if (!js_is_callable(fn)) return js_throw_type_error(ctx, "The \"callback\" argument must be of type function");
|
|
207
|
+
double delay = 1;
|
|
208
|
+
if (argc > 1 && js_to_number(ctx, &delay, argv[1]) < 0) return JS_EXCEPTION;
|
|
209
|
+
if (!(delay >= 1 && delay <= TIMEOUT_MAX)) delay = 1;
|
|
210
|
+
JSValue args = argc > 2 ? js_new_array_from(ctx, argv + 2, argc - 2) : JS_UNDEFINED;
|
|
211
|
+
JSTimers *t = &ctx->rt->timers;
|
|
212
|
+
if (++t->next_id > 2147483647u) t->next_id = 1;
|
|
213
|
+
JSTimer timer = { t->now + (uint64_t)delay, t->seq++, t->next_id, repeat ? (uint32_t)delay : 0, 0, ctx, fn, args };
|
|
214
|
+
timer_push(t, &timer);
|
|
215
|
+
return js_int32((int32_t)timer.id);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
uint32_t js_add_timer(JSContext *ctx, JSValue fn, double delay, bool unref) {
|
|
219
|
+
if (!(delay >= 1 && delay <= TIMEOUT_MAX)) delay = 1;
|
|
220
|
+
JSTimers *t = &ctx->rt->timers;
|
|
221
|
+
if (!t->now) t->now = js_os_now_ms();
|
|
222
|
+
if (++t->next_id > 2147483647u) t->next_id = 1;
|
|
223
|
+
JSTimer timer = { t->now + (uint64_t)delay, t->seq++, t->next_id, 0, unref, ctx, fn, JS_UNDEFINED };
|
|
224
|
+
timer_push(t, &timer);
|
|
225
|
+
return timer.id;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/* clearTimeout / clearInterval (either clears both kinds, as in Node). */
|
|
229
|
+
static void clear_timer(JSContext *ctx, uint32_t argc, const JSValue *argv) {
|
|
230
|
+
if (!argc || !js_is_number(argv[0])) return;
|
|
231
|
+
double d = js_as_number(argv[0]);
|
|
232
|
+
JSTimers *t = &ctx->rt->timers;
|
|
233
|
+
if (t->running && d == (double)t->running) t->running_cleared = 1;
|
|
234
|
+
for (size_t i = 0; i < t->count; i++) {
|
|
235
|
+
if ((double)t->heap[i].id == d) {
|
|
236
|
+
timer_remove(t, i);
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
#define HOST_FN(fn) static JSValue fn(JSContext *ctx, JSValue callee, JSValue this_val, uint32_t argc, const JSValue *argv, JSValue new_target)
|
|
243
|
+
|
|
244
|
+
HOST_FN(set_timeout) {
|
|
245
|
+
(void)callee, (void)this_val, (void)new_target;
|
|
246
|
+
return add_timer(ctx, argc, argv, false);
|
|
247
|
+
}
|
|
248
|
+
HOST_FN(set_interval) {
|
|
249
|
+
(void)callee, (void)this_val, (void)new_target;
|
|
250
|
+
return add_timer(ctx, argc, argv, true);
|
|
251
|
+
}
|
|
252
|
+
/* setImmediate(fn, ...args): a timer due at the loop's time, so it runs before any later deadline */
|
|
253
|
+
HOST_FN(set_immediate) {
|
|
254
|
+
(void)callee, (void)this_val, (void)new_target;
|
|
255
|
+
JSValue fn = argc ? argv[0] : JS_UNDEFINED;
|
|
256
|
+
if (!js_is_callable(fn)) return js_throw_type_error(ctx, "The \"callback\" argument must be of type function");
|
|
257
|
+
JSValue args = argc > 1 ? js_new_array_from(ctx, argv + 1, argc - 1) : JS_UNDEFINED;
|
|
258
|
+
JSTimers *t = &ctx->rt->timers;
|
|
259
|
+
if (++t->next_id > 2147483647u) t->next_id = 1;
|
|
260
|
+
JSTimer timer = { t->now, t->seq++, t->next_id, 0, 0, ctx, fn, args };
|
|
261
|
+
timer_push(t, &timer);
|
|
262
|
+
return js_int32((int32_t)timer.id);
|
|
263
|
+
}
|
|
264
|
+
HOST_FN(clear_timeout) {
|
|
265
|
+
(void)callee, (void)this_val, (void)new_target;
|
|
266
|
+
clear_timer(ctx, argc, argv);
|
|
267
|
+
return JS_UNDEFINED;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/* ---- The event loop (layer 2) ------------------------------------------------------ */
|
|
271
|
+
|
|
272
|
+
static JSValue queue_microtask(JSContext *ctx, JSValue callee, JSValue this_val, uint32_t argc,
|
|
273
|
+
const JSValue *argv, JSValue new_target) {
|
|
274
|
+
(void)callee, (void)this_val, (void)new_target;
|
|
275
|
+
JSValue fn = argc ? argv[0] : JS_UNDEFINED;
|
|
276
|
+
if (!js_is_callable(fn)) return js_throw_type_error(ctx, "The \"callback\" argument must be of type function");
|
|
277
|
+
JSJob job = { JS_JOB_CALLBACK, NULL, fn, JS_UNDEFINED, JS_UNDEFINED };
|
|
278
|
+
js_enqueue_microtask(ctx, &job);
|
|
279
|
+
return JS_UNDEFINED;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
void js_jobs_realm_init(JSContext *ctx, JSObject *global) {
|
|
283
|
+
(void)js_define_method(ctx, global, "queueMicrotask", queue_microtask, 1);
|
|
284
|
+
(void)js_define_method(ctx, global, "setTimeout", set_timeout, 2);
|
|
285
|
+
(void)js_define_method(ctx, global, "setInterval", set_interval, 2);
|
|
286
|
+
(void)js_define_method(ctx, global, "clearTimeout", clear_timeout, 1);
|
|
287
|
+
(void)js_define_method(ctx, global, "clearInterval", clear_timeout, 1);
|
|
288
|
+
(void)js_define_method(ctx, global, "setImmediate", set_immediate, 1);
|
|
289
|
+
(void)js_define_method(ctx, global, "clearImmediate", clear_timeout, 1);
|
|
290
|
+
JSTimers *t = &ctx->rt->timers;
|
|
291
|
+
if (!t->now) t->now = js_os_now_ms(); /* the base of every timer the main script sets */
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/* One task per iteration, then a full checkpoint: promise callbacks always run before the
|
|
295
|
+
* next timer. The program ends when no timer is pending (unref'd ones don't count) and no I/O is. */
|
|
296
|
+
int js_run_event_loop(JSContext *ctx, JSValue main_result) {
|
|
297
|
+
if (js_is_exception(main_result)) return js_report_uncaught(ctx);
|
|
298
|
+
if (js_run_microtasks(ctx) < 0) return 1;
|
|
299
|
+
JSRuntime *rt = ctx->rt;
|
|
300
|
+
JSTimers *t = &rt->timers;
|
|
301
|
+
while (t->refed || (rt->io && rt->io->alive(rt))) {
|
|
302
|
+
/* FinalizationRegistry cleanups run between tasks; on their own they don't keep the program alive */
|
|
303
|
+
if (js_run_finalization_cleanups(ctx) < 0) return 1;
|
|
304
|
+
if (!t->count || t->heap[0].deadline > t->now) { /* nothing due at the cached time: refresh it, or wait */
|
|
305
|
+
t->now = js_os_now_ms();
|
|
306
|
+
if (!t->count || t->heap[0].deadline > t->now) {
|
|
307
|
+
int64_t timeout = t->count ? (int64_t)(t->heap[0].deadline - t->now) : -1;
|
|
308
|
+
/* the output so far appears before the wait, as Node's does (a server's log, say) */
|
|
309
|
+
fflush(stdout);
|
|
310
|
+
fflush(stderr);
|
|
311
|
+
if (rt->io && rt->io->alive(rt)) { /* I/O callbacks run while waiting */
|
|
312
|
+
int status = rt->io->run(ctx, timeout);
|
|
313
|
+
if (status) return status;
|
|
314
|
+
} else {
|
|
315
|
+
js_os_sleep_ms(t->heap[0].deadline - t->now); /* t->refed: a timer is pending */
|
|
316
|
+
}
|
|
317
|
+
t->now = js_os_now_ms();
|
|
318
|
+
}
|
|
319
|
+
continue;
|
|
320
|
+
}
|
|
321
|
+
JSTimer timer = t->heap[0]; /* on the C stack: the conservative scan keeps it alive */
|
|
322
|
+
timer_remove(t, 0);
|
|
323
|
+
t->running = timer.repeat ? timer.id : 0;
|
|
324
|
+
t->running_cleared = 0;
|
|
325
|
+
JSValue r = js_is_undefined(timer.args) ? js_call(timer.realm, timer.fn, JS_UNDEFINED, 0, NULL)
|
|
326
|
+
: js_call_spread(timer.realm, timer.fn, JS_UNDEFINED, timer.args, NULL);
|
|
327
|
+
t->running = 0;
|
|
328
|
+
if (js_is_exception(r)) return js_report_uncaught(timer.realm);
|
|
329
|
+
if (timer.repeat && !t->running_cleared) { /* re-armed after its callback, from the loop's time */
|
|
330
|
+
timer.deadline = t->now + timer.repeat;
|
|
331
|
+
timer.seq = t->seq++;
|
|
332
|
+
timer_push(t, &timer);
|
|
333
|
+
}
|
|
334
|
+
if (js_run_microtasks(ctx) < 0) return 1;
|
|
335
|
+
}
|
|
336
|
+
return 0;
|
|
337
|
+
}
|
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
/* Number <-> string conversion (ADR 0013). */
|
|
2
|
+
#include "jsnum.h"
|
|
3
|
+
|
|
4
|
+
#include <math.h>
|
|
5
|
+
#include <stdbool.h>
|
|
6
|
+
#include <stdio.h>
|
|
7
|
+
#include <stdlib.h>
|
|
8
|
+
#include <string.h>
|
|
9
|
+
|
|
10
|
+
#include "dtoa.h"
|
|
11
|
+
|
|
12
|
+
/* QuickJS's integer formatters return the length without NUL-terminating. */
|
|
13
|
+
size_t js_i32_to_cstr(char *buf, int32_t i) {
|
|
14
|
+
size_t n = i32toa(buf, i);
|
|
15
|
+
buf[n] = '\0';
|
|
16
|
+
return n;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
static bool fits_int32(double d) {
|
|
20
|
+
return d >= -2147483648.0 && d <= 2147483647.0 && (double)(int32_t)d == d;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
static size_t dtoa(char *buf, double d, int radix, int n_digits, int flags) {
|
|
24
|
+
JSDTOATempMem tmp; /* 296 bytes, no malloc */
|
|
25
|
+
return (size_t)js_dtoa(buf, d, radix, n_digits, flags, &tmp);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
size_t js_num_to_cstr(char *buf, double d) {
|
|
29
|
+
if (fits_int32(d)) return js_i32_to_cstr(buf, (int32_t)d); /* also -0 → "0" */
|
|
30
|
+
return dtoa(buf, d, 10, 0, JS_DTOA_FORMAT_FREE);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/* Binary exponent of d as significand * 2^e with a 53-bit significand
|
|
34
|
+
* (V8's Double::Exponent). Positive means d >= 2^53. */
|
|
35
|
+
static int binary_exponent(double d) {
|
|
36
|
+
uint64_t b;
|
|
37
|
+
memcpy(&b, &d, 8);
|
|
38
|
+
int biased = (int)((b >> 52) & 0x7FF);
|
|
39
|
+
return biased == 0 ? -1074 : biased - 1075;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
static double next_double_up(double d) {
|
|
43
|
+
return nextafter(d, INFINITY);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/* Radix other than 10, with V8's algorithm so the output matches Node
|
|
47
|
+
* byte for byte (ADR 0013): fraction digits until they are within half an
|
|
48
|
+
* ulp, and integer digits beyond 2^53 filled with zeros. */
|
|
49
|
+
static size_t radix_to_cstr(char *out, double value, int radix) {
|
|
50
|
+
static const char chars[] = "0123456789abcdefghijklmnopqrstuvwxyz";
|
|
51
|
+
char buffer[2200];
|
|
52
|
+
const int mid = (int)sizeof buffer / 2;
|
|
53
|
+
int integer_cursor = mid, fraction_cursor = mid;
|
|
54
|
+
|
|
55
|
+
bool negative = value < 0;
|
|
56
|
+
if (negative) value = -value;
|
|
57
|
+
|
|
58
|
+
double integer = floor(value);
|
|
59
|
+
double fraction = value - integer;
|
|
60
|
+
double delta = 0.5 * (next_double_up(value) - value);
|
|
61
|
+
if (delta < next_double_up(0.0)) delta = next_double_up(0.0);
|
|
62
|
+
|
|
63
|
+
if (fraction >= delta) {
|
|
64
|
+
buffer[fraction_cursor++] = '.';
|
|
65
|
+
do {
|
|
66
|
+
fraction *= radix;
|
|
67
|
+
delta *= radix;
|
|
68
|
+
int digit = (int)fraction;
|
|
69
|
+
buffer[fraction_cursor++] = chars[digit];
|
|
70
|
+
fraction -= digit;
|
|
71
|
+
/* Round half to even; carry back through written digits. */
|
|
72
|
+
if (fraction > 0.5 || (fraction == 0.5 && (digit & 1))) {
|
|
73
|
+
if (fraction + delta > 1) {
|
|
74
|
+
for (;;) {
|
|
75
|
+
fraction_cursor--;
|
|
76
|
+
if (fraction_cursor == mid) { /* reached the '.' */
|
|
77
|
+
integer += 1;
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
char c = buffer[fraction_cursor];
|
|
81
|
+
int prev = c > '9' ? c - 'a' + 10 : c - '0';
|
|
82
|
+
if (prev + 1 < radix) {
|
|
83
|
+
buffer[fraction_cursor++] = chars[prev + 1];
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
} while (fraction >= delta);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/* Digits below the double's precision are zeros. */
|
|
94
|
+
while (binary_exponent(integer / radix) > 0) {
|
|
95
|
+
integer /= radix;
|
|
96
|
+
buffer[--integer_cursor] = '0';
|
|
97
|
+
}
|
|
98
|
+
do {
|
|
99
|
+
double remainder = fmod(integer, radix);
|
|
100
|
+
buffer[--integer_cursor] = chars[(int)remainder];
|
|
101
|
+
integer = (integer - remainder) / radix;
|
|
102
|
+
} while (integer > 0);
|
|
103
|
+
|
|
104
|
+
if (negative) buffer[--integer_cursor] = '-';
|
|
105
|
+
size_t n = (size_t)(fraction_cursor - integer_cursor);
|
|
106
|
+
memcpy(out, buffer + integer_cursor, n);
|
|
107
|
+
out[n] = '\0';
|
|
108
|
+
return n;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
size_t js_num_to_cstr_radix(char *buf, double d, int radix) {
|
|
112
|
+
if (radix == 10 || !isfinite(d)) return js_num_to_cstr(buf, d);
|
|
113
|
+
if (fits_int32(d)) {
|
|
114
|
+
size_t n = i64toa_radix(buf, (int32_t)d, (unsigned)radix);
|
|
115
|
+
buf[n] = '\0';
|
|
116
|
+
return n;
|
|
117
|
+
}
|
|
118
|
+
return radix_to_cstr(buf, d, radix);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
size_t js_num_to_fixed(char *buf, double d, int digits) {
|
|
122
|
+
if (!(fabs(d) < 1e21)) return js_num_to_cstr(buf, d); /* also NaN */
|
|
123
|
+
return dtoa(buf, d, 10, digits, JS_DTOA_FORMAT_FRAC);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
size_t js_num_to_precision(char *buf, double d, int digits) {
|
|
127
|
+
if (!isfinite(d)) return js_num_to_cstr(buf, d);
|
|
128
|
+
return dtoa(buf, d, 10, digits, JS_DTOA_FORMAT_FIXED);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
size_t js_num_to_exponential(char *buf, double d, int digits) {
|
|
132
|
+
if (!isfinite(d)) return js_num_to_cstr(buf, d);
|
|
133
|
+
if (digits < 0) return dtoa(buf, d, 10, 0, JS_DTOA_FORMAT_FREE | JS_DTOA_EXP_ENABLED);
|
|
134
|
+
return dtoa(buf, d, 10, digits + 1, JS_DTOA_FORMAT_FIXED | JS_DTOA_EXP_ENABLED);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/* ---- StringToNumber ---- */
|
|
138
|
+
|
|
139
|
+
/* StrWhiteSpaceChar: WhiteSpace and LineTerminator. */
|
|
140
|
+
static bool is_str_whitespace(uint32_t c) {
|
|
141
|
+
switch (c) {
|
|
142
|
+
case 0x09: case 0x0A: case 0x0B: case 0x0C: case 0x0D: case 0x20:
|
|
143
|
+
case 0xA0: case 0x1680: case 0x2028: case 0x2029: case 0x202F:
|
|
144
|
+
case 0x205F: case 0x3000: case 0xFEFF:
|
|
145
|
+
return true;
|
|
146
|
+
default:
|
|
147
|
+
return c >= 0x2000 && c <= 0x200A;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
static int digit_value(char c) {
|
|
152
|
+
if (c >= '0' && c <= '9') return c - '0';
|
|
153
|
+
if (c >= 'a' && c <= 'z') return c - 'a' + 10;
|
|
154
|
+
if (c >= 'A' && c <= 'Z') return c - 'A' + 10;
|
|
155
|
+
return 99;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
static bool is_decimal(char c) { return c >= '0' && c <= '9'; }
|
|
159
|
+
|
|
160
|
+
/* `p` is ASCII, NUL-terminated, non-empty and has no surrounding whitespace. */
|
|
161
|
+
static double ascii_to_num(const char *p, size_t n) {
|
|
162
|
+
JSATODTempMem tmp;
|
|
163
|
+
|
|
164
|
+
/* NonDecimalIntegerLiteral: unsigned only. */
|
|
165
|
+
if (n > 2 && p[0] == '0') {
|
|
166
|
+
int radix = 0;
|
|
167
|
+
switch (p[1]) {
|
|
168
|
+
case 'x': case 'X': radix = 16; break;
|
|
169
|
+
case 'o': case 'O': radix = 8; break;
|
|
170
|
+
case 'b': case 'B': radix = 2; break;
|
|
171
|
+
}
|
|
172
|
+
if (radix) {
|
|
173
|
+
for (size_t i = 2; i < n; i++)
|
|
174
|
+
if (digit_value(p[i]) >= radix) return NAN;
|
|
175
|
+
return js_atod(p + 2, NULL, radix, JS_ATOD_INT_ONLY, &tmp);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/* StrDecimalLiteral: [+-] (Infinity | digits [. digits] [e [+-] digits] | . digits [e ...]) */
|
|
180
|
+
size_t i = 0;
|
|
181
|
+
bool neg = false;
|
|
182
|
+
if (p[0] == '+' || p[0] == '-') {
|
|
183
|
+
neg = p[0] == '-';
|
|
184
|
+
i = 1;
|
|
185
|
+
}
|
|
186
|
+
if (n - i == 8 && memcmp(p + i, "Infinity", 8) == 0) return neg ? -INFINITY : INFINITY;
|
|
187
|
+
|
|
188
|
+
size_t mantissa_digits = 0;
|
|
189
|
+
while (i < n && is_decimal(p[i])) i++, mantissa_digits++;
|
|
190
|
+
if (i < n && p[i] == '.') {
|
|
191
|
+
i++;
|
|
192
|
+
while (i < n && is_decimal(p[i])) i++, mantissa_digits++;
|
|
193
|
+
}
|
|
194
|
+
if (mantissa_digits == 0) return NAN;
|
|
195
|
+
if (i < n && (p[i] == 'e' || p[i] == 'E')) {
|
|
196
|
+
i++;
|
|
197
|
+
if (i < n && (p[i] == '+' || p[i] == '-')) i++;
|
|
198
|
+
if (i == n || !is_decimal(p[i])) return NAN;
|
|
199
|
+
while (i < n && is_decimal(p[i])) i++;
|
|
200
|
+
}
|
|
201
|
+
if (i != n) return NAN;
|
|
202
|
+
return js_atod(p, NULL, 10, 0, &tmp);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
double js_str_to_num(JSStrView s) {
|
|
206
|
+
uint32_t start = 0, end = s.length;
|
|
207
|
+
while (start < end && is_str_whitespace(js_view_at(s, start))) start++;
|
|
208
|
+
while (end > start && is_str_whitespace(js_view_at(s, end - 1))) end--;
|
|
209
|
+
if (start == end) return 0.0;
|
|
210
|
+
|
|
211
|
+
size_t n = end - start;
|
|
212
|
+
char small[64];
|
|
213
|
+
char *buf = n < sizeof small ? small : malloc(n + 1);
|
|
214
|
+
if (!buf) abort(); /* out of memory; the heap's OOM policy isn't defined yet */
|
|
215
|
+
|
|
216
|
+
double result = NAN;
|
|
217
|
+
for (size_t i = 0; i < n; i++) {
|
|
218
|
+
uint32_t c = js_view_at(s, start + (uint32_t)i);
|
|
219
|
+
if (c > 0x7F) goto done; /* non-ASCII inside the literal */
|
|
220
|
+
buf[i] = (char)c;
|
|
221
|
+
}
|
|
222
|
+
buf[n] = '\0';
|
|
223
|
+
result = ascii_to_num(buf, n);
|
|
224
|
+
done:
|
|
225
|
+
if (buf != small) free(buf);
|
|
226
|
+
return result;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
size_t js_num_to_locale(char *buf, double d) {
|
|
230
|
+
if (isnan(d)) return (size_t)sprintf(buf, "NaN");
|
|
231
|
+
size_t n = 0;
|
|
232
|
+
if (signbit(d)) buf[n++] = '-'; /* also -0, as ICU */
|
|
233
|
+
if (isinf(d)) return n + (size_t)sprintf(buf + n, "\xe2\x88\x9e");
|
|
234
|
+
/* the shortest decimal, positional, rounded half-up to three fraction digits (ICU rounds
|
|
235
|
+
* the shortest decimal, not the binary value: 1.0005 is "1.001") */
|
|
236
|
+
char digits[400];
|
|
237
|
+
size_t len = dtoa(digits, fabs(d), 10, 0, JS_DTOA_FORMAT_FREE | JS_DTOA_EXP_DISABLED);
|
|
238
|
+
digits[len] = '\0';
|
|
239
|
+
char *dot = strchr(digits, '.');
|
|
240
|
+
size_t int_len = dot ? (size_t)(dot - digits) : len;
|
|
241
|
+
char frac[4] = { 0 };
|
|
242
|
+
size_t frac_len = 0;
|
|
243
|
+
if (dot) {
|
|
244
|
+
const char *f = dot + 1;
|
|
245
|
+
for (; frac_len < 3 && f[frac_len]; frac_len++) frac[frac_len] = f[frac_len];
|
|
246
|
+
if (f[frac_len] >= '5' && f[frac_len] <= '9') { /* round up, carrying into the integer part */
|
|
247
|
+
size_t i = frac_len;
|
|
248
|
+
while (i > 0 && frac[i - 1] == '9') frac[--i] = '0';
|
|
249
|
+
if (i > 0) frac[i - 1]++;
|
|
250
|
+
else {
|
|
251
|
+
size_t j = int_len;
|
|
252
|
+
while (j > 0 && digits[j - 1] == '9') digits[--j] = '0';
|
|
253
|
+
if (j > 0) digits[j - 1]++;
|
|
254
|
+
else {
|
|
255
|
+
memmove(digits + 1, digits, int_len);
|
|
256
|
+
digits[0] = '1';
|
|
257
|
+
int_len++;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
while (frac_len > 0 && frac[frac_len - 1] == '0') frac_len--;
|
|
262
|
+
}
|
|
263
|
+
for (size_t i = 0; i < int_len; i++) {
|
|
264
|
+
if (i > 0 && (int_len - i) % 3 == 0) buf[n++] = ',';
|
|
265
|
+
buf[n++] = digits[i];
|
|
266
|
+
}
|
|
267
|
+
if (frac_len) {
|
|
268
|
+
buf[n++] = '.';
|
|
269
|
+
memcpy(buf + n, frac, frac_len);
|
|
270
|
+
n += frac_len;
|
|
271
|
+
}
|
|
272
|
+
buf[n] = '\0';
|
|
273
|
+
return n;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/* ---- parseFloat / parseInt: the scanner in prefix mode ----------------- */
|
|
277
|
+
|
|
278
|
+
static uint32_t skip_whitespace(JSStrView s) {
|
|
279
|
+
uint32_t i = 0;
|
|
280
|
+
while (i < s.length && is_str_whitespace(js_view_at(s, i))) i++;
|
|
281
|
+
return i;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
static int unit_digit(JSStrView s, uint32_t i) {
|
|
285
|
+
uint32_t c = js_view_at(s, i);
|
|
286
|
+
return c < 0x80 ? digit_value((char)c) : 99;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/* Correctly rounded value of the ASCII units [start, end) of s in `radix`. */
|
|
290
|
+
static double atod_range(JSStrView s, uint32_t start, uint32_t end, int radix, int flags) {
|
|
291
|
+
size_t n = end - start;
|
|
292
|
+
char small[64];
|
|
293
|
+
char *buf = n < sizeof small ? small : malloc(n + 1);
|
|
294
|
+
if (!buf) abort();
|
|
295
|
+
for (size_t i = 0; i < n; i++) buf[i] = (char)js_view_at(s, start + (uint32_t)i);
|
|
296
|
+
buf[n] = '\0';
|
|
297
|
+
JSATODTempMem tmp;
|
|
298
|
+
double d = js_atod(buf, NULL, radix, flags, &tmp);
|
|
299
|
+
if (buf != small) free(buf);
|
|
300
|
+
return d;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
static bool unit_is(JSStrView s, uint32_t i, char c) { return i < s.length && js_view_at(s, i) == (uint32_t)c; }
|
|
304
|
+
|
|
305
|
+
double js_parse_float(JSStrView s) {
|
|
306
|
+
uint32_t i = skip_whitespace(s), start = i;
|
|
307
|
+
bool neg = false;
|
|
308
|
+
if (unit_is(s, i, '+') || unit_is(s, i, '-')) neg = js_view_at(s, i++) == '-';
|
|
309
|
+
static const char inf[] = "Infinity";
|
|
310
|
+
uint32_t k = 0;
|
|
311
|
+
while (k < 8 && unit_is(s, i + k, inf[k])) k++;
|
|
312
|
+
if (k == 8) return neg ? -INFINITY : INFINITY;
|
|
313
|
+
|
|
314
|
+
uint32_t digits = 0;
|
|
315
|
+
while (i < s.length && unit_digit(s, i) < 10) i++, digits++;
|
|
316
|
+
if (unit_is(s, i, '.')) {
|
|
317
|
+
uint32_t dot = i++;
|
|
318
|
+
while (i < s.length && unit_digit(s, i) < 10) i++, digits++;
|
|
319
|
+
if (!digits) i = dot;
|
|
320
|
+
}
|
|
321
|
+
if (!digits) return NAN;
|
|
322
|
+
if (unit_is(s, i, 'e') || unit_is(s, i, 'E')) { /* only if a digit follows */
|
|
323
|
+
uint32_t j = i + 1;
|
|
324
|
+
if (unit_is(s, j, '+') || unit_is(s, j, '-')) j++;
|
|
325
|
+
if (j < s.length && unit_digit(s, j) < 10) {
|
|
326
|
+
while (j < s.length && unit_digit(s, j) < 10) j++;
|
|
327
|
+
i = j;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
return atod_range(s, start, i, 10, 0);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
double js_parse_int(JSStrView s, int32_t radix) {
|
|
334
|
+
uint32_t i = skip_whitespace(s);
|
|
335
|
+
bool neg = false;
|
|
336
|
+
if (unit_is(s, i, '+') || unit_is(s, i, '-')) neg = js_view_at(s, i++) == '-';
|
|
337
|
+
bool strip_prefix = true;
|
|
338
|
+
if (radix != 0) {
|
|
339
|
+
if (radix < 2 || radix > 36) return NAN;
|
|
340
|
+
if (radix != 16) strip_prefix = false;
|
|
341
|
+
} else {
|
|
342
|
+
radix = 10;
|
|
343
|
+
}
|
|
344
|
+
if (strip_prefix && unit_is(s, i, '0') && (unit_is(s, i + 1, 'x') || unit_is(s, i + 1, 'X'))) {
|
|
345
|
+
i += 2;
|
|
346
|
+
radix = 16;
|
|
347
|
+
}
|
|
348
|
+
uint32_t start = i;
|
|
349
|
+
while (i < s.length && unit_digit(s, i) < radix) i++;
|
|
350
|
+
if (i == start) return NAN;
|
|
351
|
+
double d = atod_range(s, start, i, radix, JS_ATOD_INT_ONLY);
|
|
352
|
+
return neg ? -d : d; /* "-0" is -0 */
|
|
353
|
+
}
|