Skip to content

Commit

Permalink
Added From Value Implementations for References to Native Classes
Browse files Browse the repository at this point in the history
Added More Targets to Unused Dependency Check in CI
  • Loading branch information
Redfire75369 committed Nov 15, 2023
1 parent 3f7439a commit 0d69797
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 57 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,4 @@ jobs:
- name: Check for Unused Dependencies
env:
RUSTC_WRAPPER: sccache
run: cargo udeps
run: cargo udeps --all-targets --locked
32 changes: 28 additions & 4 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, Member, parse2, Path, Result, Type};
use syn::{Error, Fields, ImplItemFn, ItemImpl, ItemStruct, LitStr, 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; 4]> {
pub(super) fn impl_js_class_struct(r#struct: &mut ItemStruct) -> Result<[ItemImpl; 6]> {
let ion = &crate_from_attributes(&r#struct.attrs);

let repr_c = r#struct.attrs.iter().fold(Ok(false), |acc, attr| {
Expand Down Expand Up @@ -81,7 +81,10 @@ pub(super) fn impl_js_class_struct(r#struct: &mut ItemStruct) -> Result<[ItemImp
class_impls(ion, r#struct.span(), &ident.to_string(), &r#type, &super_field, &super_type)
}

fn class_impls(ion: &TokenStream, span: Span, name: &str, r#type: &Type, super_field: &Member, super_type: &Type) -> Result<[ItemImpl; 4]> {
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)?;

let derived_from = parse2(quote_spanned!(span => unsafe impl #ion::class::DerivedFrom<#super_type> for #r#type {}))?;
let castable = parse2(quote_spanned!(span => impl #ion::class::Castable for #r#type {}))?;

Expand Down Expand Up @@ -147,7 +150,28 @@ fn class_impls(ion: &TokenStream, span: Span, name: &str, r#type: &Type, super_f
}))?;
operations_native_class.attrs.push(parse_quote!(#[doc(hidden)]));

Ok([derived_from, castable, native_object, operations_native_class])
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))
}
}
}),
)
}

fn class_operations(span: Span) -> Result<Vec<ImplItemFn>> {
Expand Down
12 changes: 0 additions & 12 deletions runtime/src/globals/fetch/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,18 +262,6 @@ impl Headers {
}
}

impl<'cx> FromValue<'cx> for &'cx Headers {
type Config = ();
fn from_value(cx: &'cx Context, value: &Value, strict: bool, _: ()) -> Result<&'cx Headers> {
let object = Object::from_value(cx, value, strict, ())?;
if Headers::instance_of(cx, &object, None) {
Ok(Headers::get_private(&object))
} else {
Err(Error::new("Expected Headers", ErrorKind::Type))
}
}
}

pub struct HeadersIterator {
keys: vec::IntoIter<String>,
cookies: vec::IntoIter<HeaderValue>,
Expand Down
15 changes: 1 addition & 14 deletions runtime/src/globals/fetch/request/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ use hyper::{Body, Method, Uri};
use mozjs::jsapi::{Heap, JSObject};
use url::Url;

use ion::{ClassDefinition, Context, Error, ErrorKind, Object, Result, Value};
use ion::{ClassDefinition, Context, Error, ErrorKind, Result};
use ion::class::Reflector;
use ion::conversions::FromValue;
pub use options::*;

use crate::globals::abort::AbortSignal;
Expand Down Expand Up @@ -340,15 +339,3 @@ impl Clone for Request {
}
}
}

impl<'cx> FromValue<'cx> for &'cx Request {
type Config = ();
fn from_value(cx: &'cx Context, value: &Value, _: bool, _: ()) -> Result<&'cx Request> {
let object = Object::from_value(cx, value, true, ())?;
if Request::instance_of(cx, &object, None) {
Ok(Request::get_private(&object))
} else {
Err(Error::new("Expected Request", ErrorKind::Type))
}
}
}
13 changes: 0 additions & 13 deletions runtime/src/globals/file/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,3 @@ impl Blob {
future_to_promise(cx, async move { Ok::<_, ()>(ion::typedarray::ArrayBuffer::from(bytes.to_vec())) })
}
}

impl<'cx> FromValue<'cx> for &'cx Blob {
type Config = ();

fn from_value(cx: &'cx Context, value: &Value, _: bool, _: ()) -> Result<&'cx Blob> {
let object = Object::from_value(cx, value, true, ())?;
if Blob::instance_of(cx, &object, None) {
Ok(Blob::get_private(&object))
} else {
Err(Error::new("Expected Blob", ErrorKind::Type))
}
}
}
13 changes: 0 additions & 13 deletions runtime/src/globals/url/search_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,19 +180,6 @@ impl URLSearchParams {
}
}

impl<'cx> FromValue<'cx> for &'cx URLSearchParams {
type Config = ();

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

#[derive(Default)]
pub struct SearchParamsIterator(usize);

Expand Down

0 comments on commit 0d69797

Please sign in to comment.