Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into merge-upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
Arshia001 committed Mar 11, 2024
2 parents b738a6a + a0f397d commit 4354acd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 32 deletions.
40 changes: 21 additions & 19 deletions ion/src/class/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use mozjs_sys::jsapi::JS_GetFunctionObject;
use crate::{Context, Error, ErrorKind, Function, Local, Object, Value, Exception, 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 @@ -204,9 +205,7 @@ pub trait ClassDefinition: NativeObject {
}

fn get_private<'a>(cx: &Context, object: &Object<'a>) -> Result<&'a Self> {
if Self::instance_of(cx, object) // Fast path, most native objects don't have base classes and this check is way faster
|| Self::instance_or_subtype_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 @@ -224,9 +223,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) // Fast path, most native objects don't have base classes and this check is way faster
|| Self::instance_or_subtype_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 @@ -251,21 +248,26 @@ pub trait ClassDefinition: NativeObject {
}
}

fn instance_or_subtype_of(cx: &Context, object: &Object) -> Result<bool> {
let constructor: Function = cx.root(Self::class_info(cx).constructor).into();
let constructor_obj = constructor.to_object(cx);
let mut result = false;
unsafe {
if !JS_HasInstance(
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_obj.handle().into(),
Value::object(cx, object).handle().into(),
&mut result as *mut bool,
) {
return Err(Error::none());
}
constructor.handle().into(),
object.handle().into(),
&mut has_instance,
)
};
if result {
Ok(has_instance)
} else {
Err(Error::none())
}
Ok(result)
}
}

Expand Down
16 changes: 4 additions & 12 deletions ion/src/function/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,11 @@ pub(crate) unsafe extern "C" fn call_closure(cx: *mut JSContext, argc: u32, vp:
__handle_native_function_result(cx, result)
}

unsafe extern "C" fn finalise_closure_once(_: *mut GCContext, object: *mut JSObject) {
unsafe extern "C" fn finalise_closure<T>(_: *mut GCContext, object: *mut JSObject) {
let mut value = UndefinedValue();
unsafe {
JS_GetReservedSlot(object, CLOSURE_SLOT, &mut value);
let _ = Box::from_raw(value.to_private().cast::<ClosureOncePrivate>().cast_mut());
}
}

unsafe extern "C" fn finalise_closure(_: *mut GCContext, object: *mut JSObject) {
let mut value = UndefinedValue();
unsafe {
JS_GetReservedSlot(object, CLOSURE_SLOT, &mut value);
let _ = Box::from_raw(value.to_private().cast::<ClosurePrivate>().cast_mut());
let _ = Box::from_raw(value.to_private().cast::<T>().cast_mut());
}
}

Expand All @@ -118,7 +110,7 @@ static CLOSURE_ONCE_OPS: JSClassOps = JSClassOps {
newEnumerate: None,
resolve: None,
mayResolve: None,
finalize: Some(finalise_closure_once),
finalize: Some(finalise_closure::<ClosureOncePrivate>),
call: None,
construct: None,
trace: None,
Expand All @@ -140,7 +132,7 @@ static CLOSURE_OPS: JSClassOps = JSClassOps {
newEnumerate: None,
resolve: None,
mayResolve: None,
finalize: Some(finalise_closure),
finalize: Some(finalise_closure::<ClosurePrivate>),
call: None,
construct: None,
trace: None,
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/globals/console/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,11 @@ pub(crate) fn format_args<'cx>(cx: &'cx Context, args: &'cx [Value<'cx>]) -> Vec
};

if args.peek().is_none() {
output.push_str(&format[index..]);
break;
}
}

output.push_str(&format[index..]);
outputs.push(FormatArg::String(output));
outputs.extend(format_value_args(cx, args));
Ok(outputs)
Expand Down

0 comments on commit 4354acd

Please sign in to comment.