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

Feature Function.prototype.call #805

Merged
merged 3 commits into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions boa/src/builtins/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,25 @@ impl BuiltInFunctionObject {
fn prototype(_: &Value, _: &[Value], _: &mut Context) -> Result<Value> {
Ok(Value::undefined())
}

/// `Function.prototype.call`
///
/// The call() method invokes self with the first argument as the `this` value.
///
/// More information:
/// - [MDN documentation][mdn]
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-function.prototype.call
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call
fn call(this: &Value, args: &[Value], context: &mut Context) -> Result<Value> {
let func = this;
if !func.is_function() {
RageKnify marked this conversation as resolved.
Show resolved Hide resolved
return context.throw_type_error("TODO: this is not a function");
RageKnify marked this conversation as resolved.
Show resolved Hide resolved
}
let this_arg: Value = args.get(0).map_or_else(Value::default, Value::clone);
RageKnify marked this conversation as resolved.
Show resolved Hide resolved
context.call(func, &this_arg, &args[1..])
RageKnify marked this conversation as resolved.
Show resolved Hide resolved
}
}

impl BuiltIn for BuiltInFunctionObject {
Expand Down Expand Up @@ -339,6 +358,7 @@ impl BuiltIn for BuiltInFunctionObject {
)
.name(Self::NAME)
.length(Self::LENGTH)
.method(Self::call, "call", 1)
.build();

(Self::NAME, function_object.into(), Self::attribute())
Expand Down
12 changes: 12 additions & 0 deletions boa/src/builtins/function/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,15 @@ fn function_prototype_length() {
assert!(value.is_number());
assert_eq!(value.as_number().unwrap(), 0.0);
}

#[test]
fn function_prototype_call() {
let mut engine = Context::new();
let func = r#"
let e = new Error()
Object.prototype.toString.call(e)
"#;
let value = forward_val(&mut engine, func).unwrap();
assert!(value.is_string());
assert_eq!(value.as_string().unwrap(), "[object Error]");
}