test-js.cpp 5.7 KB

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