Skip to content

Commit

Permalink
Merge pull request #385 from richarddd/fix/prototype-lookup
Browse files Browse the repository at this point in the history
Fix/prototype lookup causing prototype missmatch across crates
  • Loading branch information
DelSkayn authored Nov 15, 2024
2 parents 71ffb86 + e8dc66e commit 2627859
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
3 changes: 1 addition & 2 deletions core/src/allocator/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ unsafe impl Allocator for RustAllocator {
let header = unsafe { &mut *(ptr as *mut Header) };
header.size = total_size;

let ptr = unsafe { ptr.add(HEADER_SIZE) };
ptr
unsafe { ptr.add(HEADER_SIZE) }
}

fn alloc(&mut self, size: usize) -> *mut u8 {
Expand Down
10 changes: 7 additions & 3 deletions core/src/class/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub(crate) type CallFunc = for<'a> unsafe fn(
pub(crate) type TypeIdFn = fn() -> TypeId;

pub(crate) struct VTable {
id: TypeIdFn,
id_fn: TypeIdFn,
finalizer: FinalizerFunc,
trace: TraceFunc,
call: CallFunc,
Expand Down Expand Up @@ -125,7 +125,7 @@ impl VTable {

impl<'js, C: JsClass<'js>> HasVTable for C {
const VTABLE: VTable = VTable {
id: TypeId::of::<C::Changed<'static>>,
id_fn: TypeId::of::<C::Changed<'static>>,
finalizer: VTable::finalizer_impl::<'js, C>,
trace: VTable::trace_impl::<C>,
call: VTable::call_impl::<C>,
Expand All @@ -134,8 +134,12 @@ impl VTable {
&<C as HasVTable>::VTABLE
}

pub fn id(&self) -> TypeId {
(self.id_fn)()
}

pub fn is_of_class<'js, C: JsClass<'js>>(&self) -> bool {
(self.id)() == TypeId::of::<C::Changed<'static>>()
(self.id_fn)() == TypeId::of::<C::Changed<'static>>()
}
}

Expand Down
7 changes: 4 additions & 3 deletions core/src/runtime/opaque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use super::{
InterruptHandler, UserData, UserDataError,
};
use std::{
any::Any,
any::{Any, TypeId},
cell::{Cell, UnsafeCell},
collections::{hash_map::Entry, HashMap},
marker::PhantomData,
Expand Down Expand Up @@ -37,7 +37,7 @@ pub(crate) struct Opaque<'js> {
/// The class id for rust classes which can be called.
callable_class_id: qjs::JSClassID,

prototypes: UnsafeCell<HashMap<*const VTable, Option<Object<'js>>>>,
prototypes: UnsafeCell<HashMap<TypeId, Option<Object<'js>>>>,

userdata: UserDataMap,

Expand Down Expand Up @@ -188,7 +188,8 @@ impl<'js> Opaque<'js> {
) -> Result<Option<Object<'js>>, Error> {
unsafe {
let vtable = VTable::get::<C>();
match (*self.prototypes.get()).entry(vtable) {
let id = vtable.id();
match (*self.prototypes.get()).entry(id) {
Entry::Occupied(x) => Ok(x.get().clone()),
Entry::Vacant(x) => {
let proto = C::prototype(ctx)?;
Expand Down

0 comments on commit 2627859

Please sign in to comment.