Skip to content

Commit e41560d

Browse files
committed
fix(builtins/typed_array,iterable,generator,error,bigint,eval): convert panics to EngineError::Panic using js_expect
Part of #3241. It changes the following: core/engine/src/builtins/typed_array/builtin.rs: converted 30 panics core/engine/src/builtins/typed_array/object.rs: converted 3 panics core/engine/src/builtins/iterable/async_from_sync_iterator.rs: converted 8 panics core/engine/src/builtins/generator/mod.rs: converted 3 panics core/engine/src/builtins/error/aggregate.rs: converted 1 panic core/engine/src/builtins/bigint/mod.rs: converted 1 panic core/engine/src/builtins/eval/mod.rs: converted 1 panic
1 parent f5ae9f7 commit e41560d

File tree

9 files changed

+100
-81
lines changed

9 files changed

+100
-81
lines changed

core/engine/src/builtins/bigint/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//! [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt
1414
1515
use crate::{
16-
Context, JsArgs, JsBigInt, JsResult, JsString, JsValue,
16+
Context, JsArgs, JsBigInt, JsExpect, JsResult, JsString, JsValue,
1717
builtins::BuiltInObject,
1818
context::intrinsics::{Intrinsics, StandardConstructor, StandardConstructors},
1919
error::JsNativeError,
@@ -122,7 +122,12 @@ impl BigInt {
122122
}
123123

124124
// 2. Return the BigInt value that represents ℝ(number).
125-
Ok(JsBigInt::from(number.to_bigint().expect("This conversion must be safe")).into())
125+
Ok(JsBigInt::from(
126+
number
127+
.to_bigint()
128+
.js_expect("This conversion must be safe")?,
129+
)
130+
.into())
126131
}
127132

128133
/// The abstract operation `thisBigIntValue` takes argument value.

