Skip to content

Commit

Permalink
Fixed function call with unspecified arguments (#506)
Browse files Browse the repository at this point in the history
Calling a function with less amount of arguments than
the function declaration parameters would `panic`.
  • Loading branch information
HalidOdat authored Jun 19, 2020
1 parent 8b431a4 commit a7cffe3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
15 changes: 7 additions & 8 deletions boa/src/builtins/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,21 +192,20 @@ impl Function {
let local_env = new_function_environment(
function,
None,
Some(self.environment.as_ref().unwrap().clone()),
self.environment.as_ref().cloned(),
BindingStatus::Uninitialized,
);

// Add argument bindings to the function environment
for i in 0..self.params.len() {
let param = self.params.get(i).expect("Could not get param");
for (i, param) in self.params.iter().enumerate() {
// Rest Parameters
if param.is_rest_param() {
self.add_rest_param(param, i, args_list, interpreter, &local_env);
break;
}

let value = args_list.get(i).expect("Could not get value");
self.add_arguments_to_environment(param, value.clone(), &local_env);
let value = args_list.get(i).cloned().unwrap_or_else(Value::undefined);
self.add_arguments_to_environment(param, value, &local_env);
}

// Add arguments object
Expand Down Expand Up @@ -253,7 +252,7 @@ impl Function {
let local_env = new_function_environment(
function,
Some(this.clone()),
Some(self.environment.as_ref().unwrap().clone()),
self.environment.as_ref().cloned(),
BindingStatus::Initialized,
);

Expand All @@ -265,8 +264,8 @@ impl Function {
break;
}

let value = args_list.get(i).expect("Could not get value");
self.add_arguments_to_environment(param, value.clone(), &local_env);
let value = args_list.get(i).cloned().unwrap_or_else(Value::undefined);
self.add_arguments_to_environment(param, value, &local_env);
}

// Add arguments object
Expand Down
8 changes: 3 additions & 5 deletions boa/src/builtins/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,16 +477,14 @@ pub fn property_is_enumerable(
args: &[Value],
ctx: &mut Interpreter,
) -> ResultValue {
let key = if args.is_empty() {
return Ok(Value::from(false));
} else {
args.get(0).expect("Cannot get key")
let key = match args.get(0) {
None => return Ok(Value::from(false)),
Some(key) => key,
};

let property_key = ctx.to_property_key(&mut key.clone())?;
let own_property = ctx.to_object(this).map(|obj| {
obj.as_object()
.as_deref()
.expect("Unable to deref object")
.get_own_property(&property_key)
});
Expand Down
15 changes: 15 additions & 0 deletions boa/src/exec/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -912,3 +912,18 @@ fn to_string() {
assert_eq!(engine.to_string(&Value::rational(55.0)).unwrap(), "55");
assert_eq!(engine.to_string(&Value::string("hello")).unwrap(), "hello");
}

#[test]
fn calling_function_with_unspecified_arguments() {
let realm = Realm::create();
let mut engine = Interpreter::new(realm);
let scenario = r#"
function test(a, b) {
return b;
}
test(10)
"#;

assert_eq!(forward(&mut engine, scenario), "undefined");
}

0 comments on commit a7cffe3

Please sign in to comment.