LibJS: Add console.trace()

This commit is contained in:
Linus Groh 2020-04-11 12:57:13 +01:00 committed by Andreas Kling
parent eece424694
commit 3b21c4aa56
Notes: sideshowbarker 2024-07-19 07:43:18 +09:00
3 changed files with 17 additions and 0 deletions

View file

@ -112,6 +112,7 @@ public:
}
void pop_call_frame() { m_call_stack.take_last(); }
const CallFrame& call_frame() { return m_call_stack.last(); }
const Vector<CallFrame> call_stack() { return m_call_stack; }
size_t argument_count() const
{

View file

@ -35,6 +35,7 @@ namespace JS {
ConsoleObject::ConsoleObject()
{
put_native_function("log", log);
put_native_function("trace", trace);
}
ConsoleObject::~ConsoleObject()
@ -52,4 +53,18 @@ Value ConsoleObject::log(Interpreter& interpreter)
return js_undefined();
}
Value ConsoleObject::trace(Interpreter& interpreter)
{
log(interpreter);
auto call_stack = interpreter.call_stack();
// -2 to skip the console.trace() call frame
for (ssize_t i = call_stack.size() - 2; i >= 0; --i) {
auto function_name = call_stack[i].function_name;
if (String(function_name).is_empty())
function_name = "<anonymous>";
printf("%s\n", function_name.characters());
}
return js_undefined();
}
}

View file

@ -39,6 +39,7 @@ private:
virtual const char* class_name() const override { return "ConsoleObject"; }
static Value log(Interpreter&);
static Value trace(Interpreter&);
};
}