2020-07-03 21:36:58 +00:00
/*
2021-04-22 23:53:07 +00:00
* Copyright ( c ) 2020 , Matthew Olsson < mattco @ serenityos . org >
2023-04-13 13:26:41 +00:00
* Copyright ( c ) 2020 - 2023 , Linus Groh < linusg @ serenityos . org >
2020-07-03 21:36:58 +00:00
*
2021-04-22 08:24:48 +00:00
* SPDX - License - Identifier : BSD - 2 - Clause
2020-07-03 21:36:58 +00:00
*/
2024-07-15 16:28:23 +00:00
# include <AK/Enumerate.h>
2024-01-30 16:14:32 +00:00
# include <LibCore/Environment.h>
2022-04-07 17:06:40 +00:00
# include <LibJS/Runtime/ArrayBuffer.h>
2024-09-03 14:18:43 +00:00
# include <LibJS/Runtime/Date.h>
2024-07-15 16:28:23 +00:00
# include <LibJS/Runtime/TypedArray.h>
2021-05-18 14:15:12 +00:00
# include <LibTest/JavaScriptTestRunner.h>
2024-08-24 16:07:24 +00:00
# include <LibUnicode/TimeZone.h>
2023-11-07 16:14:55 +00:00
# include <stdlib.h>
# include <time.h>
2020-07-03 21:36:58 +00:00
2024-11-09 17:25:08 +00:00
TEST_ROOT ( " Libraries/LibJS/Tests " ) ;
2020-07-03 21:36:58 +00:00
2021-05-30 06:05:44 +00:00
TESTJS_PROGRAM_FLAG ( test262_parser_tests , " Run test262 parser tests " , " test262-parser-tests " , 0 ) ;
2021-05-18 14:15:12 +00:00
TESTJS_GLOBAL_FUNCTION ( is_strict_mode , isStrictMode , 0 )
2020-07-05 17:47:40 +00:00
{
2020-10-04 11:54:44 +00:00
return JS : : Value ( vm . in_strict_mode ( ) ) ;
2020-07-05 17:47:40 +00:00
}
2021-05-18 14:15:12 +00:00
TESTJS_GLOBAL_FUNCTION ( can_parse_source , canParseSource )
2020-11-11 22:12:51 +00:00
{
2023-12-16 14:19:34 +00:00
auto source = TRY ( vm . argument ( 0 ) . to_byte_string ( vm ) ) ;
2020-11-11 22:12:51 +00:00
auto parser = JS : : Parser ( JS : : Lexer ( source ) ) ;
2021-12-02 11:00:10 +00:00
( void ) parser . parse_program ( ) ;
2020-11-11 22:12:51 +00:00
return JS : : Value ( ! parser . has_errors ( ) ) ;
}
2021-05-18 14:15:12 +00: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 20:13:29 +00:00
{
vm . run_queued_promise_jobs ( ) ;
return JS : : js_undefined ( ) ;
}
2021-05-30 06:05:44 +00:00
2021-06-09 17:10:47 +00:00
TESTJS_GLOBAL_FUNCTION ( get_weak_set_size , getWeakSetSize )
{
2023-04-13 13:26:41 +00:00
auto object = TRY ( vm . argument ( 0 ) . to_object ( vm ) ) ;
if ( ! is < JS : : WeakSet > ( * object ) )
2022-08-16 19:33:17 +00:00
return vm . throw_completion < JS : : TypeError > ( JS : : ErrorType : : NotAnObjectOfType , " WeakSet " ) ;
2023-04-13 13:26:41 +00:00
auto & weak_set = static_cast < JS : : WeakSet & > ( * object ) ;
return JS : : Value ( weak_set . values ( ) . size ( ) ) ;
2021-06-09 17:10:47 +00:00
}
2021-06-12 02:37:09 +00:00
TESTJS_GLOBAL_FUNCTION ( get_weak_map_size , getWeakMapSize )
{
2023-04-13 13:26:41 +00:00
auto object = TRY ( vm . argument ( 0 ) . to_object ( vm ) ) ;
if ( ! is < JS : : WeakMap > ( * object ) )
2022-08-16 19:33:17 +00:00
return vm . throw_completion < JS : : TypeError > ( JS : : ErrorType : : NotAnObjectOfType , " WeakMap " ) ;
2023-04-13 13:26:41 +00:00
auto & weak_map = static_cast < JS : : WeakMap & > ( * object ) ;
return JS : : Value ( weak_map . values ( ) . size ( ) ) ;
2021-06-12 02:37:09 +00:00
}
2023-07-22 04:53:22 +00:00
TESTJS_GLOBAL_FUNCTION ( mark_as_garbage , markAsGarbage )
{
auto argument = vm . argument ( 0 ) ;
if ( ! argument . is_string ( ) )
2023-08-09 06:49:02 +00:00
return vm . throw_completion < JS : : TypeError > ( JS : : ErrorType : : NotAString , argument . to_string_without_side_effects ( ) ) ;
2023-07-22 04:53:22 +00: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.
auto outer_environment = vm . execution_context_stack ( ) . last_matching ( [ & ] ( auto & execution_context ) {
return execution_context - > lexical_environment ! = nullptr ;
} ) ;
if ( ! outer_environment . has_value ( ) )
2023-12-16 14:19:34 +00:00
return vm . throw_completion < JS : : ReferenceError > ( JS : : ErrorType : : UnknownIdentifier , variable_name . byte_string ( ) ) ;
2023-07-22 04:53:22 +00:00
2023-12-16 14:19:34 +00:00
auto reference = TRY ( vm . resolve_binding ( variable_name . byte_string ( ) , outer_environment . value ( ) - > lexical_environment ) ) ;
2023-07-22 04:53:22 +00:00
auto value = TRY ( reference . get_value ( vm ) ) ;
if ( ! can_be_held_weakly ( value ) )
2023-12-16 14:19:34 +00:00
return vm . throw_completion < JS : : TypeError > ( JS : : ErrorType : : CannotBeHeldWeakly , ByteString : : formatted ( " Variable with name {} " , variable_name . byte_string ( ) ) ) ;
2023-07-22 04:53:22 +00:00
vm . heap ( ) . uproot_cell ( & value . as_cell ( ) ) ;
TRY ( reference . delete_ ( vm ) ) ;
return JS : : js_undefined ( ) ;
}
2022-04-07 17:06:40 +00:00
TESTJS_GLOBAL_FUNCTION ( detach_array_buffer , detachArrayBuffer )
{
auto array_buffer = vm . argument ( 0 ) ;
if ( ! array_buffer . is_object ( ) | | ! is < JS : : ArrayBuffer > ( array_buffer . as_object ( ) ) )
2022-08-16 19:33:17 +00:00
return vm . throw_completion < JS : : TypeError > ( JS : : ErrorType : : NotAnObjectOfType , " ArrayBuffer " ) ;
2022-04-07 17:06:40 +00:00
auto & array_buffer_object = static_cast < JS : : ArrayBuffer & > ( array_buffer . as_object ( ) ) ;
2022-08-21 16:25:31 +00:00
TRY ( JS : : detach_array_buffer ( vm , array_buffer_object , vm . argument ( 1 ) ) ) ;
2022-05-02 18:54:39 +00:00
return JS : : js_null ( ) ;
2022-04-07 17:06:40 +00:00
}
2023-11-07 16:14:55 +00:00
TESTJS_GLOBAL_FUNCTION ( set_time_zone , setTimeZone )
{
auto current_time_zone = JS : : js_null ( ) ;
2024-01-30 16:14:32 +00:00
if ( auto time_zone = Core : : Environment : : get ( " TZ " sv ) ; time_zone . has_value ( ) )
current_time_zone = JS : : PrimitiveString : : create ( vm , * time_zone ) ;
2023-11-07 16:14:55 +00:00
if ( auto time_zone = vm . argument ( 0 ) ; time_zone . is_null ( ) ) {
2024-01-30 16:14:32 +00:00
if ( auto result = Core : : Environment : : unset ( " TZ " sv ) ; result . is_error ( ) )
2023-11-07 16:14:55 +00:00
return vm . throw_completion < JS : : InternalError > ( MUST ( String : : formatted ( " Could not unset time zone: {} " , result . error ( ) ) ) ) ;
} else {
2024-01-30 16:14:32 +00:00
if ( auto result = Core : : Environment : : set ( " TZ " sv , TRY ( time_zone . to_string ( vm ) ) , Core : : Environment : : Overwrite : : Yes ) ; result . is_error ( ) )
2023-11-07 16:14:55 +00:00
return vm . throw_completion < JS : : InternalError > ( MUST ( String : : formatted ( " Could not set time zone: {} " , result . error ( ) ) ) ) ;
}
2024-09-03 14:18:43 +00:00
JS : : clear_system_time_zone_cache ( ) ;
2024-08-24 16:07:24 +00:00
Unicode : : clear_system_time_zone_cache ( ) ;
2023-11-07 16:14:55 +00:00
tzset ( ) ;
return current_time_zone ;
}
2024-07-15 16:28:23 +00:00
TESTJS_GLOBAL_FUNCTION ( to_utf8_bytes , toUTF8Bytes )
{
auto & realm = * vm . current_realm ( ) ;
auto string = TRY ( vm . argument ( 0 ) . to_string ( vm ) ) ;
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 ;
}
2023-12-16 14:19:34 +00:00
TESTJS_RUN_FILE_FUNCTION ( ByteString const & test_file , JS : : Realm & realm , JS : : ExecutionContext & )
2021-05-30 06:05:44 +00:00
{
if ( ! test262_parser_tests )
return Test : : JS : : RunFileHookResult : : RunAsNormal ;
2021-06-27 18:57:35 +00:00
auto start_time = Test : : get_time_in_ms ( ) ;
2021-05-30 06:05:44 +00:00
LexicalPath path ( test_file ) ;
2021-11-10 23:55:02 +00:00
auto dirname = path . dirname ( ) ;
2021-05-30 06:05:44 +00:00
enum {
Early ,
Fail ,
Pass ,
ExplicitPass ,
} expectation { Pass } ;
2022-07-11 17:32:29 +00:00
if ( dirname . ends_with ( " early " sv ) )
2021-05-30 06:05:44 +00:00
expectation = Early ;
2022-07-11 17:32:29 +00:00
else if ( dirname . ends_with ( " fail " sv ) )
2021-05-30 06:05:44 +00:00
expectation = Fail ;
2022-07-11 17:32:29 +00:00
else if ( dirname . ends_with ( " pass-explicit " sv ) )
2021-05-30 06:05:44 +00:00
expectation = ExplicitPass ;
2022-07-11 17:32:29 +00:00
else if ( dirname . ends_with ( " pass " sv ) )
2021-05-30 06:05:44 +00:00
expectation = Pass ;
else
return Test : : JS : : RunFileHookResult : : SkipFile ;
2022-07-11 17:32:29 +00:00
auto program_type = path . basename ( ) . ends_with ( " .module.js " sv ) ? JS : : Program : : Type : : Module : JS : : Program : : Type : : Script ;
2021-09-14 18:58:33 +00:00
bool parse_succeeded = false ;
if ( program_type = = JS : : Program : : Type : : Module )
2023-08-07 14:36:23 +00:00
parse_succeeded = ! Test : : JS : : parse_module ( test_file , realm ) . is_error ( ) ;
2021-09-14 18:58:33 +00:00
else
2023-08-07 14:36:23 +00:00
parse_succeeded = ! Test : : JS : : parse_script ( test_file , realm ) . is_error ( ) ;
2021-08-14 15:30:37 +00:00
2021-05-30 06:05:44 +00:00
bool test_passed = true ;
2023-12-16 14:19:34 +00:00
ByteString message ;
ByteString expectation_string ;
2021-05-30 06:05:44 +00:00
switch ( expectation ) {
case Early :
case Fail :
expectation_string = " File should not parse " ;
2021-09-14 18:58:33 +00:00
test_passed = ! parse_succeeded ;
2021-05-30 06:05:44 +00:00
if ( ! test_passed )
message = " Expected the file to fail parsing, but it did not " ;
break ;
case Pass :
case ExplicitPass :
expectation_string = " File should parse " ;
2021-09-14 18:58:33 +00:00
test_passed = parse_succeeded ;
2021-05-30 06:05:44 +00:00
if ( ! test_passed )
message = " Expected the file to parse, but it did not " ;
break ;
}
auto test_result = test_passed ? Test : : Result : : Pass : Test : : Result : : Fail ;
2024-10-22 17:53:25 +00:00
auto test_path = * LexicalPath : : relative_path ( test_file , Test : : JS : : g_test_root ) ;
2022-03-08 03:39:41 +00:00
auto duration_ms = Test : : get_time_in_ms ( ) - start_time ;
2021-05-30 06:05:44 +00:00
return Test : : JS : : JSFileResult {
2022-03-08 03:39:41 +00:00
test_path ,
2021-05-30 06:05:44 +00:00
{ } ,
2022-03-08 03:39:41 +00:00
duration_ms ,
2021-05-30 06:05:44 +00:00
test_result ,
2022-03-08 03:39:41 +00:00
{ Test : : Suite { test_path , " Parse file " , test_result , { { expectation_string , test_result , message , static_cast < u64 > ( duration_ms ) * 1000u } } } }
2021-05-30 06:05:44 +00:00
} ;
}