summaryrefslogtreecommitdiff
path: root/Libraries/LibJS
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2020-04-11 12:57:13 +0100
committerAndreas Kling <kling@serenityos.org>2020-04-11 14:10:42 +0200
commit3b21c4aa567f452ac501de7e917c7ebb5559c396 (patch)
treef40eb52c88c7f150eee0ea5a337d48a0488f1ae3 /Libraries/LibJS
parenteece424694951e6cb3cd8a82315e8523c3a4b30e (diff)
downloadserenity-3b21c4aa567f452ac501de7e917c7ebb5559c396.zip
LibJS: Add console.trace()
Diffstat (limited to 'Libraries/LibJS')
-rw-r--r--Libraries/LibJS/Interpreter.h1
-rw-r--r--Libraries/LibJS/Runtime/ConsoleObject.cpp15
-rw-r--r--Libraries/LibJS/Runtime/ConsoleObject.h1
3 files changed, 17 insertions, 0 deletions
diff --git a/Libraries/LibJS/Interpreter.h b/Libraries/LibJS/Interpreter.h
index 75dc635f12..c5427fe135 100644
--- a/Libraries/LibJS/Interpreter.h
+++ b/Libraries/LibJS/Interpreter.h
@@ -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
{
diff --git a/Libraries/LibJS/Runtime/ConsoleObject.cpp b/Libraries/LibJS/Runtime/ConsoleObject.cpp
index 02c8f6fc67..977b02a87d 100644
--- a/Libraries/LibJS/Runtime/ConsoleObject.cpp
+++ b/Libraries/LibJS/Runtime/ConsoleObject.cpp
@@ -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();
+}
+
}
diff --git a/Libraries/LibJS/Runtime/ConsoleObject.h b/Libraries/LibJS/Runtime/ConsoleObject.h
index f0959fd520..d1824a2d58 100644
--- a/Libraries/LibJS/Runtime/ConsoleObject.h
+++ b/Libraries/LibJS/Runtime/ConsoleObject.h
@@ -39,6 +39,7 @@ private:
virtual const char* class_name() const override { return "ConsoleObject"; }
static Value log(Interpreter&);
+ static Value trace(Interpreter&);
};
}