Commit graph

232 commits

Author SHA1 Message Date
Saúl Ibarra Corretgé
4a66289af4 Add JS_Newsymbol, an API for creating symbols
Example usage:

~~~
JSValue global = JS_GetGlobalObject(ctx);
JSValue sym = JS_NewSymbol(ctx, "my.secret.thing", TRUE);
JSAtom atom = JS_ValueToAtom(ctx, sym);
JS_DefinePropertyValue(ctx, global, atom, JS_NewString(ctx, "qjs!"), JS_PROP_C_W_E);
JS_FreeAtom(ctx, atom);
JS_FreeValue(ctx, sym);
JS_FreeValue(ctx, global);
~~~
2024-03-24 21:00:54 +01:00
Charlie Gordon
5e5b00c48c
Improve string parsing and JSON parsing (#316)
* Improve string parsing and JSON parsing

- fix JSON parsing of non ASCII string contents
- more precise string parsing errors
- more precise JSON parsing errors
- add `JS_ParseState::buf_start` to compute line/column
- fix HTML comment detection at start of source code
- improve v8 Failure messages (pulled and modified `formatFailureText` from **mjsunit.js**) 
- ignore more v8 tests
2024-03-22 11:19:36 +01:00
Enno Boland
8db7d24f98 fix compiler warning: -Wunused-variable 2024-03-18 12:34:45 +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
45f8dc247c
Improve JSON parser conformity (#303)
- add JSON specific parsers for strings and numbers
- update JSON parse error messages
- fix `JSON.stringify` handling of boxed objects
- parse Flags in v8 mjsunit test files
- update v8.txt
2024-03-14 08:19:11 +01:00
Saúl Ibarra Corretgé
473bd1d531 Fix Android build
dlmalloc has been removed and the NDK now exposes a malloc.h header with
malloc_usable_size exposed, so use that.

Also remove the duplication in js__malloc_usable_size.

Fixes: https://github.com/quickjs-ng/quickjs/issues/304
2024-03-12 10:26:10 +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
Saúl Ibarra Corretgé
f2a91e86c7 Remove custom __getClass method 2024-03-10 16:55:10 +01:00
Tyler Rockwood
33e38bec00 Enable direct dispatch for WASI
Signed-off-by: Tyler Rockwood <rockwood@redpanda.com>
2024-03-10 11:06:52 +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
Saúl Ibarra Corretgé
4d052a7e71 Log endianness when dumping memory stats 2024-03-06 11:21:50 +01:00
Aful
d11f5f600d
Implement getTimezoneOffset for Win32 (#291)
Retrieves the current time zone settings with GetTimeZoneInformation to calculate time zone offset
2024-03-03 16:23:48 +01:00
Charlie Gordon
708dbcbf5b
Fix big endian serialization (#269)
* Fix big endian serialization

Big endian serialization was broken because:
- it partially relied on `WORDS_ENDIAN` (unconditionally undef'd in cutils.h)
- endianness was not handled at all in the bc reader.
- `bc_tag_str` was missing the `"RegExp"` string
- `lre_byte_swap()` was broken for `REOP_range` and `REOP_range32`

Modifications:
- remove `WORDS_ENDIAN`
- use `bc_put_u32()` / `bc_put_u64()` in `JS_WriteBigInt()`
- use `bc_get_u32()` / `bc_get_u64()` in `JS_ReadBigInt()`
- handle host endianness in `bc_get_u16()`, `bc_get_u32()`, `bc_get_u64()` and
  `JS_ReadFunctionBytecode()`
- handle optional littleEndian argument as specified in
  `js_dataview_getValue()` and `js_dataview_setValue()`
- fix `bc_tag_str` and `lre_byte_swap()`
2024-03-02 18:38:29 +01:00
Charlie Gordon
7dd2868856
Improve Number.prototype.toString for radix other than 10 (#284)
- fix the conversions for integers and exact fractions
- approximate approach for other cases.
- bypass floating point conversions for JS_TAG_INT values
- avoid divisions for base 10 integer conversions

Fixes: https://github.com/quickjs-ng/quickjs/issues/242
2024-03-01 17:49:46 +01:00
Tyler Rockwood
ec4f957ca1
Add methods to detect arrays (#282)
I have a use case where a user can hand me many different kinds of
types, array buffer, uint8array, or a string, and I need to be able to
distingush between them.

Signed-off-by: Tyler Rockwood <rockwood@redpanda.com>
2024-02-28 12:17:18 +01:00
Tyler Rockwood
d168361207 Add documentation for promise APIs
Signed-off-by: Tyler Rockwood <rockwood@redpanda.com>
2024-02-23 19:49:52 +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
Ben Noordhuis
2d1473efbc
Revert "Fix sloppy mode arguments uninitialized value use" (#276)
This reverts commit f8b3a2e93c.

No longer necessary after commit 90d8c6bae0.
2024-02-23 11:55:51 +01:00
Charlie Gordon
ef4d8ab2ed
Force evaluation order in set_date_fields (#268) 2024-02-22 14:08:29 +01:00
Tyler Rockwood
33f72491a9
Add method to GetClassID (#275)
* Add method to GetClassID

If you want to extend a built-in class you need it's class ID and there
is no robust way to get that without this accessor.

Signed-off-by: Tyler Rockwood <rockwood@redpanda.com>

* introduce constant for invalid class ID

Signed-off-by: Tyler Rockwood <rockwood@redpanda.com>

---------

Signed-off-by: Tyler Rockwood <rockwood@redpanda.com>
2024-02-20 09:29:08 +01:00
Ben Noordhuis
b257545b6f
Better output from JS_ToCString() on exception (#274)
`ToString(object)` can fail when there is a pending exception. Add a
special case for exception objects to help debugging. Getting an empty
string when the real error was "InternalError: stack overflow" is rage
inducing.

Fixes: https://github.com/quickjs-ng/quickjs/issues/273
2024-02-19 16:31:17 +01:00
Ben Noordhuis
a0f507735d
Remove unnecessary ssize_t posix-ism (#265)
ssize_t is not always available and the cast it was used in wasn't
necessary in the first place, the value already has the right type.
2024-02-15 11:32:48 +01:00
Saúl Ibarra Corretgé
fb03ca24d2 Add WASI support 2024-02-14 08:59:15 +01:00
Saúl Ibarra Corretgé
7ded62c536 Align module export API with upstream
Partially reverts
6868fb9e25
but the same behavior can be implemented in userland by getting the
module ns and querying its properties.

Ref: c6cc6a9a5e
Fixes: https://github.com/quickjs-ng/quickjs/issues/259
2024-02-12 11:00:31 +01:00
Rob Loach
229b07b9b2 android: Additional malloc_usable_size() fixes 2024-01-31 07:39:53 +01:00
Juan Campa
ca176d4e8b Fix memory usage of rt->class_count 2024-01-29 22:29:07 +01:00
Guilherme Bernal
6868fb9e25 feat: Added functions to get access to module exports 2024-01-18 18:42:37 +01:00
Ben Noordhuis
e995085d0c Fix evaluation order of computed properties
The evaluation order is observable. Align with what test262 expects.
2024-01-16 12:43:35 +01:00
Jason
48e4c63a0e
Add support for compiling with Microsoft Visual Studio C++ (MSVC) (#246) 2024-01-16 12:42:05 +01:00
Andries Hiemstra
5f6171c722
removed some unused vars (#245) 2024-01-04 16:55:56 +01:00
Ben Noordhuis
9f9bf3c9ab
Fix for/in iteration over proxy objects (#241) 2023-12-30 22:47:32 +01:00
Ben Noordhuis
b5d6cea20e
Fix Reflect typed array element conversion (#240) 2023-12-30 10:45:33 +01:00
Ben Noordhuis
05fb3d9dc8
Fix Reflect with detached ArrayBuffer (#239) 2023-12-29 15:10:45 +01:00
Nathan Rajlich
440fc1b96b
Fix getTimezoneOffset() when tm_gmtoff is not available (#224) 2023-12-24 09:34:14 +01:00
Saúl Ibarra Corretgé
2fb838c803 Fix UB in js_dtoa1 2023-12-23 00:11:41 +01:00
Fabrice Bellard
fad030bef2 reduced JS_MAX_LOCAL_VARS (github issue #123) 2023-12-23 00:11:41 +01:00
Saúl Ibarra Corretgé
bfb4b35722 Fix: 'for of' expression cannot start with 'async'
Ref: 7cefa7b121
2023-12-23 00:11:41 +01:00
Saúl Ibarra Corretgé
7ef2ed6363 Remove incorrect await in async yield*
Ref: 43420235d5
2023-12-23 00:11:41 +01:00
Saúl Ibarra Corretgé
0a640f5040 Add container_of macro
Ref: c3599515c8
2023-12-23 00:11:41 +01:00
Fabrice Bellard
c1a3b64382 Safer typed array finalizer 2023-12-23 00:11:41 +01:00
Saúl Ibarra Corretgé
b8402ad388 Fix js_strtod with large integers
Ref: a96f440746
2023-12-23 00:11:41 +01:00
Nathan Rajlich
f94fbe2f8a Make performance configurable 2023-12-22 12:11:29 +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é
d1852b5ea2 Remove unnecessary casts
Follow-up to https://github.com/quickjs-ng/quickjs/pull/195
2023-12-20 09:02:28 +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
Saúl Ibarra Corretgé
8934101a67 Drop non-standard Error properties 2023-12-16 01:09:49 +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é
7474b28036 Remove unused member from JSContext 2023-12-16 00:44:31 +01:00
Ben Noordhuis
35e6bfceb1
Partially port bellard/quickjs@58f374ef42 (#214)
This commit merges JS_SetPropertyGeneric into JS_SetPropertyInternal2
and obsoletes commit b51b510 and partially obsoletes commit 8baafc4;
detachment and negative zero handling now fall out naturally.
2023-12-15 00:03:18 +01:00
Ben Noordhuis
ba8b80f112
Remove broken JS_READ_OBJ_ROM_DATA flag (#216)
This JS_ReadObject() flag no longer works for bytecode. The IC opcodes
are patched during execution.

Fixes: https://github.com/quickjs-ng/quickjs/issues/206
Refs: https://github.com/quickjs-ng/quickjs/pull/120
2023-12-14 15:25:29 +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
Saúl Ibarra Corretgé
39901e2b86 Fix async generator in case of exception in implicit await in the 'return' statement
Ref: 57105c7f23
2023-12-14 11:49:14 +01:00
Fabrice Bellard
864a66459b Raise an error if a private method is added twice to an object 2023-12-14 11:49:14 +01:00
Ben Noordhuis
b51b5100b0
Handle negative zero typed array indices correctly (#212)
`ta["-0"] = 42` is a thing and not just any thing but a decidedly weird
thing: it completes successful, sets no property, but still evaluates
the value for side effects.
2023-12-14 11:12:55 +01:00
Ben Noordhuis
5168db1965
Handle TypedArray detach during iteration (#209)
Per spec: detaching the TA mid-iteration is allowed.

TypedArray.prototype.sort should not throw an exception when that
happens and now no longer does.
2023-12-13 08:55:01 +01:00
Ben Noordhuis
8baafc46bd
Don't throw OOB exception for detached typed array (#208)
`a[42] = 1` where a is a detached typed array should not throw but
`Object.defineProperty()` still should. Add a check and a flag that
distinguishes between the two cases.
2023-12-12 23:14:33 +01:00
Ben Noordhuis
b478329cdd
Remove JSFunctionBytecode.has_debug flag (#207)
And merge the debug struct into JSFunctionBytecode because it is now
always present.

Refs: https://github.com/quickjs-ng/quickjs/pull/193#pullrequestreview-1774511177
2023-12-12 00:10:52 +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
dbed7be3cb
Handle TypedArray detach during iteration (#201)
Per spec: detaching the TA mid-iteration is allowed and should not
not throw an exception.

In the case of TypedArray.prototype.set, because iteration over the
source array is observable, we cannot bail out early when the TA is
first detached.
2023-12-11 09:22:02 +01:00
Ben Noordhuis
315096461b
Implement TypedArray.prototype.with (#200) 2023-12-10 21:25:31 +01:00
Ben Noordhuis
83dfc635f1
Implement TypedArray.prototype.toSorted (#199) 2023-12-10 21:23:52 +01:00
Ben Noordhuis
05f00a87f7
Implement TypedArray.prototype.toReversed (#198) 2023-12-10 21:21:21 +01:00
Fabrice Bellard
baf50f9236 fixed duplicate static private setter/getter test 2023-12-10 21:03:48 +01:00
Fabrice Bellard
e8b97048d4 Symbol.species is no longer used in TypedArray constructor from a TypedArray 2023-12-10 21:03:48 +01:00
Fabrice Bellard
46996ff258 fixed delete super.x error 2023-12-10 21:03:48 +01:00
Fabrice Bellard
30e4767e67 fixed lexical scope of 'this' with eval (github issue #192) 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
Ben Noordhuis
4c1b9f8c7f
Fix OP_FMT_none_loc bytecode dumping (#192)
Commit f404980 ("Add fused get_loc0_loc1 opcode") introduced an
off-by-one (sometimes negative) array index bug because OP_get_loc1_loc1
replaced OP_get_loc0 as the first OP_FMT_none_loc opcode.
2023-12-09 23:46:54 +01:00
Ben Noordhuis
f6ed206bd5
Change regexp flags field from uint8 to uint16 (#185)
I need the extra bits to store the 'v' flag as described in
https://github.com/tc39/proposal-regexp-v-flag
2023-12-09 16:47:05 +01:00
Ben Noordhuis
6bd3d5660e
Use JS_ToBigIntFree() instead of JS_ToBigInt() (#190)
Reduces reference count juggling in the happy path and, to a lesser
extent, in error paths.
2023-12-09 14:54:54 +01:00
Ben Noordhuis
4fc814311a
Remove js_new_bf() (#189)
And replace the open-coded JS_NewBigInt() logic at its one call site
with the real thing.
2023-12-09 14:54:33 +01:00
Ben Noordhuis
d17129035d
Remove duplicate JS_GetBigInt call (#188) 2023-12-09 14:54:13 +01:00
Ben Noordhuis
f7d2169999
Rename LRE_FLAG_UTF16 to LRE_FLAG_UNICODE (#186)
Prep work for https://github.com/tc39/proposal-regexp-v-flag a.k.a.
UnicodeSets.
2023-12-08 10:58:00 +01:00
Saúl Ibarra Corretgé
9539e3cc65 Fix private field setters
Ref: 55a4878a60
2023-12-07 22:21:48 +01:00
Saúl Ibarra Corretgé
99f12f4f2f Implement FinalizationRegistry 2023-12-05 22:41:57 +01:00
Saúl Ibarra Corretgé
18b30961ee DRY malloc_usable_size 2023-12-05 12:34:02 +01:00
Saúl Ibarra Corretgé
8128e66145 Add FreeBSD support 2023-12-05 12:34:02 +01:00
Saúl Ibarra Corretgé
478bcf74f2 Add OpenBSD support 2023-12-05 12:34:02 +01:00
Felipe Gasper
56738d8b4b
Add Cygwin compatibility 2023-12-03 18:15:07 +01:00
Ben Noordhuis
9c2614c2fa
Fix building with tcc (#170)
- tcc doesn't support 128 bits integral types, use 64 bits math instead

- suppress some unreachable code warnings
2023-12-03 13:36:51 +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é
bf1facaea3 Always enable stack checking
Seems to work in Emscripten and it can be disabled with
JS_SetMaxStackSize(rt, 0).
2023-12-01 15:29:18 +01:00
Ben Noordhuis
a5b9e54151
Re-enable stack depth checks under ASan (#161)
The default 256 kb stack is too small to run some of the test262 tests
when ASAN is enabled.

Double it to 512 kb and ensure threads created by quickjs have big
enough stacks.
2023-11-30 22:18:23 +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
3f10651dc1
Prohibit freezing/sealing module namespace objects (#160) 2023-11-30 00:42:36 +01:00
Ben Noordhuis
a140e1cf2c
Make -DDUMP_BYTECODE=64 print executed bytecode (#158)
Basically a poor man's bytecode tracer.
2023-11-29 23:53:01 +01:00
Ben Noordhuis
39c8acd312 Replace JS_DupValue() calls with js_dup() 2023-11-29 23:38:01 +01:00
Ben Noordhuis
f1d6e9e5ff Replace JS_NewBool() calls with js_bool() 2023-11-29 23:38:01 +01:00
Ben Noordhuis
a28d1b822c Replace JS_NewUint32() calls with js_uint32() 2023-11-29 23:38:01 +01:00
Ben Noordhuis
087441559e Replace JS_NewFloat64() calls with js_float64() 2023-11-29 23:38:01 +01:00
Ben Noordhuis
46155806fc Replace JS_NewInt32() calls with js_int32() 2023-11-29 23:38:01 +01:00
Ben Noordhuis
f181b3e54f
Remove dead code (#155) 2023-11-29 10:13:13 +01:00
Marcin Kolny
699744562e Enable support for GCC compler v < 4.9
GCCv4.8 and lower doesn't ship with stdatomic implementation
(even though they don't define __STD_NO_ATOMICS__ for c11).
If the code is compiled with GCCv4.8 and older, we use builtin
GCC atomic operations instead.

The patch was initially proposed in quickjs's mailing group:
https://www.freelists.org/post/quickjs-devel/PATCH-support-for-older-gcc-versions-whitespace-changes-excluded
2023-11-29 09:22:27 +01:00
Divy Srivastava
6b78c7f3e1
Implement polymorphic inline caches (#120) 2023-11-29 09:12:02 +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
a6e73ca73c
Handle serialization endianness transparently (#152)
Change JS_WriteObject() and JS_WriteObject2() to write little-endian
data and update JS_ReadObject() to byte-swap data when running on a
big-endian system.

Obsoletes the JS_WRITE_OBJ_BSWAP flag, it is now a no-op.

Fixes: https://github.com/quickjs-ng/quickjs/issues/125
2023-11-28 22:49:01 +01:00
Saúl Ibarra Corretgé
5c136edbcf Implement WeakRef 2023-11-28 13:26:33 +01:00
Saúl Ibarra Corretgé
1df9615638 Generalize weakref handling logic 2023-11-28 13:26:33 +01:00
Ben Noordhuis
b5148b212e
Implement static class initializer blocks (#144)
Spec compliance bug: "await" is illegal inside initializer blocks
_except_ when used as an identifier in a function expression, like so:

    class C {
        static {
            var f = function await() {}
        }
    }

It is somewhat complicated to make the parser understand the distinction
and such code is probably rare or non-existent so I decided to leave
well enough alone for now.
2023-11-26 21:11:48 +01:00