Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to call rust function using js "this" and "args" using a utility trait. #3588

Open
Swiiz opened this issue Jan 15, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@Swiiz
Copy link

Swiiz commented Jan 15, 2024

Engine utility feature request
Ability to call rust function using js "this" and "args" using a utility trait.
This is often needed when creating classes.

Example code

impl Class for MyClass {
    const NAME: &'static str = "MyClass";

    fn data_constructor(
        _: &boa_engine::prelude::JsValue,
        _: &[boa_engine::prelude::JsValue],
        _: &mut boa_engine::prelude::Context,
    ) -> boa_engine::JsResult<Self> {
        Ok(Self {  })
    }

    fn init(class: &mut ClassBuilder<'_>) -> boa_engine::JsResult<()> {
        class.method(
            js_string!("myFunc"),
            0,
            NativeFunction::from_fn_ptr(|this, args, context| MyClass::my_func.call_from_js(this, args, context)));

        Ok(())
    }
}

impl MyClass {
    fn my_func(&mut self, name: String, age: u32) -> JsValue {
        println!("{name} {age}");
        JsValue::Undefined
    }
}

A basic implementation would look like:

pub trait FuncFromJs<Receiver, _Marker> {
    fn call_from_js(&self, this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue>;
}

macro_rules! impl_funcfromjs {
    ($($i:tt $t:tt),+) => {
        #[allow(non_snake_case)]
        impl<Func, Receiver: NativeObject, $($t: TryFromJs),+> FuncFromJs<&Receiver, ($($t),+,)> for Func where Func: Fn(&Receiver, $($t),+) -> JsValue {
            fn call_from_js(&self, this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
                let ($($t),+,) = ($($t::try_from_js(&args[$i], context).unwrap()),+,);
                let _self = this.as_object().unwrap().downcast_ref::<Receiver>().unwrap();
                Ok(self(&_self, $($t),+))
            }
        }

        #[allow(non_snake_case)]
        impl<Func, Receiver: NativeObject, $($t: TryFromJs),+> FuncFromJs<&mut Receiver, ($($t),+,)> for Func where Func: Fn(&mut Receiver, $($t),+) -> JsValue {
            fn call_from_js(&self, this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
                let ($($t),+,) = ($($t::try_from_js(&args[$i], context).unwrap()),+,);
                let mut _self = this.as_object().unwrap().downcast_mut::<Receiver>().unwrap();
                Ok(self(&mut _self, $($t),+))
            }
        }
    };
}

impl_funcfromjs!(0 A);
impl_funcfromjs!(0 A, 1 B);
impl_funcfromjs!(0 A, 1 B, 2 C);
impl_funcfromjs!(0 A, 1 B, 2 C, 3 D);
impl_funcfromjs!(0 A, 1 B, 2 C, 3 D, 4 E);
impl_funcfromjs!(0 A, 1 B, 2 C, 3 D, 4 E, 5 F);
impl_funcfromjs!(0 A, 1 B, 2 C, 3 D, 4 E, 5 F, 6 G);
impl_funcfromjs!(0 A, 1 B, 2 C, 3 D, 4 E, 5 F, 6 G, 7 H);
@Swiiz Swiiz added the enhancement New feature or request label Jan 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant