infinyon / node-bindgen

Easy way to write Node.js module using Rust
Apache License 2.0
535 stars 43 forks source link

Can I await javascript promise from rust? #37

Open kdy1 opened 4 years ago

kdy1 commented 4 years ago

Hi. I'm the author of swc and I'm considering switching from neon to node-bindgen.

Can I store js function on struct and call it from other threads? Neon supports it via EventHandler.

Also, I need to wait for js promises to be resolved from rust code. Is it possible?

If these features are on the roadmap, please let me know.

sehz commented 4 years ago

Just aware that node-bindgen uses Rust's async infrastructure rather than node's libuv. It uses [async.rs[(https://async.rs) for executor. So far, node-bindgen atomically maps rust's async to promise. So it should be possible to support invoking node.js function or object in the async fn or closure. For example:

/// this is rust function which will invoke js function sum
async fn sum(first: i32,second: i32,js_obj: impl MyJsObj) -> i32 {
    js_obj.sum(first, second).await
}

/// define interface to Js class MyJsObj which implements sum
///
///       export class MyJsObj {
///              constructor() {}
///
///              sum (first, second) {
///                    return first + second;
///              }
//.      }
///
#[nodebingen(js_obj)]
trait MyJsObj {
   async fn sum(first: i32, second: i32)-> i32;     // this is async so it can called from rust's async
}

/// my obj can be stored and manipulated
struct SomeStruct<M: MyObj> {
     my_obj<M>
}

I can put this under roadmap. What is your timetable?

kdy1 commented 4 years ago

@sehz Great! I would really love to see it.

I can put this under roadmap. What is your timetable?

I don't have any strict time table. However, asspack, a bundler for the swc project, is almost done and as I don't have many things left to do, the sonner is the better for me

sehz commented 4 years ago

Ok, I will try you put together first phase of solution so can unblock you. Probably in next 1-2 week.

Also, there might be interesting colloberation between two projects. For example, it could be interesting to call JS using TS notation. And ability to export TS binding from Rust code.

kdy1 commented 4 years ago

@sehz

Ok, I will try you put together first phase of solution so can unblock you. Probably in next 1-2 week.

Thank you so much! I have a final exam next week so I love the plan.

Also, there might be interesting colloberation between two projects. For example, it could be interesting to call JS using TS notation. And ability to export TS binding from Rust code.

Interesting. Auto-generating .d.ts file will be great. Note that swc_ecma_parser, swc_ecma_ast and swc_ecma_codegen works on stable rust.

Ryanmtate commented 4 years ago

It looks like this might be achievable using https://nodejs.org/api/n-api.html#n_api_napi_create_async_work

feature/async branch has a work in progress on implementation. Not sure when the work will be completed, but wanted to post this note for later use.