Commit graph

65 commits

Author SHA1 Message Date
startewho
a008f1c098 Fix the Windows x86 MSVC build
Fixes: https://github.com/quickjs-ng/quickjs/issues/430
2024-06-17 09:59:46 +02:00
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
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
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
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
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
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
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
0de570988a
Fix strict name conformity cases (#335)
- reject *future strict reserved words* in `js_parse_function_check_names()`.
- add tests for reserved names in tests/test_language.js
- allow running tests/test_language.js with v8
- update v8.txt
2024-03-30 17:15:25 +01:00
Ben Noordhuis
f80a5b08cf
Implement setInterval() (#338)
Coincidentally fixes a timer ordering bug for which a regression test
has been added.

Fixes: https://github.com/quickjs-ng/quickjs/issues/279
2024-03-30 09:36:38 +01:00
Charlie Gordon
f02ed184a2
Fix more error cases (#332)
* Fix more error cases

- fix more cases of missing `sf->cur_pc`.
- use more precise error messages for number conversion methods
- add test cases in test_builtin.js
- updated v8 test results
2024-03-26 13:22:37 +01:00
Charlie Gordon
3a55b803b0
Make Object.prototype an immutable prototype object (#317)
* make `Object.prototype` an immutable prototype object
* throw an exception on `Object.setPrototypeOf(Object.prototype, xxx)`
* do not throw an exception for `Reflect.setPrototypeOf(Object.prototype, xxx)`
2024-03-16 08:53:29 +01:00
Charlie Gordon
aaa208ac8f
Improve error handling (#297)
* Improve error handling

- throw RangeError for invalid string length
- throw RangeError for stack overflow with updated message
- fix case for `BigInt` error messages
- refine stack check for `next_token` and `json_next_token`
- throw SyntaxError for too many variables, arguments, parameters...
- v8.js: disable v8 specific tests
- v8.js: disable Realm object tests
- v8.js: disable MODULE tests
- v8.js: disable RegExp static properties tests
- use more precise error messages
- reorder property lookup in `js_obj_to_desc()` according to ECMA
- set global object's [Symbol.toStringTag] to "global"
- fix error message for duplicate parameter name in strict mode
2024-03-10 17:04:06 +01:00
Charlie Gordon
648a8f5be1
Improve Date.parse (#289)
* Improve `Date.parse()`

- rewrite `Date.parse()` with separate parsers
- return `NaN` for out of bounds field values as specified
- add `js_tzabbr` and `string_get_tzabbr` to handle timezone abbreviations
- improve `string_get_milliseconds` readability
- accept up to 9 decimals for millisecond fraction but truncate at 3
- accept many more alternative date/time formats
- add test cases in **tests/test_builtin.js**
- produce readable output for `Date` objects in repl 
- use `JSON.stringify` to output `Date` and `string` values in **repl.js**
- remove `String.prototype.__quote`
- add `minimum_length` macro to specify argument array sizes (C99 except MSVC)
- v8.js: parse all environment variables and output them, update **v8.txt**
2024-03-10 10:34:26 +01:00
Ben Noordhuis
f406d6f78c
Accept /[\-]/u as a valid regular expression (#288)
The non-Unicode version of the pattern was already accepted.

test262 tests it in an inverted sense in
test/built-ins/RegExp/unicode_restricted_identity_escape.js but
it appears to be per spec and both V8 and Spidermonkey accept it.

Fixes: https://github.com/quickjs-ng/quickjs/issues/286
2024-03-02 13:29:15 +01:00
Charlie Gordon
47e07b25aa
Fix Map hash bug (#281)
- `map_hash_key` must generate the same key for JS_INT and JS_FLOAT64
   with the same value
- add test cases in tests/test_builtin.js
2024-02-23 11:57:43 +01:00
Charlie Gordon
ef4d8ab2ed
Force evaluation order in set_date_fields (#268) 2024-02-22 14:08:29 +01:00
Ben Noordhuis
9f9bf3c9ab
Fix for/in iteration over proxy objects (#241) 2023-12-30 22:47:32 +01:00
Saúl Ibarra Corretgé
b8402ad388 Fix js_strtod with large integers
Ref: a96f440746
2023-12-23 00:11:41 +01:00
Ben Noordhuis
f0ef9e1593
Implement RegExp 'v' flag, part 1 (#229)
This commit implements the flag itself and teaches the regex engine to
reject previously accepted patterns when in unicodeSets mode.

Refs: https://github.com/quickjs-ng/quickjs/issues/228
2023-12-21 19:37:31 +01:00
Saúl Ibarra Corretgé
4c929c5b6b Implement Error.stackTraceLimit
We default to 10 with a max cap of 64.

Ref: https://v8.dev/docs/stack-trace-api
2023-12-19 22:45:36 +01:00
Saúl Ibarra Corretgé
555d837334 Implement Error.prepareStackTrace support
Based on V8's API: https://v8.dev/docs/stack-trace-api.

Bits picked from Frida: 78fd25fed8

Closes: https://github.com/quickjs-ng/quickjs/issues/134
2023-12-19 15:36:44 +01:00
Ben Noordhuis
5cbf8727a6
Retain function source code in serialized bytecode (#218)
Also fix a small memory leak in the output from `qjsc -e`.

Fixes: https://github.com/quickjs-ng/quickjs/issues/217
2023-12-16 01:01:26 +01:00
Saúl Ibarra Corretgé
e5812862f9 Fix 'return' handling with 'yield' in 'for of' or with finally blocks
Ref: 4bb8c35da7
2023-12-14 11:49:14 +01:00
Ben Noordhuis
bace4f635e
Record source column positions (#193)
And:
- display them in stack traces
- expose them as Function.prototype.columnNumber

OP_line_num is renamed to OP_source_loc and the pc2line data structure
is extended with the column number in zigzag encoding.

The bytecode version number BC_VERSION is incremented because pc2line
data is read and written by JS_ReadObject() and JS_WriteObject() when
it is present.

Fixes: https://github.com/quickjs-ng/quickjs/issues/149
2023-12-11 22:36:13 +01:00
Ben Noordhuis
40771c9103
Disable flaky test on Cygwin (#202)
Unclear why sending a SIGQUIT signal sometimes works and sometimes
doesn't but it's probably some kind of race condition in Cygwin's
emulation layer.

Fixes: https://github.com/quickjs-ng/quickjs/issues/184
2023-12-11 22:02:32 +01:00
Ben Noordhuis
f7f1906989
Switch to SIGTERM in child process test (#203)
The hope is that switching from SIGQUIT to SIGTERM will resolve the
test's flakiness on Cygwin.

Refs: https://github.com/quickjs-ng/quickjs/issues/184
2023-12-11 09:21:19 +01:00
Ben Noordhuis
67d90092fe Add regression test for previous commit 2023-12-10 21:03:48 +01:00
Ben Noordhuis
f1b7b6da71
Replace JSValueConst with JSValue (#195)
JSValueConst was only used for the now removed CONFIG_CHECK_JSVALUE
build mode. It is kept around as an alias for JSValue in quickjs.h to
avoid breaking everyone's source builds but remove it everywhere else.
2023-12-10 15:15:42 +01:00
Felipe Gasper
56738d8b4b
Add Cygwin compatibility 2023-12-03 18:15:07 +01:00
Nick Vatamaniuc
6b3bed1740
Fix stack overflow in CVE-2023-31922 (#157)
isArray and proxy isArray can call each other indefinitely in a mutually
recursive loop.

Add a stack overflow check in the js_proxy_isArray function before calling
`JS_isArray(ctx, s->target)`.

Original issue: https://github.com/bellard/quickjs/issues/178
CVE: https://nvd.nist.gov/vuln/detail/CVE-2023-31922
2023-12-01 16:31:36 +01:00
Saúl Ibarra Corretgé
bfd8c381cb Add support for building with ClangCL on Windows
Since ClangCL is compatible with MSVC this should get us almost there.

Ref: https://clang.llvm.org/docs/MSVCCompatibility.html
2023-11-30 01:23:09 +01:00
Ben Noordhuis
b6b70e471c
Add os.cputime() (#159)
And use it in microbench to get slightly more accurate results.
2023-11-30 00:05:48 +01:00
Ben Noordhuis
5c3077e091
Implement RegExp serialization (#153)
JS_WriteObject() and JS_ReadObject() now support RegExp objects.
2023-11-29 08:50:53 +01:00
Ben Noordhuis
8df335a7b9
Expose class name to static initializers (#139)
Fixes: https://github.com/quickjs-ng/quickjs/issues/138
2023-11-26 01:09:18 +01:00
Saúl Ibarra Corretgé
8d496b3e3c Add queueMicrotask
Ref: https://github.com/quickjs-ng/quickjs/issues/16
2023-11-21 23:43:17 +01:00
Ben Noordhuis
e2bc6441f8
Optimize RegExp ASCII literal matching (#94)
Add REOP_char8 that matches single bytes. Compresses bytecode for the
ASCII common case by 33% and reduces regexp_ascii benchmark running time
by 4%. The regexp_utf16 benchmark is unaffected.
2023-11-19 17:26:45 +01:00
Ben Noordhuis
e49da8e96f
Unbreak microbench, add os.now() (#93)
The removal of the high-precision but non-standard clock source in
commit 5af98ca broke microbench because Date.now() is not granular
enough for the benchmark runner to make forward progress.

This commit adds a new method to the os module that returns time
with microsecond precision.
2023-11-19 17:26:25 +01:00
Ben Noordhuis
5af98ca858
Remove non-standard Date.__date_clock method (#88) 2023-11-18 19:15:05 +01:00
Saúl Ibarra Corretgé
54a4f2907e Refactor build system to use CMake only 2023-11-17 13:10:18 +01:00
Ben Noordhuis
d2e632e77a
Allow symbols as WeakMap and WeakSet keys (#58) 2023-11-16 09:07:59 +01:00
Ben Noordhuis
162a8b7409
Remove trailing whitespace (#46)
Not purely cosmetic because it breaks navigation with { and } in the
One True Editor.
2023-11-12 10:01:40 +01:00
Ben Noordhuis
7b64da2325
Improve BigInt hashing (#38)
Fixes: https://github.com/quickjs-ng/quickjs/issues/35
2023-11-10 21:01:09 +01:00
Ben Noordhuis
8d62210e7d
Remove non-standard BigInt methods (#37)
Fixes: https://github.com/quickjs-ng/quickjs/issues/20
2023-11-10 20:10:52 +01:00
Ben Noordhuis
38f88c0898
Remove CONFIG_BIGNUM, always enable BigInt (#34)
Fixes: https://github.com/quickjs-ng/quickjs/issues/17
2023-11-10 16:09:54 +01:00
Ben Noordhuis
ae17b8522d
Remove operator overloading (#32)
Part of https://github.com/quickjs-ng/quickjs/issues/17
2023-11-10 10:23:40 +01:00
Ben Noordhuis
e449cb08ef
Remove BigFloat (#31)
Part of https://github.com/quickjs-ng/quickjs/issues/17
2023-11-08 22:23:06 +01:00