1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 20:37:35 +00:00

LibJS: Make Function and CallFrame aware of their function name

This commit is contained in:
Linus Groh 2020-04-11 12:56:20 +01:00 committed by Andreas Kling
parent 4eceea7c62
commit eece424694
11 changed files with 39 additions and 22 deletions

View file

@ -48,14 +48,14 @@ Value ScopeNode::execute(Interpreter& interpreter) const
Value FunctionDeclaration::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); interpreter.set_variable(name(), function);
return js_undefined(); return js_undefined();
} }
Value FunctionExpression::execute(Interpreter& interpreter) const 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 Value ExpressionStatement::execute(Interpreter& interpreter) const
@ -117,6 +117,7 @@ Value CallExpression::execute(Interpreter& interpreter) const
} }
auto& call_frame = interpreter.push_call_frame(); auto& call_frame = interpreter.push_call_frame();
call_frame.function_name = function.name();
call_frame.arguments = move(arguments); call_frame.arguments = move(arguments);
Object* new_object = nullptr; Object* new_object = nullptr;

View file

@ -163,8 +163,8 @@ class FunctionDeclaration final
public: public:
static bool must_have_name() { return true; } static bool must_have_name() { return true; }
FunctionDeclaration(String name, NonnullRefPtr<Statement> body, Vector<FlyString> parameters = {}) FunctionDeclaration(const FlyString& name, NonnullRefPtr<Statement> body, Vector<FlyString> parameters = {})
: FunctionNode(move(name), move(body), move(parameters)) : FunctionNode(name, move(body), move(parameters))
{ {
} }

View file

@ -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) Value Interpreter::call(Function* function, Value this_value, const Vector<Value>& arguments)
{ {
auto& call_frame = push_call_frame(); auto& call_frame = push_call_frame();
call_frame.function_name = function->name();
call_frame.this_value = this_value; call_frame.this_value = this_value;
call_frame.arguments = arguments; call_frame.arguments = arguments;
auto result = function->call(*this); auto result = function->call(*this);

View file

@ -58,6 +58,7 @@ struct ScopeFrame {
}; };
struct CallFrame { struct CallFrame {
FlyString function_name;
Value this_value; Value this_value;
Vector<Value> arguments; Vector<Value> arguments;
}; };
@ -106,7 +107,7 @@ public:
CallFrame& push_call_frame() CallFrame& push_call_frame()
{ {
m_call_stack.append({ js_undefined(), {} }); m_call_stack.append({ {}, js_undefined(), {} });
return m_call_stack.last(); return m_call_stack.last();
} }
void pop_call_frame() { m_call_stack.take_last(); } void pop_call_frame() { m_call_stack.take_last(); }

View file

@ -37,6 +37,7 @@ public:
virtual Value call(Interpreter&) = 0; virtual Value call(Interpreter&) = 0;
virtual Value construct(Interpreter&) = 0; virtual Value construct(Interpreter&) = 0;
virtual const FlyString& name() const = 0;
protected: protected:
Function(); Function();

View file

@ -105,18 +105,24 @@ Value FunctionPrototype::to_string(Interpreter& interpreter)
return {}; return {};
if (!this_object->is_function()) if (!this_object->is_function())
return interpreter.throw_exception<TypeError>("Not a Function object"); 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()) { if (this_object->is_native_function()) {
auto function_source = String::format("function () {\n [%s]\n}", this_object->class_name()); function_body = String::format(" [%s]", this_object->class_name());
return js_string(interpreter, function_source); } else {
} auto& parameters = static_cast<ScriptFunction*>(this_object)->parameters();
auto parameters = static_cast<ScriptFunction*>(this_object)->parameters();
StringBuilder parameters_builder; StringBuilder parameters_builder;
parameters_builder.join(", ", parameters); parameters_builder.join(", ", parameters);
function_parameters = parameters_builder.build();
// FIXME: ASTNodes should be able to dump themselves to source strings - something like this: // FIXME: ASTNodes should be able to dump themselves to source strings - something like this:
// auto& body = static_cast<ScriptFunction*>(this_object)->body(); // auto& body = static_cast<ScriptFunction*>(this_object)->body();
// auto body_source = body.to_source(); // function_body = body.to_source();
auto function_source = String::format("function (%s) {\n %s\n}", parameters_builder.build().characters(), "???"); function_body = " ???";
}
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); return js_string(interpreter, function_source);
} }

