test-js.cpp 5.2 KB

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