Skip to content

Commit

Permalink
Added Checks for Inherited Native Classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Redfire75369 committed Feb 15, 2024
1 parent 6425540 commit c9246e2
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions ion/src/class/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ use mozjs::gc::HandleObject;
use mozjs::gc::Traceable;
use mozjs::glue::JS_GetReservedSlot;
use mozjs::jsapi::{
GCContext, Handle, JS_GetConstructor, JS_InitClass, JS_InstanceOf, JS_NewObjectWithGivenProto, JS_SetReservedSlot,
JSFunction, JSFunctionSpec, JSObject, JSPropertySpec, JSTracer,
GCContext, Handle, JS_GetConstructor, JS_HasInstance, JS_InitClass, JS_InstanceOf, JS_NewObjectWithGivenProto,
JS_SetReservedSlot, JSFunction, JSFunctionSpec, JSObject, JSPropertySpec, JSTracer,
};
use mozjs::jsval::{NullValue, PrivateValue, UndefinedValue};

use crate::{Context, Error, ErrorKind, Function, Local, Object, Result};
pub use crate::class::native::{MAX_PROTO_CHAIN_LENGTH, NativeClass, PrototypeChain, TypeIdWrapper};
pub use crate::class::reflect::{Castable, DerivedFrom, NativeObject, Reflector};
use crate::conversions::ToValue;
use crate::function::NativeFunction;

mod native;
Expand Down Expand Up @@ -146,7 +147,7 @@ pub trait ClassDefinition: NativeObject {
}

fn get_private<'a>(cx: &Context, object: &Object<'a>) -> Result<&'a Self> {
if Self::instance_of(cx, object) {
if Self::instance_of(cx, object) || Self::has_instance(cx, object)? {
Ok(unsafe { Self::get_private_unchecked(object) })
} else {
Err(private_error(Self::class()))
Expand All @@ -164,7 +165,7 @@ pub trait ClassDefinition: NativeObject {

#[allow(clippy::mut_from_ref)]
fn get_mut_private<'a>(cx: &Context, object: &Object<'a>) -> Result<&'a mut Self> {
if Self::instance_of(cx, object) {
if Self::instance_of(cx, object) || Self::has_instance(cx, object)? {
Ok(unsafe { Self::get_mut_private_unchecked(object) })
} else {
Err(private_error(Self::class()))
Expand All @@ -188,6 +189,28 @@ pub trait ClassDefinition: NativeObject {
)
}
}

fn has_instance(cx: &Context, object: &Object) -> Result<bool> {
let infos = unsafe { &mut (*cx.get_inner_data().as_ptr()).class_infos };
let constructor =
Function::from(cx.root(infos.get(&TypeId::of::<Self>()).expect("Uninitialised Class").constructor))
.to_object(cx);
let object = object.as_value(cx);
let mut has_instance = false;
let result = unsafe {
JS_HasInstance(
cx.as_ptr(),
constructor.handle().into(),
object.handle().into(),
&mut has_instance,
)
};
if result {
Ok(has_instance)
} else {
Err(Error::none())
}
}
}

trait SpecZero {
Expand Down

0 comments on commit c9246e2

Please # to comment.