View file

@ -30,8 +30,9 @@
namespace JS { namespace JS {
NativeFunction::NativeFunction(AK::Function<Value(Interpreter&)> native_function) NativeFunction::NativeFunction(const FlyString& name, AK::Function<Value(Interpreter&)> native_function)
: m_native_function(move(native_function)) : m_name(name)
, m_native_function(move(native_function))
{ {
} }

View file

@ -33,12 +33,13 @@ namespace JS {
class NativeFunction : public Function { class NativeFunction : public Function {
public: public:
explicit NativeFunction(AK::Function<Value(Interpreter&)>); explicit NativeFunction(const FlyString& name, AK::Function<Value(Interpreter&)>);
virtual ~NativeFunction() override; virtual ~NativeFunction() override;
virtual Value call(Interpreter&) override; virtual Value call(Interpreter&) override;
virtual Value construct(Interpreter&) override; virtual Value construct(Interpreter&) override;
virtual const FlyString& name() const override { return m_name; };
virtual bool has_constructor() const { return false; } virtual bool has_constructor() const { return false; }
protected: protected:
@ -48,6 +49,7 @@ private:
virtual bool is_native_function() const override { return true; } virtual bool is_native_function() const override { return true; }
virtual const char* class_name() const override { return "NativeFunction"; } virtual const char* class_name() const override { return "NativeFunction"; }
FlyString m_name;
AK::Function<Value(Interpreter&)> m_native_function; AK::Function<Value(Interpreter&)> m_native_function;
}; };

View file

@ -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) 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)); function->put("length", Value(length));
put(property_name, function); put(property_name, function);
} }

View file

@ -33,8 +33,9 @@
namespace JS { namespace JS {
ScriptFunction::ScriptFunction(const Statement& body, Vector<FlyString> parameters) ScriptFunction::ScriptFunction(const FlyString& name, const Statement& body, Vector<FlyString> parameters)
: m_body(body) : m_name(name)
, m_body(body)
, m_parameters(move(parameters)) , m_parameters(move(parameters))
{ {
put("prototype", heap().allocate<Object>()); put("prototype", heap().allocate<Object>());

View file

@ -32,7 +32,7 @@ namespace JS {
class ScriptFunction final : public Function { class ScriptFunction final : public Function {
public: public:
ScriptFunction(const Statement& body, Vector<FlyString> parameters = {}); ScriptFunction(const FlyString& name, const Statement& body, Vector<FlyString> parameters = {});
virtual ~ScriptFunction(); virtual ~ScriptFunction();
const Statement& body() const { return m_body; } const Statement& body() const { return m_body; }
@ -41,6 +41,8 @@ public:
virtual Value call(Interpreter&) override; virtual Value call(Interpreter&) override;
virtual Value construct(Interpreter&) override; virtual Value construct(Interpreter&) override;
virtual const FlyString& name() const override { return m_name; };
private: private:
virtual bool is_script_function() const final { return true; } virtual bool is_script_function() const final { return true; }
virtual const char* class_name() const override { return "ScriptFunction"; } virtual const char* class_name() const override { return "ScriptFunction"; }
@ -48,6 +50,7 @@ private:
static Value length_getter(Interpreter&); static Value length_getter(Interpreter&);
static void length_setter(Interpreter&, Value); static void length_setter(Interpreter&, Value);
FlyString m_name;
NonnullRefPtr<Statement> m_body; NonnullRefPtr<Statement> m_body;
const Vector<FlyString> m_parameters; const Vector<FlyString> m_parameters;
}; };