Skip to content

Commit 66069d3

Browse files
committed
Fixed function call with unspecified arguments
Calling a function with less amount of arguments than the function declaration parameters would `panic`.
1 parent 8b431a4 commit 66069d3

3 files changed

Lines changed: 25 additions & 13 deletions

File tree

boa/src/builtins/function/mod.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -192,21 +192,20 @@ impl Function {
192192
let local_env = new_function_environment(
193193
function,
194194
None,
195-
Some(self.environment.as_ref().unwrap().clone()),
195+
self.environment.as_ref().cloned(),
196196
BindingStatus::Uninitialized,
197197
);
198198

199199
// Add argument bindings to the function environment
200-
for i in 0..self.params.len() {
201-
let param = self.params.get(i).expect("Could not get param");
200+
for (i, param) in self.params.iter().enumerate() {
202201
// Rest Parameters
203202
if param.is_rest_param() {
204203
self.add_rest_param(param, i, args_list, interpreter, &local_env);
205204
break;
206205
}
207206

208-
let value = args_list.get(i).expect("Could not get value");
209-
self.add_arguments_to_environment(param, value.clone(), &local_env);
207+
let value = args_list.get(i).cloned().unwrap_or_else(Value::undefined);
208+
self.add_arguments_to_environment(param, value, &local_env);
210209
}
211210

212211
// Add arguments object
@@ -253,7 +252,7 @@ impl Function {
253252
let local_env = new_function_environment(
254253
function,
255254
Some(this.clone()),
256-
Some(self.environment.as_ref().unwrap().clone()),
255+
self.environment.as_ref().cloned(),
257256
BindingStatus::Initialized,
258257
);
259258

@@ -265,8 +264,8 @@ impl Function {
265264
break;
266265
}
267266

268-
let value = args_list.get(i).expect("Could not get value");
269-
self.add_arguments_to_environment(param, value.clone(), &local_env);
267+
let value = args_list.get(i).cloned().unwrap_or_else(Value::undefined);
268+
self.add_arguments_to_environment(param, value, &local_env);
270269
}
271270

272271
// Add arguments object

boa/src/builtins/object/mod.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -477,16 +477,14 @@ pub fn property_is_enumerable(
477477
args: &[Value],
478478
ctx: &mut Interpreter,
479479
) -> ResultValue {
480-
let key = if args.is_empty() {
481-
return Ok(Value::from(false));
482-
} else {
483-
args.get(0).expect("Cannot get key")
480+
let key = match args.get(0) {
481+
None => return Ok(Value::from(false)),
482+
Some(key) => key,
484483
};
485484

486485
let property_key = ctx.to_property_key(&mut key.clone())?;
487486
let own_property = ctx.to_object(this).map(|obj| {
488487
obj.as_object()
489-
.as_deref()
490488
.expect("Unable to deref object")
491489
.get_own_property(&property_key)
492490
});

boa/src/exec/tests.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,3 +912,18 @@ fn to_string() {
912912
assert_eq!(engine.to_string(&Value::rational(55.0)).unwrap(), "55");
913913
assert_eq!(engine.to_string(&Value::string("hello")).unwrap(), "hello");
914914
}
915+
916+
#[test]
917+
fn calling_function_with_unspecified_arguments() {
918+
let realm = Realm::create();
919+
let mut engine = Interpreter::new(realm);
920+
let scenario = r#"
921+
function test(a, b) {
922+
return b;
923+
}
924+
925+
test(10)
926+
"#;
927+
928+
assert_eq!(forward(&mut engine, scenario), "undefined");
929+
}

0 commit comments

Comments
 (0)