test-js.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
  3. * Copyright (c) 2020-2023, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibJS/Runtime/ArrayBuffer.h>
  8. #include <LibTest/JavaScriptTestRunner.h>
  9. TEST_ROOT("Userland/Libraries/LibJS/Tests");
  10. TESTJS_PROGRAM_FLAG(test262_parser_tests, "Run test262 parser tests", "test262-parser-tests", 0);
  11. TESTJS_GLOBAL_FUNCTION(is_bytecode_interpreter_enabled, isBytecodeInterpreterEnabled, 0)
  12. {
  13. // FIXME: Remove this function after updating the tests.
  14. return JS::Value(true);
  15. }
  16. TESTJS_GLOBAL_FUNCTION(is_strict_mode, isStrictMode, 0)
  17. {
  18. return JS::Value(vm.in_strict_mode());
  19. }
  20. TESTJS_GLOBAL_FUNCTION(can_parse_source, canParseSource)
  21. {
  22. auto source = TRY(vm.argument(0).to_deprecated_string(vm));
  23. auto parser = JS::Parser(JS::Lexer(source));
  24. (void)parser.parse_program();
  25. return JS::Value(!parser.has_errors());
  26. }
  27. TESTJS_GLOBAL_FUNCTION(run_queued_promise_jobs, runQueuedPromiseJobs)
  28. {
  29. vm.run_queued_promise_jobs();
  30. return JS::js_undefined();
  31. }
  32. TESTJS_GLOBAL_FUNCTION(get_weak_set_size, getWeakSetSize)
  33. {
  34. auto object = TRY(vm.argument(0).to_object(vm));
  35. if (!is<JS::WeakSet>(*object))
  36. return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "WeakSet");
  37. auto& weak_set = static_cast<JS::WeakSet&>(*object);
  38. return JS::Value(weak_set.values().size());
  39. }
  40. TESTJS_GLOBAL_FUNCTION(get_weak_map_size, getWeakMapSize)
  41. {
  42. auto object = TRY(vm.argument(0).to_object(vm));
  43. if (!is<JS::WeakMap>(*object))
  44. return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "WeakMap");
  45. auto& weak_map = static_cast<JS::WeakMap&>(*object);
  46. return JS::Value(weak_map.values().size());
  47. }
  48. TESTJS_GLOBAL_FUNCTION(mark_as_garbage, markAsGarbage)
  49. {
  50. auto argument = vm.argument(0);
  51. if (!argument.is_string())
  52. return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAString, TRY_OR_THROW_OOM(vm, argument.to_string_without_side_effects()));
  53. auto& variable_name = argument.as_string();
  54. // In native functions we don't have a lexical environment so get the outer via the execution stack.
  55. auto outer_environment = vm.execution_context_stack().last_matching([&](auto& execution_context) {
  56. return execution_context->lexical_environment != nullptr;
  57. });
  58. if (!outer_environment.has_value())
  59. return vm.throw_completion<JS::ReferenceError>(JS::ErrorType::UnknownIdentifier, TRY(variable_name.deprecated_string()));
  60. auto reference = TRY(vm.resolve_binding(TRY(variable_name.deprecated_string()), outer_environment.value()->lexical_environment));
  61. auto value = TRY(reference.get_value(vm));
  62. if (!can_be_held_weakly(value))
  63. return vm.throw_completion<JS::TypeError>(JS::ErrorType::CannotBeHeldWeakly, DeprecatedString::formatted("Variable with name {}", TRY(variable_name.deprecated_string())));
  64. vm.heap().uproot_cell(&value.as_cell());
  65. TRY(reference.delete_(vm));
  66. return JS::js_undefined();
  67. }
  68. TESTJS_GLOBAL_FUNCTION(detach_array_buffer, detachArrayBuffer)
  69. {
  70. auto array_buffer = vm.argument(0);
  71. if (!array_buffer.is_object() || !is<JS::ArrayBuffer>(array_buffer.as_object()))
  72. return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "ArrayBuffer");
  73. auto& array_buffer_object = static_cast<JS::ArrayBuffer&>(array_buffer.as_object());
  74. TRY(JS::detach_array_buffer(vm, array_buffer_object, vm.argument(1)));
  75. return JS::js_null();
  76. }
  77. TESTJS_RUN_FILE_FUNCTION(DeprecatedString const& test_file, JS::Realm& realm, JS::ExecutionContext&)
  78. {
  79. if (!test262_parser_tests)
  80. return Test::JS::RunFileHookResult::RunAsNormal;
  81. auto start_time = Test::get_time_in_ms();
  82. LexicalPath path(test_file);
  83. auto dirname = path.dirname();
  84. enum {
  85. Early,
  86. Fail,
  87. Pass,
  88. ExplicitPass,
  89. } expectation { Pass };
  90. if (dirname.ends_with("early"sv))
  91. expectation = Early;
  92. else if (dirname.ends_with("fail"sv))
  93. expectation = Fail;
  94. else if (dirname.ends_with("pass-explicit"sv))
  95. expectation = ExplicitPass;
  96. else if (dirname.ends_with("pass"sv))
  97. expectation = Pass;
  98. else
  99. return Test::JS::RunFileHookResult::SkipFile;
  100. auto program_type = path.basename().ends_with(".module.js"sv) ? JS::Program::Type::Module : JS::Program::Type::Script;
  101. bool parse_succeeded = false;
  102. if (program_type == JS::Program::Type::Module)
  103. parse_succeeded = !Test::JS::parse_module(test_file, realm).is_error();
  104. else
  105. parse_succeeded = !Test::JS::parse_script(test_file, realm).is_error();
  106. bool test_passed = true;
  107. DeprecatedString message;
  108. DeprecatedString expectation_string;
  109. switch (expectation) {
  110. case Early:
  111. case Fail:
  112. expectation_string = "File should not parse";
  113. test_passed = !parse_succeeded;
  114. if (!test_passed)
  115. message = "Expected the file to fail parsing, but it did not";
  116. break;
  117. case Pass:
  118. case ExplicitPass:
  119. expectation_string = "File should parse";
  120. test_passed = parse_succeeded;
  121. if (!test_passed)
  122. message = "Expected the file to parse, but it did not";
  123. break;
  124. }
  125. auto test_result = test_passed ? Test::Result::Pass : Test::Result::Fail;
  126. auto test_path = LexicalPath::relative_path(test_file, Test::JS::g_test_root);
  127. auto duration_ms = Test::get_time_in_ms() - start_time;
  128. return Test::JS::JSFileResult {
  129. test_path,
  130. {},
  131. duration_ms,
  132. test_result,
  133. { Test::Suite { test_path, "Parse file", test_result, { { expectation_string, test_result, message, static_cast<u64>(duration_ms) * 1000u } } } }
  134. };
  135. }