2020-07-03 18:36:58 -03:00
/*
2021-04-22 20:53:07 -03:00
* Copyright ( c ) 2020 , Matthew Olsson < mattco @ serenityos . org >
2023-04-13 10:26:41 -03:00
* Copyright ( c ) 2020 - 2023 , Linus Groh < linusg @ serenityos . org >
2020-07-03 18:36:58 -03:00
*
2021-04-22 05:24:48 -03:00
* SPDX - License - Identifier : BSD - 2 - Clause
2020-07-03 18:36:58 -03:00
*/
2024-07-15 13:28:23 -03:00
# include <AK/Enumerate.h>
2026-03-17 09:57:23 -03:00
# include <AK/StringView.h>
Libraries: Clean up UTF-16 source text paths
Store parser errors, source range filenames, source code filenames,
module source, and Rust parser errors as UTF-16 where they flow back
into JavaScript-visible strings. Keep byte-oriented source buffers
byte-backed.
Remove temporary PrimitiveString, ByteString, and UTF-8 detours from
JSON, RegExp, module debug logging, print formatting, and tests.
2026-06-21 14:03:19 -03:00
# include <AK/Utf16String.h>
2026-05-16 08:01:03 -03:00
# include <LibCore/TimeZone.h>
2022-04-07 14:06:40 -03:00
# include <LibJS/Runtime/ArrayBuffer.h>
2024-09-03 11:18:43 -03:00
# include <LibJS/Runtime/Date.h>
2026-05-14 18:14:31 -03:00
# include <LibJS/Runtime/FinalizationRegistry.h>
2024-07-15 13:28:23 -03:00
# include <LibJS/Runtime/TypedArray.h>
2025-09-13 15:50:09 -03:00
# include <LibJS/Runtime/ValueInlines.h>
2021-05-18 11:15:12 -03:00
# include <LibTest/JavaScriptTestRunner.h>
2020-07-03 18:36:58 -03:00
2026-01-22 07:58:59 -03:00
TEST_ROOT ( " Tests/LibJS/Runtime " ) ;
2020-07-03 18:36:58 -03:00
2021-05-30 03:05:44 -03:00
TESTJS_PROGRAM_FLAG ( test262_parser_tests , " Run test262 parser tests " , " test262-parser-tests " , 0 ) ;
2026-03-19 14:29:15 -03:00
TESTJS_GLOBAL_FUNCTION ( can_parse_source , canParseSource )
2026-03-17 09:57:23 -03:00
{
auto & realm = * vm . current_realm ( ) ;
2026-06-21 14:03:06 -03:00
auto source = TRY ( vm . argument ( 0 ) . to_utf16_string ( vm ) ) ;
auto script = JS : : Script : : parse ( source . utf16_view ( ) , realm ) ;
2026-03-19 14:29:15 -03:00
return JS : : Value ( ! script . is_error ( ) ) ;
2020-11-11 19:12:51 -03:00
}
2026-04-24 12:41:04 -03:00
TESTJS_GLOBAL_FUNCTION ( collect_garbage , gc )
{
vm . heap ( ) . collect_garbage ( ) ;
return JS : : js_undefined ( ) ;
}
2025-04-28 18:02:01 -03:00
// Based on $262.evalScript
TESTJS_GLOBAL_FUNCTION ( evaluate_source , evaluateSource )
{
auto & realm = * vm . current_realm ( ) ;
2026-06-21 14:03:06 -03:00
auto source = TRY ( vm . argument ( 0 ) . to_utf16_string ( vm ) ) ;
2025-04-28 18:02:01 -03:00
2026-06-21 14:03:06 -03:00
auto script = JS : : Script : : parse ( source . utf16_view ( ) , realm ) ;
2025-04-28 18:02:01 -03:00
if ( script . is_error ( ) )
2026-06-21 14:03:10 -03:00
return vm . throw_completion < JS : : SyntaxError > ( script . error ( ) . first ( ) . to_utf16_string ( ) ) ;
2025-04-28 18:02:01 -03:00
2026-04-13 06:54:04 -03:00
return vm . run ( script . value ( ) ) ;
2025-04-28 18:02:01 -03:00
}
2026-05-21 10:25:45 -03:00
TESTJS_GLOBAL_FUNCTION ( evaluate_module , evaluateModule )
{
auto & realm = * vm . current_realm ( ) ;
2026-06-21 14:03:01 -03:00
auto path = TRY ( vm . argument ( 0 ) . to_utf16_string ( vm ) ) . to_utf8_but_should_be_ported_to_utf16 ( ) ;
2026-05-21 10:25:45 -03:00
auto module = Test : : JS : : parse_module ( path . to_byte_string ( ) , realm ) ;
if ( module . is_error ( ) )
2026-06-21 14:03:10 -03:00
return vm . throw_completion < JS : : SyntaxError > ( module . error ( ) . error . to_utf16_string ( ) ) ;
2026-05-21 10:25:45 -03:00
return vm . run ( module . value ( ) ) ;
}
2021-05-18 11:15:12 -03:00
TESTJS_GLOBAL_FUNCTION ( run_queued_promise_jobs , runQueuedPromiseJobs )
LibJS: Add initial support for Promises
Almost a year after first working on this, it's finally done: an
implementation of Promises for LibJS! :^)
The core functionality is working and closely following the spec [1].
I mostly took the pseudo code and transformed it into C++ - if you read
and understand it, you will know how the spec implements Promises; and
if you read the spec first, the code will look very familiar.
Implemented functions are:
- Promise() constructor
- Promise.prototype.then()
- Promise.prototype.catch()
- Promise.prototype.finally()
- Promise.resolve()
- Promise.reject()
For the tests I added a new function to test-js's global object,
runQueuedPromiseJobs(), which calls vm.run_queued_promise_jobs().
By design, queued jobs normally only run after the script was fully
executed, making it improssible to test handlers in individual test()
calls by default [2].
Subsequent commits include integrations into LibWeb and js(1) -
pretty-printing, running queued promise jobs when necessary.
This has an unusual amount of dbgln() statements, all hidden behind the
PROMISE_DEBUG flag - I'm leaving them in for now as they've been very
useful while debugging this, things can get quite complex with so many
asynchronously executed functions.
I've not extensively explored use of these APIs for promise-based
functionality in LibWeb (fetch(), Notification.requestPermission()
etc.), but we'll get there in due time.
[1]: https://tc39.es/ecma262/#sec-promise-objects
[2]: https://tc39.es/ecma262/#sec-jobs-and-job-queues
2021-04-01 17:13:29 -03:00
{
vm . run_queued_promise_jobs ( ) ;
return JS : : js_undefined ( ) ;
}
2021-05-30 03:05:44 -03:00
2026-05-15 03:42:59 -03:00
TESTJS_GLOBAL_FUNCTION ( clear_kept_objects , clearKeptObjects )
{
vm . finish_execution_generation ( ) ;
return JS : : js_undefined ( ) ;
}
2026-05-14 11:20:34 -03:00
TESTJS_GLOBAL_FUNCTION ( run_queued_finalization_registry_cleanup_jobs , runQueuedFinalizationRegistryCleanupJobs )
{
vm . run_queued_finalization_registry_cleanup_jobs ( ) ;
return JS : : js_undefined ( ) ;
}
2021-06-09 14:10:47 -03:00
TESTJS_GLOBAL_FUNCTION ( get_weak_set_size , getWeakSetSize )
{
2023-04-13 10:26:41 -03:00
auto object = TRY ( vm . argument ( 0 ) . to_object ( vm ) ) ;
if ( ! is < JS : : WeakSet > ( * object ) )
2022-08-16 16:33:17 -03:00
return vm . throw_completion < JS : : TypeError > ( JS : : ErrorType : : NotAnObjectOfType , " WeakSet " ) ;
2023-04-13 10:26:41 -03:00
auto & weak_set = static_cast < JS : : WeakSet & > ( * object ) ;
2026-05-06 06:33:55 -03:00
return JS : : Value ( weak_set . weak_set_size ( ) ) ;
2021-06-09 14:10:47 -03:00
}
2021-06-11 23:37:09 -03:00
TESTJS_GLOBAL_FUNCTION ( get_weak_map_size , getWeakMapSize )
{
2023-04-13 10:26:41 -03:00
auto object = TRY ( vm . argument ( 0 ) . to_object ( vm ) ) ;
if ( ! is < JS : : WeakMap > ( * object ) )
2022-08-16 16:33:17 -03:00
return vm . throw_completion < JS : : TypeError > ( JS : : ErrorType : : NotAnObjectOfType , " WeakMap " ) ;
2023-04-13 10:26:41 -03:00
auto & weak_map = static_cast < JS : : WeakMap & > ( * object ) ;
2026-05-06 06:33:55 -03:00
return JS : : Value ( weak_map . weak_map_size ( ) ) ;
2021-06-11 23:37:09 -03:00
}
2023-07-22 01:53:22 -03:00
TESTJS_GLOBAL_FUNCTION ( mark_as_garbage , markAsGarbage )
{
auto argument = vm . argument ( 0 ) ;
if ( ! argument . is_string ( ) )
2025-12-05 04:01:44 -03:00
return vm . throw_completion < JS : : TypeError > ( JS : : ErrorType : : NotAString , argument ) ;
2023-07-22 01:53:22 -03:00
auto & variable_name = argument . as_string ( ) ;
// In native functions we don't have a lexical environment so get the outer via the execution stack.
2026-04-13 07:49:41 -03:00
auto outer_environment = vm . last_execution_context_matching ( [ & ] ( auto * execution_context ) {
2023-07-22 01:53:22 -03:00
return execution_context - > lexical_environment ! = nullptr ;
} ) ;
if ( ! outer_environment . has_value ( ) )
2026-06-21 14:02:35 -03:00
return vm . throw_completion < JS : : ReferenceError > ( JS : : ErrorType : : UnknownIdentifier , variable_name . utf16_string_view ( ) ) ;
2023-07-22 01:53:22 -03:00
2025-10-28 16:25:12 -03:00
auto reference = TRY ( vm . resolve_binding ( variable_name . utf16_string ( ) , JS : : Strict : : No , outer_environment . value ( ) - > lexical_environment ) ) ;
2023-07-22 01:53:22 -03:00
auto value = TRY ( reference . get_value ( vm ) ) ;
if ( ! can_be_held_weakly ( value ) )
2026-06-21 14:02:35 -03:00
return vm . throw_completion < JS : : TypeError > ( JS : : ErrorType : : CannotBeHeldWeakly , ByteString : : formatted ( " Variable with name {} " , variable_name . utf16_string_view ( ) ) ) ;
2023-07-22 01:53:22 -03:00
2026-05-15 03:42:59 -03:00
TRY ( reference . put_value ( vm , JS : : js_undefined ( ) ) ) ;
( void ) TRY ( reference . delete_ ( vm ) ) ;
2023-07-22 01:53:22 -03:00
vm . heap ( ) . uproot_cell ( & value . as_cell ( ) ) ;
return JS : : js_undefined ( ) ;
}
2026-05-14 18:14:31 -03:00
TESTJS_GLOBAL_FUNCTION ( cleanup_finalization_registry , cleanupFinalizationRegistry )
{
auto finalization_registry = vm . argument ( 0 ) . as_if < JS : : FinalizationRegistry > ( ) ;
if ( ! finalization_registry )
return vm . throw_completion < JS : : TypeError > ( JS : : ErrorType : : NotAnObjectOfType , " FinalizationRegistry " ) ;
auto callback = vm . argument ( 1 ) ;
if ( vm . argument_count ( ) > 1 & & ! callback . is_function ( ) )
return vm . throw_completion < JS : : TypeError > ( JS : : ErrorType : : NotAFunction , callback ) ;
GC : : Ptr < JS : : JobCallback > cleanup_callback ;
if ( ! callback . is_undefined ( ) )
cleanup_callback = vm . host_make_job_callback ( callback . as_function ( ) ) ;
TRY ( finalization_registry - > cleanup ( cleanup_callback ) ) ;
return JS : : js_undefined ( ) ;
}
2022-04-07 14:06:40 -03:00
TESTJS_GLOBAL_FUNCTION ( detach_array_buffer , detachArrayBuffer )
{
2026-02-28 08:52:15 -03:00
auto array_buffer = vm . argument ( 0 ) . as_if < JS : : ArrayBuffer > ( ) ;
if ( ! array_buffer )
2022-08-16 16:33:17 -03:00
return vm . throw_completion < JS : : TypeError > ( JS : : ErrorType : : NotAnObjectOfType , " ArrayBuffer " ) ;
2022-04-07 14:06:40 -03:00
2026-02-28 08:52:15 -03:00
TRY ( JS : : detach_array_buffer ( vm , * array_buffer , vm . argument ( 1 ) ) ) ;
2022-05-02 15:54:39 -03:00
return JS : : js_null ( ) ;
2022-04-07 14:06:40 -03:00
}
2023-11-07 13:14:55 -03:00
TESTJS_GLOBAL_FUNCTION ( set_time_zone , setTimeZone )
{
2026-06-21 14:03:10 -03:00
auto current_time_zone = Core : : TimeZone : : current_time_zone ( ) ;
auto current_time_zone_string = JS : : PrimitiveString : : create ( vm , Utf16String : : from_ascii_without_validation ( current_time_zone . bytes_as_string_view ( ) . bytes ( ) ) ) ;
2026-06-21 14:03:01 -03:00
auto time_zone = TRY ( vm . argument ( 0 ) . to_utf16_string ( vm ) ) . to_utf8_but_should_be_ported_to_utf16 ( ) ;
2023-11-07 13:14:55 -03:00
2026-05-16 08:01:03 -03:00
if ( auto result = Core : : TimeZone : : set_current_time_zone ( time_zone ) ; result . is_error ( ) )
Libraries: Clean up UTF-16 source text paths
Store parser errors, source range filenames, source code filenames,
module source, and Rust parser errors as UTF-16 where they flow back
into JavaScript-visible strings. Keep byte-oriented source buffers
byte-backed.
Remove temporary PrimitiveString, ByteString, and UTF-8 detours from
JSON, RegExp, module debug logging, print formatting, and tests.
2026-06-21 14:03:19 -03:00
return vm . throw_completion < JS : : InternalError > ( Utf16String : : formatted ( " Could not set time zone: {} " , result . error ( ) ) ) ;
2023-11-07 13:14:55 -03:00
2024-09-03 11:18:43 -03:00
JS : : clear_system_time_zone_cache ( ) ;
2026-06-21 14:03:10 -03:00
return current_time_zone_string ;
2023-11-07 13:14:55 -03:00
}
2024-07-15 13:28:23 -03:00
TESTJS_GLOBAL_FUNCTION ( to_utf8_bytes , toUTF8Bytes )
{
auto & realm = * vm . current_realm ( ) ;
2026-06-21 14:03:01 -03:00
auto string = TRY ( vm . argument ( 0 ) . to_utf16_string ( vm ) ) . to_utf8_but_should_be_ported_to_utf16 ( ) ;
2024-07-15 13:28:23 -03:00
auto typed_array = TRY ( JS : : Uint8Array : : create ( realm , string . bytes ( ) . size ( ) ) ) ;
for ( auto [ i , byte ] : enumerate ( string . bytes ( ) ) )
typed_array - > set_value_in_buffer ( i , JS : : Value { byte } , JS : : ArrayBuffer : : Order : : SeqCst ) ;
return typed_array ;
}
2026-04-30 09:31:10 -03:00
TESTJS_GLOBAL_FUNCTION ( create_default_typed_array , createDefaultTypedArray )
{
auto & realm = * vm . current_realm ( ) ;
auto * typed_array = TRY ( JS : : typed_array_from ( vm , vm . argument ( 0 ) ) ) ;
auto length = TRY ( vm . argument ( 1 ) . to_index ( vm ) ) ;
if ( length > NumericLimits < u32 > : : max ( ) )
return vm . throw_completion < JS : : RangeError > ( JS : : ErrorType : : InvalidLength , " typed array " ) ;
return TRY ( typed_array - > create_default ( realm , length ) ) ;
}
2023-12-16 11:19:34 -03:00
TESTJS_RUN_FILE_FUNCTION ( ByteString const & test_file , JS : : Realm & realm , JS : : ExecutionContext & )
2021-05-30 03:05:44 -03:00
{
if ( ! test262_parser_tests )
return Test : : JS : : RunFileHookResult : : RunAsNormal ;
2021-06-27 15:57:35 -03:00
auto start_time = Test : : get_time_in_ms ( ) ;
2021-05-30 03:05:44 -03:00
LexicalPath path ( test_file ) ;
2021-11-10 20:55:02 -03:00
auto dirname = path . dirname ( ) ;
2021-05-30 03:05:44 -03:00
enum {
Early ,
Fail ,
Pass ,
ExplicitPass ,
} expectation { Pass } ;
2022-07-11 14:32:29 -03:00
if ( dirname . ends_with ( " early " sv ) )
2021-05-30 03:05:44 -03:00
expectation = Early ;
2022-07-11 14:32:29 -03:00
else if ( dirname . ends_with ( " fail " sv ) )
2021-05-30 03:05:44 -03:00
expectation = Fail ;
2022-07-11 14:32:29 -03:00
else if ( dirname . ends_with ( " pass-explicit " sv ) )
2021-05-30 03:05:44 -03:00
expectation = ExplicitPass ;
2022-07-11 14:32:29 -03:00
else if ( dirname . ends_with ( " pass " sv ) )
2021-05-30 03:05:44 -03:00
expectation = Pass ;
else
return Test : : JS : : RunFileHookResult : : SkipFile ;
2026-03-19 14:41:49 -03:00
bool const is_module = path . basename ( ) . ends_with ( " .module.js " sv ) ;
2021-09-14 15:58:33 -03:00
bool parse_succeeded = false ;
2026-03-19 14:41:49 -03:00
if ( is_module )
2023-08-07 11:36:23 -03:00
parse_succeeded = ! Test : : JS : : parse_module ( test_file , realm ) . is_error ( ) ;
2021-09-14 15:58:33 -03:00
else
2023-08-07 11:36:23 -03:00
parse_succeeded = ! Test : : JS : : parse_script ( test_file , realm ) . is_error ( ) ;
2021-08-14 12:30:37 -03:00
2021-05-30 03:05:44 -03:00
bool test_passed = true ;
2025-02-17 17:04:56 -03:00
String message ;
2025-02-17 14:18:27 -03:00
String expectation_string ;
2021-05-30 03:05:44 -03:00
switch ( expectation ) {
case Early :
case Fail :
2025-02-17 14:18:27 -03:00
expectation_string = " File should not parse " _string ;
2021-09-14 15:58:33 -03:00
test_passed = ! parse_succeeded ;
2021-05-30 03:05:44 -03:00
if ( ! test_passed )
2025-02-17 17:04:56 -03:00
message = " Expected the file to fail parsing, but it did not " _string ;
2021-05-30 03:05:44 -03:00
break ;
case Pass :
case ExplicitPass :
2025-02-17 14:18:27 -03:00
expectation_string = " File should parse " _string ;
2021-09-14 15:58:33 -03:00
test_passed = parse_succeeded ;
2021-05-30 03:05:44 -03:00
if ( ! test_passed )
2025-02-17 17:04:56 -03:00
message = " Expected the file to parse, but it did not " _string ;
2021-05-30 03:05:44 -03:00
break ;
}
auto test_result = test_passed ? Test : : Result : : Pass : Test : : Result : : Fail ;
2024-10-22 14:53:25 -03:00
auto test_path = * LexicalPath : : relative_path ( test_file , Test : : JS : : g_test_root ) ;
2022-03-08 00:39:41 -03:00
auto duration_ms = Test : : get_time_in_ms ( ) - start_time ;
2021-05-30 03:05:44 -03:00
return Test : : JS : : JSFileResult {
2022-03-08 00:39:41 -03:00
test_path ,
2021-05-30 03:05:44 -03:00
{ } ,
2022-03-08 00:39:41 -03:00
duration_ms ,
2021-05-30 03:05:44 -03:00
test_result ,
2025-02-17 17:04:56 -03:00
{ Test : : Suite { test_path , " Parse file " _string , test_result , { { move ( expectation_string ) , test_result , move ( message ) , static_cast < u64 > ( duration_ms ) * 1000u } } } }
2021-05-30 03:05:44 -03:00
} ;
}