js.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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/Value.h>
  31. #include <stdio.h>
  32. //static void build_program_1(JS::Program&);
  33. //static void build_program_2(JS::Program&);
  34. static void build_program_3(JS::Program&);
  35. int main()
  36. {
  37. auto program = make<JS::Program>();
  38. build_program_3(*program);
  39. program->dump(0);
  40. JS::Interpreter interpreter;
  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 0
  49. void build_program_1(JS::Program& program)
  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. #endif
  66. #if 0
  67. void build_program_2(JS::Program& program)
  68. {
  69. // c = 1;
  70. // function foo() {
  71. // var a = 5;
  72. // var b = 7;
  73. // return a + b + c;
  74. // }
  75. // foo();
  76. program.append<JS::AssignmentExpression>(
  77. JS::AssignmentOp::Assign,
  78. make<JS::Identifier>("c"),
  79. make<JS::Literal>(JS::Value(1)));
  80. auto block = make<JS::BlockStatement>();
  81. block->append<JS::VariableDeclaration>(
  82. make<JS::Identifier>("a"),
  83. make<JS::Literal>(JS::Value(5)));
  84. block->append<JS::VariableDeclaration>(
  85. make<JS::Identifier>("b"),
  86. make<JS::Literal>(JS::Value(7)));
  87. block->append<JS::ReturnStatement>(
  88. make<JS::BinaryExpression>(
  89. JS::BinaryOp::Plus,
  90. make<JS::BinaryExpression>(
  91. JS::BinaryOp::Plus,
  92. make<JS::Identifier>("a"),
  93. make<JS::Identifier>("b")),
  94. make<JS::Identifier>("c")));
  95. program.append<JS::FunctionDeclaration>("foo", move(block));
  96. program.append<JS::CallExpression>("foo");
  97. }
  98. #endif
  99. void build_program_3(JS::Program& program)
  100. {
  101. // function foo() {
  102. // var x = {};
  103. // $gc();
  104. // }
  105. // foo();
  106. auto block = make<JS::BlockStatement>();
  107. block->append<JS::VariableDeclaration>(
  108. make<JS::Identifier>("x"),
  109. make<JS::ObjectExpression>());
  110. block->append<JS::CallExpression>("$gc");
  111. program.append<JS::FunctionDeclaration>("foo", move(block));
  112. program.append<JS::CallExpression>("foo");
  113. }