mirror of
https://github.com/RGBCube/serenity
synced 2025-05-23 18:15:06 +00:00
LibJS: Implement ConsoleObject::count() as a Console::count() wrapper
Also implement ConsoleObject::count_clear() as a wrapper for Console::count_clear()
This commit is contained in:
parent
2e92c2e5e1
commit
e9c7d4524a
3 changed files with 37 additions and 28 deletions
|
@ -24,7 +24,9 @@
|
||||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <AK/String.h>
|
||||||
#include <LibJS/Console.h>
|
#include <LibJS/Console.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
namespace JS {
|
namespace JS {
|
||||||
|
|
||||||
|
@ -33,4 +35,29 @@ Console::Console(Interpreter& interpreter)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsigned Console::count(String label)
|
||||||
|
{
|
||||||
|
auto counter_value = m_counters.get(label);
|
||||||
|
if (!counter_value.has_value()) {
|
||||||
|
printf("%s: 1\n", label.characters());
|
||||||
|
m_counters.set(label, 1);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto new_counter_value = counter_value.value() + 1;
|
||||||
|
printf("%s: %d\n", label.characters(), new_counter_value);
|
||||||
|
m_counters.set(label, new_counter_value);
|
||||||
|
return new_counter_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Console::count_reset(String label)
|
||||||
|
{
|
||||||
|
if (!m_counters.contains(label))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
m_counters.remove(label);
|
||||||
|
printf("%s: 0\n", label.characters());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,6 +43,9 @@ public:
|
||||||
|
|
||||||
HashMap<String, unsigned>& counters() { return m_counters; }
|
HashMap<String, unsigned>& counters() { return m_counters; }
|
||||||
|
|
||||||
|
unsigned count(String label = "default");
|
||||||
|
bool count_reset(String label = "default");
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Interpreter& m_interpreter;
|
Interpreter& m_interpreter;
|
||||||
|
|
||||||
|
|
|
@ -28,7 +28,7 @@
|
||||||
|
|
||||||
#include <AK/FlyString.h>
|
#include <AK/FlyString.h>
|
||||||
#include <AK/Function.h>
|
#include <AK/Function.h>
|
||||||
#include <AK/HashMap.h>
|
#include <LibJS/Console.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>
|
||||||
|
@ -115,40 +115,19 @@ Value ConsoleObject::trace(Interpreter& interpreter)
|
||||||
|
|
||||||
Value ConsoleObject::count(Interpreter& interpreter)
|
Value ConsoleObject::count(Interpreter& interpreter)
|
||||||
{
|
{
|
||||||
String counter_name;
|
if (interpreter.argument_count())
|
||||||
if (!interpreter.argument_count())
|
interpreter.console().count(interpreter.argument(0).to_string());
|
||||||
counter_name = "default";
|
|
||||||
else
|
else
|
||||||
counter_name = interpreter.argument(0).to_string();
|
interpreter.console().count();
|
||||||
|
|
||||||
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();
|
return js_undefined();
|
||||||
}
|
}
|
||||||
|
|
||||||
Value ConsoleObject::count_reset(Interpreter& interpreter)
|
Value ConsoleObject::count_reset(Interpreter& interpreter)
|
||||||
{
|
{
|
||||||
String counter_name;
|
if (interpreter.argument_count())
|
||||||
if (!interpreter.argument_count())
|
interpreter.console().count_reset(interpreter.argument(0).to_string());
|
||||||
counter_name = "default";
|
|
||||||
else
|
else
|
||||||
counter_name = interpreter.argument(0).to_string();
|
interpreter.console().count_reset();
|
||||||
|
|
||||||
auto& counters = interpreter.console().counters();
|
|
||||||
|
|
||||||
if (counters.contains(counter_name)) {
|
|
||||||
counters.remove(counter_name);
|
|
||||||
printf("%s: 0\n", counter_name.characters());
|
|
||||||
}
|
|
||||||
|
|
||||||
return js_undefined();
|
return js_undefined();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue