Create typescript definitions with embind

1,709 views
Skip to first unread message

matthias

unread,
Jun 2, 2015, 10:27:45 AM6/2/15
to emscripte...@googlegroups.com
With embind I am able to create some class library written in C++ (which is very nice).
Is there a possibility that emscripten/embind also creates a typescript definition file (*.d.ts) file?
Or are there any plans to support that?

If it would be possible to create such a file you could use the emscripten/embind library in a typescript project and typescript would give you errors if you use the wrong names, parameters, types and so on.

Alon Zakai

unread,
Jun 2, 2015, 12:41:00 PM6/2/15
to emscripte...@googlegroups.com
I'm not sure about embind - the declarations are in the C++ code, so the C++ compiler would need to be involved in order to do this.

For the WebIDL binder approach, we could just convert WebIDL into a .d.ts file. Probably there already exists such a tool, I assume, for using all the current WebIDL specs of web APIs?

- Alon


--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

matthias

unread,
Jun 10, 2015, 9:35:51 AM6/10/15
to emscripte...@googlegroups.com
Right now I'm not sure if I would go for Embind or webidl.

But nevertheless I did some first tests with webidl.
I did not find a lot of tools to create a typescript definition file (*.d.ts) from a webidl file.
I used https://github.com/hfmanson/idl2typings (convert2 branch).

But out of the box it is not working:
- in the webidl file there is no Module definition, therefore idl2typings can't create a typescript definition file with a Module declared (but you need it because the js file created by webidl_binder.py makes use of a Module)
- the declared Module in the d.ts file would also need some more stuff (created by webidl_binder.py), for example the Module.destroy function
- in addition the Module in the d.ts should also contain the core emscripten definitions (as for example defined in https://github.com/borisyankov/DefinitelyTyped/blob/master/emscripten/emscripten.d.ts), so the Module need to be a mix of Emscripten core and the webidl generated definitions)
- the idl2typings does create interfaces which do not have constructors (instead it have methods with the same name as the class), but you need constructors otherwise you could not create the class with new)

ric...@snowcoders.com

unread,
Nov 16, 2016, 2:07:31 AM11/16/16
to emscripten-discuss
Were you ever able to figure out how to automatically generate the d.ts? I was hoping to incorporate that in my latest project if you did.

matthias

unread,
Nov 21, 2016, 7:45:59 AM11/21/16
to emscripten-discuss
The WebIdl to Typescript tool (https://github.com/hfmanson/idl) did not work out of the box. You would need to extend it.
On the other hand I think Embind has more features/possibilities than WebIdl.

We tried to create a wrapper arround Embind to create embind bindings + code to create typescript definitions. Unfortunately you can not output this directly with a compiler. Instead you have to translate it into JavaScript and run it for example in Node.js and this can output typescript definitions. If you use emscripten::val it is even more complicated because this can be anything.

Juha Järvi

unread,
Nov 22, 2016, 6:36:25 AM11/22/16
to emscripten-discuss
nbind has a somewhat shorter syntax than Embind and generates TypeScript definitions out of the box. You can also compile native binary Node.js addons with the exact same binding code, just by changing a build flag.

rickyp

unread,
Nov 25, 2016, 4:38:33 PM11/25/16
to emscripten-discuss
Matthias - Thanks, I figured it would be a bit difficult but given that it ends up in JavaScript I figured there maybe a way to extend the compiler a bit more. I tried a few js -> d.ts generators but they all seemed to fail.

Juha - I'll give that a shot. Is nbind replacing embind or is it completely separate?

Juha Järvi

unread,
Nov 27, 2016, 9:00:41 AM11/27/16
to emscripten-discuss
nbind is a replacement for embind because otherwise it seemed very difficult or impossible to:

- Support also targeting Node.js and Electron addons
- Design C++ APIs without constraints (using pointers and references etc. as normal)
- Pass objects by value so they belong to an appropriate class in both C++ and JavaScript

Since everything had to be redesigned anyway for the above reasons, it was also possible to:

- Autogenerate TypeScript definitions
- Avoid having to register vectors and smart pointers to use them
- Avoid generating JavaScript dispatcher functions when argument and return values are numeric
- Support multiple inheritance
- Support passing 64-bit integers (converting to JavaScript double or using any JavaScript bignum library)
- Write internal code in ES6 style TypeScript instead of ES5
- Write all internal tests using plain JavaScript and set up Travis CI
- Use more compact syntax:

embind:

EMSCRIPTEN_BINDINGS(my_class_example) {
  class_<MyClass>("MyClass")
    .constructor<int, std::string>()
    .function("incrementX", &MyClass::incrementX)
    .property("x", &MyClass::getX, &MyClass::setX)
    .class_function("getStringFromInstance", &MyClass::getStringFromInstance)
    ;
}

nbind:

NBIND_CLASS(MyClass) {
    construct<int, std::string>();
    method(incrementX);
    getset(getX, setX);
    method(getStringFromInstance);
}

Other future improvements are also possible but not sure about when there's time:

- Method overloading based on argument types (multiple inheritance makes this a bit more complicated)
- Support compiling to native code on mobile (React Native, Weex...)
- Precompile a single native Node.js addon for all versions Node.js and Electron on the same platform

Again the latter two are out of scope or impossible with embind.
Message has been deleted

rickyp

unread,
Dec 2, 2016, 12:50:44 PM12/2/16
to emscripten-discuss
Sorry for the delays in responses, things are a bit hectic around here now-a-days.

Are there plans to update the documentation on the website? It seems that nbind has been around for quite a while... why hasn't embind been deprecated?

Cheers,
Ricky

Alon Zakai

unread,
Dec 2, 2016, 1:33:12 PM12/2/16
to emscripten-discuss
It might be a good idea to open an issue about that. We can cc the embind creators and contributors (easiest to do on github), and check on the maintenance and development status of embind. It does seem less active these days, but we should confirm, and also should do an in-depth comparison between nbind and embind there.

Ricky

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-discuss+unsub...@googlegroups.com.

rickyp

unread,
Dec 3, 2016, 5:39:52 PM12/3/16
to emscripten-discuss
Here is what I got right now... If you want to add documentation providing an external link to nbind, you could just make the change yourself. Emscripten is hosted off GitHub.io which means their html files specifically for connecting cpp to javascript would be here:

Just create a pull request that gives a basic description of nbind and an external link.

As for the conversation on deprecating embind for nbind and the path to do that:
Reply all
Reply to author
Forward
0 new messages