js.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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/PrimitiveString.h>
  31. #include <LibJS/Value.h>
  32. #include <stdio.h>
  33. #define PROGRAM 4
  34. static void build_program(JS::Program&, JS::Heap&);
  35. int main()
  36. {
  37. JS::Interpreter interpreter;
  38. auto program = make<JS::Program>();
  39. build_program(*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. void build_program(JS::Program& 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. program.append<JS::FunctionDeclaration>("foo", move(block));
  63. program.append<JS::CallExpression>("foo");
  64. }
  65. #elif PROGRAM == 2
  66. void build_program(JS::Program& program, JS::Heap&)
  67. {
  68. // c = 1;
  69. // function foo() {
  70. // var a = 5;
  71. // var b = 7;
  72. // return a + b + c;
  73. // }
  74. // foo();
  75. program.append<JS::AssignmentExpression>(
  76. JS::AssignmentOp::Assign,
  77. make<JS::Identifier>("c"),
  78. make<JS::Literal>(JS::Value(1)));
  79. auto block = make<JS::BlockStatement>();
  80. block->append<JS::VariableDeclaration>(
  81. make<JS::Identifier>("a"),
  82. make<JS::Literal>(JS::Value(5)),
  83. JS::DeclarationType::Var);
  84. block->append<JS::VariableDeclaration>(
  85. make<JS::Identifier>("b"),
  86. make<JS::Literal>(JS::Value(7)),
  87. JS::DeclarationType::Var);
  88. block->append<JS::ReturnStatement>(
  89. make<JS::BinaryExpression>(
  90. JS::BinaryOp::Plus,
  91. make<JS::BinaryExpression>(
  92. JS::BinaryOp::Plus,
  93. make<JS::Identifier>("a"),
  94. make<JS::Identifier>("b")),
  95. make<JS::Identifier>("c")));
  96. program.append<JS::FunctionDeclaration>("foo", move(block));
  97. program.append<JS::CallExpression>("foo");
  98. }
  99. #elif PROGRAM == 3
  100. void build_program(JS::Program& program, JS::Heap&)
  101. {
  102. // function foo() {
  103. // var x = {};
  104. // $gc();
  105. // }
  106. // foo();
  107. auto block = make<JS::BlockStatement>();
  108. block->append<JS::VariableDeclaration>(
  109. make<JS::Identifier>("x"),
  110. make<JS::ObjectExpression>(),
  111. JS::DeclarationType::Var);
  112. block->append<JS::CallExpression>("$gc");
  113. program.append<JS::FunctionDeclaration>("foo", move(block));
  114. program.append<JS::CallExpression>("foo");
  115. }
  116. #elif PROGRAM == 4
  117. void build_program(JS::Program& program, JS::Heap&)
  118. {
  119. // function foo() {
  120. // function bar() {
  121. // var y = 6;
  122. // }
  123. //
  124. // bar()
  125. // return y;
  126. // }
  127. // 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()
  128. auto block_bar = make<JS::BlockStatement>();
  129. block_bar->append<JS::VariableDeclaration>(make<JS::Identifier>("y"), make<JS::Literal>(JS::Value(6)), JS::DeclarationType::Var);
  130. auto block_foo = make<JS::BlockStatement>();
  131. block_foo->append<JS::FunctionDeclaration>("bar", move(block_bar));
  132. block_foo->append<JS::CallExpression>("bar");
  133. block_foo->append<JS::ReturnStatement>(make<JS::Identifier>("y"));
  134. program.append<JS::FunctionDeclaration>("foo", move(block_foo));
  135. program.append<JS::CallExpression>("foo");
  136. }
  137. #elif PROGRAM == 5
  138. void build_program(JS::Program& program, JS::Heap& heap)
  139. {
  140. // "hello friends".length
  141. program.append<JS::MemberExpression>(
  142. make<JS::Literal>(JS::Value(js_string(heap, "hello friends"))),
  143. make<JS::Identifier>("length"));
  144. }
  145. #endif