test-js.cpp 4.9 KB

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