wasmerio / rusty_jsc

Rust bindings for the JavaScriptCore engine.
MIT License
99 stars 7 forks source link

Rusty JSC developments #8

Closed adrien-zinger closed 1 year ago

adrien-zinger commented 1 year ago

This PR includes a few enhancements to meet my expectations. Now I can set callbacks with arguments like:

#[callback]
fn log(context: JSContext, _function: JSObject, _this: JSObject, arguments: Vec<JSValue>) {
    arguments.iter().for_each(|value| {
        if value.is_string(&context) {
            println!("{}", value.to_string(&context));
        }
        // I also have added is_date and is_symbol
        if value.is_date(&context) {
            println!("date inthere!")
        }
    });
}

Parameters of callback functions follow a fall-through rule, 1 parameter = { context }, 2 = { context, function }, etc. But callbacks can also optionally return a JSValue, so I can define an identity function or anything similar.

#[callback]
fn echo(
    context: JSContext,
    _function: JSObject,
    _this: JSObject,
    arguments: Vec<JSValue>,
) -> JSValue {
    if let Some(argument) = arguments.first() {
        argument.clone()
    } else {
        JSValue::string(&context, "").unwrap()
    }
}

I can define a class with an internal function and write the constructor like we used to do with callbacks. The constructor definition can have up to 3 arguments corresponding to { context, this, arguments }. Then, with the resulting object, I could set parameters dynamically.

#[constructor]
fn constructor() {
    println!("into the user obj cnstructor");
}

let user_obj = JSObject::class(&context, "UserObject", Some(constructor));

The current PR contains several modifications, here is a list of all that we can find:

syrusakbary commented 1 year ago

Hey @adrien-zinger , we are working already on a new branch that supports callbacks (and callback closures) fully.

Stay tuned!

penberg commented 1 year ago

Hey @adrien-zinger, thanks for doing this! There's a bigger pull request coming from @syrusakbary. Happy to work with you on getting any remaining bits in this pull request merged once @syrusakbary's PR lands in main.

syrusakbary commented 1 year ago

Here we go. Feel free to review the big PR and provide feedback: #9

adrien-zinger commented 1 year ago

I have added also some management of retained objects (like classes and protected values) that allow to store and send values like callbacks with more security. The new version contains also the possibility to create promises.

I can still rebase that on the big PR in a while, it wont be a big problem :+1:

syrusakbary commented 1 year ago

Hey @adrien-zinger if you want to rebase your changes on top of master now would be a great time!

adrien-zinger commented 1 year ago

Hey @adrien-zinger if you want to rebase your changes on top of master now would be a great time!

Thank you @syrusakbary.

syrusakbary commented 1 year ago

Also, we need some automated tests on Github CI... any help there would be greatly appreciated!

syrusakbary commented 1 year ago

Note: the callbacks now accept variable arguments and can return a Result<JSValue, JSValue>. Please check the callbacks examples!

syrusakbary commented 1 year ago

Apologizes for taking so long to review @adrien-zinger, but completely forgot and just stumbled upon the PR again (feel free to ping me on other PRs)