Add a #[boa_module] macro to automatically implement a Module#4277
Add a #[boa_module] macro to automatically implement a Module#4277hansl merged 7 commits intoboa-dev:mainfrom
#[boa_module] macro to automatically implement a Module#4277Conversation
This adds a `pub(super) fn boa_module(context: &mut Context) -> JsResult<Module>`
function to the same module that builds the module from its content.
It understands the following:
1. `fn some_func(...) -> ... {}` will be added as functions to the module.
2. `const SOME_CONST: ... = ...` will be added as values. The type of the
constant value must support `impl Into<JsValue>`.
3. `type SomeType = ...` will be added as a class. The class will be
exported using the `SomeType` name (with renaming support).
Two schemes of renaming are now supported, one for classes and one for
regular imports. Use `#[boa(rename_class = "...")]` to select the one
you want.
A PascalCase renaming is also added to allow for classes.
Simple tests added for everything.
|
Hmmm, I think it's REALLY surprising to have common Rust syntax be what determines what gets exported in the module... Can we add an |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4277 +/- ##
==========================================
+ Coverage 47.24% 52.85% +5.60%
==========================================
Files 476 491 +15
Lines 46892 51078 +4186
==========================================
+ Hits 22154 26995 +4841
+ Misses 24738 24083 -655 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Could you give me an example of what that would look like? You can use the current example test module as template (export a const, function and class). Keep in mind the idea is to have a function that returns the module, so we would have to add that to the source as well. I felt that having a |
|
I stand corrected. I was thinking it would be a bit clunky to have something like a private |
jedel1043
left a comment
There was a problem hiding this comment.
Really impressive work! Couple of discussion points that I wanted to raise.
|
@jedel1043 Done, changed the defaults to be more sensible. |
This adds a
pub(super) fn boa_module(context: &mut Context) -> JsResult<Module>function to the same module that builds the module from its content.It understands the following:
fn some_func(...) -> ... {}will be added as functions to the module.const SOME_CONST: ... = ...will be added as values. The type of the constant value must supportimpl Into<JsValue>.type SomeType = ...will be added as a class. The class will be exported using theSomeTypename (with renaming support).Two schemes of renaming are now supported, one for classes and one for regular imports. Use
#[boa(rename_class = "...")]to select the one you want.A PascalCase renaming is also added to allow for classes.
Simple tests added for everything.