Skip to content
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

Fix for register_vector and optional class_ collision. #21957

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

wffurr
Copy link
Contributor

@wffurr wffurr commented May 16, 2024

Fixes BindingError: Cannot register type 'emscripten::val' twice error. This happened with pre-existing binding code that was migrated to Emscripten 3.1.52 which introduces the register_optional function, used internally by register_vector, in #21076.

The fix ignores duplicate registrations of emscripten::val, and doesn't rely on the boolean already-registered flag local to the register_optional function.

@wffurr wffurr force-pushed the register-optional-check-twice branch from fb3011e to ebd2523 Compare May 16, 2024 19:05
Fixes `BindingError: Cannot register type 'emscripten::val' twice` error.
This happened with pre-existing binding code that was migrated to
Emscripten 3.1.52 which introduces the register_optional function, used
internally by register_vector.

The fix ignores duplicate registrations of emscripten::val, and doesn't
rely on the boolean already-registered flag local to the
register_optional function.
@wffurr wffurr force-pushed the register-optional-check-twice branch from ebd2523 to 05ea0d0 Compare May 16, 2024 19:26
@sbc100 sbc100 requested a review from brendandahl May 16, 2024 19:56
@wffurr
Copy link
Contributor Author

wffurr commented May 17, 2024

Tests pass except for the binary size, +7 bytes. Please take a look and let me know if this change is a good one and if so I'll fix that test.

@sbc100
Copy link
Collaborator

sbc100 commented May 17, 2024

Tests pass except for the binary size, +7 bytes. Please take a look and let me know if this change is a good one and if so I'll fix that test.

Rebasing/merging should fix those failure. They are related I believe.

return;
}
hasRun = true;
// Optional types are automatically registered for some internal types; the
Copy link
Collaborator

Choose a reason for hiding this comment

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

Allowing duplicate registrations will lead to a slightly strange behavior where the order matters. e.g.

The following will fail:

  register_optional<Foo*>();
  class_<std::optional<Foo*>>("OptionalFoo");

but swapping them will work:

  class_<std::optional<Foo*>>("OptionalFoo");
  register_optional<Foo*>();

I guess if a user is creating their own binding for std::optional and they want to override the behavior that could be fine.

@wffurr
Copy link
Contributor Author

wffurr commented May 21, 2024

This PR also doesn't fully address backwards incompatibility between optional<T> as a class_ binding and optional<T> as undefined|T. Namely, the class_ version of the optional needs to be deleted from JS or it leaks heap memory, whereas the undefined|T version does not, unless T itself needs to be deleted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants