Skip to content

Commit

Permalink
Remove some uses of JsValue::get_field
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat committed Feb 21, 2022
1 parent 2f65260 commit 5ac4c19
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 33 deletions.
13 changes: 8 additions & 5 deletions boa/src/builtins/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> {
fn is_concat_spreadable(o: &JsValue, context: &mut Context) -> JsResult<bool> {
// 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 ]`
Expand Down
51 changes: 32 additions & 19 deletions boa/src/builtins/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{
},
profiler::BoaProfiler,
property::Attribute,
Context, JsResult, JsValue,
Context, JsResult, JsValue, JsString,
};

pub(crate) mod eval;
Expand Down Expand Up @@ -108,33 +108,46 @@ impl Error {
_: &[JsValue],
context: &mut Context,
) -> JsResult<JsValue> {
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())
}
}
2 changes: 1 addition & 1 deletion boa/src/builtins/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
8 changes: 4 additions & 4 deletions boa/src/builtins/json/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,31 +343,31 @@ 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,
2u8
);
assert_eq!(
result
.get_field("1", &mut context)
.get_v("1", &mut context)
.unwrap()
.to_number(&mut context)
.unwrap() as u8,
4u8
);
assert_eq!(
result
.get_field("2", &mut context)
.get_v("2", &mut context)
.unwrap()
.to_number(&mut context)
.unwrap() as u8,
6u8
);
assert_eq!(
result
.get_field("3", &mut context)
.get_v("3", &mut context)
.unwrap()
.to_number(&mut context)
.unwrap() as u8,
Expand Down
4 changes: 3 additions & 1 deletion boa/src/builtins/string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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).
Expand Down
8 changes: 5 additions & 3 deletions boa/src/object/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 5ac4c19

Please sign in to comment.