1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 10:35:00 +00:00
serenity/Tests/LibJS/test-js.cpp
Andreas Kling 6232ad3a0d LibJS: Remove "uprooting" mechanism from garbage collector
The Heap::uproot_cell() API was used to implement markAsGarbage() which
was used in 3 tests to forcibly destroy a value, even if it had
references on the stack or elsewhere.

This patch rewrites the 3 tests that used this mechanism to be
structured in a way that allows garbage collection to collect the values
as intended without hacks. And now that the uprooting mechanism is no
longer needed, it's uprooted as well.

This fixes 3 test-js tests in bytecode mode. :^)
2023-07-21 14:14:00 +02:00

128 lines
4.3 KiB
C++

/*
* Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
* Copyright (c) 2020-2023, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/ArrayBuffer.h>
#include <LibTest/JavaScriptTestRunner.h>
TEST_ROOT("Userland/Libraries/LibJS/Tests");
TESTJS_PROGRAM_FLAG(test262_parser_tests, "Run test262 parser tests", "test262-parser-tests", 0);
TESTJS_GLOBAL_FUNCTION(is_strict_mode, isStrictMode, 0)
{
return JS::Value(vm.in_strict_mode());
}
TESTJS_GLOBAL_FUNCTION(can_parse_source, canParseSource)
{
auto source = TRY(vm.argument(0).to_deprecated_string(vm));
auto parser = JS::Parser(JS::Lexer(source));
(void)parser.parse_program();
return JS::Value(!parser.has_errors());
}
TESTJS_GLOBAL_FUNCTION(run_queued_promise_jobs, runQueuedPromiseJobs)
{
vm.run_queued_promise_jobs();
return JS::js_undefined();
}
TESTJS_GLOBAL_FUNCTION(get_weak_set_size, getWeakSetSize)
{
auto object = TRY(vm.argument(0).to_object(vm));
if (!is<JS::WeakSet>(*object))
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "WeakSet");
auto& weak_set = static_cast<JS::WeakSet&>(*object);
return JS::Value(weak_set.values().size());
}
TESTJS_GLOBAL_FUNCTION(get_weak_map_size, getWeakMapSize)
{
auto object = TRY(vm.argument(0).to_object(vm));
if (!is<JS::WeakMap>(*object))
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "WeakMap");
auto& weak_map = static_cast<JS::WeakMap&>(*object);
return JS::Value(weak_map.values().size());
}
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()))
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "ArrayBuffer");
auto& array_buffer_object = static_cast<JS::ArrayBuffer&>(array_buffer.as_object());
TRY(JS::detach_array_buffer(vm, array_buffer_object, vm.argument(1)));
return JS::js_null();
}
TESTJS_RUN_FILE_FUNCTION(DeprecatedString const& test_file, JS::Interpreter& interpreter, JS::ExecutionContext&)
{
if (!test262_parser_tests)
return Test::JS::RunFileHookResult::RunAsNormal;
auto start_time = Test::get_time_in_ms();
LexicalPath path(test_file);
auto dirname = path.dirname();
enum {
Early,
Fail,
Pass,
ExplicitPass,
} expectation { Pass };
if (dirname.ends_with("early"sv))
expectation = Early;
else if (dirname.ends_with("fail"sv))
expectation = Fail;
else if (dirname.ends_with("pass-explicit"sv))
expectation = ExplicitPass;
else if (dirname.ends_with("pass"sv))
expectation = Pass;
else
return Test::JS::RunFileHookResult::SkipFile;
auto program_type = path.basename().ends_with(".module.js"sv) ? JS::Program::Type::Module : JS::Program::Type::Script;
bool parse_succeeded = false;
if (program_type == JS::Program::Type::Module)
parse_succeeded = !Test::JS::parse_module(test_file, interpreter.realm()).is_error();
else
parse_succeeded = !Test::JS::parse_script(test_file, interpreter.realm()).is_error();
bool test_passed = true;
DeprecatedString message;
DeprecatedString expectation_string;
switch (expectation) {
case Early:
case Fail:
expectation_string = "File should not parse";
test_passed = !parse_succeeded;
if (!test_passed)
message = "Expected the file to fail parsing, but it did not";
break;
case Pass:
case ExplicitPass:
expectation_string = "File should parse";
test_passed = parse_succeeded;
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;
auto test_path = LexicalPath::relative_path(test_file, Test::JS::g_test_root);
auto duration_ms = Test::get_time_in_ms() - start_time;
return Test::JS::JSFileResult {
test_path,
{},
duration_ms,
test_result,
{ Test::Suite { test_path, "Parse file", test_result, { { expectation_string, test_result, message, static_cast<u64>(duration_ms) * 1000u } } } }
};
}