mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 19:17:44 +00:00
LibJS: Make Function and CallFrame aware of their function name
This commit is contained in:
parent
4eceea7c62
commit
eece424694
11 changed files with 39 additions and 22 deletions
|
@ -37,6 +37,7 @@ public:
|
|||
|
||||
virtual Value call(Interpreter&) = 0;
|
||||
virtual Value construct(Interpreter&) = 0;
|
||||
virtual const FlyString& name() const = 0;
|
||||
|
||||
protected:
|
||||
Function();
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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))
|
||||
{
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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>());
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue