mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 14:17:36 +00:00
js: Ignore the first line of input if it starts with "#!"
This allows us to create executable programs in JavaScript :^)
This commit is contained in:
parent
3823d13e21
commit
0bc6bcc2ed
1 changed files with 13 additions and 1 deletions
|
@ -57,10 +57,22 @@ int main(int argc, char** argv)
|
||||||
}
|
}
|
||||||
auto file_contents = file->read_all();
|
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;
|
JS::Interpreter interpreter;
|
||||||
interpreter.heap().set_should_collect_on_every_allocation(gc_on_every_allocation);
|
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)
|
if (dump_ast)
|
||||||
program->dump(0);
|
program->dump(0);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue