Skip to content

Commit

Permalink
Changed StableTraceable Trait Methods
Browse files Browse the repository at this point in the history
Deprecated `Context::root_*` Methods
  • Loading branch information
Redfire75369 committed Dec 5, 2023
1 parent 303831e commit 103a747
Show file tree
Hide file tree
Showing 34 changed files with 155 additions and 143 deletions.
2 changes: 1 addition & 1 deletion ion-proc/src/class/constructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub(super) fn impl_constructor(ion: &TokenStream, mut constructor: ItemFn, ty: &
let cx = &#ion::Context::new_unchecked(cx);
let args = &mut #ion::Arguments::new(cx, argc, vp);
let mut this = #ion::Object::from(
cx.root_object(
cx.root(
::mozjs::jsapi::JS_NewObjectForConstructor(cx.as_ptr(), &<#ty as #ion::ClassDefinition>::class().base, &args.call_args())
)
);
Expand Down
12 changes: 6 additions & 6 deletions ion/src/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,25 @@ pub struct BigInt {
impl BigInt {
/// Creates a [BigInt] from a boolean.
pub fn from_bool(cx: &Context, boolean: bool) -> BigInt {
BigInt::from(cx.root_bigint(unsafe { BigIntFromBool(cx.as_ptr(), boolean) }))
BigInt::from(cx.root(unsafe { BigIntFromBool(cx.as_ptr(), boolean) }))
}

/// Creates a [BigInt] from a 64-bit signed integer.
pub fn from_i64(cx: &Context, number: i64) -> BigInt {
BigInt::from(cx.root_bigint(unsafe { BigIntFromInt64(cx.as_ptr(), number) }))
BigInt::from(cx.root(unsafe { BigIntFromInt64(cx.as_ptr(), number) }))
}

/// Creates a [BigInt] from a 64-bit unsigned integer.
pub fn from_u64(cx: &Context, number: u64) -> BigInt {
BigInt::from(cx.root_bigint(unsafe { BigIntFromUint64(cx.as_ptr(), number) }))
BigInt::from(cx.root(unsafe { BigIntFromUint64(cx.as_ptr(), number) }))
}

/// Creates a [BigInt] from a double.
/// Returns an error if `number` is `NaN`, `Infinity`, `-Infinity` or contains a fractional component.
pub fn from_f64(cx: &Context, number: f64) -> Result<BigInt, Exception> {
let bi = unsafe { NumberToBigInt(cx.as_ptr(), number) };
if !bi.is_null() {
Ok(BigInt::from(cx.root_bigint(bi)))
Ok(BigInt::from(cx.root(bi)))
} else {
Err(Exception::new(cx).unwrap())
}
Expand Down Expand Up @@ -72,7 +72,7 @@ impl BigInt {
};
let bi = unsafe { StringToBigInt1(cx.as_ptr(), chars) };
if !bi.is_null() {
Ok(BigInt::from(cx.root_bigint(bi)))
Ok(BigInt::from(cx.root(bi)))
} else {
Err(Exception::new(cx))
}
Expand Down Expand Up @@ -109,7 +109,7 @@ impl BigInt {
None
} else {
let string = unsafe { BigIntToString(cx.as_ptr(), self.handle().into(), radix) };
Some(String::from(cx.root_string(string)))
Some(String::from(cx.root(string)))
}
}

Expand Down
4 changes: 2 additions & 2 deletions ion/src/class/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ pub trait ClassDefinition: NativeObject {
static_functions.as_ptr(),
)
};
let prototype = cx.root_object(class);
let prototype = cx.root(class);

let constructor = unsafe { JS_GetConstructor(cx.as_ptr(), prototype.handle().into()) };
let constructor = Object::from(cx.root_object(constructor));
let constructor = Object::from(cx.root(constructor));
let constructor = Function::from_object(cx, &constructor).unwrap();

let class_info = ClassInfo {
Expand Down
24 changes: 11 additions & 13 deletions ion/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ use crate::class::ClassInfo;
use crate::module::ModuleLoader;
use crate::Root;

pub trait StableTraceable: Traceable + Sized + 'static {
type Traced;

fn traced(&self) -> *const Self::Traced;
pub trait StableTraceable
where
Heap<Self::Traced>: Traceable,
{
type Traced: Copy + GCMethods + 'static;

fn traceable(&self) -> *const dyn Traceable;
fn heap(&self) -> &Heap<Self::Traced>;
}

impl<T: Copy + GCMethods + 'static> StableTraceable for Box<Heap<T>>
Expand All @@ -38,12 +39,8 @@ where
{
type Traced = T;

fn traced(&self) -> *const T {
self.get_unsafe().cast_const()
}

fn traceable(&self) -> *const dyn Traceable {
self.traced() as *const Heap<T> as *const _
fn heap(&self) -> &Heap<T> {
self
}
}

Expand Down Expand Up @@ -151,6 +148,7 @@ macro_rules! impl_root_methods {
($(($fn_name:ident, $pointer:ty, $key:ident, $gc_type:ident)$(,)?)*) => {
$(
#[doc = concat!("Roots a [", stringify!($pointer), "](", stringify!($pointer), ") as a ", stringify!($gc_type), " ands returns a [Local] to it.")]
#[deprecated]
pub fn $fn_name(&self, ptr: $pointer) -> Root<Box<Heap<$pointer>>> {
self.root(ptr)
}
Expand All @@ -170,8 +168,8 @@ impl Context {

unsafe {
let roots = &(*self.get_inner_data().as_ptr()).roots;
roots.root(heap.traceable());
Root::new(heap, NonNull::new(roots as *const _ as *mut _).unwrap())
roots.root(heap.heap());
Root::new(heap, roots)
}
}

Expand Down
14 changes: 7 additions & 7 deletions ion/src/conversions/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ impl_to_key_for_integer!(u32);

impl ToPropertyKey for *mut JSString {
fn to_key(&self, cx: &Context) -> Option<PropertyKey> {
String::from(cx.root_string(*self)).to_key(cx)
String::from(cx.root(*self)).to_key(cx)
}
}

impl ToPropertyKey for String {
fn to_key(&self, cx: &Context) -> Option<PropertyKey> {
rooted!(in(cx.as_ptr()) let mut key = VoidId());
if unsafe { JS_StringToId(cx.as_ptr(), self.handle().into(), key.handle_mut().into()) } {
Some(PropertyKey::from(cx.root_property_key(key.get())))
Some(PropertyKey::from(cx.root(key.get())))
} else {
None
}
Expand All @@ -72,7 +72,7 @@ impl ToPropertyKey for &str {

impl ToPropertyKey for *mut JSSymbol {
fn to_key(&self, cx: &Context) -> Option<PropertyKey> {
Some(cx.root_property_key(SymbolId(*self)).into())
Some(cx.root(SymbolId(*self)).into())
}
}

Expand All @@ -90,15 +90,15 @@ impl ToPropertyKey for WellKnownSymbolCode {

impl ToPropertyKey for JSVal {
fn to_key(&self, cx: &Context) -> Option<PropertyKey> {
Value::from(cx.root_value(*self)).to_key(cx)
Value::from(cx.root(*self)).to_key(cx)
}
}

impl ToPropertyKey for Value {
fn to_key(&self, cx: &Context) -> Option<PropertyKey> {
rooted!(in(cx.as_ptr()) let mut key = VoidId());
if unsafe { JS_ValueToId(cx.as_ptr(), self.handle().into(), key.handle_mut().into()) } {
Some(PropertyKey::from(cx.root_property_key(key.get())))
Some(PropertyKey::from(cx.root(key.get())))
} else {
None
}
Expand All @@ -107,7 +107,7 @@ impl ToPropertyKey for Value {

impl ToPropertyKey for JSPropertyKey {
fn to_key(&self, cx: &Context) -> Option<PropertyKey> {
Some(cx.root_property_key(*self).into())
Some(cx.root(*self).into())
}
}

Expand All @@ -123,7 +123,7 @@ impl ToPropertyKey for OwnedKey {
OwnedKey::Int(i) => i.to_key(cx),
OwnedKey::String(str) => str.to_key(cx),
OwnedKey::Symbol(symbol) => symbol.to_key(cx),
OwnedKey::Void => Some(cx.root_property_key(VoidId()).into()),
OwnedKey::Void => Some(cx.root(VoidId()).into()),
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions ion/src/conversions/value/from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl FromValue for crate::String {
type Config = ();

fn from_value(cx: &Context, value: &Value, strict: bool, config: ()) -> Result<crate::String> {
<*mut JSString>::from_value(cx, value, strict, config).map(|str| crate::String::from(cx.root_string(str)))
<*mut JSString>::from_value(cx, value, strict, config).map(|str| crate::String::from(cx.root(str)))
}
}

Expand Down Expand Up @@ -315,7 +315,7 @@ impl FromValue for Symbol {
type Config = ();

fn from_value(cx: &Context, value: &Value, strict: bool, config: Self::Config) -> Result<Symbol> {
<*mut JSSymbol>::from_value(cx, value, strict, config).map(|s| cx.root_symbol(s).into())
<*mut JSSymbol>::from_value(cx, value, strict, config).map(|s| cx.root(s).into())
}
}

Expand All @@ -339,7 +339,7 @@ impl FromValue for Value {
unsafe {
AssertSameCompartment1(cx.as_ptr(), value.into());
}
Ok(cx.root_value(value.get()).into())
Ok(cx.root(value.get()).into())
}
}

Expand All @@ -362,7 +362,7 @@ struct ForOfIteratorGuard<'a> {

impl<'a> ForOfIteratorGuard<'a> {
fn new(cx: &Context, root: &'a mut ForOfIterator) -> Self {
cx.root_object(root.iterator.ptr);
cx.root(root.iterator.ptr);
ForOfIteratorGuard { root }
}
}
Expand Down Expand Up @@ -436,7 +436,7 @@ impl<T: TypedArrayElement, S: JSObjectStorage> FromValue for TypedArray<T, S> {
let value = value.handle();
if value.is_object() {
let object = value.to_object();
cx.root_object(object);
cx.root(object);
TypedArray::from(object).map_err(|_| Error::new("Expected Typed Array", ErrorKind::Type))
} else {
Err(Error::new("Expected Object", ErrorKind::Type))
Expand Down
10 changes: 5 additions & 5 deletions ion/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl Error {

pub fn to_object(&self, cx: &Context) -> Option<Object> {
if let Some(object) = self.object {
return Some(cx.root_object(object).into());
return Some(cx.root(object).into());
}
if self.kind != ErrorKind::None {
unsafe {
Expand All @@ -150,17 +150,17 @@ impl Error {
.map(|location| (&*location.file, location.lineno, location.column))
.unwrap_or_default();

let stack = Object::from(cx.root_object(stack.object.unwrap()));
let stack = Object::from(cx.root(stack.object.unwrap()));

let file = file.to_value(cx).unwrap();

let file_name = cx.root_string(file.handle().to_string());
let file_name = cx.root(file.handle().to_string());

let message = (!self.message.is_empty()).then(|| {
let value = self.message.to_value(cx).unwrap();
crate::String::from(cx.root_string(value.handle().to_string()))
crate::String::from(cx.root(value.handle().to_string()))
});
let message = message.unwrap_or_else(|| crate::String::from(cx.root_string(ptr::null_mut())));
let message = message.unwrap_or_else(|| crate::String::from(cx.root(ptr::null_mut())));

rooted!(in(cx.as_ptr()) let mut error = UndefinedValue());

Expand Down
8 changes: 4 additions & 4 deletions ion/src/exception.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl Exception {
Exception::Other(value) => {
format!(
"Uncaught Exception - {}",
format_value(cx, Config::default(), &cx.root_value(*value).into())
format_value(cx, Config::default(), &cx.root(*value).into())
)
}
}
Expand All @@ -137,7 +137,7 @@ impl ThrowException for Exception {
match self {
Exception::Error(error) => {
if let Error { object: Some(object), .. } = error {
let exception = Value::from(cx.root_value(ObjectValue(*object)));
let exception = Value::from(cx.root(ObjectValue(*object)));
unsafe {
JS_SetPendingException(
cx.as_ptr(),
Expand All @@ -150,7 +150,7 @@ impl ThrowException for Exception {
}
}
Exception::Other(value) => {
let value = Value::from(cx.root_value(*value));
let value = Value::from(cx.root(*value));
unsafe { JS_SetPendingException(cx.as_ptr(), value.handle().into(), ExceptionStackBehavior::Capture) }
}
}
Expand Down Expand Up @@ -202,7 +202,7 @@ impl ErrorReport {
stack_: Rooted::new_unrooted(),
};
if GetPendingExceptionStack(cx.as_ptr(), &mut exception_stack) {
let exception = Value::from(cx.root_value(exception_stack.exception_.ptr));
let exception = Value::from(cx.root(exception_stack.exception_.ptr));
let exception = Exception::from_value(cx, &exception);
let stack = Stack::from_object(cx, exception_stack.stack_.ptr);
Exception::clear(cx);
Expand Down
2 changes: 1 addition & 1 deletion ion/src/format/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl Display for ObjectDisplay<'_> {

let cx = self.cx;
let cfg = self.cfg;
let object = Object::from(cx.root_object(self.object.handle().get()));
let object = Object::from(cx.root(self.object.handle().get()));

let class = self.object.get_builtin_class(cx);

Expand Down
4 changes: 2 additions & 2 deletions ion/src/format/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ impl Display for PrimitiveDisplay<'_> {
} else if value.is_undefined() {
write!(f, "{}", "undefined".color(colours.undefined))
} else if value.is_bigint() {
let bi = BigInt::from(self.cx.root_bigint(value.to_bigint()));
let bi = BigInt::from(self.cx.root(value.to_bigint()));
write!(
f,
"{}{}",
bi.to_string(self.cx, 10).unwrap().to_owned(self.cx).color(colours.bigint),
"n".color(colours.bigint)
)
} else if value.is_symbol() {
let symbol = Symbol::from(self.cx.root_symbol(value.to_symbol()));
let symbol = Symbol::from(self.cx.root(value.to_symbol()));
write!(f, "{}", format_symbol(self.cx, self.cfg, &symbol))
} else if value.is_magic() {
write!(f, "{}", "<magic>".color(colours.boolean))
Expand Down
6 changes: 3 additions & 3 deletions ion/src/functions/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub type Closure = dyn FnMut(&mut Arguments) -> ResultExc<Value> + 'static;

pub(crate) fn create_closure_object(cx: &Context, closure: Box<Closure>) -> Object {
unsafe {
let object = Object::from(cx.root_object(JS_NewObject(cx.as_ptr(), &CLOSURE_CLASS)));
let object = Object::from(cx.root(JS_NewObject(cx.as_ptr(), &CLOSURE_CLASS)));
JS_SetReservedSlot(
object.handle().get(),
CLOSURE_SLOT,
Expand All @@ -38,8 +38,8 @@ pub(crate) unsafe extern "C" fn call_closure(cx: *mut JSContext, argc: u32, vp:
let cx = &unsafe { Context::new_unchecked(cx) };
let args = &mut unsafe { Arguments::new(cx, argc, vp) };

let callee = cx.root_object(args.call_args().callee());
let reserved = cx.root_value(unsafe { *GetFunctionNativeReserved(callee.get(), 0) });
let callee = cx.root(args.call_args().callee());
let reserved = cx.root(unsafe { *GetFunctionNativeReserved(callee.get(), 0) });

let mut value = UndefinedValue();
unsafe { JS_GetReservedSlot(reserved.handle().to_object(), CLOSURE_SLOT, &mut value) };
Expand Down
11 changes: 5 additions & 6 deletions ion/src/functions/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,22 @@ impl Function {
pub fn new(cx: &Context, name: &str, func: Option<NativeFunction>, nargs: u32, flags: PropertyFlags) -> Function {
let name = CString::new(name).unwrap();
Function {
function: cx
.root_function(unsafe { JS_NewFunction(cx.as_ptr(), func, nargs, flags.bits() as u32, name.as_ptr()) }),
function: cx.root(unsafe { JS_NewFunction(cx.as_ptr(), func, nargs, flags.bits() as u32, name.as_ptr()) }),
}
}

/// Creates a new [Function] with the given [`spec`](JSFunctionSpec).
pub fn from_spec(cx: &Context, spec: &JSFunctionSpec) -> Function {
Function {
function: cx.root_function(unsafe { NewFunctionFromSpec1(cx.as_ptr(), spec) }),
function: cx.root(unsafe { NewFunctionFromSpec1(cx.as_ptr(), spec) }),
}
}

/// Creates a new [Function] with a [Closure].
pub fn from_closure(cx: &Context, name: &str, closure: Box<Closure>, nargs: u32, flags: PropertyFlags) -> Function {
unsafe {
let function = Function {
function: cx.root_function(NewFunctionWithReserved(
function: cx.root(NewFunctionWithReserved(
cx.as_ptr(),
Some(call_closure),
nargs,
Expand All @@ -75,7 +74,7 @@ impl Function {
pub fn from_object(cx: &Context, obj: &Root<Box<Heap<*mut JSObject>>>) -> Option<Function> {
if unsafe { Function::is_function_raw(obj.get()) } {
Some(Function {
function: cx.root_function(unsafe { JS_GetObjectFunction(obj.get()) }),
function: cx.root(unsafe { JS_GetObjectFunction(obj.get()) }),
})
} else {
None
Expand All @@ -84,7 +83,7 @@ impl Function {

/// Converts the [Function] into an [Object].
pub fn to_object(&self, cx: &Context) -> Object {
cx.root_object(unsafe { JS_GetFunctionObject(self.get()) }).into()
cx.root(unsafe { JS_GetFunctionObject(self.get()) }).into()
}

/// Converts the [Function] into a [String] in the form of its definition/source.
Expand Down
Loading

0 comments on commit 103a747

Please sign in to comment.