From 5ac4c196432f9ead6e2da94833e80422b485b433 Mon Sep 17 00:00:00 2001 From: HalidOdat Date: Mon, 21 Feb 2022 11:08:16 +0100 Subject: [PATCH] Remove some uses of JsValue::get_field --- boa/src/builtins/array/mod.rs | 13 +++++---- boa/src/builtins/error/mod.rs | 51 +++++++++++++++++++++------------- boa/src/builtins/json/mod.rs | 2 +- boa/src/builtins/json/tests.rs | 8 +++--- boa/src/builtins/string/mod.rs | 4 ++- boa/src/object/operations.rs | 8 ++++-- 6 files changed, 53 insertions(+), 33 deletions(-) diff --git a/boa/src/builtins/array/mod.rs b/boa/src/builtins/array/mod.rs index 455dcab9588..94b626989c1 100644 --- a/boa/src/builtins/array/mod.rs +++ b/boa/src/builtins/array/mod.rs @@ -277,20 +277,23 @@ impl Array { /// Returns a Boolean valued property that if `true` indicates that /// an object should be flattened to its array elements /// by `Array.prototype.concat`. - fn is_concat_spreadable(this: &JsValue, context: &mut Context) -> JsResult { + fn is_concat_spreadable(o: &JsValue, context: &mut Context) -> JsResult { // 1. If Type(O) is not Object, return false. - if !this.is_object() { + let o = if let Some(o) = o.as_object() { + o + } else { return Ok(false); - } + }; + // 2. Let spreadable be ? Get(O, @@isConcatSpreadable). - let spreadable = this.get_field(WellKnownSymbols::is_concat_spreadable(), context)?; + let spreadable = o.get(WellKnownSymbols::is_concat_spreadable(), context)?; // 3. If spreadable is not undefined, return ! ToBoolean(spreadable). if !spreadable.is_undefined() { return Ok(spreadable.to_boolean()); } // 4. Return ? IsArray(O). - this.is_array(context) + Ok(o.is_array()) } /// `get Array [ @@species ]` diff --git a/boa/src/builtins/error/mod.rs b/boa/src/builtins/error/mod.rs index 1319b70061b..a6996c1c596 100644 --- a/boa/src/builtins/error/mod.rs +++ b/boa/src/builtins/error/mod.rs @@ -18,7 +18,7 @@ use crate::{ }, profiler::BoaProfiler, property::Attribute, - Context, JsResult, JsValue, + Context, JsResult, JsValue, JsString, }; pub(crate) mod eval; @@ -108,33 +108,46 @@ impl Error { _: &[JsValue], context: &mut Context, ) -> JsResult { - if !this.is_object() { + // 1. Let O be the this value. + let o = if let Some(o) = this.as_object() { + o + // 2. If Type(O) is not Object, throw a TypeError exception. + } else { return context.throw_type_error("'this' is not an Object"); - } - let name = this.get_field("name", context)?; - let name_to_string; + }; + + // 3. Let name be ? Get(O, "name"). + let name = o.get("name", context)?; + + // 4. If name is undefined, set name to "Error"; otherwise set name to ? ToString(name). let name = if name.is_undefined() { - "Error" + JsString::new("Error") } else { - name_to_string = name.to_string(context)?; - name_to_string.as_str() + name.to_string(context)? }; - let message = this.get_field("message", context)?; - let message_to_string; - let message = if message.is_undefined() { - "" + // 5. Let msg be ? Get(O, "message"). + let msg = o.get("message", context)?; + + // 6. If msg is undefined, set msg to the empty String; otherwise set msg to ? ToString(msg). + let msg = if msg.is_undefined() { + JsString::empty() } else { - message_to_string = message.to_string(context)?; - message_to_string.as_str() + msg.to_string(context)? }; + // 7. If name is the empty String, return msg. if name.is_empty() { - Ok(message.into()) - } else if message.is_empty() { - Ok(name.into()) - } else { - Ok(format!("{name}: {message}").into()) + return Ok(msg.into()); } + + // 8. If msg is the empty String, return name. + if msg.is_empty() { + return Ok(name.into()); + } + + // 9. Return the string-concatenation of name, the code unit 0x003A (COLON), + // the code unit 0x0020 (SPACE), and msg. + Ok(format!("{name}: {msg}").into()) } } diff --git a/boa/src/builtins/json/mod.rs b/boa/src/builtins/json/mod.rs index 98cc4868210..384b8378e75 100644 --- a/boa/src/builtins/json/mod.rs +++ b/boa/src/builtins/json/mod.rs @@ -373,7 +373,7 @@ impl Json { // 2. If Type(value) is Object or BigInt, then if value.is_object() || value.is_bigint() { // a. Let toJSON be ? GetV(value, "toJSON"). - let to_json = value.get_field("toJSON", context)?; + let to_json = value.get_v("toJSON", context)?; // b. If IsCallable(toJSON) is true, then if let Some(obj) = to_json.as_object() { diff --git a/boa/src/builtins/json/tests.rs b/boa/src/builtins/json/tests.rs index 9d473ffb421..be350aae4a9 100644 --- a/boa/src/builtins/json/tests.rs +++ b/boa/src/builtins/json/tests.rs @@ -343,7 +343,7 @@ fn json_parse_array_with_reviver() { .unwrap(); assert_eq!( result - .get_field("0", &mut context) + .get_v("0", &mut context) .unwrap() .to_number(&mut context) .unwrap() as u8, @@ -351,7 +351,7 @@ fn json_parse_array_with_reviver() { ); assert_eq!( result - .get_field("1", &mut context) + .get_v("1", &mut context) .unwrap() .to_number(&mut context) .unwrap() as u8, @@ -359,7 +359,7 @@ fn json_parse_array_with_reviver() { ); assert_eq!( result - .get_field("2", &mut context) + .get_v("2", &mut context) .unwrap() .to_number(&mut context) .unwrap() as u8, @@ -367,7 +367,7 @@ fn json_parse_array_with_reviver() { ); assert_eq!( result - .get_field("3", &mut context) + .get_v("3", &mut context) .unwrap() .to_number(&mut context) .unwrap() as u8, diff --git a/boa/src/builtins/string/mod.rs b/boa/src/builtins/string/mod.rs index 437203ac511..f81971100b8 100644 --- a/boa/src/builtins/string/mod.rs +++ b/boa/src/builtins/string/mod.rs @@ -2064,6 +2064,8 @@ pub(crate) fn get_substitution( result.push_str("$<"); } else { // a. Assert: Type(namedCaptures) is Object. + let named_captures = named_captures.as_object() + .expect("should be an object acording to spec"); // b. Scan until the next > U+003E (GREATER-THAN SIGN). let mut group_name = StdString::new(); @@ -2088,7 +2090,7 @@ pub(crate) fn get_substitution( } else { // i. Let groupName be the enclosed substring. // ii. Let capture be ? Get(namedCaptures, groupName). - let capture = named_captures.get_field(group_name, context)?; + let capture = named_captures.get(group_name, context)?; // iii. If capture is undefined, replace the text through > with the empty String. // iv. Otherwise, replace the text through > with ? ToString(capture). diff --git a/boa/src/object/operations.rs b/boa/src/object/operations.rs index 02db55f6e16..bec8ff14525 100644 --- a/boa/src/object/operations.rs +++ b/boa/src/object/operations.rs @@ -449,12 +449,14 @@ impl JsObject { } // 4. If Type(C) is not Object, throw a TypeError exception. - if !c.is_object() { + let c = if let Some(c) = c.as_object() { + c + } else { return context.throw_type_error("property 'constructor' is not an object"); - } + }; // 5. Let S be ? Get(C, @@species). - let s = c.get_field(WellKnownSymbols::species(), context)?; + let s = c.get(WellKnownSymbols::species(), context)?; // 6. If S is either undefined or null, return defaultConstructor. if s.is_null_or_undefined() {