Commit graph

375 commits

Author SHA1 Message Date
Charlie Gordon
9e67b47c0d
Improve number to string conversions (#400)
integer conversions:
- improve `u32toa_radix` and `u64toa_radix`, add `i32toa_radix`
- use `i32toa_radix` for small ints in `js_number_toString`

floating point conversions (`js_dtoa`):
- complete rewrite with fewer calls to `snprintf`
- remove `JS_DTOA_FORMAT`, define 4 possible modes for `js_dtoa`
- remove the radix argument in `js_dtoa`
- merge `js_dtoa1` into `js_dtoa`
- add `js_dtoa_infinite` for non finite values
- simplify sign handling
- handle locale specific decimal point transparently

helper function `js_fcvt`:
- simplify `js_fcvt`, remove `js_fcvt1`, reduce overhead
- round up manually instead of using `fesetround(FE_UPWARD)`.

helper function `js_ecvt`:
- document `js_ecvt` and `js_ecvt1` behavior
- avoid redundant `js_ecvt1` calls in `js_ecvt`
- fixed buffer contents, no buffer copies
- simplify decimal point handling
- round up manually instead of using `fesetround(FE_UPWARD)`.

miscellaneous:
- remove `CONFIG_PRINTF_RNDN`. This fixes some of the conversion errors
  on Windows. Updated the tests accordingly
- this fixes a v8.sh bug on macOS: `0.5.toFixed(0)` used to produce `0` instead of `1`
- add regression tests, update test_conv unit tests
- add benchmarks for `toFixed`, `toPrecision` and `toExponential` number methods
- benchmarks show all conversions are now 40 to 45% faster (M2)
2024-05-26 08:06:36 +02:00
Charlie Gordon
139b51fe4b
Simplify number parsing (#386)
- use single test in `js_strtod` loop.
- use more explicit `ATOD_xxx` flags
- remove `ATOD_TYPE_MASK`, use `ATOD_WANT_BIG_INT` instead
- remove unused arguments `flags` and `pexponent` in `js_string_to_bigint`
- merge `js_atof` and `js_atof2`, remove `slimb_t *pexponent` argument
- simplify and document `js_atof` parser, remove cumbersome labels,
- simplify `js_parseInt` test for zero radix for `ATOD_ACCEPT_HEX_PREFIX`
- simplify `next_token` number parsing, handle legacy octal in parser only
- simplify `JS_StringToBigInt`, use flags only.
- remove unused `slimb_t exponent` token field
- add number syntax tests
2024-05-26 00:17:04 +02:00
Charlie Gordon
1baa6763f8
Improve UTF-8 decoding and encoding functions (#410)
Ensure proper UTF-8 encoding (1 to 4 bytes).
Handle invalid encodings (return 0xFFFD and consume a single byte)
Individually encoded surrogate code points are accepted.

- add `utf8_scan()` to analyze a byte array for UTF-8 contents
  detects invalid encoding, computes number of codepoints and content kind:
  plain ASCII, 8-bit, 16-bit or larger codepoints.
- add `utf8_encode_len(c)` to compute the number of bytes to encode `c`
- rename `unicode_to_utf8` as `utf8_encode`
- rename `unicode_from_utf8` as `utf8_decode`
- add `utf8_decode_buf8(dest, size, src, len)` to decode a UTF-8 encoded
  byte array known to contain only ASCII and 8-bit codepoints.
- add `utf8_decode_buf16(dest, size, src, len)` to decode a UTF-8 encoded
  byte array into an array of 16-bit codepoints using UTF-16 surrogate pairs
  for non-BMP1 codepoints.
- add `utf8_encode_buf8(dest, size, src, len)` to encode an array of 8-bit
  codepoints as a UTF-8 encoded null terminated string
- add `utf16_encode_buf8(dest, size, src, len)` to decode an array of 16-bit
  codepoints (including surrogate pairs) as a UTF-8 encoded null terminated string
- detect invalid UTF-8 encoding in RegExp parser
- simplify `JS_AtomGetStrRT`, `JS_NewStringLen` using the above functions
- simplify UTF-8 decoding and error testing
2024-05-21 14:08:33 +02:00
KaruroChori
f588210641
Cherrypick https://github.com/bellard/quickjs/pull/289 (#404)
Co-authored-by: karurochari <nope>
2024-05-18 10:15:34 +02:00
Charlie Gordon
5a7e578482
Improve parsing error messages (#405)
- output more informative error messages in `js_parse_expect`.

The previous code was bogus:
```
    return js_parse_error(s, "expecting '%c'", tok);
```
this was causing a bug on `eval("do;")` where `tok` is `TOK_WHILE` (-70, 0xBA)
creating an invalid UTF-8 encoding (lone trailing byte).
This would ultimately have caused a failure in `JS_ThrowError2` if `JS_NewString`
failed when converting the error message to a string if the conversion detected the invalid
UTF-8 encoding and throwed an error (it currently does not, but should).

- test for `JS_NewString` failure in `JS_ThrowError2`
- test for `JS_FreeCString` failure in run-test262.c
- add more test cases
2024-05-14 20:36:10 +02:00
KaruroChori
99c6719b7d
Fix invalid exception for class method with name "get"
Ref: https://github.com/bellard/quickjs/pull/258
2024-05-14 09:16:26 +02:00
Jonathan Barronville
5ca3c509d0
Fixed CMakeLists.txt for Emscripten builds (#403)
Prevent compilation of __run-test262__  for Emscripten (fails due to the `ftw()` usage, but not needed anyway).
2024-05-14 08:44:46 +02:00
Charlie Gordon
b81d4deee4
Improve internal string allocation methods (#398)
String values are allocated as temporary or final results. This commit
attempts to improve the consistency and performance of this step.

- define `JS_NewString` as an inline function to allow simple expansion
  of `strlen()` for string literals
- document string contents constraints regarding UTF-8 encoding.
- rename `js_new_string8` as `js_new_string8_len`. takes `const char *`.
- new inline function `js_new_string8` takes `const char *`, computes
  string length with `strlen` and calls `js_new_string8_len`. No overhead
  for string literals
- rename `js_new_string16` to `js_new_string16_len`
- use internal string allocation functions where appropriate, remove overhead
- allocate extra byte for null terminator in source code string
2024-05-10 12:43:35 +02:00
Charlie Gordon
f9ecc1a598
Fix encoding bug in js_dtoa_radix (#399)
- fix radix conversion rounding code: incrementing the digit
  does not work for '9'.  We can assume ASCII so it works for
  all other digits, especially all letters
- also avoid recomputing the string length
2024-05-07 19:35:34 +02:00
Saúl Ibarra Corretgé
6cb1301305 Accept "kmg" suffixes for memory limits
Switch the default in the CLI to kilobytes too.
2024-05-06 11:22:16 +02:00
Saúl Ibarra Corretgé
e5ae6cf106 Fix handling of memory limit
Default to 0, which is "disabled", just like the stack limit.
2024-05-06 11:22:16 +02:00
Saúl Ibarra Corretgé
2050bc782a Fix CLI memory stats output 2024-05-05 19:12:42 +02:00
Saúl Ibarra Corretgé
840ce4e719 Make sure repos are updated before installing valgrind in CI 2024-04-23 19:54:12 +02:00
Saúl Ibarra Corretgé
38f9e429e4 Update gcc-4.8 CI to Ubuntu 18.04 2024-04-23 18:37:31 +02:00
Saúl Ibarra Corretgé
3241b46220 Remove unused variable 2024-04-22 11:48:04 +02:00
Charlie Gordon
f227746c6e
Add util.inspect emulation in REPL (#387)
- output values with controlable depth and detail
- add `.hidden` and `.depth` directives
- remove `eval_mode`
- add `use_strict` and `.strict` meta command
- add missing closures on global objects
- save and load command history to/from `~/.qjs_history`
- use USEPROFILE variable on Windows in addition to HOME
- use the same style names as util.inspect
2024-04-21 08:46:17 +02:00
Charlie Gordon
a77873d657
Optimize String.fromCharCode and String.fromCodePoint (#391)
- test for common case: single integer argument and create string directly
2024-04-21 08:28:02 +02:00
Charlie Gordon
83726bb00c
Add utility functions for string to integer conversions (#366)
* Add utility functions, improve integer conversion functions

- move `is_be()` to cutils.h
- add `is_upper_ascii()` and `to_upper_ascii()`
- add extensive benchmark for integer conversion variants in **tests/test_conv.c**
- add `u32toa()`, `i32toa()`, `u64toa()`, `i64toa()` based on register shift variant
- add  `u32toa_radix()`, `u64toa_radix()`, `i64toa_radix()` based on length_loop variant
- use direct converters instead of `snprintf()`
- copy NaN and Infinity directly in `js_dtoa1()`
- optimize `js_number_toString()` for small integers
- use `JS_NewStringLen()` instead of `JS_NewString()` when possible
- add more precise conversion tests in microbench.js
- disable some benchmark tests for gcc (they cause ASAN failures)
2024-04-19 11:35:44 +02:00
Charlie Gordon
f326a7a195
Add strip option in qjsc to reduce object size (#388)
- `-s` strips the source code
- `-ss` strips source and line/column numbers information
- `qjsc repl.js` generates an object size of **105726** bytes
- `qjsc -s repl.js` generates an object size of **20853** bytes
- `qjsc -ss repl.js` generates an object size of only **16147** bytes
- compile repl.js with `-ss`
- bump byte code version to 12
2024-04-19 08:41:12 +02:00
Charlie Gordon
43dc65d605
Fix potential conversion errors (#384)
- fix undefined behavior in double to int conversions
- do not pass an `int64_t` to `js_bool()`
2024-04-16 23:18:02 +02:00
Charlie Gordon
70a60f0aa1
Add REPL dark and light color themes (#383)
- detect terminal background from COLORFGBG environment variable
- add `.dark` and `.light` meta commands
- catch `loadScript` exceptions
2024-04-16 14:18:37 +02:00
bptato
29b45337f0
Fix member accesses for non-decimal numeric literals (#377)
* Fix member accesses for non-decimal numeric literals
    e.g. 0x0.a should return undefined, not SyntaxError.
* Remove ineffective non-decimal float parsing code and redundant checks on `is_float && radix != 10`
    (The code already wasn't doing anything because of the `is_float` check.)
2024-04-16 14:17:50 +02:00
Charlie Gordon
5797f2a716
Improve DUMP_READ_OBJECT (#382)
- improve `JS_DumpString`: use `L` prefix for wide strings
- dump variable kind and flags for locals and closures
- disassemble byte code in DUMP_READ_OBJECT
- pass start_pos to `dump_byte_code` and `dump_single_byte_code`
- write constant pool before function bytecode (bump version to 11)
- update generated code
2024-04-16 09:24:21 +02:00
Cryse Hillmes
2c47b7beb1
Expose public equality comparison and sameness public API. (#373)
* Expose public equality comparison and sameness public API.
- add `JS_IsEqual` (operator `==`), returns an `int`: `-1` if an exception was thrown
- add `JS_IsStrictEqual` (operator `===`) always succeeds, returns a `JS_BOOL`
- add `JS_IsSameValue` always succeeds, returns a `JS_BOOL`
- add `JS_IsSameValueZero` always succeeds, returns a `JS_BOOL`
2024-04-16 08:46:22 +02:00
Saúl Ibarra Corretgé
18c632c754 Fix performance.now() to return a double 2024-04-15 16:16:04 +02:00
Charlie Gordon
7597fc7fb0
Fix potential atom leak in JS_ReadFunctionTag (#380) 2024-04-15 14:03:24 +02:00
Null
8dcdb92047
fix crash in js_typed_array_slice caused by memory overlap (#379)
Use memmove instead of memcpy to prevent UB.
Fixes: https://github.com/quickjs-ng/quickjs/issues/378
Co-authored-by: zhang.yuping <zhangyuping.ypz@bytedance.com>
2024-04-15 06:40:00 +02:00
Charlie Gordon
4fb2e38b8a
Simplify arrow function parsing (#360)
- parse arrow functions only in `js_parse_cond_expr`
- remove `PF_ARROW_FUNC` flag and simplify parsing functions with flags
2024-04-14 02:44:34 +02:00
Charlie Gordon
16e7661fa0
Improve dump option support (#344)
- DUMP_XXX defined as nothing or 0 produces unconditional output
- DUMP_XXX defined as a bitmask produces conditional output based
    on command line option -d<bitmask>
- add `JS_SetDumpFlags()` to select active dump options
- accept -d[<hex mask>] and --dump[=<hex mask>] to specify active
    dump options, generalize command line option handling
- improve DUMP_READ_OBJECT output, fix indentation issue
2024-04-14 02:00:19 +02:00
Saúl Ibarra Corretgé
bb674c0c3b
Add iOS build to CI 2024-04-12 12:24:18 +02:00
Saúl Ibarra Corretgé
38fa7d7cf6 Fix crash in FinalizationRegistry when the observed object is GC'd
In the pathological case shown in
https://github.com/quickjs-ng/quickjs/issues/367 both the object and the
registry will be destroyed as part of the GC phase of JS_FreeRuntime.
When the GC sweep happens it's possible we are holding on to a corpse so
avoid calling the registry callback in that case.

This is similar to how Weak{Map,Set} deal with iterators being freed as
part of a cycle.

Fixes: https://github.com/quickjs-ng/quickjs/issues/367
2024-04-12 12:23:58 +02:00
Ben Noordhuis
325ce95c5e
Remove js_unlikely macro (#370)
It was a wrapper around gcc's __builtin_expect macro but it was only
used in three places and not in a way that suggests it really helps
branch prediction on modern (or even not so modern) CPUs.

Refs: https://github.com/quickjs-ng/quickjs/issues/369
2024-04-12 12:21:20 +02:00
Charlie Gordon
b20aad8d1a
Add faster test262 test target (#362)
* Add faster test262 test target
- add test262-fast.conf with lengthy tests disabled
- add test262-fast corresponding target
- make valgrind use test262-fast
2024-04-09 10:36:49 +02:00
Charlie Gordon
6d801de3e5
Improve js_array_lastIndexOf and friends (#359)
- special case fast arrays in `js_array_lastIndexOf`
- simplify `js_array_indexOf` and `js_array_includes` for consistency.
2024-04-08 23:08:49 +02:00
Charlie Gordon
0658d9c3e9
Fix js_math_imul (#356)
- follow ECMA specification
- remove implementation defined signed conversion
2024-04-08 22:50:39 +02:00
Charlie Gordon
97c918662b
Fix crashes in DUMP output (#350)
- avoid crashing on invalid atoms in `JS_AtomGetStrRT`
- do not dump objects and function_bytecode during
  `JS_GC_PHASE_REMOVE_CYCLES` phase
- fix crash in `print_lines` on null source
2024-04-08 21:25:01 +02:00
Charlie Gordon
56593f419b
Fix JS_ReadString for wide strings on big endian targets (#354)
swap words of wide character strings upon loading on a big endian target.
2024-04-08 17:02:20 +02:00
Charlie Gordon
f62b90daa2
Improve REPL directive support (#348)
* Improve REPL directive support

- use . on column 0 as directive prefix
- use `directives` object properties for genericity
- accept non ambiguous directive abbreviations
- reject invalid directive with extra characters
- simplify `handle_directive` and `handle_cmd`
- document ".help" instead of "\h"
- document ".load"
2024-04-08 15:34:30 +02:00
Charlie Gordon
d308a13579
Use string_get for clarity (#352) 2024-04-07 19:35:32 +02:00
Charlie Gordon
3f06c95558
Use more functions for explicit surrogate handling (#353)
- add `is_surrogate`, `get_hi_surrogate` and `get_lo_surrogate`
- use surrogate functions instead of hard coded computations
2024-04-07 18:19:55 +02:00
Charlie Gordon
1db884b140
Unify JS_DumpValue functions (#349)
- merge `JS_DumpValue(ctx, val)` and `JS_DumpValueShort(rt, val)` as `JS_DumpValue(rt, val)`
- remove unused `JS_PrintValue(ctx, val)`
2024-04-07 16:25:55 +02:00
Charlie Gordon
b8a2cf40d8
Fix fix-js-get-string AM/PM computation for Date.prototype.toLocaleString (#355)
- Fix AM/PM computation for Date.prototype.toLocalString: 11:00 and 23:00 used to convert to -1:00
2024-04-07 16:25:03 +02:00
Charlie Gordon
d61988211c
Accept shell scripts in JS_DetectModule (#358)
- use `skip_shebang` in `JS_DetectModule` before scanning for
  `import` statements
2024-04-07 16:23:50 +02:00
Charlie Gordon
15c6a773b6
remove v8 output files in make clean (#357) 2024-04-07 16:22:54 +02:00
Saúl Ibarra Corretgé
c33b8c9b13 Add Windows + Ninja to CI 2024-04-07 00:08:19 +02:00
Saúl Ibarra Corretgé
7fe17cc2d3 Remove unnecessary shell change in CI 2024-04-07 00:08:19 +02:00
Saúl Ibarra Corretgé
573a60bfc7 Fix compilation on MSVC 2022 in release mode
Fixes: https://github.com/quickjs-ng/quickjs/issues/309
2024-04-07 00:08:19 +02:00
Saúl Ibarra Corretgé
51608ce6d2 Fix CI to make actual release builds on Windows
CMAKE_BUILD_TYPE only applies on single-configuration generators: https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html

For multi-configuration generators like Visual Studio (or Xcode) --config needs to be used in order to build that specific configuration.
2024-04-07 00:08:19 +02:00
Charlie Gordon
fd99929f5d
Improve completion in REPL (#343)
* Improve completion in REPL

- refine `get_context_object` to avoid throwing errors (eg: q.<TAB>)
- do not call `eval` in `get_context_object` to avoid throwing errors
  and reduce bloat caused by variable closures.
- support completion of directives
2024-04-06 21:37:19 +02:00
Charlie Gordon
02c06d0036
fix memory leaks in run-test262 (#345) 2024-04-05 12:06:40 +02:00