Skip to content

fix: use Object.setPrototypeOf in JS bindings #2922

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

Merged
merged 1 commit into from
May 29, 2025

Conversation

CountBleck
Copy link
Member

Fixes #2659.

Changes proposed in this pull request:
⯈ Use Object.setPrototypeOf({ /* instrumented imports */ }, __moduleN) instead of Object.assign(Object.create(__moduleN), { /* instrumented imports */ })

  • I've read the contributing guidelines
  • I've added my name and email to the NOTICE file

@CountBleck CountBleck changed the title Use Object.setPrototypeOf instead of Object.create+assign for bindings fix: use Object.setPrototypeOf instead of Object.create+assign for bindings May 25, 2025
@CountBleck CountBleck changed the title fix: use Object.setPrototypeOf instead of Object.create+assign for bindings fix: use Object.setPrototypeOf in JS bindings May 25, 2025
@CountBleck CountBleck requested a review from HerrCai0907 May 29, 2025 00:45
Apparently some bundlers use objects with read-only properties for
imported modules. This conflicts with Object.assign even though the
object being assigned to only has read-only properties on its prototype.
Regardless, if the assignment is done before the prototype is set,
everything works just fine.

Fixes AssemblyScript#2659
Copy link
Member

@HerrCai0907 HerrCai0907 left a comment

Choose a reason for hiding this comment

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

Code looks good. But i am not familiar with JavaScript object API. please make sure it works.

@CountBleck
Copy link
Member Author

Code looks good. But i am not familiar with JavaScript object API. please make sure it works.

The issue was that using Object.assign assigned every value to the new object, but since the object's prototype (the argument to Object.create) can have readonly properties (like in module objects from Webpack), this can throw an error. If we assign the properties first and set the prototype afterward, we don't get any errors thrown, and we can override methods even if they were readonly on the prototype.

@CountBleck CountBleck merged commit 44b658d into AssemblyScript:main May 29, 2025
14 checks passed
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.

Compiling esm esm cannot be used directly in the bundler module
2 participants