Skip to content

Commit

Permalink
Simplified Rooting Methods
Browse files Browse the repository at this point in the history
Added More Clippy Lints
Fixed CLI for Running Module Scripts
  • Loading branch information
Redfire75369 committed Feb 9, 2024
1 parent ae98983 commit 027bfb1
Show file tree
Hide file tree
Showing 54 changed files with 302 additions and 207 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ encoding_rs = "0.8.33"
futures = "0.3.29"
indent = "0.1.1"
mozjs = { package = "mozjs", git = "https://github.com/servo/mozjs" }
mozjs_sys = { package = "mozjs_sys", git = "https://github.com/servo/mozjs" }
sourcemap = "6.4.1"
url = "2.5.0"

Expand All @@ -42,6 +41,12 @@ default-features = false
[workspace.lints.rust]
unsafe_op_in_unsafe_fn = "forbid"

[workspace.lints.clippy]
cast_lossless = "forbid"
clone_on_ref_ptr = "forbid"
ptr_as_ptr = "forbid"
ptr_cast_constness = "forbid"

[profile.release]
lto = "fat"
strip = "symbols"
2 changes: 1 addition & 1 deletion cli/src/evaluate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub(crate) async fn eval_module(path: &Path) {
if let Some(sourcemap) = sourcemap {
save_sourcemap(path, sourcemap);
}
let result = Module::compile(rt.cx(), &filename, Some(path), &script);
let result = Module::compile_and_evaluate(rt.cx(), &filename, Some(path), &script);

if let Err(mut error) = result {
transform_error_report_with_sourcemaps(&mut error.report);
Expand Down
4 changes: 2 additions & 2 deletions ion-proc/src/class/accessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub(super) fn get_accessor_name(mut name: String, is_setter: bool) -> String {
pub(super) fn impl_accessor(
ion: &TokenStream, method: ItemFn, ty: &Type, is_setter: bool,
) -> Result<(Method, Parameters)> {
let expected_args = is_setter as i32;
let expected_args = i32::from(is_setter);
let error_message = if is_setter {
format!("Expected Setter to have {} argument", expected_args)
} else {
Expand All @@ -101,7 +101,7 @@ pub(super) fn impl_accessor(
let nargs: i32 = parameters
.parameters
.iter()
.map(|param| matches!(&*param.pat_ty.ty, Type::Path(_)) as i32)
.map(|param| i32::from(matches!(&*param.pat_ty.ty, Type::Path(_))))
.sum();
(nargs == expected_args).then_some(()).ok_or(error)
})?;
Expand Down
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
2 changes: 1 addition & 1 deletion ion-proc/src/class/impl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ fn class_definition(
}))?;
spec_impls.attrs.push(parse_quote!(#[doc(hidden)]));

let constructor_nargs = constructor.nargs as u32;
let constructor_nargs = u32::from(constructor.nargs);
let class_definition = parse2(quote_spanned!(span => impl #ion::ClassDefinition for #r#type {
fn class() -> &'static #ion::class::NativeClass {
static __ION_NATIVE_CLASS: &#ion::class::NativeClass = #r#type::__ion_native_class();
Expand Down
4 changes: 2 additions & 2 deletions ion-proc/src/class/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ pub(super) enum MethodReceiver {
pub(super) struct Method {
pub(super) receiver: MethodReceiver,
pub(super) method: ItemFn,
pub(super) nargs: usize,
pub(super) nargs: u16,
pub(super) names: Vec<Name>,
}

impl Method {
pub(super) fn to_specs(&self, ion: &TokenStream, class: &Ident) -> Vec<TokenStream> {
let ident = &self.method.sig.ident;
let nargs = self.nargs as u16;
let nargs = self.nargs;

self.names
.iter()
Expand Down
11 changes: 7 additions & 4 deletions ion-proc/src/function/parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub(crate) struct ThisParameter {
pub(crate) struct Parameters {
pub(crate) parameters: Vec<Parameter>,
pub(crate) this: Option<(ThisParameter, Ident, usize)>,
pub(crate) nargs: usize,
pub(crate) nargs: u16,
}

impl Parameter {
Expand Down Expand Up @@ -151,7 +151,7 @@ impl ThisParameter {

impl Parameters {
pub(crate) fn parse(parameters: &Punctuated<FnArg, Token![,]>, ty: Option<&Type>) -> Result<Parameters> {
let mut nargs = 0;
let mut nargs: u16 = 0;
let mut this: Option<(ThisParameter, Ident, usize)> = None;

let parameters: Vec<_> = parameters
Expand Down Expand Up @@ -183,7 +183,10 @@ impl Parameters {
};
if let Type::Path(ty) = &*param.pat_ty.ty {
if !path_ends_with(&ty.path, "Opt") && !path_ends_with(&ty.path, "Rest") {
nargs += 1;
nargs = match nargs.checked_add(1) {
Some(nargs) => nargs,
None => return Some(Err(Error::new(arg.span(), "Function has too many arguments"))),
}
}
}
Some(Ok(param))
Expand Down Expand Up @@ -220,7 +223,7 @@ impl Parameters {
}

pub(crate) fn to_args(&self) -> Vec<FnArg> {
let mut args = Vec::with_capacity(self.parameters.len() + self.this.is_some() as usize);
let mut args = Vec::with_capacity(self.parameters.len() + usize::from(self.this.is_some()));

args.extend(
self.parameters
Expand Down
1 change: 0 additions & 1 deletion ion/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ chrono.workspace = true
encoding_rs.workspace = true
indent.workspace = true
mozjs.workspace = true
mozjs_sys.workspace = true

[dependencies.futures]
workspace = true
Expand Down
4 changes: 2 additions & 2 deletions ion/examples/macros/js_class/toggle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl Toggle {

#[ion(get, alias = ["switch"])]
pub fn get_toggle(&self) -> i32 {
self.toggled * self.toggle as i32
self.toggled * i32::from(self.toggle)
}

#[ion(set)]
Expand All @@ -42,7 +42,7 @@ impl Toggle {
if !self.reset() {
return false;
}
self.toggled = toggled + toggle as i32;
self.toggled = toggled + i32::from(toggle);
self.toggle = toggle;
toggle
}
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<'b> {
impl<'b> BigInt<'b> {
/// 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<'b> BigInt<'b> {
};
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).map_err(|e| Some(Exception::Error(e)))?)
}
Expand Down Expand Up @@ -109,7 +109,7 @@ impl<'b> BigInt<'b> {
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
12 changes: 6 additions & 6 deletions ion/src/class/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ use std::any::TypeId;
use std::collections::hash_map::Entry;
use std::ffi::CStr;
use std::ptr;
use mozjs::gc::Traceable;

use mozjs::gc::HandleObject;
use mozjs::gc::Traceable;
use mozjs::glue::JS_GetReservedSlot;
use mozjs::jsapi::{
GCContext, Handle, JS_GetConstructor, JS_InitClass, JS_InstanceOf, JS_NewObjectWithGivenProto, JS_SetReservedSlot,
JSFunction, JSFunctionSpec, JSObject, JSPropertySpec, JSTracer,
};
use mozjs::jsval::{NullValue, PrivateValue, UndefinedValue};
use mozjs::rust::HandleObject;

use crate::{Context, Error, ErrorKind, Function, Local, Object, Result};
pub use crate::class::native::{MAX_PROTO_CHAIN_LENGTH, NativeClass, PrototypeChain, TypeIdWrapper};
Expand Down Expand Up @@ -70,7 +70,7 @@ pub trait ClassDefinition: NativeObject {
match infos.entry(TypeId::of::<Self>()) {
Entry::Occupied(o) => (false, o.into_mut()),
Entry::Vacant(entry) => {
let proto_class = Self::proto_class().map_or_else(ptr::null, |class| &class.base as *const _);
let proto_class = Self::proto_class().map_or_else(ptr::null, |class| &class.base);
let parent_proto = Self::parent_prototype(cx).map_or_else(HandleObject::null, |proto| proto.handle());

let (constructor, nargs) = Self::constructor();
Expand Down Expand Up @@ -100,10 +100,10 @@ pub trait ClassDefinition: NativeObject {
unwrap_specs(static_functions),
)
};
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 Expand Up @@ -141,7 +141,7 @@ pub trait ClassDefinition: NativeObject {
unsafe {
let mut value = UndefinedValue();
JS_GetReservedSlot(object.handle().get(), 0, &mut value);
&*(value.to_private() as *const Self)
&*(value.to_private().cast::<Self>())
}
}

Expand Down
Loading

0 comments on commit 027bfb1

Please sign in to comment.