mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 14:57:35 +00:00
LibJS: Implement console.assert()
This commit is contained in:
parent
e37421bddc
commit
8d490aba76
12 changed files with 89 additions and 9 deletions
|
@ -124,4 +124,25 @@ JS::Value BrowserConsoleClient::count_reset()
|
|||
return JS::js_undefined();
|
||||
}
|
||||
|
||||
JS::Value BrowserConsoleClient::assert_()
|
||||
{
|
||||
auto& vm = this->vm();
|
||||
if (!vm.argument(0).to_boolean()) {
|
||||
StringBuilder html;
|
||||
if (vm.argument_count() > 1) {
|
||||
html.append("<span class=\"error\">");
|
||||
html.append("Assertion failed:");
|
||||
html.append("</span>");
|
||||
html.append(" ");
|
||||
html.append(escape_html_entities(vm.join_arguments(1)));
|
||||
} else {
|
||||
html.append("<span class=\"error\">");
|
||||
html.append("Assertion failed");
|
||||
html.append("</span>");
|
||||
}
|
||||
m_console_widget.print_html(html.string_view());
|
||||
}
|
||||
return JS::js_undefined();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -53,6 +53,7 @@ private:
|
|||
virtual JS::Value trace() override;
|
||||
virtual JS::Value count() override;
|
||||
virtual JS::Value count_reset() override;
|
||||
virtual JS::Value assert_() override;
|
||||
|
||||
ConsoleWidget& m_console_widget;
|
||||
};
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Emanuele Torre <torreemanuele6@gmail.com>
|
||||
* Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
|
||||
* Copyright (c) 2020-2021, Linus Groh <mail@linusgroh.de>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -118,6 +118,13 @@ Value Console::count_reset()
|
|||
return js_undefined();
|
||||
}
|
||||
|
||||
Value Console::assert_()
|
||||
{
|
||||
if (m_client)
|
||||
return m_client->assert_();
|
||||
return js_undefined();
|
||||
}
|
||||
|
||||
unsigned Console::counter_increment(String label)
|
||||
{
|
||||
auto value = m_counters.get(label);
|
||||
|
|
|
@ -57,13 +57,11 @@ public:
|
|||
Value info();
|
||||
Value log();
|
||||
Value warn();
|
||||
|
||||
Value clear();
|
||||
|
||||
Value trace();
|
||||
|
||||
Value count();
|
||||
Value count_reset();
|
||||
Value assert_();
|
||||
|
||||
unsigned counter_increment(String label);
|
||||
bool counter_reset(String label);
|
||||
|
@ -77,7 +75,7 @@ private:
|
|||
|
||||
class ConsoleClient {
|
||||
public:
|
||||
ConsoleClient(Console& console)
|
||||
explicit ConsoleClient(Console& console)
|
||||
: m_console(console)
|
||||
{
|
||||
}
|
||||
|
@ -91,6 +89,7 @@ public:
|
|||
virtual Value trace() = 0;
|
||||
virtual Value count() = 0;
|
||||
virtual Value count_reset() = 0;
|
||||
virtual Value assert_() = 0;
|
||||
|
||||
protected:
|
||||
virtual ~ConsoleClient() = default;
|
||||
|
|
|
@ -71,6 +71,7 @@ namespace JS {
|
|||
P(asUintN) \
|
||||
P(asin) \
|
||||
P(asinh) \
|
||||
P(assert) \
|
||||
P(at) \
|
||||
P(atan) \
|
||||
P(atan2) \
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
||||
* Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
|
||||
* Copyright (c) 2020-2021, Linus Groh <mail@linusgroh.de>
|
||||
* Copyright (c) 2020, Emanuele Torre <torreemanuele6@gmail.com>
|
||||
* All rights reserved.
|
||||
*
|
||||
|
@ -50,6 +50,7 @@ void ConsoleObject::initialize(GlobalObject& global_object)
|
|||
define_native_function(vm.names.count, count);
|
||||
define_native_function(vm.names.countReset, count_reset);
|
||||
define_native_function(vm.names.clear, clear);
|
||||
define_native_function(vm.names.assert, assert_);
|
||||
}
|
||||
|
||||
ConsoleObject::~ConsoleObject()
|
||||
|
@ -101,4 +102,9 @@ JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::clear)
|
|||
return global_object.console().clear();
|
||||
}
|
||||
|
||||
JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::assert_)
|
||||
{
|
||||
return global_object.console().assert_();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -48,6 +48,7 @@ private:
|
|||
JS_DECLARE_NATIVE_FUNCTION(count);
|
||||
JS_DECLARE_NATIVE_FUNCTION(count_reset);
|
||||
JS_DECLARE_NATIVE_FUNCTION(clear);
|
||||
JS_DECLARE_NATIVE_FUNCTION(assert_);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -321,10 +321,10 @@ void VM::throw_exception(Exception& exception)
|
|||
unwind(ScopeType::Try);
|
||||
}
|
||||
|
||||
String VM::join_arguments() const
|
||||
String VM::join_arguments(size_t start_index) const
|
||||
{
|
||||
StringBuilder joined_arguments;
|
||||
for (size_t i = 0; i < argument_count(); ++i) {
|
||||
for (size_t i = start_index; i < argument_count(); ++i) {
|
||||
joined_arguments.append(argument(i).to_string_without_side_effects().characters());
|
||||
if (i != argument_count() - 1)
|
||||
joined_arguments.append(' ');
|
||||
|
|
|
@ -222,7 +222,7 @@ public:
|
|||
|
||||
Value construct(Function&, Function& new_target, Optional<MarkedValueList> arguments, GlobalObject&);
|
||||
|
||||
String join_arguments() const;
|
||||
String join_arguments(size_t start_index = 0) const;
|
||||
|
||||
Value resolve_this_binding(GlobalObject&) const;
|
||||
const ScopeObject* find_this_scope() const;
|
||||
|
|
|
@ -168,4 +168,25 @@ JS::Value WebContentConsoleClient::count_reset()
|
|||
return JS::js_undefined();
|
||||
}
|
||||
|
||||
JS::Value WebContentConsoleClient::assert_()
|
||||
{
|
||||
auto& vm = this->vm();
|
||||
if (!vm.argument(0).to_boolean()) {
|
||||
StringBuilder html;
|
||||
if (vm.argument_count() > 1) {
|
||||
html.append("<span class=\"error\">");
|
||||
html.append("Assertion failed:");
|
||||
html.append("</span>");
|
||||
html.append(" ");
|
||||
html.append(escape_html_entities(vm.join_arguments(1)));
|
||||
} else {
|
||||
html.append("<span class=\"error\">");
|
||||
html.append("Assertion failed");
|
||||
html.append("</span>");
|
||||
}
|
||||
print_html(html.string_view());
|
||||
}
|
||||
return JS::js_undefined();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -56,6 +56,7 @@ private:
|
|||
virtual JS::Value trace() override;
|
||||
virtual JS::Value count() override;
|
||||
virtual JS::Value count_reset() override;
|
||||
virtual JS::Value assert_() override;
|
||||
|
||||
ClientConnection& m_client;
|
||||
WeakPtr<JS::Interpreter> m_interpreter;
|
||||
|
|
|
@ -650,32 +650,38 @@ public:
|
|||
outln("{}", vm().join_arguments());
|
||||
return JS::js_undefined();
|
||||
}
|
||||
|
||||
virtual JS::Value info() override
|
||||
{
|
||||
outln("(i) {}", vm().join_arguments());
|
||||
return JS::js_undefined();
|
||||
}
|
||||
|
||||
virtual JS::Value debug() override
|
||||
{
|
||||
outln("\033[36;1m{}\033[0m", vm().join_arguments());
|
||||
return JS::js_undefined();
|
||||
}
|
||||
|
||||
virtual JS::Value warn() override
|
||||
{
|
||||
outln("\033[33;1m{}\033[0m", vm().join_arguments());
|
||||
return JS::js_undefined();
|
||||
}
|
||||
|
||||
virtual JS::Value error() override
|
||||
{
|
||||
outln("\033[31;1m{}\033[0m", vm().join_arguments());
|
||||
return JS::js_undefined();
|
||||
}
|
||||
|
||||
virtual JS::Value clear() override
|
||||
{
|
||||
out("\033[3J\033[H\033[2J");
|
||||
fflush(stdout);
|
||||
return JS::js_undefined();
|
||||
}
|
||||
|
||||
virtual JS::Value trace() override
|
||||
{
|
||||
outln("{}", vm().join_arguments());
|
||||
|
@ -687,6 +693,7 @@ public:
|
|||
}
|
||||
return JS::js_undefined();
|
||||
}
|
||||
|
||||
virtual JS::Value count() override
|
||||
{
|
||||
auto label = vm().argument_count() ? vm().argument(0).to_string_without_side_effects() : "default";
|
||||
|
@ -694,6 +701,7 @@ public:
|
|||
outln("{}: {}", label, counter_value);
|
||||
return JS::js_undefined();
|
||||
}
|
||||
|
||||
virtual JS::Value count_reset() override
|
||||
{
|
||||
auto label = vm().argument_count() ? vm().argument(0).to_string_without_side_effects() : "default";
|
||||
|
@ -703,6 +711,20 @@ public:
|
|||
outln("\033[33;1m\"{}\" doesn't have a count\033[0m", label);
|
||||
return JS::js_undefined();
|
||||
}
|
||||
|
||||
virtual JS::Value assert_() override
|
||||
{
|
||||
auto& vm = this->vm();
|
||||
if (!vm.argument(0).to_boolean()) {
|
||||
if (vm.argument_count() > 1) {
|
||||
out("\033[31;1mAssertion failed:\033[0m");
|
||||
outln(" {}", vm.join_arguments(1));
|
||||
} else {
|
||||
outln("\033[31;1mAssertion failed\033[0m");
|
||||
}
|
||||
}
|
||||
return JS::js_undefined();
|
||||
}
|
||||
};
|
||||
|
||||
int main(int argc, char** argv)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue