Skip to content

Commit c5045fe

Browse files
committed
fix(vm/opcode/get,set,binary_ops,copy,call,iteration): convert panics to EngineError::Panic using js_expect
1 parent 1d0d8cb commit c5045fe

File tree

8 files changed

+33
-35
lines changed

8 files changed

+33
-35
lines changed

core/engine/src/vm/opcode/binary_ops/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::{IndexOperand, RegisterOperand};
2-
use crate::{Context, JsResult, error::JsNativeError, vm::opcode::Operation};
2+
use crate::{Context, JsExpect, JsResult, error::JsNativeError, vm::opcode::Operation};
33

44
pub(crate) mod logical;
55
pub(crate) mod macro_defined;
@@ -129,7 +129,7 @@ impl InPrivate {
129129
.frame()
130130
.environments
131131
.resolve_private_identifier(name)
132-
.expect("private name must be in environment");
132+
.js_expect("private name must be in environment")?;
133133

134134
let value = rhs.private_element_find(&name, true, true).is_some();
135135

core/engine/src/vm/opcode/call/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use dynify::Dynify;
55

66
use super::{IndexOperand, RegisterOperand};
77
use crate::{
8-
Context, JsError, JsObject, JsResult, JsValue, NativeFunction,
8+
Context, JsError, JsExpect, JsObject, JsResult, JsValue, NativeFunction,
99
builtins::{Promise, promise::PromiseCapability},
1010
error::JsNativeError,
1111
job::NativeAsyncJob,
@@ -107,12 +107,12 @@ impl CallEvalSpread {
107107
let arguments_array = context.vm.stack.pop();
108108
let arguments_array_object = arguments_array
109109
.as_object()
110-
.expect("arguments array in call spread function must be an object");
110+
.js_expect("arguments array in call spread function must be an object")?;
111111
let arguments = arguments_array_object
112112
.borrow()
113113
.properties()
114114
.to_dense_indexed_properties()
115-
.expect("arguments array in call spread function must be dense");
115+
.js_expect("arguments array in call spread function must be dense")?;
116116

117117
let func = context.vm.stack.calling_convention_get_function(0);
118118

@@ -223,12 +223,12 @@ impl CallSpread {
223223
let arguments_array = context.vm.stack.pop();
224224
let arguments_array_object = arguments_array
225225
.as_object()
226-
.expect("arguments array in call spread function must be an object");
226+
.js_expect("arguments array in call spread function must be an object")?;
227227
let arguments = arguments_array_object
228228
.borrow()
229229
.properties()
230230
.to_dense_indexed_properties()
231-
.expect("arguments array in call spread function must be dense");
231+
.js_expect("arguments array in call spread function must be dense")?;
232232

233233
let argument_count = arguments.len();
234234
context
@@ -303,13 +303,13 @@ fn parse_import_attributes(
303303
for entry in entries {
304304
let entry = entry
305305
.as_object()
306-
.expect("entry from EnumerableOwnProperties must be an object");
306+
.js_expect("entry from EnumerableOwnProperties must be an object")?;
307307

308308
// 1. Let key be entry.[[Key]].
309309
let key = entry.get(0, context)?;
310310
let key_str = key
311311
.as_string()
312-
.expect("key from EnumerableOwnProperties must be a string")
312+
.js_expect("key from EnumerableOwnProperties must be a string")?
313313
.clone();
314314

315315
// 2. Let value be entry.[[Value]].

core/engine/src/vm/opcode/copy/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::RegisterOperand;
2-
use crate::{Context, JsResult, vm::opcode::Operation};
2+
use crate::{Context, JsExpect, JsResult, vm::opcode::Operation};
33
use thin_vec::ThinVec;
44

55
/// `CopyDataProperties` implements the Opcode Operation for `Opcode::CopyDataProperties`
@@ -22,10 +22,10 @@ impl CopyDataProperties {
2222
let key = context.vm.get_register(key.into()).clone();
2323
excluded_keys.push(
2424
key.to_property_key(context)
25-
.expect("key must be property key"),
25+
.js_expect("key must be property key")?,
2626
);
2727
}
28-
let object = object.as_object().expect("not an object");
28+
let object = object.as_object().js_expect("not an object")?;
2929
object.copy_data_properties(&source, excluded_keys, context)?;
3030
Ok(())
3131
}

core/engine/src/vm/opcode/get/name.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{
2-
Context, JsResult, JsValue,
2+
Context, JsExpect, JsResult, JsValue,
33
error::JsNativeError,
44
object::{internal_methods::InternalMethodPropertyContext, shape::slot::SlotAttributes},
55
property::PropertyKey,
@@ -62,7 +62,7 @@ impl GetNameGlobal {
6262
let object_borrowed = object.borrow();
6363
if let Some((shape, slot)) = ic.get(object_borrowed.shape()) {
6464
let mut result = if slot.attributes.contains(SlotAttributes::PROTOTYPE) {
65-
let prototype = shape.prototype().expect("prototype should have value");
65+
let prototype = shape.prototype().js_expect("prototype should have value")?;
6666
let prototype = prototype.borrow();
6767
prototype.properties().storage[slot.index as usize].clone()
6868
} else {
@@ -71,11 +71,10 @@ impl GetNameGlobal {
7171

7272
drop(object_borrowed);
7373
if slot.attributes.has_get() && result.is_object() {
74-
result = result.as_object().expect("should contain getter").call(
75-
&object.clone().into(),
76-
&[],
77-
context,
78-
)?;
74+
result = result
75+
.as_object()
76+
.js_expect("should contain getter")?
77+
.call(&object.clone().into(), &[], context)?;
7978
}
8079
context.vm.set_register(dst.into(), result);
8180
return Ok(());

core/engine/src/vm/opcode/get/private.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{
2-
Context, JsResult,
2+
Context, JsExpect, JsResult,
33
vm::opcode::{IndexOperand, Operation, RegisterOperand},
44
};
55

@@ -28,7 +28,7 @@ impl GetPrivateField {
2828
.frame()
2929
.environments
3030
.resolve_private_identifier(name)
31-
.expect("private name must be in environment");
31+
.js_expect("private name must be in environment")?;
3232

3333
let result = object.private_get(&name, context)?;
3434
context.vm.set_register(dst.into(), result);

core/engine/src/vm/opcode/get/property.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use boa_string::StaticJsStrings;
22

33
use crate::{
4-
Context, JsResult, JsValue, js_string,
4+
Context, JsExpect, JsResult, JsValue, js_string,
55
object::{internal_methods::InternalMethodPropertyContext, shape::slot::SlotAttributes},
66
property::PropertyKey,
77
vm::opcode::{IndexOperand, Operation, RegisterOperand},
@@ -41,7 +41,7 @@ fn get_by_name<const LENGTH: bool>(
4141
let object_borrowed = object.borrow();
4242
if let Some((shape, slot)) = ic.get(object_borrowed.shape()) {
4343
let mut result = if slot.attributes.contains(SlotAttributes::PROTOTYPE) {
44-
let prototype = shape.prototype().expect("prototype should have value");
44+
let prototype = shape.prototype().js_expect("prototype should have value")?;
4545
let prototype = prototype.borrow();
4646
prototype.properties().storage[slot.index as usize].clone()
4747
} else {
@@ -50,11 +50,10 @@ fn get_by_name<const LENGTH: bool>(
5050

5151
drop(object_borrowed);
5252
if slot.attributes.has_get() && result.is_object() {
53-
result =
54-
result
55-
.as_object()
56-
.expect("should contain getter")
57-
.call(receiver, &[], context)?;
53+
result = result
54+
.as_object()
55+
.js_expect("should contain getter")?
56+
.call(receiver, &[], context)?;
5857
}
5958
context.vm.set_register(dst.into(), result);
6059
return Ok(());

core/engine/src/vm/opcode/iteration/iterator.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ impl IteratorNext {
143143
.frame_mut()
144144
.iterators
145145
.pop()
146-
.expect("iterator stack should have at least an iterator");
146+
.js_expect("iterator stack should have at least an iterator")?;
147147

148148
iterator.step(context)?;
149149

@@ -178,7 +178,7 @@ impl IteratorFinishAsyncNext {
178178
.frame_mut()
179179
.iterators
180180
.pop()
181-
.expect("iterator on the call frame must exist");
181+
.js_expect("iterator on the call frame must exist")?;
182182

183183
let resume_kind = context
184184
.vm
@@ -249,7 +249,7 @@ impl IteratorValue {
249249
.frame_mut()
250250
.iterators
251251
.pop()
252-
.expect("iterator on the call frame must exist");
252+
.js_expect("iterator on the call frame must exist")?;
253253

254254
let iter_value = iterator.value(context)?;
255255
context.vm.set_register(value.into(), iter_value);
@@ -358,7 +358,7 @@ impl IteratorToArray {
358358
.frame_mut()
359359
.iterators
360360
.pop()
361-
.expect("iterator on the call frame must exist");
361+
.js_expect("iterator on the call frame must exist")?;
362362

363363
let mut values = Vec::new();
364364

core/engine/src/vm/opcode/set/private.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{
2-
Context, JsResult, js_str, js_string,
2+
Context, JsExpect, JsResult, js_str, js_string,
33
object::PrivateElement,
44
property::PropertyDescriptor,
55
vm::opcode::{IndexOperand, Operation, RegisterOperand},
@@ -31,7 +31,7 @@ impl SetPrivateField {
3131
.frame()
3232
.environments
3333
.resolve_private_identifier(name)
34-
.expect("private name must be in environment");
34+
.js_expect("private name must be in environment")?;
3535

3636
base_obj.private_set(&name, value.clone(), context)?;
3737
Ok(())
@@ -67,7 +67,7 @@ impl DefinePrivateField {
6767

6868
let object = object
6969
.as_object()
70-
.expect("class prototype must be an object");
70+
.js_expect("class prototype must be an object")?;
7171

7272
let name = object.private_name(name);
7373
object.private_field_add(&name, value, context)?;

0 commit comments

Comments
 (0)