1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 06:47:34 +00:00

LibJS: Remove ConsoleMessage from LibJS

We don't need to store the past messages in LibJS.
We'll implement a way to let LibJS users expand the vanilla Console.
This commit is contained in:
Emanuele Torre 2020-05-04 12:02:16 +02:00 committed by Andreas Kling
parent 7dd49047f3
commit 046f9cf115
3 changed files with 20 additions and 84 deletions

View file

@ -37,44 +37,34 @@ Console::Console(Interpreter& interpreter)
{ {
} }
void Console::add_message(ConsoleMessageKind kind, String text)
{
ConsoleMessage message = { kind, text };
m_messages.append(message);
if (on_new_message)
on_new_message(message);
}
void Console::debug(String text) void Console::debug(String text)
{ {
add_message(ConsoleMessageKind::Debug, text); dbg() << "debug: " << text;
} }
void Console::error(String text) void Console::error(String text)
{ {
add_message(ConsoleMessageKind::Error, text); dbg() << "error: " << text;
} }
void Console::info(String text) void Console::info(String text)
{ {
add_message(ConsoleMessageKind::Info, text); dbg() << "info: " << text;
} }
void Console::log(String text) void Console::log(String text)
{ {
add_message(ConsoleMessageKind::Log, text); dbg() << "log: " << text;
} }
void Console::warn(String text) void Console::warn(String text)
{ {
add_message(ConsoleMessageKind::Warn, text); dbg() << "warn: " << text;
} }
void Console::clear() void Console::clear()
{ {
m_messages.clear(); dbg() << "clear:";
add_message(ConsoleMessageKind::Clear, {});
} }
void Console::trace(String title) void Console::trace(String title)
@ -86,38 +76,38 @@ void Console::trace(String title)
// -2 to skip the console.trace() call frame // -2 to skip the console.trace() call frame
for (ssize_t i = call_stack.size() - 2; i >= 0; --i) { for (ssize_t i = call_stack.size() - 2; i >= 0; --i) {
auto function_name = call_stack[i].function_name; auto function_name = call_stack[i].function_name;
message_text.append("\n\t"); message_text.append("\n -> ");
if (String(function_name).is_empty()) if (String(function_name).is_empty())
function_name = "<anonymous>"; function_name = "<anonymous>";
message_text.append(function_name); message_text.append(function_name);
} }
add_message(ConsoleMessageKind::Trace, message_text.build()); dbg() << "log: " << message_text.build();
} }
unsigned Console::count(String label) void Console::count(String label)
{ {
auto counter_value = m_counters.get(label); auto counter_value = m_counters.get(label);
if (!counter_value.has_value()) { if (!counter_value.has_value()) {
add_message(ConsoleMessageKind::Count, String::format("%s: 1", label.characters())); dbg() << "log: " << label << ": 1";
m_counters.set(label, 1); m_counters.set(label, 1);
return 1; return;
} }
auto new_counter_value = counter_value.value() + 1; auto new_counter_value = counter_value.value() + 1;
add_message(ConsoleMessageKind::Count, String::format("%s: %u", label.characters(), new_counter_value)); dbg() << "log: " << label << ": " << new_counter_value;
m_counters.set(label, new_counter_value); m_counters.set(label, new_counter_value);
return new_counter_value;
} }
bool Console::count_reset(String label) void Console::count_reset(String label)
{ {
if (!m_counters.contains(label)) if (m_counters.contains(label)) {
return false; dbg() << "warn: \"" << label << "\" doesn't have a count";
return;
}
m_counters.remove(label); m_counters.remove(label);
add_message(ConsoleMessageKind::Count, String::format("%s: 0", label.characters())); dbg() << "log: " << label << ": 0";
return true;
} }
} }

View file

@ -33,22 +33,6 @@
namespace JS { namespace JS {
enum class ConsoleMessageKind {
Clear,
Count,
Debug,
Error,
Info,
Log,
Trace,
Warn,
};
struct ConsoleMessage {
ConsoleMessageKind kind;
String text;
};
class Console { class Console {
AK_MAKE_NONCOPYABLE(Console); AK_MAKE_NONCOPYABLE(Console);
AK_MAKE_NONMOVABLE(Console); AK_MAKE_NONMOVABLE(Console);
@ -70,17 +54,12 @@ public:
void trace(String title); void trace(String title);
unsigned count(String label = "default"); void count(String label = "default");
bool count_reset(String label = "default"); void count_reset(String label = "default");
void add_message(ConsoleMessageKind, String text);
AK::Function<void(ConsoleMessage&)> on_new_message;
private: private:
Interpreter& m_interpreter; Interpreter& m_interpreter;
Vector<ConsoleMessage> m_messages;
HashMap<String, unsigned> m_counters; HashMap<String, unsigned> m_counters;
}; };

View file

@ -388,37 +388,6 @@ void sigint_handler()
interrupt_interpreter(); interrupt_interpreter();
} }
void console_message_handler(JS::ConsoleMessage& message)
{
switch (message.kind) {
case JS::ConsoleMessageKind::Count:
case JS::ConsoleMessageKind::Log:
case JS::ConsoleMessageKind::Info:
case JS::ConsoleMessageKind::Trace:
puts(message.text.characters());
break;
case JS::ConsoleMessageKind::Debug:
printf("\033[36;1m");
puts(message.text.characters());
printf("\033[0m");
break;
case JS::ConsoleMessageKind::Warn:
printf("\033[33;1m");
puts(message.text.characters());
printf("\033[0m");
break;
case JS::ConsoleMessageKind::Error:
printf("\033[31;1m");
puts(message.text.characters());
printf("\033[0m");
break;
case JS::ConsoleMessageKind::Clear:
printf("\033[3J\033[H\033[2J");
fflush(stdout);
break;
}
};
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
bool gc_on_every_allocation = false; bool gc_on_every_allocation = false;
@ -446,7 +415,6 @@ int main(int argc, char** argv)
if (script_path == nullptr) { if (script_path == nullptr) {
interpreter = JS::Interpreter::create<ReplObject>(); interpreter = JS::Interpreter::create<ReplObject>();
interpreter->console().on_new_message = console_message_handler;
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);
@ -671,7 +639,6 @@ int main(int argc, char** argv)
repl(*interpreter); repl(*interpreter);
} else { } else {
interpreter = JS::Interpreter::create<JS::GlobalObject>(); interpreter = JS::Interpreter::create<JS::GlobalObject>();
interpreter->console().on_new_message = console_message_handler;
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);