test-bytecode-js.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (c) 2022, David Tuin <davidot@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/AST.h>
  7. #include <LibJS/Bytecode/Generator.h>
  8. #include <LibJS/Bytecode/Interpreter.h>
  9. #include <LibJS/Bytecode/PassManager.h>
  10. #include <LibJS/Interpreter.h>
  11. #include <LibJS/Runtime/VM.h>
  12. #include <LibJS/Script.h>
  13. #include <LibTest/TestCase.h>
  14. #define SETUP_AND_PARSE(source) \
  15. auto vm = MUST(JS::VM::create()); \
  16. auto ast_interpreter = JS::Interpreter::create<JS::GlobalObject>(*vm); \
  17. \
  18. auto script = MUST(JS::Script::parse(source##sv, ast_interpreter->realm())); \
  19. auto const& program = script->parse_node(); \
  20. JS::Bytecode::Interpreter bytecode_interpreter(ast_interpreter->realm());
  21. #define EXPECT_NO_EXCEPTION(executable) \
  22. auto executable = MUST(JS::Bytecode::Generator::generate(program)); \
  23. auto result = bytecode_interpreter.run(*script); \
  24. if (result.is_error()) { \
  25. FAIL("unexpected exception"); \
  26. dbgln("Error: {}", MUST(result.throw_completion().value()->to_deprecated_string(vm))); \
  27. }
  28. #define EXPECT_NO_EXCEPTION_WITH_OPTIMIZATIONS(executable) \
  29. auto& passes = JS::Bytecode::Interpreter::optimization_pipeline(); \
  30. passes.perform(*executable); \
  31. \
  32. auto result_with_optimizations = bytecode_interpreter.run(*executable); \
  33. \
  34. if (result_with_optimizations.is_error()) { \
  35. FAIL("unexpected exception"); \
  36. dbgln("Error: {}", MUST(result_with_optimizations.throw_completion().value()->to_deprecated_string(vm))); \
  37. }
  38. #define EXPECT_NO_EXCEPTION_ALL(source) \
  39. SETUP_AND_PARSE("(() => {\n" source "\n})()") \
  40. EXPECT_NO_EXCEPTION(executable) \
  41. EXPECT_NO_EXCEPTION_WITH_OPTIMIZATIONS(executable)
  42. TEST_CASE(empty_program)
  43. {
  44. EXPECT_NO_EXCEPTION_ALL("");
  45. }
  46. TEST_CASE(if_statement_pass)
  47. {
  48. EXPECT_NO_EXCEPTION_ALL("if (false) throw new Exception('failed');");
  49. }
  50. TEST_CASE(if_statement_fail)
  51. {
  52. SETUP_AND_PARSE("if (true) throw new Exception('failed');");
  53. auto executable = MUST(JS::Bytecode::Generator::generate(program));
  54. auto result = bytecode_interpreter.run(*executable);
  55. EXPECT(result.is_error());
  56. }
  57. TEST_CASE(trivial_program)
  58. {
  59. EXPECT_NO_EXCEPTION_ALL("if (1 + 1 !== 2) throw new Exception('failed');");
  60. }
  61. TEST_CASE(variables)
  62. {
  63. EXPECT_NO_EXCEPTION_ALL("var a = 1; \n"
  64. "if (a + 1 !== 2) throw new Exception('failed'); ");
  65. }
  66. TEST_CASE(function_call)
  67. {
  68. EXPECT_NO_EXCEPTION_ALL("if (!isNaN(NaN)) throw new Exception('failed'); ");
  69. }
  70. TEST_CASE(function_delcaration_and_call)
  71. {
  72. EXPECT_NO_EXCEPTION_ALL("var passed = false; \n"
  73. "function f() { passed = true; return 1; }\n"
  74. "if (f() !== 1) throw new Exception('failed');\n"
  75. // The passed !== true is needed as otherwise UBSAN
  76. // complains about unaligned access, until that
  77. // is fixed or ignored care must be taken to prevent such cases in tests.
  78. "if (passed !== true) throw new Exception('failed');");
  79. }
  80. TEST_CASE(generator_function_call)
  81. {
  82. EXPECT_NO_EXCEPTION_ALL("function *g() { yield 2; }\n"
  83. "var gen = g();\n"
  84. "var result = gen.next();\n"
  85. "if (result.value !== 2) throw new Exception('failed');");
  86. }
  87. TEST_CASE(loading_multiple_files)
  88. {
  89. // This is a testcase which is very much like test-js and test262
  90. // which load some common files first and only then the actual test file.
  91. SETUP_AND_PARSE("function f() { return 'hello'; }");
  92. {
  93. EXPECT_NO_EXCEPTION(common_file_executable);
  94. }
  95. {
  96. auto test_file_script = MUST(JS::Script::parse(
  97. "if (f() !== 'hello') throw new Exception('failed'); "sv, ast_interpreter->realm()));
  98. // TODO: This could be TRY_OR_FAIL(), if someone implements Formatter<JS::Completion>.
  99. MUST(bytecode_interpreter.run(test_file_script));
  100. }
  101. }
  102. TEST_CASE(catch_exception)
  103. {
  104. // FIXME: Currently it seems that try/catch with finally is broken so we test both at once.
  105. EXPECT_NO_EXCEPTION_ALL("var hitCatch = false;\n"
  106. "var hitFinally = false;\n"
  107. "try {\n"
  108. " a();\n"
  109. "} catch (e) {\n"
  110. " hitCatch = e instanceof ReferenceError;\n"
  111. " !1\n" // This is here to fix the alignment issue until that is actually resolved.
  112. "} finally {\n"
  113. " hitFinally = true;\n"
  114. "}\n"
  115. "if (hitCatch !== true) throw new Exception('failed');\n"
  116. "if (hitFinally !== true) throw new Exception('failed');");
  117. }