autodesk-forks / MaterialX

MaterialX C++ and Python libraries
http://www.materialx.org/
Apache License 2.0
107 stars 23 forks source link

JS Bindings: Conveniently bind functions with optional parameters #1192

Closed frericp closed 3 years ago

frericp commented 3 years ago

This PR introduces a way to conveniently create bindings for functions with optional parameters. The following compares the old with the new approach:

Old: Create a single binding that expects all parameters, including optional ones (due to a limitation of Emscripten). Allow to not provide optional parameters by adding additional logic on the JavaScript side, which essentially fills in all non-provided optional parameters.

New: Use macros to automatically register multiple bindings for a function, each taking a different number of parameters. This has several advantages:

All functions of the Document class that had default parameters defined in the JS boilerplate code have been redefined for illustration purposes.

Furthermore, this PR fixes an issue with the JS boilerplate code, which broke the function overloading capabilities of Emscripten.

FYI @sdunkel @kohakukun

frericp commented 3 years ago

That's funny, because I explicitly renamed it from BIND_FUNC to BIND_CLASS_FUNC before, since it only works on class member functions (i.e. you have to provide a class parameter to the macro). But I wasn't aware of the emscripten::class_function. Will change it back...