quickjs-rs
                                
                                
                                
                                    quickjs-rs copied to clipboard
                            
                            
                            
                        module init macro
It'd be cool to have a fancy macro to build the extern function required to create a native module that can be imported by the qjs CLI. With qjs you can build and import native modules from *.so files by simply having a function something like:
JSModuleDef* js_init_module_qjsc_my_module(JSContext* ctx, const char* module_name);
Then in JS you just do:
import * from 'my_module.so'
                                    
                                    
                                    
                                
Not quite sure I follow.
You would like to write Rust code that can be easily compiled to qjs CLI compatible native modules?
Yes, which requires an entrypoint function with that signature. It'd be nice to have a macro that lets you write a function with the rust-wrapped types and the macro will wrap it appropriately to expose the C types.
So basically you'd build with crate-type=dylib and have an extern function in the lib with that signature and it would allow qjs to just import that as-is. This makes it very easy to use in a highly modular way. You just mix and match whatever native modules you need for your given app, much like you would with native modules in Node.js.
That would indeed be convenient.
I guess the interesting questions are around how to provide variables, functions, ... from Rust in a ergonomic way.
I'll probably work on a Webassembly runtime and some other features like async integration first, but contributions are definitely welcome.
It would definitely help if you could sketch out how you would expect code to look like.
I might get around to contributing it myself, if I can find the time at some point and no one else gets to it first. My thinking is something like:
use quick_js::{Context,Module};
#[quickjs_init_module(my_module)]
fn init_module(context: Context, name: String) -> Module {
  context.new_module(name, |module| {
    module.add_callback("add", |a: i32, b: i32| a + b).unwrap();
  })
}
For reference, here's what it looks like in C: https://github.com/Qard/quickjs-glfw/blob/master/glfw.c#L178-L188