js.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/NonnullOwnPtr.h>
  27. #include <LibJS/AST.h>
  28. #include <LibJS/Interpreter.h>
  29. #include <LibJS/Object.h>
  30. #include <LibJS/Parser.h>
  31. #include <LibJS/PrimitiveString.h>
  32. #include <LibJS/Value.h>
  33. #include <stdio.h>
  34. #define PROGRAM 6
  35. static NonnullOwnPtr<JS::Program> build_program(JS::Heap&);
  36. int main()
  37. {
  38. JS::Interpreter interpreter;
  39. auto program = build_program(interpreter.heap());
  40. program->dump(0);
  41. auto result = interpreter.run(*program);
  42. dbg() << "Interpreter returned " << result;
  43. printf("%s\n", result.to_string().characters());
  44. dbg() << "Collecting garbage on exit...";
  45. interpreter.heap().collect_garbage();
  46. return 0;
  47. }
  48. #if PROGRAM == 1
  49. NonnullOwnPtr<JS::Program> build_program(JS::Heap&)
  50. {
  51. // function foo() { return (1 + 2) + 3; }
  52. // foo();
  53. auto block = make<JS::BlockStatement>();
  54. block->append<JS::ReturnStatement>(
  55. make<JS::BinaryExpression>(
  56. JS::BinaryOp::Plus,
  57. make<JS::BinaryExpression>(
  58. JS::BinaryOp::Plus,
  59. make<JS::Literal>(JS::Value(1)),
  60. make<JS::Literal>(JS::Value(2))),
  61. make<JS::Literal>(JS::Value(3))));
  62. auto program = make<JS::Program>();
  63. program->append<JS::FunctionDeclaration>("foo", move(block));
  64. program->append<JS::ExpressionStatement>(make<JS::CallExpression>("foo"));
  65. return program;
  66. }
  67. #elif PROGRAM == 2
  68. NonnullOwnPtr<JS::Program> build_program(JS::Heap&)
  69. {
  70. // c = 1;
  71. // function foo() {
  72. // var a = 5;
  73. // var b = 7;
  74. // return a + b + c;
  75. // }
  76. // foo();
  77. auto program = make<JS::Program>();
  78. program->append<JS::ExpressionStatement>(make<JS::AssignmentExpression>(
  79. JS::AssignmentOp::Assign,
  80. make<JS::Identifier>("c"),
  81. make<JS::Literal>(JS::Value(1))));
  82. auto block = make<JS::BlockStatement>();
  83. block->append<JS::VariableDeclaration>(
  84. make<JS::Identifier>("a"),
  85. make<JS::Literal>(JS::Value(5)),
  86. JS::DeclarationType::Var);
  87. block->append<JS::VariableDeclaration>(
  88. make<JS::Identifier>("b"),
  89. make<JS::Literal>(JS::Value(7)),
  90. JS::DeclarationType::Var);
  91. block->append<JS::ReturnStatement>(
  92. make<JS::BinaryExpression>(
  93. JS::BinaryOp::Plus,
  94. make<JS::BinaryExpression>(
  95. JS::BinaryOp::Plus,
  96. make<JS::Identifier>("a"),
  97. make<JS::Identifier>("b")),
  98. make<JS::Identifier>("c")));
  99. program->append<JS::FunctionDeclaration>("foo", move(block));
  100. program->append<JS::ExpressionStatement>(make<JS::CallExpression>("foo"));
  101. return program;
  102. }
  103. #elif PROGRAM == 3
  104. NonnullOwnPtr<JS::Program> build_program(JS::Heap&)
  105. {
  106. // function foo() {
  107. // var x = {};
  108. // $gc();
  109. // }
  110. // foo();
  111. auto block = make<JS::BlockStatement>();
  112. block->append<JS::VariableDeclaration>(
  113. make<JS::Identifier>("x"),
  114. make<JS::ObjectExpression>(),
  115. JS::DeclarationType::Var);
  116. block->append<JS::ExpressionStatement>(make<JS::CallExpression>("$gc"));
  117. auto program = make<JS::Program>();
  118. program->append<JS::FunctionDeclaration>("foo", move(block));
  119. program->append<JS::ExpressionStatement>(make<JS::CallExpression>("foo"));
  120. return program;
  121. }
  122. #elif PROGRAM == 4
  123. NonnullOwnPtr<JS::Program> build_program(JS::Heap&)
  124. {
  125. // function foo() {
  126. // function bar() {
  127. // var y = 6;
  128. // }
  129. //
  130. // bar()
  131. // return y;
  132. // }
  133. // foo(); //I should return `undefined` because y is bound to the inner-most enclosing function, i.e the nested one (bar()), therefore, it's undefined in the scope of foo()
  134. auto block_bar = make<JS::BlockStatement>();
  135. block_bar->append<JS::VariableDeclaration>(make<JS::Identifier>("y"), make<JS::Literal>(JS::Value(6)), JS::DeclarationType::Var);
  136. auto block_foo = make<JS::BlockStatement>();
  137. block_foo->append<JS::FunctionDeclaration>("bar", move(block_bar));
  138. block_foo->append<JS::ExpressionStatement>(make<JS::CallExpression>("bar"));
  139. block_foo->append<JS::ReturnStatement>(make<JS::Identifier>("y"));
  140. auto program = make<JS::Program>();
  141. program->append<JS::FunctionDeclaration>("foo", move(block_foo));
  142. program->append<JS::ExpressionStatement>(make<JS::CallExpression>("foo"));
  143. return program;
  144. }
  145. #elif PROGRAM == 5
  146. NonnullOwnPtr<JS::Program> build_program(JS::Heap& heap)
  147. {
  148. // "hello friends".length
  149. auto program = make<JS::Program>();
  150. program->append<JS::ExpressionStatement>(make<JS::MemberExpression>(
  151. make<JS::Literal>(JS::Value(js_string(heap, "hello friends"))),
  152. make<JS::Identifier>("length")));
  153. return program;
  154. }
  155. #elif PROGRAM == 6
  156. NonnullOwnPtr<JS::Program> build_program(JS::Heap&)
  157. {
  158. const char* source = "var foo = 1;\n"
  159. "function bar() {\n"
  160. " return 38;\n"
  161. "}\n"
  162. "foo = {};\n"
  163. "foo = bar() + 4;\n"
  164. "foo;\n";
  165. auto parser = JS::Parser(JS::Lexer(source));
  166. return parser.parse_program();
  167. }
  168. #endif