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: methods cannot be used as constructor #1774

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

Conversation

andreabergia
Copy link
Contributor

The spec says that invoking a method as a constructor is not valid, but invoking normal functions is. This is, for example, node:

> o = { method() {}, notAMethod: function() {} }
{ method: [Function: method], notAMethod: [Function: notAMethod] }
> new o.method()
Uncaught TypeError: o.method is not a constructor
> new o.notAMethod()
notAMethod {}

This PR implements the same behavior in Rhino, and fixes #1299.

@gbrail gbrail added this to the Release 1.8.0 milestone Dec 24, 2024
@gbrail
Copy link
Collaborator

gbrail commented Dec 24, 2024

This makes sense to me, but I wonder how much existing code it could break and whether we should put it behind a "VERSION_ES6" or a "VERSION_ECMASCRIPT" flag just in case -- then again, since it didn't break any of MozillaSuiteTest perhaps it's not a big problem.

@gbrail gbrail removed this from the Release 1.8.0 milestone Dec 30, 2024
Copy link
Collaborator

@gbrail gbrail left a comment

Choose a reason for hiding this comment

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

I'm willing to be convinced on this, but I think that since this breaks things that may have worked before, we should check that language version is >= VERSION_ES6 before throwing this exception. I'm not sure that we will put this in 1.8.0, but can merge it soon after.

The spec says that invoking a method as a constructor is not valid,
but invoking normal functions is. This is, for example, `node`:

```js
> o = { method() {}, notAMethod: function() {} }
{ method: [Function: method], notAMethod: [Function: notAMethod] }
> new o.method()
Uncaught TypeError: o.method is not a constructor
> new o.notAMethod()
notAMethod {}
```

This PR implements the same behavior in Rhino, and fixes mozilla#1299.
@andreabergia
Copy link
Contributor Author

andreabergia commented Jan 7, 2025

I'm willing to be convinced on this, but I think that since this breaks things that may have worked before, we should check that language version is >= VERSION_ES6 before throwing this exception.

Rebased onto master and added a check on the language version.

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.

Support ES2015 Method Definitions - prevent methods as constructor
2 participants