Skip to content

Commit e6fb287

Browse files
committed
js
1 parent 7a3e632 commit e6fb287

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

src/tracing/js/bindings.rs

+29-26
Original file line numberDiff line numberDiff line change
@@ -79,29 +79,29 @@ struct GuardedNullableGc<Val: 'static> {
7979
impl<Val: 'static> GuardedNullableGc<Val> {
8080
/// Creates a garbage collectible value to the given reference.
8181
///
82-
/// SAFETY; the caller must ensure that the guard is dropped before the value is dropped.
83-
fn r#ref(val: &Val) -> (Self, GcGuard<'_, Val>) {
82+
/// # Safety
83+
///
84+
/// The caller must ensure that the guard is dropped before the value is dropped.
85+
fn new_ref(val: &Val) -> (Self, GcGuard<'_, Val>) {
8486
Self::new(Guarded::Ref(val))
8587
}
8688

8789
/// Creates a garbage collectible value to the given reference.
8890
///
89-
/// SAFETY; the caller must ensure that the guard is dropped before the value is dropped.
90-
fn r#owned<'a>(val: Val) -> (Self, GcGuard<'a, Val>) {
91+
/// # Safety
92+
///
93+
/// The caller must ensure that the guard is dropped before the value is dropped.
94+
fn new_owned<'a>(val: Val) -> (Self, GcGuard<'a, Val>) {
9195
Self::new(Guarded::Owned(val))
9296
}
9397

9498
fn new(val: Guarded<'_, Val>) -> (Self, GcGuard<'_, Val>) {
9599
let inner = Rc::new(RefCell::new(Some(val)));
96100
let guard = GcGuard { inner: Rc::clone(&inner) };
97101

98-
// SAFETY: guard enforces that the value is removed from the refcell before it is dropped
99-
let this = Self {
100-
inner: unsafe {
101-
#[allow(clippy::missing_transmute_annotations)]
102-
std::mem::transmute(inner)
103-
},
104-
};
102+
// SAFETY: guard enforces that the value is removed from the refcell before it is dropped.
103+
#[allow(clippy::missing_transmute_annotations)]
104+
let this = Self { inner: unsafe { std::mem::transmute(inner) } };
105105

106106
(this, guard)
107107
}
@@ -111,10 +111,7 @@ impl<Val: 'static> GuardedNullableGc<Val> {
111111
where
112112
F: FnOnce(&Val) -> R,
113113
{
114-
self.inner.borrow().as_ref().map(|val| match val {
115-
Guarded::Ref(val) => f(val),
116-
Guarded::Owned(val) => f(val),
117-
})
114+
self.inner.borrow().as_ref().map(|guard| f(guard.as_ref()))
118115
}
119116
}
120117

@@ -131,6 +128,16 @@ enum Guarded<'a, T> {
131128
Owned(T),
132129
}
133130

131+
impl<T> Guarded<'_, T> {
132+
#[inline]
133+
fn as_ref(&self) -> &T {
134+
match self {
135+
Guarded::Ref(val) => val,
136+
Guarded::Owned(val) => val,
137+
}
138+
}
139+
}
140+
134141
/// Guard the inner value, once this value is dropped the inner value is also removed.
135142
///
136143
/// This type guarantees that it never outlives the wrapped value.
@@ -232,7 +239,7 @@ pub(crate) struct MemoryRef(GuardedNullableGc<SharedMemory>);
232239
impl MemoryRef {
233240
/// Creates a new stack reference
234241
pub(crate) fn new(mem: &SharedMemory) -> (Self, GcGuard<'_, SharedMemory>) {
235-
let (inner, guard) = GuardedNullableGc::r#ref(mem);
242+
let (inner, guard) = GuardedNullableGc::new_ref(mem);
236243
(Self(inner), guard)
237244
}
238245

@@ -324,7 +331,7 @@ pub(crate) struct StateRef(GuardedNullableGc<State>);
324331
impl StateRef {
325332
/// Creates a new stack reference
326333
pub(crate) fn new(state: &State) -> (Self, GcGuard<'_, State>) {
327-
let (inner, guard) = GuardedNullableGc::r#ref(state);
334+
let (inner, guard) = GuardedNullableGc::new_ref(state);
328335
(Self(inner), guard)
329336
}
330337

@@ -349,7 +356,7 @@ where
349356
{
350357
/// Creates a new stack reference
351358
fn new<'a>(db: DB) -> (Self, GcGuard<'a, DB>) {
352-
let (inner, guard) = GuardedNullableGc::owned(db);
359+
let (inner, guard) = GuardedNullableGc::new_owned(db);
353360
(Self(inner), guard)
354361
}
355362
}
@@ -417,7 +424,7 @@ pub(crate) struct StackRef(GuardedNullableGc<Stack>);
417424
impl StackRef {
418425
/// Creates a new stack reference
419426
pub(crate) fn new(stack: &Stack) -> (Self, GcGuard<'_, Stack>) {
420-
let (inner, guard) = GuardedNullableGc::r#ref(stack);
427+
let (inner, guard) = GuardedNullableGc::new_ref(stack);
421428
(Self(inner), guard)
422429
}
423430

@@ -790,19 +797,15 @@ impl EvmDbRef {
790797
return JsArrayBuffer::new(0, ctx);
791798
}
792799

793-
let Some(Ok(code)) = self
794-
.inner
795-
.db
796-
.0
797-
.with_inner(|db| db.code_by_hash_ref(code_hash).map(|code| code.bytecode_bytes()))
800+
let Some(Ok(bytecode)) = self.inner.db.0.with_inner(|db| db.code_by_hash_ref(code_hash))
798801
else {
799802
return Err(JsError::from_native(
800803
JsNativeError::error()
801-
.with_message(format!("Failed to read code hash {code_hash:?} from database",)),
804+
.with_message(format!("Failed to read code hash {code_hash:?} from database")),
802805
));
803806
};
804807

805-
to_buf(code.into(), ctx)
808+
to_buf(bytecode.bytes().to_vec(), ctx)
806809
}
807810

808811
fn read_state(

0 commit comments

Comments
 (0)