Skip to content

Commit 75c0a75

Browse files
authored
fix(jsproxy): convert panics to EngineError::Panic using js_expect (#4854)
Part of #3241 Converts all 13 panics in `jsproxy.rs` to `EngineError::Panic` using `js_expect` introduced in #4828. `build()` and `build_revocable()` return types changed from `JsProxy`/ `JsRevocableProxy` to `JsResult<JsProxy>`/`JsResult<JsRevocableProxy>`. Checked for external callers —> only a type re-export in `mod.rs`, so the cascade is limited to 1 file. Note: The two `JsNativeError::typ()` usages in `from_object()` and `TryFromJs` are intentionally left unchanged (becauze i think these are correct user-facing TypeErrors, not internal implementation failures.)
1 parent d57edb7 commit 75c0a75

1 file changed

Lines changed: 19 additions & 21 deletions

File tree

core/engine/src/object/builtins/jsproxy.rs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! A Rust API wrapper for the `Proxy` Builtin ECMAScript Object
22
use super::JsFunction;
33
use crate::{
4-
Context, JsNativeError, JsResult, JsValue,
4+
Context, JsExpect, JsNativeError, JsResult, JsValue,
55
builtins::Proxy,
66
js_string,
77
native_function::{NativeFunction, NativeFunctionPointer},
@@ -393,8 +393,7 @@ impl JsProxyBuilder {
393393
/// Equivalent to the `Proxy ( target, handler )` constructor, but returns a
394394
/// [`JsObject`] in case there's a need to manipulate the returned object
395395
/// inside Rust code.
396-
#[must_use]
397-
pub fn build(self, context: &mut Context) -> JsProxy {
396+
pub fn build(self, context: &mut Context) -> JsResult<JsProxy> {
398397
let handler = JsObject::with_object_proto(context.intrinsics());
399398

400399
if let Some(apply) = self.apply {
@@ -403,7 +402,7 @@ impl JsProxyBuilder {
403402
.build();
404403
handler
405404
.create_data_property_or_throw(js_string!("apply"), f, context)
406-
.expect("new object should be writable");
405+
.js_expect("new object should be writable")?;
407406
}
408407
if let Some(construct) = self.construct {
409408
let f =
@@ -412,7 +411,7 @@ impl JsProxyBuilder {
412411
.build();
413412
handler
414413
.create_data_property_or_throw(js_string!("construct"), f, context)
415-
.expect("new object should be writable");
414+
.js_expect("new object should be writable")?;
416415
}
417416
if let Some(define_property) = self.define_property {
418417
let f = FunctionObjectBuilder::new(
@@ -423,7 +422,7 @@ impl JsProxyBuilder {
423422
.build();
424423
handler
425424
.create_data_property_or_throw(js_string!("defineProperty"), f, context)
426-
.expect("new object should be writable");
425+
.js_expect("new object should be writable")?;
427426
}
428427
if let Some(delete_property) = self.delete_property {
429428
let f = FunctionObjectBuilder::new(
@@ -434,15 +433,15 @@ impl JsProxyBuilder {
434433
.build();
435434
handler
436435
.create_data_property_or_throw(js_string!("deleteProperty"), f, context)
437-
.expect("new object should be writable");
436+
.js_expect("new object should be writable")?;
438437
}
439438
if let Some(get) = self.get {
440439
let f = FunctionObjectBuilder::new(context.realm(), NativeFunction::from_fn_ptr(get))
441440
.length(3)
442441
.build();
443442
handler
444443
.create_data_property_or_throw(js_string!("get"), f, context)
445-
.expect("new object should be writable");
444+
.js_expect("new object should be writable")?;
446445
}
447446
if let Some(get_own_property_descriptor) = self.get_own_property_descriptor {
448447
let f = FunctionObjectBuilder::new(
@@ -453,7 +452,7 @@ impl JsProxyBuilder {
453452
.build();
454453
handler
455454
.create_data_property_or_throw(js_string!("getOwnPropertyDescriptor"), f, context)
456-
.expect("new object should be writable");
455+
.js_expect("new object should be writable")?;
457456
}
458457
if let Some(get_prototype_of) = self.get_prototype_of {
459458
let f = FunctionObjectBuilder::new(
@@ -464,15 +463,15 @@ impl JsProxyBuilder {
464463
.build();
465464
handler
466465
.create_data_property_or_throw(js_string!("getPrototypeOf"), f, context)
467-
.expect("new object should be writable");
466+
.js_expect("new object should be writable")?;
468467
}
469468
if let Some(has) = self.has {
470469
let f = FunctionObjectBuilder::new(context.realm(), NativeFunction::from_fn_ptr(has))
471470
.length(2)
472471
.build();
473472
handler
474473
.create_data_property_or_throw(js_string!("has"), f, context)
475-
.expect("new object should be writable");
474+
.js_expect("new object should be writable")?;
476475
}
477476
if let Some(is_extensible) = self.is_extensible {
478477
let f = FunctionObjectBuilder::new(
@@ -483,7 +482,7 @@ impl JsProxyBuilder {
483482
.build();
484483
handler
485484
.create_data_property_or_throw(js_string!("isExtensible"), f, context)
486-
.expect("new object should be writable");
485+
.js_expect("new object should be writable")?;
487486
}
488487
if let Some(own_keys) = self.own_keys {
489488
let f =
@@ -492,7 +491,7 @@ impl JsProxyBuilder {
492491
.build();
493492
handler
494493
.create_data_property_or_throw(js_string!("ownKeys"), f, context)
495-
.expect("new object should be writable");
494+
.js_expect("new object should be writable")?;
496495
}
497496
if let Some(prevent_extensions) = self.prevent_extensions {
498497
let f = FunctionObjectBuilder::new(
@@ -503,15 +502,15 @@ impl JsProxyBuilder {
503502
.build();
504503
handler
505504
.create_data_property_or_throw(js_string!("preventExtensions"), f, context)
506-
.expect("new object should be writable");
505+
.js_expect("new object should be writable")?;
507506
}
508507
if let Some(set) = self.set {
509508
let f = FunctionObjectBuilder::new(context.realm(), NativeFunction::from_fn_ptr(set))
510509
.length(4)
511510
.build();
512511
handler
513512
.create_data_property_or_throw(js_string!("set"), f, context)
514-
.expect("new object should be writable");
513+
.js_expect("new object should be writable")?;
515514
}
516515
if let Some(set_prototype_of) = self.set_prototype_of {
517516
let f = FunctionObjectBuilder::new(
@@ -522,7 +521,7 @@ impl JsProxyBuilder {
522521
.build();
523522
handler
524523
.create_data_property_or_throw(js_string!("setPrototypeOf"), f, context)
525-
.expect("new object should be writable");
524+
.js_expect("new object should be writable")?;
526525
}
527526

528527
let proxy = JsObject::from_proto_and_data_with_shared_shape(
@@ -532,7 +531,7 @@ impl JsProxyBuilder {
532531
)
533532
.upcast();
534533

535-
JsProxy { inner: proxy }
534+
Ok(JsProxy { inner: proxy })
536535
}
537536

538537
/// Builds a [`JsObject`] of kind [`Proxy`] and a [`JsFunction`] that, when
@@ -542,11 +541,10 @@ impl JsProxyBuilder {
542541
/// but returns a [`JsObject`] for the proxy and a [`JsFunction`] for the
543542
/// revoker in case there's a need to manipulate the returned objects
544543
/// inside Rust code.
545-
#[must_use]
546-
pub fn build_revocable(self, context: &mut Context) -> JsRevocableProxy {
547-
let proxy = self.build(context);
544+
pub fn build_revocable(self, context: &mut Context) -> JsResult<JsRevocableProxy> {
545+
let proxy = self.build(context)?;
548546
let revoker = Proxy::revoker(proxy.inner.clone(), context);
549547

550-
JsRevocableProxy { proxy, revoker }
548+
Ok(JsRevocableProxy { proxy, revoker })
551549
}
552550
}

0 commit comments

Comments
 (0)