diff --git a/boa/src/builtins/json/mod.rs b/boa/src/builtins/json/mod.rs index 6b8f8bb4247..cfc73e0d893 100644 --- a/boa/src/builtins/json/mod.rs +++ b/boa/src/builtins/json/mod.rs @@ -268,7 +268,7 @@ impl Json { // 10. Perform ! CreateDataPropertyOrThrow(wrapper, the empty String, value). wrapper - .create_data_property_or_throw("", args.get(0).cloned().unwrap_or_default(), context) + .create_data_property_or_throw("", args.get_or_undefined(0).clone(), context) .expect("CreateDataPropertyOrThrow should never fail here"); // 11. Let state be the Record { [[ReplacerFunction]]: ReplacerFunction, [[Stack]]: stack, [[Indent]]: indent, [[Gap]]: gap, [[PropertyList]]: PropertyList }. diff --git a/boa/src/builtins/mod.rs b/boa/src/builtins/mod.rs index 78173840c3c..e9f17db73ba 100644 --- a/boa/src/builtins/mod.rs +++ b/boa/src/builtins/mod.rs @@ -114,6 +114,17 @@ pub fn init(context: &mut Context) { } pub trait JsArgs { + /// Utility function to `get` a parameter from + /// a `[JsValue]` or default to `JsValue::Undefined` + /// if `get` returns `None`. + /// + /// Call this if you are thinking of calling something similar to + /// `args.get(n).cloned().unwrap_or_default()` or + /// `args.get(n).unwrap_or(&undefined)`. + /// + /// This returns a reference for efficiency, in case + /// you only need to call methods of `JsValue`, so + /// try to minimize calling `clone`. fn get_or_undefined(&self, index: usize) -> &JsValue; }