Skip to content

feat(embind): add support for type-based overloading#17445

Open
bugra9 wants to merge 1 commit intoemscripten-core:mainfrom
bugra9:embind-overloading-support
Open

feat(embind): add support for type-based overloading#17445
bugra9 wants to merge 1 commit intoemscripten-core:mainfrom
bugra9:embind-overloading-support

Conversation

@bugra9
Copy link

@bugra9 bugra9 commented Jul 15, 2022

 class A {};
 class B : public A {};
 class C {};

 class HasOverloadedMethods {
 public:
     HasOverloadedMethods(std::string i); // javascript type of parameter: string
     HasOverloadedMethods(int i); // javascript type of parameter: number

     void foo();
     void foo(std::string i); // javascript type of parameter: string
     void foo(short i); // javascript type of parameter: number
     void foo(A i); // javascript type of parameter: A
     void foo(std::shared_ptr<C> i); // javascript type of parameter: C
     void foo(int i, int j); // javascript types of parameters: number, number
     void foo(double i); // javascript type of parameter: number
     void foo(float f) const; // javascript type of parameter: number
     void foo(B i); // javascript type of parameter: B (derived from A)
     
     static void staticFoo(std::string i); // javascript type of parameter: string
     static void staticFoo(int i); // javascript type of parameter: number
};

 // Binding code
 EMSCRIPTEN_BINDING(overloads) {
     class_<HasOverloadedMethods>("HasOverloadedMethods")
         .constructor<std::string>()
         .constructor<int>()
         // .smart_ptr_constructor("HasOverloadedMethods", &std::make_shared<HasOverloadedMethods, std::string>)
         // .smart_ptr_constructor("HasOverloadedMethods", &std::make_shared<HasOverloadedMethods, int>)
         .function("foo", select_overload<void()>(&HasOverloadedMethods::foo))
         .function("foo", select_overload<void(std::string)>(&HasOverloadedMethods::foo))
         .function("foo", select_overload<void(short)>(&HasOverloadedMethods::foo))
         .function("foo", select_overload<void(A)>(&HasOverloadedMethods::foo))
         .function("foo", select_overload<void(std::shared_ptr<C>)>(&HasOverloadedMethods::foo))
         .function("foo", select_overload<void(int, int)>(&HasOverloadedMethods::foo))
         .function("foo_double", select_overload<void(double)>(&HasOverloadedMethods::foo))
         .function("foo_float", select_overload<void(float)const>(&HasOverloadedMethods::foo))
         .function("foo_B", select_overload<void(B)>(&HasOverloadedMethods::foo))
         .class_function("staticFoo", select_overload<void(std::string)>(&HasOverloadedMethods::staticFoo))
         .class_function("staticFoo", select_overload<void(int)>(&HasOverloadedMethods::staticFoo))
         ;
 }
  • Functions can be overloaded using parameter js types.
  • Class methods can be overloaded using parameter js types.
  • Static class methods can be overloaded using parameter js types.
  • Class constructors can be overloaded using parameter js types. (constructor and smart_ptr_constructor)
  • Add tests.
  • Update the documentation.
  • Run test/runner other.test_embind.
  • Run EMTEST_SKIP_V8=1 test/runner core0 skip:core0.test_time.

Copy link
Collaborator

@brendandahl brendandahl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For current consumers of the overload feature this is going to make things much slower, since there's a lot more work that will happen on every call to an overloaded function. Some thoughts on how we could improve this:

  • First do a check for argument length, if there's only one method that matches call it. Otherwise, do the expensive type checking.
  • There's a lot of split/joining and string matching. I think not relying on strings and keeping things split will be more efficient (see some comments inline).

Last, please update the documentation.

@bugra9
Copy link
Author

bugra9 commented Sep 15, 2022

Done. I refactored all my code performance-oriented and updated the documentation.

For current consumers of the overload feature this is going to make things much slower, since there's a lot more work that will happen on every call to an overloaded function. Some thoughts on how we could improve this:

  • First do a check for argument length, if there's only one method that matches call it. Otherwise, do the expensive type checking.
  • There's a lot of split/joining and string matching. I think not relying on strings and keeping things split will be more efficient (see some comments inline).

Last, please update the documentation.

@bugra9 bugra9 requested a review from brendandahl September 15, 2022 18:27
@bugra9
Copy link
Author

bugra9 commented Oct 23, 2022

Hi @brendandahl, do you have any suggestions?

@bugra9 bugra9 force-pushed the embind-overloading-support branch from 0fecb52 to 76a459b Compare December 23, 2023 14:52
@bugra9 bugra9 changed the title Add overloading support using parameter types for embind feat(embind): add support for type-based overloading Dec 23, 2023
@bugra9 bugra9 force-pushed the embind-overloading-support branch from 76a459b to 60a0d81 Compare December 24, 2023 16:08
@bugra9 bugra9 force-pushed the embind-overloading-support branch from 60a0d81 to 074e3ec Compare February 8, 2025 23:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants