test-bytecode-js.cpp 5.4 KB

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