From eece424694951e6cb3cd8a82315e8523c3a4b30e Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sat, 11 Apr 2020 12:56:20 +0100 Subject: [PATCH] LibJS: Make Function and CallFrame aware of their function name --- Libraries/LibJS/AST.cpp | 5 ++-- Libraries/LibJS/AST.h | 4 +-- Libraries/LibJS/Interpreter.cpp | 1 + Libraries/LibJS/Interpreter.h | 3 ++- Libraries/LibJS/Runtime/Function.h | 1 + Libraries/LibJS/Runtime/FunctionPrototype.cpp | 26 ++++++++++++------- Libraries/LibJS/Runtime/NativeFunction.cpp | 5 ++-- Libraries/LibJS/Runtime/NativeFunction.h | 4 ++- Libraries/LibJS/Runtime/Object.cpp | 2 +- Libraries/LibJS/Runtime/ScriptFunction.cpp | 5 ++-- Libraries/LibJS/Runtime/ScriptFunction.h | 5 +++- 11 files changed, 39 insertions(+), 22 deletions(-) diff --git a/Libraries/LibJS/AST.cpp b/Libraries/LibJS/AST.cpp index 86f4dc0bc02..427594753ed 100644 --- a/Libraries/LibJS/AST.cpp +++ b/Libraries/LibJS/AST.cpp @@ -48,14 +48,14 @@ Value ScopeNode::execute(Interpreter& interpreter) const Value FunctionDeclaration::execute(Interpreter& interpreter) const { - auto* function = interpreter.heap().allocate(body(), parameters()); + auto* function = interpreter.heap().allocate(name(), body(), parameters()); interpreter.set_variable(name(), function); return js_undefined(); } Value FunctionExpression::execute(Interpreter& interpreter) const { - return interpreter.heap().allocate(body(), parameters()); + return interpreter.heap().allocate(name(), body(), parameters()); } Value ExpressionStatement::execute(Interpreter& interpreter) const @@ -117,6 +117,7 @@ Value CallExpression::execute(Interpreter& interpreter) const } auto& call_frame = interpreter.push_call_frame(); + call_frame.function_name = function.name(); call_frame.arguments = move(arguments); Object* new_object = nullptr; diff --git a/Libraries/LibJS/AST.h b/Libraries/LibJS/AST.h index dc2b398055c..fe4d806e14a 100644 --- a/Libraries/LibJS/AST.h +++ b/Libraries/LibJS/AST.h @@ -163,8 +163,8 @@ class FunctionDeclaration final public: static bool must_have_name() { return true; } - FunctionDeclaration(String name, NonnullRefPtr body, Vector parameters = {}) - : FunctionNode(move(name), move(body), move(parameters)) + FunctionDeclaration(const FlyString& name, NonnullRefPtr body, Vector parameters = {}) + : FunctionNode(name, move(body), move(parameters)) { } diff --git a/Libraries/LibJS/Interpreter.cpp b/Libraries/LibJS/Interpreter.cpp index 3d78095e081..5bd91139e06 100644 --- a/Libraries/LibJS/Interpreter.cpp +++ b/Libraries/LibJS/Interpreter.cpp @@ -205,6 +205,7 @@ void Interpreter::gather_roots(Badge, HashTable& roots) Value Interpreter::call(Function* function, Value this_value, const Vector& arguments) { auto& call_frame = push_call_frame(); + call_frame.function_name = function->name(); call_frame.this_value = this_value; call_frame.arguments = arguments; auto result = function->call(*this); diff --git a/Libraries/LibJS/Interpreter.h b/Libraries/LibJS/Interpreter.h index 9d8baff992c..75dc635f12e 100644 --- a/Libraries/LibJS/Interpreter.h +++ b/Libraries/LibJS/Interpreter.h @@ -58,6 +58,7 @@ struct ScopeFrame { }; struct CallFrame { + FlyString function_name; Value this_value; Vector arguments; }; @@ -106,7 +107,7 @@ public: CallFrame& push_call_frame() { - m_call_stack.append({ js_undefined(), {} }); + m_call_stack.append({ {}, js_undefined(), {} }); return m_call_stack.last(); } void pop_call_frame() { m_call_stack.take_last(); } diff --git a/Libraries/LibJS/Runtime/Function.h b/Libraries/LibJS/Runtime/Function.h index 4cdcaf24675..242f671b9e3 100644 --- a/Libraries/LibJS/Runtime/Function.h +++ b/Libraries/LibJS/Runtime/Function.h @@ -37,6 +37,7 @@ public: virtual Value call(Interpreter&) = 0; virtual Value construct(Interpreter&) = 0; + virtual const FlyString& name() const = 0; protected: Function(); diff --git a/Libraries/LibJS/Runtime/FunctionPrototype.cpp b/Libraries/LibJS/Runtime/FunctionPrototype.cpp index bfe63ec9f6b..33f36335afe 100644 --- a/Libraries/LibJS/Runtime/FunctionPrototype.cpp +++ b/Libraries/LibJS/Runtime/FunctionPrototype.cpp @@ -105,18 +105,24 @@ Value FunctionPrototype::to_string(Interpreter& interpreter) return {}; if (!this_object->is_function()) return interpreter.throw_exception("Not a Function object"); - // FIXME: Functions should be able to know their name, if any + + String function_name = static_cast(this_object)->name(); + String function_parameters = ""; + String function_body; + if (this_object->is_native_function()) { - auto function_source = String::format("function () {\n [%s]\n}", this_object->class_name()); - return js_string(interpreter, function_source); + function_body = String::format(" [%s]", this_object->class_name()); + } else { + auto& parameters = static_cast(this_object)->parameters(); + StringBuilder parameters_builder; + parameters_builder.join(", ", parameters); + function_parameters = parameters_builder.build(); + // FIXME: ASTNodes should be able to dump themselves to source strings - something like this: + // auto& body = static_cast(this_object)->body(); + // function_body = body.to_source(); + function_body = " ???"; } - auto parameters = static_cast(this_object)->parameters(); - StringBuilder parameters_builder; - parameters_builder.join(", ", parameters); - // FIXME: ASTNodes should be able to dump themselves to source strings - something like this: - // auto& body = static_cast(this_object)->body(); - // auto body_source = body.to_source(); - auto function_source = String::format("function (%s) {\n %s\n}", parameters_builder.build().characters(), "???"); + auto function_source = String::format("function %s(%s) {\n%s\n}", function_name.characters(), function_parameters.characters(), function_body.characters()); return js_string(interpreter, function_source); } diff --git a/Libraries/LibJS/Runtime/NativeFunction.cpp b/Libraries/LibJS/Runtime/NativeFunction.cpp index f1363ca8679..e50988105c8 100644 --- a/Libraries/LibJS/Runtime/NativeFunction.cpp +++ b/Libraries/LibJS/Runtime/NativeFunction.cpp @@ -30,8 +30,9 @@ namespace JS { -NativeFunction::NativeFunction(AK::Function native_function) - : m_native_function(move(native_function)) +NativeFunction::NativeFunction(const FlyString& name, AK::Function native_function) + : m_name(name) + , m_native_function(move(native_function)) { } diff --git a/Libraries/LibJS/Runtime/NativeFunction.h b/Libraries/LibJS/Runtime/NativeFunction.h index 4b661e634de..8495cb7cb84 100644 --- a/Libraries/LibJS/Runtime/NativeFunction.h +++ b/Libraries/LibJS/Runtime/NativeFunction.h @@ -33,12 +33,13 @@ namespace JS { class NativeFunction : public Function { public: - explicit NativeFunction(AK::Function); + explicit NativeFunction(const FlyString& name, AK::Function); virtual ~NativeFunction() override; virtual Value call(Interpreter&) override; virtual Value construct(Interpreter&) override; + virtual const FlyString& name() const override { return m_name; }; virtual bool has_constructor() const { return false; } protected: @@ -48,6 +49,7 @@ private: virtual bool is_native_function() const override { return true; } virtual const char* class_name() const override { return "NativeFunction"; } + FlyString m_name; AK::Function m_native_function; }; diff --git a/Libraries/LibJS/Runtime/Object.cpp b/Libraries/LibJS/Runtime/Object.cpp index 9382f4d8786..251599ae7c1 100644 --- a/Libraries/LibJS/Runtime/Object.cpp +++ b/Libraries/LibJS/Runtime/Object.cpp @@ -235,7 +235,7 @@ void Object::put(PropertyName property_name, Value value) void Object::put_native_function(const FlyString& property_name, AK::Function native_function, i32 length) { - auto* function = heap().allocate(move(native_function)); + auto* function = heap().allocate(property_name, move(native_function)); function->put("length", Value(length)); put(property_name, function); } diff --git a/Libraries/LibJS/Runtime/ScriptFunction.cpp b/Libraries/LibJS/Runtime/ScriptFunction.cpp index 2f196bc2e08..3560ea77b4f 100644 --- a/Libraries/LibJS/Runtime/ScriptFunction.cpp +++ b/Libraries/LibJS/Runtime/ScriptFunction.cpp @@ -33,8 +33,9 @@ namespace JS { -ScriptFunction::ScriptFunction(const Statement& body, Vector parameters) - : m_body(body) +ScriptFunction::ScriptFunction(const FlyString& name, const Statement& body, Vector parameters) + : m_name(name) + , m_body(body) , m_parameters(move(parameters)) { put("prototype", heap().allocate()); diff --git a/Libraries/LibJS/Runtime/ScriptFunction.h b/Libraries/LibJS/Runtime/ScriptFunction.h index cb85dafca9f..18afbe131dc 100644 --- a/Libraries/LibJS/Runtime/ScriptFunction.h +++ b/Libraries/LibJS/Runtime/ScriptFunction.h @@ -32,7 +32,7 @@ namespace JS { class ScriptFunction final : public Function { public: - ScriptFunction(const Statement& body, Vector parameters = {}); + ScriptFunction(const FlyString& name, const Statement& body, Vector parameters = {}); virtual ~ScriptFunction(); const Statement& body() const { return m_body; } @@ -41,6 +41,8 @@ public: virtual Value call(Interpreter&) override; virtual Value construct(Interpreter&) override; + virtual const FlyString& name() const override { return m_name; }; + private: virtual bool is_script_function() const final { return true; } virtual const char* class_name() const override { return "ScriptFunction"; } @@ -48,6 +50,7 @@ private: static Value length_getter(Interpreter&); static void length_setter(Interpreter&, Value); + FlyString m_name; NonnullRefPtr m_body; const Vector m_parameters; };