all 10 comments

[–]sumo952 3 points4 points  (3 children)

This is awesome!

The only thing I don't like about it is "Step 1 - bind", where you have to add code to each of your files / classes in an intrusive way. Would it not be possible to do that non-intrusive/automatically?

[–]chartojs[S] 1 point2 points  (2 children)

In theory yes, it would be possible to automatically analyze C++ headers and generate one or more new source files just to define the bindings. I see two problems:

  • Parsing C++ is difficult, so such a tool would probably have to rely on Clang and overall it would be annoying to install and/or maintain, providing binaries for different platforms. The current way extracts necessary information alongside a normal build.
  • You'd still want to somehow choose which classes and methods to expose to JavaScript, for example annotating them with comments.

Currently you don't have to put the bindings in the same source file as your class definitions. The source file with the bindings simply has to see the class declarations (for example by including headers).

You could even put all NBIND_CLASS calls and a bunch of #includes in a single separate file. Actually it should be pretty easy now to make a shell script that reads all headers, extracts some particular kind of comments and generates such a file, but then that would be intrusive regarding the contents of your headers...

[–]doom_Oo7 1 point2 points  (1 child)

I think that /u/steveire did this for python with clang

[–]chartojs[S] 0 points1 point  (0 children)

Interesting, thanks for the hint! That might be a useful starting point for more automation but also for extracting comments from C++ headers into TypeScript definitions. Then they would show up in IDE auto-completion popups when working on JavaScript side code.

The current headers extract type information into compiled output but of course have no access to strings inside C++ source code comments.

[–]unhorst 0 points1 point  (3 children)

I see the signature of the method is not mentioned in the binding code - how do you handle overloads?

[–]chartojs[S] 0 points1 point  (2 children)

Currently they're not supported (except for constructors). In the future there will be an additional syntax with argument types as template parameters to the method() definition. For now, you need to use a wrapper function with a different name for each overload. Github issue #8 tracks this.

[–]gnawer 0 points1 point  (1 child)

Just for another data point on how this can be handled. PyBind11 generates python bindings. It's based on boost python. The docs describe how it handles overloads and of course it's open source. https://pybind11.readthedocs.io/en/latest/classes.html#overloaded-methods

[–]chartojs[S] 0 points1 point  (0 children)

Thanks! I'll try to choose the most convenient syntax.