-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Using embind to generate TypeScript typings #7083
Comments
Yes, I like that idea. I would go even further and introduce some kind of "ownership" expressed by marker interfaces. Something like this:
This way your API can be explicit on what objects you need to call |
Good point! If we look at the class example in the documentation, we could generate the following typings:
As not everyone uses TypeScript, this part should be optional. Could be a separate bind switch, for example
|
I think the interface inheritance should go the other way around. You should be able to mark some type as Owning when the declare namespace Module
{
export interface Owner<T> : T
{
delete();
}
export interface MyClass
{
x: number;
incrementX(): void;
}
export var MyClass:
{
new(x: number, y: string): Owner<MyClass>; // js needs to destroy it
getSomeNotOwningPointer(): MyClass; // js doesnt need to destroy it
getStringFromInstance(a: MyClass): string;
}
} With this design, |
I believe this is totally possible, given the verbosity and details we provide to |
as far as I know no one started working on this |
I'm working with a prototype of this, created by a colleague. If I can get it working on a couple of different embind examples, then I will see about contributing it upstream. |
Any updates on this? |
The tool itself works fine for the subset of embind it supports. I'm having trouble making it work for my project due to project-specific issues. I'm also not sure the best way to integrate it with the Emscripten toolchain. I might have something by the end of the year. |
This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 30 days. Feel free to re-open at any time if this issue is still relevant. |
Reopening, as this still sounds useful. @wffurr , any updates? |
@jjrv Did a really great job on this with I have been working with WebIDL with emscripten and auto-generating types from it, but I have run into a brick wall because WebIDL is incredibly unsophisticated when it comes to generics, templates and typedefs, which is one of the main things you want from C++ world. I think at this point for sure I will need to move over to using |
@wffurr Some of the code in |
https://github.com/ted537/tsembind I've created a rudimentary typescript bindings generator that works on existing EMBind libraries. There are some hacky things present (mostly grabbing context) but are only necessary for an external implementation. |
@ted537 Am I correct in understanding that your generator will take the abc.js file generated by emscripten during compilation and output a ts file? I tried installing it from your github link but all it installs are a bunch of js/ts files, no executable. Anything I am missing? By the way, we were thinking of writing a js script that would read c++ source files and generate a module.d.ts file. I think your project already does that? |
Yep that's correct! Emscripten registers things like typenames at runtime, so that's why I targeted the
A similar strategy should be possible from the C++ side as well. I imagine a drop in |
Thanks! I get this error when I try to run this on my js file though - failed to asynchronously prepare wasm: LinkError: WebAssembly.instantiate(): Import #114 module="env" function="memory" error: memory import must be a WebAssembly.Memory object |
Could you create an issue for this? I've only tested it with some of the formats for the |
Done! |
I am creating this right now in our product. The approach I took is to compile all the binding C++ code twice, with the second pass using a replacement header and library that replicates Embind's API (instead of the wrapping you mentioned here). Running this second executable writes the declarations to a file and this can be nicely automated from CMake as a custom build step. |
lovely, can you make the repo public so we could contribute? |
I'd love to! But it's not a "repo" I could just "make public" - we use Perforce, and this code is not an isolated library or tool, it is part of our larger codebase. I'd have to work to isolate it, write a readme, etc. and I'd have to go through some corporate procedure to open source it. |
Well, if you go through all the pain, the least I can promise you that we'll try to use it in production and contribute a bit. |
@mmarczell-graphisoft Honestly, even a simple gists of that replacement header file would be of extreme help. I would contribut too to make it production ready |
We actually submitted a talk to WASM I/O about generating JavaScript bindings to C# with C++ Reflection TS, Cross Language Polymorphism, Embind and SWIG Unfortunately got rejected, barrier to entry was too high XD |
@gracicot @devshgraphicsprogramming |
TSembind completely failed us with regards to namespaces, nested classes, etc. I'm hoping that our client allows us to finally mention them by name and accepts our proposal to let us speak about the tooling we developed at WASM Summit. |
@marczellm tsembind is useless for ES6 modules, it only works with CommonJS output. |
moreover TSembind won't reconstruct your namespaces and nested classes. |
I've landed the new emcc option Closing at fixed. |
@brendandahl Is this already published? I upgraded to emscripten 3.1.43 and added that option to my build script (which uses
If it is already released I can open a new issue for this. |
That hasn't been implemented. I've filed issues for the remaining features: |
Thanks for the work being done here! I've written up a TL;DR on Stack Overflow here that I'll keep updated as things progress. |
Hello. To export C++ structs and classes, one can use embind. As the export is very explicit, I was thinking, one could use embind to generate TypeScript typings (.d.ts) too. I would be happy to hear your comments on that :-)
The text was updated successfully, but these errors were encountered: