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

[RFC] Rework embind_gen.js to be able to improve typings based on actual Emscripten settings #20968

Open
Dabolus opened this issue Dec 21, 2023 · 8 comments
Assignees

Comments

@Dabolus
Copy link
Contributor

Dabolus commented Dec 21, 2023

As of today, TypeScript typings generation is a bit limited by the fact that-to run the JS wrapper script that generates the typings-the settings provided to Emscripten need to be tweaked (see https://github.com/emscripten-core/emscripten/blob/main/tools/link.py#L1907-L1955).

TS generation is also limited by the current approach to accessing settings in the scripts, that doesn't allow e.g. to iterate over the exported runtime methods to decide what to expose on the typings side.

I'm not sure what the best approach would be, but it would be great to make the script receive the original settings as native JS object somehow.

To demonstrate what I mean, I created a PoC branch on my fork (see main...Dabolus:feat/better-output-types), where I successfully managed to create more reliable types by providing the original settings as a base64 encoded arg to the JS script (I know this isn't ideal, but again, it's just to better explain my point).

@sbc100
Copy link
Collaborator

sbc100 commented Dec 21, 2023

Is this a duplicate of #20296?

@Dabolus
Copy link
Contributor Author

Dabolus commented Dec 21, 2023

Not exactly, but it's related to it. Finding a proper way to provide Emscripten settings to the JS tool would solve that issue, plus many other use cases. A good example is what I did in my branch by adding the const declaration and the default export when the corresponding flags are provided, which is something I think isn't tracked anywhere but that would definitely make the typings more accurate.

@sbc100
Copy link
Collaborator

sbc100 commented Dec 21, 2023

I think that is what #20296 is all about. The status quo is that --embind-emit-tsd specifically relates only toe embind stuff (i.e. stuff defined in EMSCRIPTEN_BINDINGS).

Extending our typescript generation to apply to other things like EXPORTED_RUNTIME_METHODS is what #20296 is about. It will probably involve renaming the setting to something less embind-specific and running the code somewhere that is not in src/embind/ I think.

@sbc100
Copy link
Collaborator

sbc100 commented Dec 21, 2023

BTW, the JS library code such as embind_gen.js should already have full access to all settings as global variables. You can use the {{{ }}} macro system to embed them at build time.

For example var foo = {{{ EXPORTED_RUNTIME_METHODS }}}; will generate code that statically contains the EXPORTED_RUNTIME_METHODS list.

@sbc100
Copy link
Collaborator

sbc100 commented Dec 21, 2023

I think the direction you are taking here looks good though! Would you be interesting in help work on this feature? I think @brendandahl has some thought about the way forward.

One thing I think we need to more testing of the typescript bindings in our CI so we can verify that working and correct over time.

@Dabolus
Copy link
Contributor Author

Dabolus commented Dec 21, 2023

Cool, didn't know about the {{{ }}} macro! However, those are still the currently defined Emscripten settings, right?
Cause one issue I've found while trying working on this is that some settings are overridden when running embind_gen.js, so for example you have no way to understand whether EXPORT_ES6 was initially enabled or not due to https://github.com/emscripten-core/emscripten/blob/main/tools/link.py#L1927, which is the reason why I provided to the script as arg original_settings instead of just settings.

Any help would definitely be appreciated! I started using Emscripten only recently, so my knowledge is still very limited. 🙏🏻

@Dabolus
Copy link
Contributor Author

Dabolus commented Jun 28, 2024

Bumping this issue since I'm still very interested in helping with this 🙏🏻

In particular, the main blocking point is having a proper way to access the original_settings variable from the embind_gen.js tool

@brendandahl
Copy link
Collaborator

brendandahl commented Jul 2, 2024

I added support for generating ts definitions from runtime exports in #21517. Was there more that is needed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants