mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-05 15:49:11 +00:00
Utilities: Port js.exe on windows without repl
This commit allows building js.cpp on windows. The repl functionality is ifdef'ed out. To decrease the number of ifdefs the code that runs the repl on Linux was moved into one place. Some globals that are unused as a result of that are markes maybe_unused. The following commit enables building and testing js in cmake for windows.
This commit is contained in:
parent
550aace91c
commit
b0cc87c276
Notes:
github-actions[bot]
2025-05-29 09:27:33 +00:00
Author: https://github.com/R-Goc
Commit: b0cc87c276
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/4774
Reviewed-by: https://github.com/ADKaster ✅
Reviewed-by: https://github.com/trflynn89
1 changed files with 337 additions and 316 deletions
295
Utilities/js.cpp
295
Utilities/js.cpp
|
@ -2,12 +2,14 @@
|
|||
* Copyright (c) 2020-2023, Andreas Kling <andreas@ladybird.org>
|
||||
* Copyright (c) 2020-2023, Linus Groh <linusg@serenityos.org>
|
||||
* Copyright (c) 2020-2022, Ali Mohammad Pur <mpfard@serenityos.org>
|
||||
* Copyright (c) 2025, Ryszard Goc <ryszardgoc@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/JsonValue.h>
|
||||
#include <AK/NeverDestroyed.h>
|
||||
#include <AK/Platform.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <LibCore/ArgsParser.h>
|
||||
#include <LibCore/ConfigFile.h>
|
||||
|
@ -26,11 +28,14 @@
|
|||
#include <LibJS/Runtime/StringPrototype.h>
|
||||
#include <LibJS/Runtime/ValueInlines.h>
|
||||
#include <LibJS/SourceTextModule.h>
|
||||
#include <LibLine/Editor.h>
|
||||
#include <LibMain/Main.h>
|
||||
#include <LibTextCodec/Decoder.h>
|
||||
#include <signal.h>
|
||||
|
||||
#if !defined(AK_OS_WINDOWS)
|
||||
# include <LibLine/Editor.h>
|
||||
#endif
|
||||
|
||||
// FIXME: https://github.com/LadybirdBrowser/ladybird/issues/2412
|
||||
// We should be able to destroy the VM on process exit.
|
||||
NeverDestroyed<RefPtr<JS::VM>> g_vm_storage;
|
||||
|
@ -81,9 +86,11 @@ static bool s_as_module = false;
|
|||
static bool s_print_last_result = false;
|
||||
static bool s_strip_ansi = false;
|
||||
static bool s_disable_source_location_hints = false;
|
||||
#if !defined(AK_OS_WINDOWS)
|
||||
static RefPtr<Line::Editor> s_editor;
|
||||
#endif
|
||||
static String s_history_path = String {};
|
||||
static int s_repl_line_level = 0;
|
||||
[[maybe_unused]] static int s_repl_line_level = 0;
|
||||
static bool s_keep_running_repl = true;
|
||||
static int s_exit_code = 0;
|
||||
|
||||
|
@ -105,7 +112,7 @@ static ErrorOr<void> print(JS::Value value, PrintTarget target = PrintTarget::St
|
|||
}
|
||||
|
||||
static size_t s_ctrl_c_hit_count = 0;
|
||||
static ErrorOr<String> prompt_for_level(int level)
|
||||
[[maybe_unused]] static ErrorOr<String> prompt_for_level(int level)
|
||||
{
|
||||
static StringBuilder prompt_builder;
|
||||
prompt_builder.clear();
|
||||
|
@ -119,79 +126,6 @@ static ErrorOr<String> prompt_for_level(int level)
|
|||
return prompt_builder.to_string();
|
||||
}
|
||||
|
||||
static ErrorOr<String> read_next_piece()
|
||||
{
|
||||
StringBuilder piece;
|
||||
|
||||
auto line_level_delta_for_next_line { 0 };
|
||||
|
||||
do {
|
||||
auto line_result = s_editor->get_line(TRY(prompt_for_level(s_repl_line_level)).to_byte_string());
|
||||
|
||||
s_ctrl_c_hit_count = 0;
|
||||
line_level_delta_for_next_line = 0;
|
||||
|
||||
if (line_result.is_error()) {
|
||||
s_keep_running_repl = false;
|
||||
return String {};
|
||||
}
|
||||
|
||||
auto& line = line_result.value();
|
||||
s_editor->add_to_history(line);
|
||||
|
||||
piece.append(line);
|
||||
piece.append('\n');
|
||||
auto lexer = JS::Lexer(line);
|
||||
|
||||
enum {
|
||||
NotInLabelOrObjectKey,
|
||||
InLabelOrObjectKeyIdentifier,
|
||||
InLabelOrObjectKey
|
||||
} label_state { NotInLabelOrObjectKey };
|
||||
|
||||
for (JS::Token token = lexer.next(); token.type() != JS::TokenType::Eof; token = lexer.next()) {
|
||||
switch (token.type()) {
|
||||
case JS::TokenType::BracketOpen:
|
||||
case JS::TokenType::CurlyOpen:
|
||||
case JS::TokenType::ParenOpen:
|
||||
label_state = NotInLabelOrObjectKey;
|
||||
s_repl_line_level++;
|
||||
break;
|
||||
case JS::TokenType::BracketClose:
|
||||
case JS::TokenType::CurlyClose:
|
||||
case JS::TokenType::ParenClose:
|
||||
label_state = NotInLabelOrObjectKey;
|
||||
s_repl_line_level--;
|
||||
break;
|
||||
|
||||
case JS::TokenType::Identifier:
|
||||
case JS::TokenType::StringLiteral:
|
||||
if (label_state == NotInLabelOrObjectKey)
|
||||
label_state = InLabelOrObjectKeyIdentifier;
|
||||
else
|
||||
label_state = NotInLabelOrObjectKey;
|
||||
break;
|
||||
case JS::TokenType::Colon:
|
||||
if (label_state == InLabelOrObjectKeyIdentifier)
|
||||
label_state = InLabelOrObjectKey;
|
||||
else
|
||||
label_state = NotInLabelOrObjectKey;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (label_state == InLabelOrObjectKey) {
|
||||
// If there's a label or object literal key at the end of this line,
|
||||
// prompt for more lines but do not change the line level.
|
||||
line_level_delta_for_next_line += 1;
|
||||
}
|
||||
} while (s_repl_line_level + line_level_delta_for_next_line > 0);
|
||||
|
||||
return piece.to_string();
|
||||
}
|
||||
|
||||
static ErrorOr<void> write_to_file(String const& path)
|
||||
{
|
||||
auto file = TRY(Core::File::open(path, Core::File::OpenMode::Write, 0666));
|
||||
|
@ -437,19 +371,6 @@ JS_DEFINE_NATIVE_FUNCTION(ScriptObject::print)
|
|||
return JS::js_undefined();
|
||||
}
|
||||
|
||||
static ErrorOr<void> repl(JS::Realm& realm)
|
||||
{
|
||||
while (s_keep_running_repl) {
|
||||
auto const piece = TRY(read_next_piece());
|
||||
if (Utf8View { piece }.trim(JS::whitespace_characters).is_empty())
|
||||
continue;
|
||||
|
||||
g_repl_statements.append(piece);
|
||||
TRY(parse_and_run(realm, piece, "REPL"sv));
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
class ReplConsoleClient final : public JS::ConsoleClient {
|
||||
GC_CELL(ReplConsoleClient, JS::ConsoleClient);
|
||||
|
||||
|
@ -528,62 +449,95 @@ private:
|
|||
int m_group_stack_depth { 0 };
|
||||
};
|
||||
|
||||
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||
#if !defined(AK_OS_WINDOWS)
|
||||
static ErrorOr<String> read_next_piece()
|
||||
{
|
||||
bool gc_on_every_allocation = false;
|
||||
bool disable_syntax_highlight = false;
|
||||
bool disable_debug_printing = false;
|
||||
bool use_test262_global = false;
|
||||
StringView evaluate_script;
|
||||
Vector<StringView> script_paths;
|
||||
StringBuilder piece;
|
||||
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.set_general_help("This is a JavaScript interpreter.");
|
||||
args_parser.add_option(s_dump_ast, "Dump the AST", "dump-ast", 'A');
|
||||
args_parser.add_option(JS::Bytecode::g_dump_bytecode, "Dump the bytecode", "dump-bytecode", 'd');
|
||||
args_parser.add_option(s_as_module, "Treat as module", "as-module", 'm');
|
||||
args_parser.add_option(s_print_last_result, "Print last result", "print-last-result", 'l');
|
||||
args_parser.add_option(s_strip_ansi, "Disable ANSI colors", "disable-ansi-colors", 'i');
|
||||
args_parser.add_option(s_disable_source_location_hints, "Disable source location hints", "disable-source-location-hints", 'h');
|
||||
args_parser.add_option(gc_on_every_allocation, "GC on every allocation", "gc-on-every-allocation", 'g');
|
||||
args_parser.add_option(disable_syntax_highlight, "Disable live syntax highlighting", "no-syntax-highlight", 's');
|
||||
args_parser.add_option(disable_debug_printing, "Disable debug output", "disable-debug-output", {});
|
||||
args_parser.add_option(evaluate_script, "Evaluate argument as a script", "evaluate", 'c', "script");
|
||||
args_parser.add_option(use_test262_global, "Use test262 global ($262)", "use-test262-global", {});
|
||||
args_parser.add_positional_argument(script_paths, "Path to script files", "scripts", Core::ArgsParser::Required::No);
|
||||
args_parser.parse(arguments);
|
||||
auto line_level_delta_for_next_line { 0 };
|
||||
|
||||
bool syntax_highlight = !disable_syntax_highlight;
|
||||
do {
|
||||
auto line_result = s_editor->get_line(TRY(prompt_for_level(s_repl_line_level)).to_byte_string());
|
||||
|
||||
AK::set_debug_enabled(!disable_debug_printing);
|
||||
s_history_path = TRY(String::formatted("{}/.js-history", Core::StandardPaths::home_directory()));
|
||||
s_ctrl_c_hit_count = 0;
|
||||
line_level_delta_for_next_line = 0;
|
||||
|
||||
g_vm_storage.get() = JS::VM::create();
|
||||
g_vm = g_vm_storage->ptr();
|
||||
g_vm->set_dynamic_imports_allowed(true);
|
||||
|
||||
if (!disable_debug_printing) {
|
||||
// NOTE: These will print out both warnings when using something like Promise.reject().catch(...) -
|
||||
// which is, as far as I can tell, correct - a promise is created, rejected without handler, and a
|
||||
// handler then attached to it. The Node.js REPL doesn't warn in this case, so it's something we
|
||||
// might want to revisit at a later point and disable warnings for promises created this way.
|
||||
g_vm->on_promise_unhandled_rejection = [](auto& promise) {
|
||||
warn("WARNING: A promise was rejected without any handlers");
|
||||
warn(" (result: ");
|
||||
(void)print(promise.result(), PrintTarget::StandardError);
|
||||
warnln(")");
|
||||
};
|
||||
g_vm->on_promise_rejection_handled = [](auto& promise) {
|
||||
warn("WARNING: A handler was added to an already rejected promise");
|
||||
warn(" (result: ");
|
||||
(void)print(promise.result(), PrintTarget::StandardError);
|
||||
warnln(")");
|
||||
};
|
||||
if (line_result.is_error()) {
|
||||
s_keep_running_repl = false;
|
||||
return String {};
|
||||
}
|
||||
|
||||
// FIXME: Figure out some way to interrupt the interpreter now that vm.exception() is gone.
|
||||
auto& line = line_result.value();
|
||||
s_editor->add_to_history(line);
|
||||
|
||||
if (evaluate_script.is_empty() && script_paths.is_empty()) {
|
||||
piece.append(line);
|
||||
piece.append('\n');
|
||||
auto lexer = JS::Lexer(line);
|
||||
|
||||
enum {
|
||||
NotInLabelOrObjectKey,
|
||||
InLabelOrObjectKeyIdentifier,
|
||||
InLabelOrObjectKey
|
||||
} label_state { NotInLabelOrObjectKey };
|
||||
|
||||
for (JS::Token token = lexer.next(); token.type() != JS::TokenType::Eof; token = lexer.next()) {
|
||||
switch (token.type()) {
|
||||
case JS::TokenType::BracketOpen:
|
||||
case JS::TokenType::CurlyOpen:
|
||||
case JS::TokenType::ParenOpen:
|
||||
label_state = NotInLabelOrObjectKey;
|
||||
s_repl_line_level++;
|
||||
break;
|
||||
case JS::TokenType::BracketClose:
|
||||
case JS::TokenType::CurlyClose:
|
||||
case JS::TokenType::ParenClose:
|
||||
label_state = NotInLabelOrObjectKey;
|
||||
s_repl_line_level--;
|
||||
break;
|
||||
|
||||
case JS::TokenType::Identifier:
|
||||
case JS::TokenType::StringLiteral:
|
||||
if (label_state == NotInLabelOrObjectKey)
|
||||
label_state = InLabelOrObjectKeyIdentifier;
|
||||
else
|
||||
label_state = NotInLabelOrObjectKey;
|
||||
break;
|
||||
case JS::TokenType::Colon:
|
||||
if (label_state == InLabelOrObjectKeyIdentifier)
|
||||
label_state = InLabelOrObjectKey;
|
||||
else
|
||||
label_state = NotInLabelOrObjectKey;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (label_state == InLabelOrObjectKey) {
|
||||
// If there's a label or object literal key at the end of this line,
|
||||
// prompt for more lines but do not change the line level.
|
||||
line_level_delta_for_next_line += 1;
|
||||
}
|
||||
} while (s_repl_line_level + line_level_delta_for_next_line > 0);
|
||||
|
||||
return piece.to_string();
|
||||
}
|
||||
|
||||
static ErrorOr<void> repl(JS::Realm& realm)
|
||||
{
|
||||
while (s_keep_running_repl) {
|
||||
auto const piece = TRY(read_next_piece());
|
||||
if (Utf8View { piece }.trim(JS::whitespace_characters).is_empty())
|
||||
continue;
|
||||
|
||||
g_repl_statements.append(piece);
|
||||
TRY(parse_and_run(realm, piece, "REPL"sv));
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
static ErrorOr<int> run_repl(bool gc_on_every_allocation, bool syntax_highlight)
|
||||
{
|
||||
s_print_last_result = true;
|
||||
|
||||
auto root_execution_context = JS::create_simple_execution_context<ReplObject>(*g_vm);
|
||||
|
@ -810,6 +764,73 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
s_editor->on_tab_complete = move(complete);
|
||||
TRY(repl(realm));
|
||||
s_editor->save_history(s_history_path.to_byte_string());
|
||||
return s_exit_code;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||
{
|
||||
bool gc_on_every_allocation = false;
|
||||
bool disable_syntax_highlight = false;
|
||||
bool disable_debug_printing = false;
|
||||
bool use_test262_global = false;
|
||||
StringView evaluate_script;
|
||||
Vector<StringView> script_paths;
|
||||
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.set_general_help("This is a JavaScript interpreter.");
|
||||
args_parser.add_option(s_dump_ast, "Dump the AST", "dump-ast", 'A');
|
||||
args_parser.add_option(JS::Bytecode::g_dump_bytecode, "Dump the bytecode", "dump-bytecode", 'd');
|
||||
args_parser.add_option(s_as_module, "Treat as module", "as-module", 'm');
|
||||
args_parser.add_option(s_print_last_result, "Print last result", "print-last-result", 'l');
|
||||
args_parser.add_option(s_strip_ansi, "Disable ANSI colors", "disable-ansi-colors", 'i');
|
||||
args_parser.add_option(s_disable_source_location_hints, "Disable source location hints", "disable-source-location-hints", 'h');
|
||||
args_parser.add_option(gc_on_every_allocation, "GC on every allocation", "gc-on-every-allocation", 'g');
|
||||
args_parser.add_option(disable_syntax_highlight, "Disable live syntax highlighting", "no-syntax-highlight", 's');
|
||||
args_parser.add_option(disable_debug_printing, "Disable debug output", "disable-debug-output", {});
|
||||
args_parser.add_option(evaluate_script, "Evaluate argument as a script", "evaluate", 'c', "script");
|
||||
args_parser.add_option(use_test262_global, "Use test262 global ($262)", "use-test262-global", {});
|
||||
args_parser.add_positional_argument(script_paths, "Path to script files", "scripts", Core::ArgsParser::Required::No);
|
||||
args_parser.parse(arguments);
|
||||
|
||||
[[maybe_unused]] bool syntax_highlight = !disable_syntax_highlight;
|
||||
|
||||
AK::set_debug_enabled(!disable_debug_printing);
|
||||
s_history_path = TRY(String::formatted("{}/.js-history", Core::StandardPaths::home_directory()));
|
||||
|
||||
g_vm_storage.get() = JS::VM::create();
|
||||
g_vm = g_vm_storage->ptr();
|
||||
g_vm->set_dynamic_imports_allowed(true);
|
||||
|
||||
if (!disable_debug_printing) {
|
||||
// NOTE: These will print out both warnings when using something like Promise.reject().catch(...) -
|
||||
// which is, as far as I can tell, correct - a promise is created, rejected without handler, and a
|
||||
// handler then attached to it. The Node.js REPL doesn't warn in this case, so it's something we
|
||||
// might want to revisit at a later point and disable warnings for promises created this way.
|
||||
g_vm->on_promise_unhandled_rejection = [](auto& promise) {
|
||||
warn("WARNING: A promise was rejected without any handlers");
|
||||
warn(" (result: ");
|
||||
(void)print(promise.result(), PrintTarget::StandardError);
|
||||
warnln(")");
|
||||
};
|
||||
g_vm->on_promise_rejection_handled = [](auto& promise) {
|
||||
warn("WARNING: A handler was added to an already rejected promise");
|
||||
warn(" (result: ");
|
||||
(void)print(promise.result(), PrintTarget::StandardError);
|
||||
warnln(")");
|
||||
};
|
||||
}
|
||||
|
||||
// FIXME: Figure out some way to interrupt the interpreter now that vm.exception() is gone.
|
||||
|
||||
if (evaluate_script.is_empty() && script_paths.is_empty()) {
|
||||
#if defined(AK_OS_WINDOWS)
|
||||
dbgln("REPL functionality is not supported on Windows");
|
||||
VERIFY_NOT_REACHED();
|
||||
#else
|
||||
return run_repl(gc_on_every_allocation, syntax_highlight);
|
||||
#endif
|
||||
} else {
|
||||
OwnPtr<JS::ExecutionContext> root_execution_context;
|
||||
if (use_test262_global)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue