js.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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/ByteBuffer.h>
  27. #include <AK/NonnullOwnPtr.h>
  28. #include <LibCore/ArgsParser.h>
  29. #include <LibCore/File.h>
  30. #include <LibJS/AST.h>
  31. #include <LibJS/Interpreter.h>
  32. #include <LibJS/Parser.h>
  33. #include <LibJS/Runtime/Object.h>
  34. #include <LibJS/Runtime/PrimitiveString.h>
  35. #include <LibJS/Runtime/Value.h>
  36. #include <stdio.h>
  37. #define PROGRAM 6
  38. int main(int argc, char** argv)
  39. {
  40. bool dump_ast = false;
  41. bool gc_on_every_allocation = false;
  42. const char* script_path = nullptr;
  43. Core::ArgsParser args_parser;
  44. args_parser.add_option(dump_ast, "Dump the AST", "ast-dump", 'A');
  45. args_parser.add_option(gc_on_every_allocation, "GC on every allocation", "gc-on-every-allocation", 'g');
  46. args_parser.add_positional_argument(script_path, "Path to script file", "script");
  47. args_parser.parse(argc, argv);
  48. auto file = Core::File::construct(script_path);
  49. if (!file->open(Core::IODevice::ReadOnly)) {
  50. fprintf(stderr, "Failed to open %s: %s\n", script_path, file->error_string());
  51. return 1;
  52. }
  53. auto file_contents = file->read_all();
  54. StringView source;
  55. if (file_contents.size() >= 2 && file_contents[0] == '#' && file_contents[1] == '!') {
  56. size_t i = 0;
  57. for (i = 2; i < file_contents.size(); ++i) {
  58. if (file_contents[i] == '\n')
  59. break;
  60. }
  61. source = StringView((const char*)file_contents.data() + i, file_contents.size() - i);
  62. } else {
  63. source = file_contents;
  64. }
  65. JS::Interpreter interpreter;
  66. interpreter.heap().set_should_collect_on_every_allocation(gc_on_every_allocation);
  67. auto program = JS::Parser(JS::Lexer(source)).parse_program();
  68. if (dump_ast)
  69. program->dump(0);
  70. auto result = interpreter.run(*program);
  71. dbg() << "Interpreter returned " << result;
  72. printf("%s\n", result.to_string().characters());
  73. dbg() << "Collecting garbage on exit...";
  74. interpreter.heap().collect_garbage();
  75. return 0;
  76. }