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

module init macro #68

Open
Qard opened this issue May 21, 2020 · 4 comments
Open

module init macro #68

Qard opened this issue May 21, 2020 · 4 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@Qard
Copy link

Qard commented May 21, 2020

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'
@theduke
Copy link
Owner

theduke commented May 25, 2020

Not quite sure I follow.

You would like to write Rust code that can be easily compiled to qjs CLI compatible native modules?

@theduke theduke added enhancement New feature or request needs-more-info labels May 25, 2020
@Qard
Copy link
Author

Qard commented May 25, 2020

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.

@theduke
Copy link
Owner

theduke commented May 25, 2020

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.

@theduke theduke added help wanted Extra attention is needed and removed needs-more-info labels May 25, 2020
@Qard
Copy link
Author

Qard commented May 25, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants