Skip to content

Commit

Permalink
Merge branch 'master' into fix-prototype-chain
Browse files Browse the repository at this point in the history
  • Loading branch information
Arshia001 authored Jan 16, 2024
2 parents 81ceae5 + 403dd73 commit dd003b1
Show file tree
Hide file tree
Showing 110 changed files with 1,786 additions and 1,640 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ jobs:
mv ./target/$TARGET/release/cli ./target/$TARGET/release/spiderfire
- name: Upload Executables as Artifacts
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: spiderfire-${{ github.sha }}-${{ matrix.id }}
path: target/${{ matrix.target }}/release/spiderfire${{ matrix.id == 'windows' && '.exe' || '' }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ jobs:
strip ./target/release/spiderfire
- name: Upload Executables as Artifacts
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
if: matrix.rust == 'stable'
with:
name: spiderfire-${{ github.sha }}-${{ matrix.id }}
Expand Down
23 changes: 17 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cli/src/evaluate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use runtime::{Runtime, RuntimeBuilder};
use runtime::cache::locate_in_cache;
use runtime::cache::map::{save_sourcemap, transform_error_report_with_sourcemaps};
use runtime::config::Config;
use runtime::modules::Loader;
use runtime::module::Loader;

pub(crate) async fn eval_inline(rt: &Runtime<'_>, source: &str) {
let result = Script::compile_and_evaluate(rt.cx(), Path::new("inline.js"), source);
Expand Down
2 changes: 1 addition & 1 deletion ion-proc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ prettyplease = "0.2.15"
proc-macro2 = "1.0.71"

[dependencies.syn]
version = "2.0.42"
version = "2.0.45"
features = ["visit-mut"]

[lints]
Expand Down
51 changes: 7 additions & 44 deletions ion-proc/src/attribute/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,58 +6,21 @@

use syn::{LitStr, Result};
use syn::meta::ParseNestedMeta;
use syn::parse::{Parse, ParseStream};

use crate::attribute::{ArgumentError, ParseArgument, ParseArgumentWith, ParseAttribute};
use crate::attribute::name::Name;
use crate::class::method::MethodKind;

mod keywords {
custom_keyword!(name);
custom_keyword!(class);
}

#[allow(dead_code)]
pub(crate) struct ClassNameAttribute {
_kw: keywords::name,
_eq: Token![=],
pub(crate) name: LitStr,
}

impl Parse for ClassNameAttribute {
fn parse(input: ParseStream) -> Result<ClassNameAttribute> {
let lookahead = input.lookahead1();
if lookahead.peek(keywords::name) {
Ok(ClassNameAttribute {
_kw: input.parse()?,
_eq: input.parse()?,
name: input.parse()?,
})
} else {
Err(lookahead.error())
}
}
}

// TODO: Add `inspectable` to provide `toString` and `toJSON`
#[allow(dead_code)]
pub(crate) enum ClassAttribute {
Name(ClassNameAttribute),
Class(keywords::class),
#[derive(Default)]
pub(crate) struct ClassAttribute {
pub(crate) name: Option<LitStr>,
}

impl Parse for ClassAttribute {
fn parse(input: ParseStream) -> Result<ClassAttribute> {
use ClassAttribute as CA;

let lookahead = input.lookahead1();
if lookahead.peek(keywords::name) {
Ok(CA::Name(input.parse()?))
} else if lookahead.peek(keywords::class) {
Ok(CA::Class(input.parse()?))
} else {
Err(lookahead.error())
}
impl ParseAttribute for ClassAttribute {
fn parse(&mut self, meta: &ParseNestedMeta) -> Result<()> {
self.name.parse_argument(meta, "name", "Class")?;
Ok(())
}
}

Expand Down
10 changes: 2 additions & 8 deletions ion-proc/src/attribute/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,16 @@ use crate::attribute::{ParseArgument, ParseAttribute};
#[derive(Default)]
pub(crate) struct ParameterAttribute {
pub(crate) this: bool,
pub(crate) varargs: bool,
pub(crate) convert: Option<Box<Expr>>,
pub(crate) strict: bool,
}

impl ParseAttribute for ParameterAttribute {
fn parse(&mut self, meta: &ParseNestedMeta) -> Result<()> {
self.this.parse_argument(meta, "this", "Parameter")?;
self.varargs.parse_argument(meta, "varargs", "Parameter")?;
self.convert.parse_argument(meta, "convert", "Parameter")?;
self.strict.parse_argument(meta, "strict", "Parameter")?;

if self.this && (self.varargs || self.convert.is_some() || self.strict) {
return Err(
meta.error("Parameter with `this` attribute cannot have `varargs`, `convert`, or `strict` attributes.")
);
if self.this && self.convert.is_some() {
return Err(meta.error("Parameter with `this` attribute cannot have `convert` attributes."));
}

Ok(())
Expand Down
15 changes: 6 additions & 9 deletions ion-proc/src/class/accessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use syn::{Error, ItemFn, Result, Type};
use syn::spanned::Spanned;

use crate::class::method::{impl_method, Method};
use crate::function::parameters::{Parameter, Parameters};
use crate::function::parameter::Parameters;

pub(super) struct Accessor(pub(super) Option<Method>, Option<Method>);

Expand Down Expand Up @@ -98,14 +98,11 @@ pub(super) fn impl_accessor(
};
let (mut accessor, parameters) = impl_method(ion, method, ty, |sig| {
let parameters = Parameters::parse(&sig.inputs, Some(ty))?;
let nargs = parameters.parameters.iter().fold(0, |mut acc, param| {
if let Parameter::Regular { pat_ty, .. } = &param {
if let Type::Path(_) = &*pat_ty.ty {
acc += 1;
}
}
acc
});
let nargs: i32 = parameters
.parameters
.iter()
.map(|param| matches!(&*param.pat_ty.ty, Type::Path(_)) as i32)
.sum();
(nargs == expected_args).then_some(()).ok_or(error)
})?;
accessor.method.sig.ident = ident;
Expand Down
6 changes: 3 additions & 3 deletions ion-proc/src/class/constructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub(super) fn impl_constructor(ion: &TokenStream, mut constructor: ItemFn, ty: &
let args = &mut #ion::Arguments::new(cx, argc, vp);
let mut this = #ion::Object::from(
cx.root_object(
::mozjs::jsapi::JS_NewObjectForConstructor(cx.as_ptr(), &<#ty as #ion::ClassDefinition>::class().base, &args.call_args())
::mozjs::jsapi::JS_NewObjectForConstructor(cx.as_ptr(), &<#ty as #ion::ClassDefinition>::class().base, args.call_args())
)
);

Expand All @@ -38,15 +38,15 @@ pub(super) fn impl_constructor(ion: &TokenStream, mut constructor: ItemFn, ty: &
wrapper(cx, args, &mut this)
}));

#ion::functions::__handle_native_constructor_result(cx, result, &this, args.rval())
#ion::function::__handle_native_constructor_result(cx, result, &this, &mut args.rval())
});
constructor.block = body;
constructor.sig.ident = format_ident!("__ion_bindings_constructor", span = constructor.sig.ident.span());

let method = Method {
receiver: MethodReceiver::Static,
method: constructor,
nargs: parameters.nargs.0,
nargs: parameters.nargs,
names: vec![],
};
Ok(method)
Expand Down
34 changes: 13 additions & 21 deletions ion-proc/src/class/impl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::attribute::ParseAttribute;
use crate::class::accessor::{get_accessor_name, impl_accessor, insert_accessor};
use crate::class::constructor::impl_constructor;
use crate::class::method::{impl_method, Method, MethodKind, MethodReceiver};
use crate::class::property::Property;
use crate::class::property::{Property, PropertyType};
use crate::class::r#impl::spec::PrototypeSpecs;

mod spec;
Expand Down Expand Up @@ -71,6 +71,13 @@ pub(super) fn impl_js_class_impl(r#impl: &mut ItemImpl) -> Result<[ItemImpl; 2]>
_ => (),
}
}
specs.properties.0.push(Property {
ty: PropertyType::String,
ident: parse_quote!(__ION_TO_STRING_TAG),
names: vec![Name::Symbol(
parse_quote!(#ion::symbol::WellKnownSymbolCode::ToStringTag),
)],
});

let constructor = match constructor {
Some(constructor) => constructor,
Expand Down Expand Up @@ -170,21 +177,19 @@ fn parse_class_method(
fn class_definition(
ion: &TokenStream, span: Span, r#type: &Type, ident: &Ident, constructor: Method, specs: PrototypeSpecs,
) -> Result<[ItemImpl; 2]> {
let spec_functions = specs.to_spec_functions(ion, span, ident)?.into_array();
let (spec_fns, def_fns) = specs.to_impl_fns(ion, span, ident)?;
let constructor_function = constructor.method;
let functions = specs.into_functions().into_iter().map(|method| method.method);

let mut spec_impls: ItemImpl = parse2(quote_spanned!(span => impl #r#type {
#constructor_function
#(#functions)*
#(#spec_functions)*
#(#spec_fns)*
}))?;
spec_impls.attrs.push(parse_quote!(#[doc(hidden)]));

let constructor_nargs = constructor.nargs as u32;
let class_definition = parse2(quote_spanned!(span => impl #ion::ClassDefinition for #r#type {
const NAME: &'static str = ::std::stringify!(#ident);

fn class() -> &'static #ion::class::NativeClass {
Self::__ion_native_class()
}
Expand All @@ -193,25 +198,12 @@ fn class_definition(
Self::__ion_parent_class_info(cx)
}

fn constructor() -> (#ion::functions::NativeFunction, ::core::primitive::u32) {
fn constructor() -> (#ion::function::NativeFunction, ::core::primitive::u32) {
(Self::__ion_bindings_constructor, #constructor_nargs)
}

fn functions() -> &'static [::mozjs::jsapi::JSFunctionSpec] {
Self::__ion_function_specs()
}

fn properties() -> &'static [::mozjs::jsapi::JSPropertySpec] {
Self::__ion_property_specs()
}

fn static_functions() -> &'static [::mozjs::jsapi::JSFunctionSpec] {
Self::__ion_static_function_specs()
}

fn static_properties() -> &'static [::mozjs::jsapi::JSPropertySpec] {
Self::__ion_static_property_specs()
}
#(#def_fns)*
}))?;

Ok([spec_impls, class_definition])
}
Loading

0 comments on commit dd003b1

Please sign in to comment.