summaryrefslogtreecommitdiff
path: root/Libraries
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2020-04-11 12:56:20 +0100
committerAndreas Kling <kling@serenityos.org>2020-04-11 14:10:42 +0200
commiteece424694951e6cb3cd8a82315e8523c3a4b30e (patch)
treee0809580f97ebd69580cabbe1d11a7d48e035017 /Libraries
parent4eceea7c62da8cc95a0943020cbdb3593e337c97 (diff)
downloadserenity-eece424694951e6cb3cd8a82315e8523c3a4b30e.zip
LibJS: Make Function and CallFrame aware of their function name
Diffstat (limited to 'Libraries')
-rw-r--r--Libraries/LibJS/AST.cpp5
-rw-r--r--Libraries/LibJS/AST.h4
-rw-r--r--Libraries/LibJS/Interpreter.cpp1
-rw-r--r--Libraries/LibJS/Interpreter.h3
-rw-r--r--Libraries/LibJS/Runtime/Function.h1
-rw-r--r--Libraries/LibJS/Runtime/FunctionPrototype.cpp26
-rw-r--r--Libraries/LibJS/Runtime/NativeFunction.cpp5
-rw-r--r--Libraries/LibJS/Runtime/NativeFunction.h4
-rw-r--r--Libraries/LibJS/Runtime/Object.cpp2
-rw-r--r--Libraries/LibJS/Runtime/ScriptFunction.cpp5
-rw-r--r--Libraries/LibJS/Runtime/ScriptFunction.h5
11 files changed, 39 insertions, 22 deletions
diff --git a/Libraries/LibJS/AST.cpp b/Libraries/LibJS/AST.cpp
index 86f4dc0bc0..427594753e 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<ScriptFunction>(body(), parameters());
+ auto* function = interpreter.heap().allocate<ScriptFunction>(name(), body(), parameters());
interpreter.set_variable(name(), function);
return js_undefined();
}
Value FunctionExpression::execute(Interpreter& interpreter) const
{
- return interpreter.heap().allocate<ScriptFunction>(body(), parameters());
+ return interpreter.heap().allocate<ScriptFunction>(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 dc2b398055..fe4d806e14 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<Statement> body, Vector<FlyString> parameters = {})
- : FunctionNode(move(name), move(body), move(parameters))
+ FunctionDeclaration(const FlyString& name, NonnullRefPtr<Statement> body, Vector<FlyString> parameters = {})
+ : FunctionNode(name, move(body), move(parameters))
{
}
diff --git a/Libraries/LibJS/Interpreter.cpp b/Libraries/LibJS/Interpreter.cpp
index 3d78095e08..5bd91139e0 100644
--- a/Libraries/LibJS/Interpreter.cpp
+++ b/Libraries/LibJS/Interpreter.cpp
@@ -205,6 +205,7 @@ void Interpreter::gather_roots(Badge<Heap>, HashTable<Cell*>& roots)
Value Interpreter::call(Function* function, Value this_value, const Vector<Value>& 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 9d8baff992..75dc635f12 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<Value> 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 4cdcaf2467..242f671b9e 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 bfe63ec9f6..33f36335af 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<TypeError>("Not a Function object");
- // FIXME: Functions should be able to know their name, if any
+
+ String function_name = static_cast<Function*>(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<ScriptFunction*>(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<ScriptFunction*>(this_object)->body();
+ // function_body = body.to_source();
+ function_body = " ???";
}
- auto parameters = static_cast<ScriptFunction*>(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<ScriptFunction*>(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 f1363ca867..e50988105c 100644
--- a/Libraries/LibJS/Runtime/NativeFunction.cpp
+++ b/Libraries/LibJS/Runtime/NativeFunction.cpp
@@ -30,8 +30,9 @@
namespace JS {
-NativeFunction::NativeFunction(AK::Function<Value(Interpreter&)> native_function)
- : m_native_function(move(native_function))
+NativeFunction::NativeFunction(const FlyString& name, AK::Function<Value(Interpreter&)> 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 4b661e634d..8495cb7cb8 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<Value(Interpreter&)>);
+ explicit NativeFunction(const FlyString& name, AK::Function<Value(Interpreter&)>);
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<Value(Interpreter&)> m_native_function;
};
diff --git a/Libraries/LibJS/Runtime/Object.cpp b/Libraries/LibJS/Runtime/Object.cpp
index 9382f4d878..251599ae7c 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<Value(Interpreter&)> native_function, i32 length)
{
- auto* function = heap().allocate<NativeFunction>(move(native_function));
+ auto* function = heap().allocate<NativeFunction>(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 2f196bc2e0..3560ea77b4 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<FlyString> parameters)
- : m_body(body)
+ScriptFunction::ScriptFunction(const FlyString& name, const Statement& body, Vector<FlyString> parameters)
+ : m_name(name)
+ , m_body(body)
, m_parameters(move(parameters))
{
put("prototype", heap().allocate<Object>());
diff --git a/Libraries/LibJS/Runtime/ScriptFunction.h b/Libraries/LibJS/Runtime/ScriptFunction.h
index cb85dafca9..18afbe131d 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<FlyString> parameters = {});
+ ScriptFunction(const FlyString& name, const Statement& body, Vector<FlyString> 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<Statement> m_body;
const Vector<FlyString> m_parameters;
};