test-bytecode-js.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (c) 2022, David Tuin <davidot@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Bytecode/Generator.h>
  7. #include <LibJS/Bytecode/Interpreter.h>
  8. #include <LibJS/Interpreter.h>
  9. #include <LibJS/Runtime/VM.h>
  10. #include <LibJS/Script.h>
  11. #include <LibTest/TestCase.h>
  12. #define SETUP_AND_PARSE(source) \
  13. auto vm = JS::VM::create(); \
  14. auto ast_interpreter = JS::Interpreter::create<JS::GlobalObject>(*vm); \
  15. \
  16. auto script_or_error = JS::Script::parse(source##sv, ast_interpreter->realm()); \
  17. EXPECT(!script_or_error.is_error()); \
  18. \
  19. auto script = script_or_error.release_value(); \
  20. auto const& program = script->parse_node(); \
  21. JS::Bytecode::Interpreter bytecode_interpreter(ast_interpreter->global_object(), ast_interpreter->realm());
  22. #define EXPECT_NO_EXCEPTION(executable) \
  23. auto executable = MUST(JS::Bytecode::Generator::generate(program)); \
  24. auto result = bytecode_interpreter.run(*executable); \
  25. EXPECT(!result.is_error()); \
  26. if (result.is_error()) \
  27. dbgln("Error: {}", MUST(result.throw_completion().value()->to_string(vm)));
  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. EXPECT(!result_with_optimizations.is_error()); \
  35. if (result_with_optimizations.is_error()) \
  36. dbgln("Error: {}", MUST(result_with_optimizations.throw_completion().value()->to_string(vm)));
  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_or_error = JS::Script::parse("if (f() !== 'hello') throw new Exception('failed'); "sv, ast_interpreter->realm());
  96. EXPECT(!test_file_script_or_error.is_error());
  97. auto test_file_script = test_file_script_or_error.release_value();
  98. auto const& test_file_program = test_file_script->parse_node();
  99. auto executable = MUST(JS::Bytecode::Generator::generate(test_file_program));
  100. auto result = bytecode_interpreter.run(*executable);
  101. EXPECT(!result.is_error());
  102. }
  103. }
  104. TEST_CASE(catch_exception)
  105. {
  106. // FIXME: Currently it seems that try/catch with finally is broken so we test both at once.
  107. EXPECT_NO_EXCEPTION_ALL("var hitCatch = false;\n"
  108. "var hitFinally = false;\n"
  109. "try {\n"
  110. " a();\n"
  111. "} catch (e) {\n"
  112. " hitCatch = e instanceof ReferenceError;\n"
  113. " !1\n" // This is here to fix the alignment issue until that is actually resolved.
  114. "} finally {\n"
  115. " hitFinally = true;\n"
  116. "}\n"
  117. "if (hitCatch !== true) throw new Exception('failed');\n"
  118. "if (hitFinally !== true) throw new Exception('failed');");
  119. }