diff options
author | davidot <david.tuin@gmail.com> | 2021-09-07 17:14:05 +0200 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-09-08 08:53:02 +0100 |
commit | 43b17f27a31f160eb2c92ed51bcc78b1efd93042 (patch) | |
tree | 23b68a2e2a686e85d590a58e28c55d59d1a163e9 /Tests | |
parent | 33730909939ecb742ff7815a7f4525443c1b8636 (diff) | |
download | serenity-43b17f27a31f160eb2c92ed51bcc78b1efd93042.zip |
test-js: Add a mark_as_garbage method to force GC to collect that object
This should fix the flaky tests of test-js.
It also fixes the tests when running with the -g flag since the values
will not be garbage collected too soon.
Diffstat (limited to 'Tests')
-rw-r--r-- | Tests/LibJS/test-js.cpp | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/Tests/LibJS/test-js.cpp b/Tests/LibJS/test-js.cpp index 242c004e29..5bd418c769 100644 --- a/Tests/LibJS/test-js.cpp +++ b/Tests/LibJS/test-js.cpp @@ -58,6 +58,42 @@ TESTJS_GLOBAL_FUNCTION(get_weak_map_size, getWeakMapSize) return JS::Value(weak_map->values().size()); } +TESTJS_GLOBAL_FUNCTION(mark_as_garbage, markAsGarbage) +{ + auto argument = vm.argument(0); + if (!argument.is_string()) { + vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAString, argument.to_string_without_side_effects()); + return {}; + } + + 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()) { + vm.throw_exception<JS::ReferenceError>(global_object, JS::ErrorType::UnknownIdentifier, variable_name.string()); + return {}; + } + + auto variable = outer_environment.value()->lexical_environment->get_from_environment(variable_name.string()); + if (!variable.has_value()) { + vm.throw_exception<JS::ReferenceError>(global_object, JS::ErrorType::UnknownIdentifier, variable_name.string()); + return {}; + } + + if (!variable->value.is_object()) { + vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObject, String::formatted("Variable with name {}", variable_name.string())); + return {}; + } + + vm.heap().uproot_cell(&variable->value.as_object()); + outer_environment.value()->lexical_environment->delete_from_environment(variable_name.string()); + + return JS::js_undefined(); +} + TESTJS_RUN_FILE_FUNCTION(const String& test_file, JS::Interpreter&) { if (!test262_parser_tests) |