core/engine/src/builtins/error/aggregate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//! [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError
99
1010
use crate::{
11-
Context, JsArgs, JsResult, JsString, JsValue,
11+
Context, JsArgs, JsExpect, JsResult, JsString, JsValue,
1212
builtins::{
1313
Array, BuiltInBuilder, BuiltInConstructor, BuiltInObject, IntrinsicObject,
1414
iterable::IteratorHint,
@@ -128,7 +128,7 @@ impl BuiltInConstructor for AggregateError {
128128
.build(),
129129
context,
130130
)
131-
.expect("should not fail according to spec");
131+
.js_expect("should not fail according to spec")?;
132132

133133
// 5. Return O.
134134
Ok(o.into())

core/engine/src/builtins/eval/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//! [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval
1111
1212
use crate::{
13-
Context, JsArgs, JsResult, JsString, JsValue, SpannedSourceText,
13+
Context, JsArgs, JsExpect, JsResult, JsString, JsValue, SpannedSourceText,
1414
builtins::{BuiltInObject, function::OrdinaryFunction},
1515
bytecompiler::{ByteCompiler, prepare_eval_declaration_instantiation},
1616
context::intrinsics::Intrinsics,
@@ -152,7 +152,7 @@ impl Eval {
152152
.slots()
153153
.function_object()
154154
.downcast_ref::<OrdinaryFunction>()
155-
.expect("must be function object");
155+
.js_expect("must be function object")?;
156156

157157
// ii. Set inFunction to true.
158158
let mut flags = Flags::IN_FUNCTION;

core/engine/src/builtins/generator/mod.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
//! [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator
1111
1212
use crate::{
13-
Context, JsArgs, JsData, JsError, JsResult, JsString,
13+
Context, JsArgs, JsData, JsError, JsExpect, JsResult, JsString,
1414
builtins::iterable::create_iter_result_object,
1515
context::intrinsics::Intrinsics,
1616
error::JsNativeError,
17+
error::PanicError,
1718
js_string,
1819
object::{CONSTRUCTOR, JsObject},
1920
property::Attribute,
@@ -101,7 +102,9 @@ impl GeneratorContext {
101102
context: &mut Context,
102103
) -> CompletionRecord {
103104
std::mem::swap(&mut context.vm.stack, &mut self.stack);
104-
let frame = self.call_frame.take().expect("should have a call frame");
105+
let Some(frame) = self.call_frame.take() else {
106+
return CompletionRecord::Throw(PanicError::new("should have a call frame").into());
107+
};
105108
let fp = frame.fp;
106109
let rp = frame.rp;
107110
context.vm.push_frame(frame);
@@ -125,16 +128,21 @@ impl GeneratorContext {
125128
}
126129

127130
/// Returns the async generator object, if the function that this [`GeneratorContext`] is from an async generator, [`None`] otherwise.
128-
pub(crate) fn async_generator_object(&self) -> Option<JsObject> {
129-
let frame = self.call_frame.as_ref()?;
131+
pub(crate) fn async_generator_object(&self) -> JsResult<Option<JsObject>> {
132+
let Some(frame) = self.call_frame.as_ref() else {
133+
return Ok(None);
134+
};
135+
130136
if !frame.code_block().is_async_generator() {
131-
return None;
137+
return Ok(None);
132138
}
133139

134-
self.stack
140+
let val = self
141+
.stack
135142
.get_register(frame, CallFrame::ASYNC_GENERATOR_OBJECT_REGISTER_INDEX)
136-
.expect("registers must have an async generator object")
137-
.as_object()
143+
.js_expect("registers must have an async generator object")?;
144+
145+
Ok(val.as_object())
138146
}
139147
}
140148

@@ -306,7 +314,7 @@ impl Generator {
306314

307315
let mut r#gen = generator_obj
308316
.downcast_mut::<Self>()
309-
.expect("already checked this object type");
317+
.js_expect("already checked this object type")?;
310318

311319
// 8. Push genContext onto the execution context stack; genContext is now the running execution context.
312320
// 9. Resume the suspended evaluation of genContext using NormalCompletion(value) as the result of the operation that suspended it. Let result be the value returned by the resumed computation.

core/engine/src/builtins/iterable/async_from_sync_iterator.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{
2-
Context, JsArgs, JsData, JsError, JsNativeError, JsResult, JsValue,
2+
Context, JsArgs, JsData, JsError, JsExpect, JsNativeError, JsResult, JsValue,
33
builtins::{
44
BuiltInBuilder, IntrinsicObject, Promise,
55
iterable::{IteratorRecord, IteratorResult, create_iter_result_object},
@@ -99,7 +99,7 @@ impl AsyncFromSyncIterator {
9999
let mut sync_iterator_record = object
100100
.as_ref()
101101
.and_then(JsObject::downcast_ref::<Self>)
102-
.expect("async from sync iterator prototype must be object")
102+
.js_expect("async from sync iterator prototype must be object")?
103103
.sync_iterator_record
104104
.clone();
105105

@@ -108,7 +108,7 @@ impl AsyncFromSyncIterator {
108108
&context.intrinsics().constructors().promise().constructor(),
109109
context,
110110
)
111-
.expect("cannot fail with promise constructor");
111+
.js_expect("cannot fail with promise constructor")?;
112112

113113
// 5. If value is present, then
114114
// a. Let result be Completion(IteratorNext(syncIteratorRecord, value)).
@@ -143,7 +143,7 @@ impl AsyncFromSyncIterator {
143143
let sync_iterator_record = object
144144
.as_ref()
145145
.and_then(JsObject::downcast_ref::<Self>)
146-
.expect("async from sync iterator prototype must be object")
146+
.js_expect("async from sync iterator prototype must be object")?
147147
.sync_iterator_record
148148
.clone();
149149
// 5. Let syncIterator be syncIteratorRecord.[[Iterator]].
@@ -154,7 +154,7 @@ impl AsyncFromSyncIterator {
154154
&context.intrinsics().constructors().promise().constructor(),
155155
context,
156156
)
157-
.expect("cannot fail with promise constructor");
157+
.js_expect("cannot fail with promise constructor")?;
158158

159159
// 6. Let return be Completion(GetMethod(syncIterator, "return")).
160160
let r#return = sync_iterator.get_method(js_string!("return"), context);
@@ -173,7 +173,7 @@ impl AsyncFromSyncIterator {
173173
promise_capability
174174
.resolve()
175175
.call(&JsValue::undefined(), &[iter_result], context)
176-
.expect("cannot fail according to spec");
176+
.js_expect("cannot fail according to spec")?;
177177

178178
// c. Return promiseCapability.[[Promise]].
179179
return Ok(promise_capability.promise().clone().into());
@@ -225,7 +225,7 @@ impl AsyncFromSyncIterator {
225225
let sync_iterator_record = object
226226
.as_ref()
227227
.and_then(JsObject::downcast_ref::<Self>)
228-
.expect("async from sync iterator prototype must be object")
228+
.js_expect("async from sync iterator prototype must be object")?
229229
.sync_iterator_record
230230
.clone();
231231
// 5. Let syncIterator be syncIteratorRecord.[[Iterator]].
@@ -236,7 +236,7 @@ impl AsyncFromSyncIterator {
236236
&context.intrinsics().constructors().promise().constructor(),
237237
context,
238238
)
239-
.expect("cannot fail with promise constructor");
239+
.js_expect("cannot fail with promise constructor")?;
240240

241241
// 6. Let throw be Completion(GetMethod(syncIterator, "throw")).
242242
let throw = sync_iterator.get_method(js_string!("throw"), context);
@@ -267,7 +267,7 @@ impl AsyncFromSyncIterator {
267267
.into()],
268268
context,
269269
)
270-
.expect("cannot fail according to spec");
270+
.js_expect("cannot fail according to spec")?;
271271

272272
// h. Return promiseCapability.[[Promise]].
273273
return Ok(promise_capability.promise().clone().into());

0 commit comments

Comments
 (0)