Browse Source

Fixed function call with unspecified arguments (#506)

Calling a function with less amount of arguments than
the function declaration parameters would `panic`.
pull/516/head
HalidOdat 4 years ago committed by GitHub
parent
commit
a7cffe3c7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 15
      boa/src/builtins/function/mod.rs
  2. 8
      boa/src/builtins/object/mod.rs
  3. 15
      boa/src/exec/tests.rs

15
boa/src/builtins/function/mod.rs

@ -192,21 +192,20 @@ impl Function {
let local_env = new_function_environment( let local_env = new_function_environment(
function, function,
None, None,
Some(self.environment.as_ref().unwrap().clone()), self.environment.as_ref().cloned(),
BindingStatus::Uninitialized, BindingStatus::Uninitialized,
); );
// Add argument bindings to the function environment // Add argument bindings to the function environment
for i in 0..self.params.len() { for (i, param) in self.params.iter().enumerate() {
let param = self.params.get(i).expect("Could not get param");
// Rest Parameters // Rest Parameters
if param.is_rest_param() { if param.is_rest_param() {
self.add_rest_param(param, i, args_list, interpreter, &local_env); self.add_rest_param(param, i, args_list, interpreter, &local_env);
break; break;
} }
let value = args_list.get(i).expect("Could not get value"); let value = args_list.get(i).cloned().unwrap_or_else(Value::undefined);
self.add_arguments_to_environment(param, value.clone(), &local_env); self.add_arguments_to_environment(param, value, &local_env);
} }
// Add arguments object // Add arguments object
@ -253,7 +252,7 @@ impl Function {
let local_env = new_function_environment( let local_env = new_function_environment(
function, function,
Some(this.clone()), Some(this.clone()),
Some(self.environment.as_ref().unwrap().clone()), self.environment.as_ref().cloned(),
BindingStatus::Initialized, BindingStatus::Initialized,
); );
@ -265,8 +264,8 @@ impl Function {
break; break;
} }
let value = args_list.get(i).expect("Could not get value"); let value = args_list.get(i).cloned().unwrap_or_else(Value::undefined);
self.add_arguments_to_environment(param, value.clone(), &local_env); self.add_arguments_to_environment(param, value, &local_env);
} }
// Add arguments object // Add arguments object

8
boa/src/builtins/object/mod.rs

@ -477,16 +477,14 @@ pub fn property_is_enumerable(
args: &[Value], args: &[Value],
ctx: &mut Interpreter, ctx: &mut Interpreter,
) -> ResultValue { ) -> ResultValue {
let key = if args.is_empty() { let key = match args.get(0) {
return Ok(Value::from(false)); None => return Ok(Value::from(false)),
} else { Some(key) => key,
args.get(0).expect("Cannot get key")
}; };
let property_key = ctx.to_property_key(&mut key.clone())?; let property_key = ctx.to_property_key(&mut key.clone())?;
let own_property = ctx.to_object(this).map(|obj| { let own_property = ctx.to_object(this).map(|obj| {
obj.as_object() obj.as_object()
.as_deref()
.expect("Unable to deref object") .expect("Unable to deref object")
.get_own_property(&property_key) .get_own_property(&property_key)
}); });

15
boa/src/exec/tests.rs

@ -912,3 +912,18 @@ fn to_string() {
assert_eq!(engine.to_string(&Value::rational(55.0)).unwrap(), "55"); assert_eq!(engine.to_string(&Value::rational(55.0)).unwrap(), "55");
assert_eq!(engine.to_string(&Value::string("hello")).unwrap(), "hello"); 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");
}

Loading…
Cancel
Save