Skip to content

Commit

Permalink
Feat: Implement Function.prototype.call and test case
Browse files Browse the repository at this point in the history
closes #797
  • Loading branch information
RageKnify committed Oct 5, 2020
1 parent c836ec9 commit ae9be6d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
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() {
return context.throw_type_error("TODO: this is not a function");
}
let this_arg: Value = args.get(0).map_or_else(Value::default, Value::clone);
context.call(func, &this_arg, &args[1..])
}
}

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]");
}

0 comments on commit ae9be6d

Please sign in to comment.