Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Display for function objects #1309

Merged
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
74 changes: 74 additions & 0 deletions boa/src/builtins/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,79 @@ impl BuiltInFunctionObject {
// TODO?: 5. PrepareForTailCall
context.call(this, &this_arg, &arg_list)
}

fn to_string(this: &Value, _: &[Value], context: &mut Context) -> Result<Value> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You seem to not be using the this variable here, and it's causing a clippy error. You can change it to _this to avoid it.

let name = {
// Is there a case here where if there is no name field on a value
// We it should default to None? Do all functions have names set?
RageKnify marked this conversation as resolved.
Show resolved Hide resolved
let value = this.get_field("name", &mut *context)?;
if value.is_null_or_undefined() {
None
} else {
Some(value.to_string(context)?)
}
};

let function = {
let object = this.as_object().map(|object| {
let func = object.borrow().as_function().map(|f| f.clone());
kvnvelasco marked this conversation as resolved.
Show resolved Hide resolved
func
});

if let Some(Some(function)) = object {
function
} else {
return context.throw_type_error("Not a function");
}
};

match (&function, name) {
(Function::BuiltIn(_, _), Some(name)) => {
Ok(format!("function {}() {{\n [native Code]\n}}", &name).into())
}
(Function::Ordinary { body, params, .. }, Some(name)) => {
let arguments: String = params
.iter()
.map(|param| param.name())
.collect::<Vec<&str>>()
.join(", ");

let statement_list = &*body;
// This is a kluge. The implementaion in browser seems to suggest that
// the value here is printed exactly as defined in source. I'm not sure if
// that's possible here, but for now here's a dumb heuristic that prints functions
let is_multiline = {
let value = statement_list.to_string();
value.lines().count() > 1
};
if is_multiline {
Ok(
// ?? For some reason statement_list string implementation
// sticks a \n at the end no matter what
format!(
"{}({}) {{\n{}}}",
&name,
arguments,
statement_list.to_string()
)
.into(),
)
} else {
Ok(format!(
"{}({}) {{{}}}",
&name,
arguments,
// The trim here is to remove a \n stuck at the end
// of the statement_list to_string method
statement_list.to_string().trim()
)
.into())
}
}

_ => Ok("TODO".into()),
}
}
}

impl BuiltIn for BuiltInFunctionObject {
Expand Down Expand Up @@ -358,6 +431,7 @@ impl BuiltIn for BuiltInFunctionObject {
.length(Self::LENGTH)
.method(Self::call, "call", 1)
.method(Self::apply, "apply", 1)
.method(Self::to_string, "toString", 0)
.build();

(Self::NAME, function_object.into(), Self::attribute())
Expand Down