Giter Site home page Giter Site logo

Comments (6)

Redfire75369 avatar Redfire75369 commented on May 23, 2024

This has now been completed with f4e4483.

The API is as follows:

let future = async {
    return Ok(4);
};

let promise = IonPromise::from_future(rt.cx(), future);
unsafe { global.set_as(rt.cx(), "promiser".into(), IonObject::from(promise.raw())); }
assert!(IonScript::compile_and_evaluate(rt.cx(), "inline.js", "promiser.then(v => console.log(v))").is_ok());

As of 7a2faf5, #[js_fn] macros can also be used to create functions that return promises.

async fn async_increment(int: u64) -> u64 { int + 1 }

#[js_fn]
async unsafe fn async_fn(int: u64) -> Result<u64, ()>{
    Ok(async_increment(int).await)
}

This issue will be kept open for a week for any bugs in the API or possible improvements to the API.

from spiderfire.

sagudev avatar sagudev commented on May 23, 2024

Why function unsafe?

from spiderfire.

Redfire75369 avatar Redfire75369 commented on May 23, 2024

In #[js_fn]? It's the same for synchronous functions too. It's across FFI, so I think it's fair.

from spiderfire.

sagudev avatar sagudev commented on May 23, 2024

Yes but this function is called from other unsafe function that does the args conversion, which means that this function (async_increment) is safe, but it's callee is unsafe. So I would make unsafe fn on js_fn optional.

from spiderfire.

Redfire75369 avatar Redfire75369 commented on May 23, 2024

After having talks with some people on the safety/unsafety of this function, it seems that it is still unsafe as long as panics have undefined behaviour. The solution to that seems to be catch_unwind.

I suppose that it could just return an Err() if a panic occurs. This means it would return false from the native function, showing that there was an internal "exception".

from spiderfire.

Redfire75369 avatar Redfire75369 commented on May 23, 2024

As of e333e88, panicking within native functions is no longer undefined behaviour. The output is still a bit weird, but can be improved later.

As there are no further comments on the API for asynchronous functions, this issue will be closed.

from spiderfire.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.