summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-03-20 14:49:17 +0100
committerAndreas Kling <kling@serenityos.org>2020-03-20 14:49:17 +0100
commit0bc6bcc2ed3f64bd8fc4a29dcea2f602f75bfb9b (patch)
tree893bb8d892eead1a2e65a8dcb09d82f73fc67704
parent3823d13e2131c6174609811c43d212b2b72c339b (diff)
downloadserenity-0bc6bcc2ed3f64bd8fc4a29dcea2f602f75bfb9b.zip
js: Ignore the first line of input if it starts with "#!"
This allows us to create executable programs in JavaScript :^)
-rw-r--r--Userland/js.cpp14
1 files changed, 13 insertions, 1 deletions
diff --git a/Userland/js.cpp b/Userland/js.cpp
index 9cc0f6ab32..baf54a037f 100644
--- a/Userland/js.cpp
+++ b/Userland/js.cpp
@@ -57,10 +57,22 @@ int main(int argc, char** argv)
}
auto file_contents = file->read_all();
+ StringView source;
+ if (file_contents.size() >= 2 && file_contents[0] == '#' && file_contents[1] == '!') {
+ size_t i = 0;
+ for (i = 2; i < file_contents.size(); ++i) {
+ if (file_contents[i] == '\n')
+ break;
+ }
+ source = StringView((const char*)file_contents.data() + i, file_contents.size() - i);
+ } else {
+ source = file_contents;
+ }
+
JS::Interpreter interpreter;
interpreter.heap().set_should_collect_on_every_allocation(gc_on_every_allocation);
- auto program = JS::Parser(JS::Lexer(file_contents)).parse_program();
+ auto program = JS::Parser(JS::Lexer(source)).parse_program();
if (dump_ast)
program->dump(0);