Skip to content

Commit 242be67

Browse files
committed
fix(object/operations,string): convert remaining panics to EngineError::Panic
1 parent d6d76d8 commit 242be67

2 files changed

Lines changed: 18 additions & 10 deletions

File tree

core/engine/src/object/internal_methods/string.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub(crate) fn string_exotic_get_own_property(
3939
Ok(desc)
4040
} else {
4141
// 4. Return ! StringGetOwnProperty(S, P).
42-
Ok(string_get_own_property(obj, key))
42+
string_get_own_property(obj, key)
4343
}
4444
}
4545

@@ -57,7 +57,7 @@ pub(crate) fn string_exotic_define_own_property(
5757
) -> JsResult<bool> {
5858
// 1. Assert: IsPropertyKey(P) is true.
5959
// 2. Let stringDesc be ! StringGetOwnProperty(S, P).
60-
let string_desc = string_get_own_property(obj, key);
60+
let string_desc = string_get_own_property(obj, key)?;
6161

6262
// 3. If stringDesc is not undefined, then
6363
if let Some(string_desc) = string_desc {
@@ -133,7 +133,10 @@ pub(crate) fn string_exotic_own_property_keys(
133133
/// - [ECMAScript reference][spec]
134134
///
135135
/// [spec]: https://tc39.es/ecma262/#sec-stringgetownproperty
136-
fn string_get_own_property(obj: &JsObject, key: &PropertyKey) -> Option<PropertyDescriptor> {
136+
fn string_get_own_property(
137+
obj: &JsObject,
138+
key: &PropertyKey,
139+
) -> JsResult<Option<PropertyDescriptor>> {
137140
// 1. Assert: S is an Object that has a [[StringData]] internal slot.
138141
// 2. Assert: IsPropertyKey(P) is true.
139142
// 3. If Type(P) is not String, return undefined.
@@ -143,20 +146,23 @@ fn string_get_own_property(obj: &JsObject, key: &PropertyKey) -> Option<Property
143146
// 7. If index is -0𝔽, return undefined.
144147
let pos = match key {
145148
PropertyKey::Index(index) => index.get() as usize,
146-
_ => return None,
149+
_ => return Ok(None),
147150
};
148151

149152
// 8. Let str be S.[[StringData]].
150153
// 9. Assert: Type(str) is String.
151154
let string = obj
152155
.downcast_ref::<JsString>()
153-
.expect("string exotic method should only be callable from string objects")
156+
.js_expect("string exotic method should only be callable from string objects")?
154157
.clone();
155158

156159
// 10. Let len be the length of str.
157160
// 11. If ℝ(index) < 0 or len ≤ ℝ(index), return undefined.
158-
// 12. Let resultStr be the String value of length 1, containing one code unit from str, specifically the code unit at index ℝ(index).
159-
let result_str = string.get(pos..=pos)?;
161+
// 12. Let resultStr be the String value of length 1, containing one code unit from str,
162+
// specifically the code unit at index ℝ(index).
163+
let Some(result_str) = string.get(pos..=pos) else {
164+
return Ok(None);
165+
};
160166

161167
// 13. Return the PropertyDescriptor { [[Value]]: resultStr, [[Writable]]: false, [[Enumerable]]: true, [[Configurable]]: false }.
162168
let desc = PropertyDescriptor::builder()
@@ -166,5 +172,5 @@ fn string_get_own_property(obj: &JsObject, key: &PropertyKey) -> Option<Property
166172
.configurable(false)
167173
.build();
168174

169-
Some(desc)
175+
Ok(Some(desc))
170176
}

core/engine/src/object/operations.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,7 @@ impl JsObject {
906906
// 4. Let from be ! ToObject(source).
907907
let from = source
908908
.to_object(context)
909-
.expect("function ToObject should never complete abruptly here");
909+
.js_expect("function ToObject should never complete abruptly here")?;
910910

911911
// 5. Let keys be ? from.[[OwnPropertyKeys]]().
912912
// 6. For each element nextKey of keys, do
@@ -939,7 +939,9 @@ impl JsObject {
939939

940940
// 2. Perform ! CreateDataPropertyOrThrow(target, nextKey, propValue).
941941
self.create_data_property_or_throw(key, prop_value, context)
942-
.expect("CreateDataPropertyOrThrow should never complete abruptly here");
942+
.js_expect(
943+
"CreateDataPropertyOrThrow should never complete abruptly here",
944+
)?;
943945
}
944946
}
945947
}

0 commit comments

Comments
 (0)