mirror of
https://github.com/RGBCube/serenity
synced 2025-06-01 08:58:11 +00:00
LibJS: Implement console.count()
This commit is contained in:
parent
28ef654d13
commit
8c60ba1e42
3 changed files with 29 additions and 0 deletions
|
@ -162,6 +162,8 @@ public:
|
||||||
|
|
||||||
Value last_value() const { return m_last_value; }
|
Value last_value() const { return m_last_value; }
|
||||||
|
|
||||||
|
HashMap<String, unsigned>& console_counters() { return m_console_counters; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Interpreter();
|
Interpreter();
|
||||||
|
|
||||||
|
@ -177,6 +179,8 @@ private:
|
||||||
Exception* m_exception { nullptr };
|
Exception* m_exception { nullptr };
|
||||||
|
|
||||||
ScopeType m_unwind_until { ScopeType::None };
|
ScopeType m_unwind_until { ScopeType::None };
|
||||||
|
|
||||||
|
HashMap<String, unsigned> m_console_counters;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
||||||
* Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
|
* Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
|
||||||
|
* Copyright (c) 2020, Emanuele Torre <torreemanuele6@gmail.com>
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
@ -27,6 +28,7 @@
|
||||||
|
|
||||||
#include <AK/FlyString.h>
|
#include <AK/FlyString.h>
|
||||||
#include <AK/Function.h>
|
#include <AK/Function.h>
|
||||||
|
#include <AK/HashMap.h>
|
||||||
#include <LibJS/Interpreter.h>
|
#include <LibJS/Interpreter.h>
|
||||||
#include <LibJS/Runtime/ConsoleObject.h>
|
#include <LibJS/Runtime/ConsoleObject.h>
|
||||||
#include <LibJS/Runtime/GlobalObject.h>
|
#include <LibJS/Runtime/GlobalObject.h>
|
||||||
|
@ -53,6 +55,7 @@ ConsoleObject::ConsoleObject()
|
||||||
put_native_function("warn", warn);
|
put_native_function("warn", warn);
|
||||||
put_native_function("error", error);
|
put_native_function("error", error);
|
||||||
put_native_function("trace", trace);
|
put_native_function("trace", trace);
|
||||||
|
put_native_function("count", count);
|
||||||
}
|
}
|
||||||
|
|
||||||
ConsoleObject::~ConsoleObject()
|
ConsoleObject::~ConsoleObject()
|
||||||
|
@ -109,4 +112,25 @@ Value ConsoleObject::trace(Interpreter& interpreter)
|
||||||
return js_undefined();
|
return js_undefined();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Value ConsoleObject::count(Interpreter& interpreter)
|
||||||
|
{
|
||||||
|
String counter_name;
|
||||||
|
if (!interpreter.argument_count())
|
||||||
|
counter_name = "default";
|
||||||
|
else
|
||||||
|
counter_name = interpreter.argument(0).to_string();
|
||||||
|
|
||||||
|
auto& counters = interpreter.console_counters();
|
||||||
|
auto counter_value = counters.get(counter_name);
|
||||||
|
|
||||||
|
if (counter_value.has_value()) {
|
||||||
|
printf("%s: %d\n", counter_name.characters(), counter_value.value() + 1);
|
||||||
|
counters.set(counter_name, counter_value.value() + 1);
|
||||||
|
} else {
|
||||||
|
printf("%s: 1\n", counter_name.characters());
|
||||||
|
counters.set(counter_name, 1);
|
||||||
|
}
|
||||||
|
return js_undefined();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,6 +44,7 @@ private:
|
||||||
static Value warn(Interpreter&);
|
static Value warn(Interpreter&);
|
||||||
static Value error(Interpreter&);
|
static Value error(Interpreter&);
|
||||||
static Value trace(Interpreter&);
|
static Value trace(Interpreter&);
|
||||||
|
static Value count(Interpreter&);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue