test-bytecode-js.cpp 5.8 KB

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