test-js.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 <AK/Enumerate.h>
  8. #include <LibCore/Environment.h>
  9. #include <LibJS/Runtime/ArrayBuffer.h>
  10. #include <LibJS/Runtime/Date.h>
  11. #include <LibJS/Runtime/TypedArray.h>
  12. #include <LibTest/JavaScriptTestRunner.h>
  13. #include <LibUnicode/TimeZone.h>
  14. #include <stdlib.h>
  15. #include <time.h>
  16. TEST_ROOT("Userland/Libraries/LibJS/Tests");
  17. TESTJS_PROGRAM_FLAG(test262_parser_tests, "Run test262 parser tests", "test262-parser-tests", 0);
  18. TESTJS_GLOBAL_FUNCTION(is_strict_mode, isStrictMode, 0)
  19. {
  20. return JS::Value(vm.in_strict_mode());
  21. }
  22. TESTJS_GLOBAL_FUNCTION(can_parse_source, canParseSource)
  23. {
  24. auto source = TRY(vm.argument(0).to_byte_string(vm));
  25. auto parser = JS::Parser(JS::Lexer(source));
  26. (void)parser.parse_program();
  27. return JS::Value(!parser.has_errors());
  28. }
  29. TESTJS_GLOBAL_FUNCTION(run_queued_promise_jobs, runQueuedPromiseJobs)
  30. {
  31. vm.run_queued_promise_jobs();
  32. return JS::js_undefined();
  33. }
  34. TESTJS_GLOBAL_FUNCTION(get_weak_set_size, getWeakSetSize)
  35. {
  36. auto object = TRY(vm.argument(0).to_object(vm));
  37. if (!is<JS::WeakSet>(*object))
  38. return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "WeakSet");
  39. auto& weak_set = static_cast<JS::WeakSet&>(*object);
  40. return JS::Value(weak_set.values().size());
  41. }
  42. TESTJS_GLOBAL_FUNCTION(get_weak_map_size, getWeakMapSize)
  43. {
  44. auto object = TRY(vm.argument(0).to_object(vm));
  45. if (!is<JS::WeakMap>(*object))
  46. return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "WeakMap");
  47. auto& weak_map = static_cast<JS::WeakMap&>(*object);
  48. return JS::Value(weak_map.values().size());
  49. }
  50. TESTJS_GLOBAL_FUNCTION(mark_as_garbage, markAsGarbage)
  51. {
  52. auto argument = vm.argument(0);
  53. if (!argument.is_string())
  54. return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAString, argument.to_string_without_side_effects());
  55. auto& variable_name = argument.as_string();
  56. // In native functions we don't have a lexical environment so get the outer via the execution stack.
  57. auto outer_environment = vm.execution_context_stack().last_matching([&](auto& execution_context) {
  58. return execution_context->lexical_environment != nullptr;
  59. });
  60. if (!outer_environment.has_value())
  61. return vm.throw_completion<JS::ReferenceError>(JS::ErrorType::UnknownIdentifier, variable_name.byte_string());
  62. auto reference = TRY(vm.resolve_binding(variable_name.byte_string(), outer_environment.value()->lexical_environment));
  63. auto value = TRY(reference.get_value(vm));
  64. if (!can_be_held_weakly(value))
  65. return vm.throw_completion<JS::TypeError>(JS::ErrorType::CannotBeHeldWeakly, ByteString::formatted("Variable with name {}", variable_name.byte_string()));
  66. vm.heap().uproot_cell(&value.as_cell());
  67. TRY(reference.delete_(vm));
  68. return JS::js_undefined();
  69. }
  70. TESTJS_GLOBAL_FUNCTION(detach_array_buffer, detachArrayBuffer)
  71. {
  72. auto array_buffer = vm.argument(0);
  73. if (!array_buffer.is_object() || !is<JS::ArrayBuffer>(array_buffer.as_object()))
  74. return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "ArrayBuffer");
  75. auto& array_buffer_object = static_cast<JS::ArrayBuffer&>(array_buffer.as_object());
  76. TRY(JS::detach_array_buffer(vm, array_buffer_object, vm.argument(1)));
  77. return JS::js_null();
  78. }
  79. TESTJS_GLOBAL_FUNCTION(set_time_zone, setTimeZone)
  80. {
  81. auto current_time_zone = JS::js_null();
  82. if (auto time_zone = Core::Environment::get("TZ"sv); time_zone.has_value())
  83. current_time_zone = JS::PrimitiveString::create(vm, *time_zone);
  84. if (auto time_zone = vm.argument(0); time_zone.is_null()) {
  85. if (auto result = Core::Environment::unset("TZ"sv); result.is_error())
  86. return vm.throw_completion<JS::InternalError>(MUST(String::formatted("Could not unset time zone: {}", result.error())));
  87. } else {
  88. if (auto result = Core::Environment::set("TZ"sv, TRY(time_zone.to_string(vm)), Core::Environment::Overwrite::Yes); result.is_error())
  89. return vm.throw_completion<JS::InternalError>(MUST(String::formatted("Could not set time zone: {}", result.error())));
  90. }
  91. JS::clear_system_time_zone_cache();
  92. Unicode::clear_system_time_zone_cache();
  93. tzset();
  94. return current_time_zone;
  95. }
  96. TESTJS_GLOBAL_FUNCTION(to_utf8_bytes, toUTF8Bytes)
  97. {
  98. auto& realm = *vm.current_realm();
  99. auto string = TRY(vm.argument(0).to_string(vm));
  100. auto typed_array = TRY(JS::Uint8Array::create(realm, string.bytes().size()));
  101. for (auto [i, byte] : enumerate(string.bytes()))
  102. typed_array->set_value_in_buffer(i, JS::Value { byte }, JS::ArrayBuffer::Order::SeqCst);
  103. return typed_array;
  104. }
  105. TESTJS_RUN_FILE_FUNCTION(ByteString const& test_file, JS::Realm& realm, JS::ExecutionContext&)
  106. {
  107. if (!test262_parser_tests)
  108. return Test::JS::RunFileHookResult::RunAsNormal;
  109. auto start_time = Test::get_time_in_ms();
  110. LexicalPath path(test_file);
  111. auto dirname = path.dirname();
  112. enum {
  113. Early,
  114. Fail,
  115. Pass,
  116. ExplicitPass,
  117. } expectation { Pass };
  118. if (dirname.ends_with("early"sv))
  119. expectation = Early;
  120. else if (dirname.ends_with("fail"sv))
  121. expectation = Fail;
  122. else if (dirname.ends_with("pass-explicit"sv))
  123. expectation = ExplicitPass;
  124. else if (dirname.ends_with("pass"sv))
  125. expectation = Pass;
  126. else
  127. return Test::JS::RunFileHookResult::SkipFile;
  128. auto program_type = path.basename().ends_with(".module.js"sv) ? JS::Program::Type::Module : JS::Program::Type::Script;
  129. bool parse_succeeded = false;
  130. if (program_type == JS::Program::Type::Module)
  131. parse_succeeded = !Test::JS::parse_module(test_file, realm).is_error();
  132. else
  133. parse_succeeded = !Test::JS::parse_script(test_file, realm).is_error();
  134. bool test_passed = true;
  135. ByteString message;
  136. ByteString expectation_string;
  137. switch (expectation) {
  138. case Early:
  139. case Fail:
  140. expectation_string = "File should not parse";
  141. test_passed = !parse_succeeded;
  142. if (!test_passed)
  143. message = "Expected the file to fail parsing, but it did not";
  144. break;
  145. case Pass:
  146. case ExplicitPass:
  147. expectation_string = "File should parse";
  148. test_passed = parse_succeeded;
  149. if (!test_passed)
  150. message = "Expected the file to parse, but it did not";
  151. break;
  152. }
  153. auto test_result = test_passed ? Test::Result::Pass : Test::Result::Fail;
  154. auto test_path = *LexicalPath::relative_path(test_file, Test::JS::g_test_root);
  155. auto duration_ms = Test::get_time_in_ms() - start_time;
  156. return Test::JS::JSFileResult {
  157. test_path,
  158. {},
  159. duration_ms,
  160. test_result,
  161. { Test::Suite { test_path, "Parse file", test_result, { { expectation_string, test_result, message, static_cast<u64>(duration_ms) * 1000u } } } }
  162. };
  163. }