mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 04:27:44 +00:00
js: Customise the behaviour of JS::Console with ReplConsoleClient
This also makes our JavaScript tests not fail.
This commit is contained in:
parent
e91ab0cb02
commit
73a7a589c2
1 changed files with 81 additions and 0 deletions
|
@ -30,6 +30,7 @@
|
||||||
#include <LibCore/ArgsParser.h>
|
#include <LibCore/ArgsParser.h>
|
||||||
#include <LibCore/File.h>
|
#include <LibCore/File.h>
|
||||||
#include <LibJS/AST.h>
|
#include <LibJS/AST.h>
|
||||||
|
#include <LibJS/Console.h>
|
||||||
#include <LibJS/Interpreter.h>
|
#include <LibJS/Interpreter.h>
|
||||||
#include <LibJS/Parser.h>
|
#include <LibJS/Parser.h>
|
||||||
#include <LibJS/Runtime/Array.h>
|
#include <LibJS/Runtime/Array.h>
|
||||||
|
@ -388,6 +389,82 @@ void sigint_handler()
|
||||||
interrupt_interpreter();
|
interrupt_interpreter();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class ReplConsoleClient final : public JS::ConsoleClient {
|
||||||
|
public:
|
||||||
|
ReplConsoleClient(JS::Console& console)
|
||||||
|
: ConsoleClient(console)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual JS::Value log() override
|
||||||
|
{
|
||||||
|
puts(interpreter().join_arguments().characters());
|
||||||
|
return JS::js_undefined();
|
||||||
|
}
|
||||||
|
virtual JS::Value info() override
|
||||||
|
{
|
||||||
|
printf("(i) %s\n", interpreter().join_arguments().characters());
|
||||||
|
return JS::js_undefined();
|
||||||
|
}
|
||||||
|
virtual JS::Value debug() override
|
||||||
|
{
|
||||||
|
printf("\033[36;1m");
|
||||||
|
puts(interpreter().join_arguments().characters());
|
||||||
|
printf("\033[0m");
|
||||||
|
return JS::js_undefined();
|
||||||
|
}
|
||||||
|
virtual JS::Value warn() override
|
||||||
|
{
|
||||||
|
printf("\033[33;1m");
|
||||||
|
puts(interpreter().join_arguments().characters());
|
||||||
|
printf("\033[0m");
|
||||||
|
return JS::js_undefined();
|
||||||
|
}
|
||||||
|
virtual JS::Value error() override
|
||||||
|
{
|
||||||
|
printf("\033[31;1m");
|
||||||
|
puts(interpreter().join_arguments().characters());
|
||||||
|
printf("\033[0m");
|
||||||
|
return JS::js_undefined();
|
||||||
|
}
|
||||||
|
virtual JS::Value clear() override
|
||||||
|
{
|
||||||
|
printf("\033[3J\033[H\033[2J");
|
||||||
|
fflush(stdout);
|
||||||
|
return JS::js_undefined();
|
||||||
|
}
|
||||||
|
virtual JS::Value trace() override
|
||||||
|
{
|
||||||
|
puts(interpreter().join_arguments().characters());
|
||||||
|
auto trace = interpreter().get_trace();
|
||||||
|
for (auto function_name : trace) {
|
||||||
|
if (String(function_name).is_empty())
|
||||||
|
function_name = "<anonymous>";
|
||||||
|
printf(" -> %s\n", function_name.characters());
|
||||||
|
}
|
||||||
|
return JS::js_undefined();
|
||||||
|
}
|
||||||
|
virtual JS::Value count() override
|
||||||
|
{
|
||||||
|
auto label = interpreter().argument_count() ? interpreter().argument(0).to_string() : "default";
|
||||||
|
auto counter_value = m_console.counter_increment(label);
|
||||||
|
printf("%s: %u\n", label.characters(), counter_value);
|
||||||
|
return JS::js_undefined();
|
||||||
|
}
|
||||||
|
virtual JS::Value count_reset() override
|
||||||
|
{
|
||||||
|
auto label = interpreter().argument_count() ? interpreter().argument(0).to_string() : "default";
|
||||||
|
if (m_console.counter_reset(label)) {
|
||||||
|
printf("%s: 0\n", label.characters());
|
||||||
|
} else {
|
||||||
|
printf("\033[33;1m");
|
||||||
|
printf("\"%s\" doesn't have a count\n", label.characters());
|
||||||
|
printf("\033[0m");
|
||||||
|
}
|
||||||
|
return JS::js_undefined();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
bool gc_on_every_allocation = false;
|
bool gc_on_every_allocation = false;
|
||||||
|
@ -415,6 +492,8 @@ int main(int argc, char** argv)
|
||||||
|
|
||||||
if (script_path == nullptr) {
|
if (script_path == nullptr) {
|
||||||
interpreter = JS::Interpreter::create<ReplObject>();
|
interpreter = JS::Interpreter::create<ReplObject>();
|
||||||
|
ReplConsoleClient console_client(interpreter->console());
|
||||||
|
interpreter->console().set_client(console_client);
|
||||||
interpreter->heap().set_should_collect_on_every_allocation(gc_on_every_allocation);
|
interpreter->heap().set_should_collect_on_every_allocation(gc_on_every_allocation);
|
||||||
if (test_mode)
|
if (test_mode)
|
||||||
enable_test_mode(*interpreter);
|
enable_test_mode(*interpreter);
|
||||||
|
@ -639,6 +718,8 @@ int main(int argc, char** argv)
|
||||||
repl(*interpreter);
|
repl(*interpreter);
|
||||||
} else {
|
} else {
|
||||||
interpreter = JS::Interpreter::create<JS::GlobalObject>();
|
interpreter = JS::Interpreter::create<JS::GlobalObject>();
|
||||||
|
ReplConsoleClient console_client(interpreter->console());
|
||||||
|
interpreter->console().set_client(console_client);
|
||||||
interpreter->heap().set_should_collect_on_every_allocation(gc_on_every_allocation);
|
interpreter->heap().set_should_collect_on_every_allocation(gc_on_every_allocation);
|
||||||
if (test_mode)
|
if (test_mode)
|
||||||
enable_test_mode(*interpreter);
|
enable_test_mode(*interpreter);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue