Skip to content

Commit

Permalink
Updated Runtime and Modules Crate with New Rooting Mechanism
Browse files Browse the repository at this point in the history
Replaces FromValue Implementation for Native Object References with Pointers
Fixed Runtime Dependencies
Fixed FromValue Derive Macro
Fixed Cow<T> ToValue Implementation
Fixed Conversions Test
Removed Lifetimes from FromValue
  • Loading branch information
Redfire75369 committed Dec 1, 2023
1 parent 52bb5dc commit f6f0c1f
Show file tree
Hide file tree
Showing 37 changed files with 409 additions and 441 deletions.
43 changes: 4 additions & 39 deletions ion-proc/src/class/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
use std::ffi::CString;

use proc_macro2::{Ident, Span, TokenStream};
use syn::{Error, Fields, ImplItemFn, ItemImpl, ItemStruct, LitStr, Member, parse2, Path, Result, Type};
use syn::{Error, Fields, ImplItemFn, ItemImpl, ItemStruct, Member, parse2, Path, Result, Type};
use syn::punctuated::Punctuated;
use syn::spanned::Spanned;

use crate::attribute::krate::crate_from_attributes;
use crate::utils::path_ends_with;

pub(super) fn impl_js_class_struct(r#struct: &mut ItemStruct) -> Result<[ItemImpl; 6]> {
pub(super) fn impl_js_class_struct(r#struct: &mut ItemStruct) -> Result<[ItemImpl; 4]> {
let ion = &crate_from_attributes(&r#struct.attrs);

let repr_c = r#struct.attrs.iter().fold(Ok(false), |acc, attr| {
Expand Down Expand Up @@ -96,10 +96,7 @@ pub(super) fn impl_js_class_struct(r#struct: &mut ItemStruct) -> Result<[ItemImp

fn class_impls(
ion: &TokenStream, span: Span, name: &str, r#type: &Type, super_field: &Member, super_type: &Type,
) -> Result<[ItemImpl; 6]> {
let from_value = impl_from_value(ion, span, name, r#type, false)?;
let from_value_mut = impl_from_value(ion, span, name, r#type, true)?;

) -> Result<[ItemImpl; 4]> {
let derived_from = quote_spanned!(span => unsafe impl #ion::class::DerivedFrom<#super_type> for #r#type {});
let derived_from = parse2(derived_from)?;
let castable = parse2(quote_spanned!(span => impl #ion::class::Castable for #r#type {}))?;
Expand Down Expand Up @@ -166,39 +163,7 @@ fn class_impls(
}))?;
operations_native_class.attrs.push(parse_quote!(#[doc(hidden)]));

Ok([
from_value,
from_value_mut,
derived_from,
castable,
native_object,
operations_native_class,
])
}

fn impl_from_value(ion: &TokenStream, span: Span, name: &str, r#type: &Type, mutable: bool) -> Result<ItemImpl> {
let from_value_error = LitStr::new(&format!("Expected {}", name), span);
let function = if mutable {
quote!(get_mut_private)
} else {
quote!(get_private)
};
let mutable = mutable.then(<Token![mut]>::default);

parse2(
quote_spanned!(span => impl<'cx> #ion::conversions::FromValue<'cx> for &'cx #mutable #r#type {
type Config = ();

fn from_value(cx: &'cx #ion::Context, value: &#ion::Value, strict: ::core::primitive::bool, _: ()) -> #ion::Result<&'cx #mutable #r#type> {
let #mutable object = #ion::Object::from_value(cx, value, strict, ())?;
if <#r#type as #ion::class::ClassDefinition>::instance_of(cx, &object, None) {
Ok(<#r#type as #ion::class::ClassDefinition>::#function(&#mutable object))
} else {
Err(#ion::Error::new(#from_value_error, #ion::ErrorKind::Type))
}
}
}),
)
Ok([derived_from, castable, native_object, operations_native_class])
}

fn class_operations(span: Span) -> Result<Vec<ImplItemFn>> {
Expand Down
19 changes: 4 additions & 15 deletions ion-proc/src/value/from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use convert_case::{Case, Casing};
use proc_macro2::{Ident, Span, TokenStream};
use syn::{Block, Data, DeriveInput, Error, Field, Fields, GenericParam, Generics, ItemImpl, Meta, parse2, Result, Type};
use syn::{Block, Data, DeriveInput, Error, Field, Fields, Generics, ItemImpl, Meta, parse2, Result, Type};
use syn::punctuated::Punctuated;
use syn::spanned::Spanned;

Expand All @@ -19,18 +19,7 @@ pub(crate) fn impl_from_value(mut input: DeriveInput) -> Result<ItemImpl> {

add_trait_bounds(&mut input.generics, &parse_quote!(#ion::conversions::FromValue));
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
let mut impl_generics: Generics = parse2(quote_spanned!(impl_generics.span() => #impl_generics))?;

let has_cx = impl_generics.params.iter().any(|param| {
if let GenericParam::Lifetime(lt) = param {
lt.lifetime == parse_quote!('cx)
} else {
false
}
});
if !has_cx {
impl_generics.params.push(parse2(quote!('cx))?);
}
let impl_generics: Generics = parse2(quote_spanned!(impl_generics.span() => #impl_generics))?;

let mut tag = Tag::default();
let mut inherit = false;
Expand Down Expand Up @@ -86,10 +75,10 @@ pub(crate) fn impl_from_value(mut input: DeriveInput) -> Result<ItemImpl> {

parse2(quote_spanned!(input.span() =>
#[automatically_derived]
impl #impl_generics #ion::conversions::FromValue<'cx> for #name #ty_generics #where_clause {
impl #impl_generics #ion::conversions::FromValue for #name #ty_generics #where_clause {
type Config = ();

fn from_value(cx: &'cx #ion::Context, value: &#ion::Value, strict: bool, _: ()) -> #ion::Result<Self> {
fn from_value(cx: &#ion::Context, value: &#ion::Value, strict: bool, _: ()) -> #ion::Result<Self> {
#object
#body
}
Expand Down
29 changes: 28 additions & 1 deletion ion/src/class/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ use mozjs::jsapi::{
};
use mozjs::jsval::{PrivateValue, UndefinedValue};

use crate::{Arguments, Context, Function, Object, Root};
use crate::{Arguments, Context, Error, ErrorKind, Function, Object, Result, Root, Value};
pub use crate::class::native::{MAX_PROTO_CHAIN_LENGTH, NativeClass, PrototypeChain, TypeIdWrapper};
pub use crate::class::reflect::{Castable, DerivedFrom, NativeObject, Reflector};
use crate::conversions::FromValue;
use crate::functions::NativeFunction;

mod native;
Expand Down Expand Up @@ -160,3 +161,29 @@ pub trait ClassDefinition: NativeObject {
}
}
}

impl<T: ClassDefinition> FromValue for *const T {
type Config = ();

fn from_value(cx: &Context, value: &Value, strict: bool, _: ()) -> Result<*const T> {
let object = Object::from_value(cx, value, strict, ())?;
if T::instance_of(cx, &object, None) {
Ok(T::get_private(&object))
} else {
Err(Error::new(&format!("Expected {}", T::NAME), ErrorKind::Type))
}
}
}

impl<T: ClassDefinition> FromValue for *mut T {
type Config = ();

fn from_value(cx: &Context, value: &Value, strict: bool, _: ()) -> Result<*mut T> {
let mut object = Object::from_value(cx, value, strict, ())?;
if T::instance_of(cx, &object, None) {
Ok(T::get_mut_private(&mut object))
} else {
Err(Error::new(&format!("Expected {}", T::NAME), ErrorKind::Type))
}
}
}
12 changes: 6 additions & 6 deletions ion/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ use crate::module::ModuleLoader;
use crate::Root;

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

fn stable_trace(&self) -> *const Self::Trace;
fn traced(&self) -> *const Self::Traced;

fn traceable(&self) -> *const dyn Traceable;
}
Expand All @@ -36,14 +36,14 @@ impl<T: Copy + GCMethods + 'static> StableTraceable for Box<Heap<T>>
where
Heap<T>: Traceable,
{
type Trace = T;
type Traced = T;

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

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

Expand Down Expand Up @@ -171,7 +171,7 @@ 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 _))
Root::new(heap, NonNull::new(roots as *const _ as *mut _).unwrap())
}
}

Expand Down
Loading

0 comments on commit f6f0c1f

Please sign in to comment.