test-js.cpp 5.4 KB

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