Commit graph

332 commits

Author SHA1 Message Date
Saúl Ibarra Corretgé
4933e17d1b Remove error handling for JS_NewInt64, it's infallible
Also use js_int64(v) internally.
2024-11-12 22:33:22 +01:00
Saúl Ibarra Corretgé
c68c416e2e
Implement Iterator.prototype.filter 2024-11-12 22:01:45 +01:00
Ben Noordhuis
000061f635
Improve performance of variable resolver (#672)
Switch to a hash table when the number of variables grows beyond
a threshold.

Speeds up the test case from the linked issue by about 70%.

Fixes: https://github.com/quickjs-ng/quickjs/issues/456
2024-11-12 21:22:47 +01:00
Saúl Ibarra Corretgé
0362c0a4eb
Implement Iterator.prototype.take (#676) 2024-11-12 08:10:19 +01:00
Ben Noordhuis
487129055f
Work around broken atomics in tinycc (#669) 2024-11-12 01:01:52 +01:00
Saúl Ibarra Corretgé
c41ee4f15f Implement Iterator.prototype.drop
Includes the scaffold for other iterator helper methods that require an
Iterator Helper object.
2024-11-11 22:25:24 +01:00
Ben Noordhuis
f641d4ff18 Don't use rt->user_opaque in quickjs-libc.c
JS_SetRuntimeOpaque() and JS_GetRuntimeOpaque() are intended for
embedders. Stop using them in quickjs-libc.c
2024-11-09 23:14:20 +01:00
Saúl Ibarra Corretgé
d7c5c7c302 Remove qjs namespace backwards compatibility
Fixes: https://github.com/quickjs-ng/quickjs/issues/650
2024-11-08 22:10:53 +01:00
Richard Davison
04307af779 Make "performance" global writable 2024-11-08 08:52:45 +01:00
Ben Noordhuis
73cc00e57e
Improve line:column tracking (#660)
Emit source locations manually for more precise tracking. Don't infer
them from emitted bytecode opcodes because that leads to inaccurate
and sometimes surprising results.

Speeds up code generation (although infinitesimally) as a bonus.

Fixes: https://github.com/quickjs-ng/quickjs/issues/236
2024-11-07 22:03:00 +01:00
Saúl Ibarra Corretgé
c8be383367 Prevent JS_SetOpaque from overriding internal class state
Fixes: https://github.com/quickjs-ng/quickjs/issues/657
2024-11-07 21:26:28 +01:00
Saúl Ibarra Corretgé
e30da0e8bc Don't throw oob exception when setting numeric indexes on TAs
Relevant spec section: https://tc39.es/ecma262/multipage/ordinary-and-exotic-objects-behaviours.html#sec-typedarraysetelement

It should only throw if Object.defineProperty is used and the TA is
detached or OOB if a RAB is used.

Fixes: https://github.com/quickjs-ng/quickjs/issues/645
2024-11-07 19:48:05 +01:00
Ben Noordhuis
9c5c441744
Fix FinalizationRegistry refcounting bug (#656)
Introduced in commit 61c8fe6 from last month that moved the callback
into the job queue:

1. It leaked `fre->held_val` when no job was enqueued

2. It fumbled the reference count when enqueuing; JS_EnqueueJob already
   takes care of incrementing and decrementing it

Reverts commit 0a70623 from earlier today because that didn't turn out
to be a complete fix.

Fixes: https://github.com/quickjs-ng/quickjs/issues/648
2024-11-07 09:12:34 +01:00
Ben Noordhuis
aedd829e61
Fix DUMP_LEAKS memory leak false positive (#655)
Run DUMP_LEAKS after finalizers run; they call js_free_rt too.

Fixes: https://github.com/quickjs-ng/quickjs/issues/654
2024-11-06 22:56:10 +01:00
Ben Noordhuis
83fe8f1166
Fix exception in WeakRef.prototype.deref (#653)
Set the object's opaque to a sentinel value instead of NULL, to stop
JS_GetOpaque2 from raising an "illegal class" exception.

Fixes: https://github.com/quickjs-ng/quickjs/issues/651
2024-11-06 22:54:47 +01:00
Ben Noordhuis
0a706236d7
Move "no more objects" assert to right place (#649)
No test because I can only get it to trigger with qjs, not run-test262,
but the problem is that we need to run FinalizationRegistry finalizers
before asserting no objects remain.

Fixes: https://github.com/quickjs-ng/quickjs/issues/648
2024-11-06 14:54:52 +01:00
Ben Noordhuis
37fe427d59
Add resizable ArrayBuffers (#646)
This commit implements resizable ArrayBuffers - RABs for short - and
extends typed arrays (TAs) to support fixed-length and length-tracking
modes.

SharedArrayBuffers (SABs) also support the maxByteLength option now but
I cheated and allocate all memory upfront because atomically resizing
memory allocations is hard and this commit is already big and complex.

The lion's share is updating all the TA prototype methods to deal with
RABs resizing underneath them. Method arguments can be arbitrary objects
with arbitrary .valueOf methods and arbitrary side effects, like...
resizing the RAB we're currently operating on.

Fixes: https://github.com/quickjs-ng/quickjs/issues/477
2024-11-05 21:55:42 +01:00
Ben Noordhuis
dfe5675f25
Allow 'undefined' in let or const declaration (#639)
Except at the global scope of a classic script because... who knows,
that's just how it is.

Fixes: https://github.com/quickjs-ng/quickjs/issues/633
2024-10-29 22:55:22 +01:00
Ben Noordhuis
42262a7c53
Don't segfault on missing line number data (#641)
Can be reproduced on the CLI but not from a script:

    # should throw "unsupported object class" TypeError
    $ qjs --std -e 'bjson.write(function(){})'
    /path/to/quickjs.c:6510:15: runtime error: applying zero offset to null pointer
2024-10-29 22:52:56 +01:00
Saúl Ibarra Corretgé
82607f4deb Implement Iterator.prototype.some 2024-10-25 22:37:05 +02:00
Saúl Ibarra Corretgé
de58faaee0 Implement Iterator.prototype.reduce 2024-10-25 21:23:44 +02:00
Saúl Ibarra Corretgé
f78d1e6b94 Implement Iterator.prototype.find 2024-10-25 21:23:26 +02:00
Saúl Ibarra Corretgé
cc11a829e8
Prefix stdlib modules with "qjs:"
Fixes: https://github.com/quickjs-ng/quickjs/issues/616
2024-10-24 22:24:03 +02:00
Saúl Ibarra Corretgé
3339ef7137 Implement Iterator.prototype.forEach 2024-10-24 19:34:58 +02:00
Saúl Ibarra Corretgé
b9a22f9bdd Implement Iterator.prototype.every 2024-10-24 18:07:51 +02:00
Ben Noordhuis
caa1bf544d Handle bytecode without IC state
Deserialized bytecode does not have IC state, i.e., `bc->ic == NULL`.
That may or may not be bug (IMO, it is and we should rebuild the
IC state during deserialization) but, either way, don't segfault.

DRY add_ic_slot() and its call sites in a hopefully NFC manner.
2024-10-24 09:11:34 +02:00
Ben Noordhuis
0a79b84ef9 Improve deserializer error message for bytecode
Don't raise a "invalid tag 12" exception when encountering bytecode
and JS_READ_OBJ_BYTECODE is not set, because no one knows what "tag 12"
means without looking it up, not even quickjs maintainers.
2024-10-24 09:11:34 +02:00
Ben Noordhuis
4fbce79521 Fix UndefinedBehaviorSanitizer error
UBSan is right to complain that `s->ptr_last == NULL` when tracing is
disabled.
2024-10-24 09:11:34 +02:00
Adam Satko
62f4713780
Fix stdc atomics detection and add vs2019 msvc job 2024-10-22 20:02:15 +02:00
Ben Noordhuis
763076d109
Rework inline cache handling (#609)
Don't store the update flag in the IC because that's a) an out-of-band
signalling mechanism, and b) makes JSInlineCache bigger than it needs
to be. One is allocated per function so it adds up.

Another reason for making this change is that it makes visible what
I strongly suspect are bugs in the original implementation.
2024-10-20 13:02:09 +02:00
Ben Noordhuis
8cd59bf7c4
Improve JS_DetectModule (#610)
It's still not infallible (I don't think it can ever be, the whole
premise is wrong) but hopefully it's a little less fallible now.

Fixes: https://github.com/quickjs-ng/quickjs/issues/606
2024-10-20 12:42:21 +02:00
Ben Noordhuis
966dbfc1f9 Fix crash in deserializer on bad regexp 2024-10-18 10:01:34 +02:00
Ben Noordhuis
7be9d99d15 Restrict atom count in deserializer to 1 million
Otherwise it's too easy to tie up too many resources (cpu, memory) by
crafting inputs with a very large atom count (up to 4 billion.)

This may need some finetuning. If the limit proves too restrictive for
very large snapshots, we can make it relative to the size of the input.
2024-10-18 10:01:34 +02:00
Ben Noordhuis
a1d1bce0b7
Fix crash in deserializer (#602)
Check inside the deserializer that const atoms are indeed const, don't
trust the input. The serializer only writes type 0 records for const
atoms but the byte stream may have been corrupted or manipulated.

Overlooked during review of c25aad7 ("Add ability to (de)serialize
symbols")

Found with libfuzzer and it found it _really_ fast. Great tool.
2024-10-17 08:45:04 +02:00
Richard Davison
afeeebf89e
Expose ctx->function_proto 2024-10-16 20:39:09 +02:00
Ben Noordhuis
36227a5310
Fix cyclic import/export segfault (#568)
Before this commit it segfaulted, now it throws a SyntaxError.
That's still not correct behavior but better than segfaulting.
To be continued.

Includes a small run-test262 fix to handle Windows line endings.

Refs: https://github.com/quickjs-ng/quickjs/issues/567
2024-10-16 10:13:38 +02:00
Saúl Ibarra Corretgé
d9d6939b20 Implement Promise.try 2024-10-15 14:52:40 +02:00
Ben Noordhuis
e145244999
Replace js_mode with is_strict_mode bit field (#590)
Shrinks some structures by one or more bytes and is easier to read.
2024-10-10 19:31:47 +02:00
satk0
2344d19220 Fix MSVC compilation when atomics experimental feature is not set 2024-10-07 22:34:01 +02:00
satk0
ba863b1a82 Follow declaration of variables before for loop 2024-10-07 22:34:01 +02:00
satk0
86b1853a21 Improve gcc warning fix 2024-10-07 22:34:01 +02:00
Saúl Ibarra Corretgé
52e0f24048 Fix computed reference on null or undefined 2024-10-07 21:39:59 +02:00
Saúl Ibarra Corretgé
acc0dd9273 Implement proper Symbol.toStringTag for iterators 2024-10-07 21:39:02 +02:00
Ben Noordhuis
9a37c57779
Fix thread-safety issue in quickjs-libc (#578)
`JS_NewClassID(rt, &class_id)` where `class_id` is a global variable
is unsafe when called from multiple threads but that is exactly what
quickjs-libc.c did.

Add a new JS_AddRuntimeFinalizer function that lets quickjs-libc
store the class ids in JSRuntimeState and defer freeing the memory
until the runtime is destroyed. Necessary because object finalizers
such as js_std_file_finalizer need to know the class id and run after
js_std_free_handlers runs.

Fixes: https://github.com/quickjs-ng/quickjs/issues/577
2024-10-07 21:27:38 +02:00
Saúl Ibarra Corretgé
a0877514b9 fixup! 2024-10-05 12:35:47 +02:00
Saúl Ibarra Corretgé
3dcadf1518 Fix next token parsing after a function definition
Ref: c06c399f4f
Fixes: https://github.com/quickjs-ng/quickjs/issues/572
2024-10-05 12:35:47 +02:00
Richard Davison
5590756b93
Move JS_{Dup,Free}Value and the RT variants from header, reduced duplication 2024-10-04 09:18:12 +02:00
Ben Noordhuis
1eb9608d64
Fix regexp split with zero-length capture group (#566)
The expected result of `"ab".split(/(c)*/)[1]` is `undefined` but
was in fact `"undefined"` due to unintentional stringification.

Fixes: https://github.com/quickjs-ng/quickjs/issues/565
2024-10-01 01:05:01 +02:00
Ben Noordhuis
348d60985d
Add Iterator.from (#555) 2024-09-27 00:16:50 +02:00
Ben Noordhuis
9a618ed426
Add Iterator.prototype.toArray (#552)
Churn in test262_errors.txt is due to fixing Iterator inheritance,
necessary to make the prototype method actually callable.
2024-09-25 22:45:02 +02:00