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

Function.prototype.call versus Array.prototype.find #232

Open
gbrail opened this issue Sep 1, 2015 · 2 comments
Open

Function.prototype.call versus Array.prototype.find #232

gbrail opened this issue Sep 1, 2015 · 2 comments
Labels
Ecma Incompatibility Issues about Rhino being incompatible with the EcmaScript spec Strict Mode Issues related to non-compliance with the ES5 Strict Mode spec

Comments

@gbrail
Copy link
Collaborator

gbrail commented Sep 1, 2015

The recent changes to enable Array.prototype.find seem to be causing some side-effects with Function.prototype.call, and I'm not sure how to resolve them.

If I look here:

http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf

Section 19.2.3.3 says that "Function.prototype.call" should pass "null" and "undefined" as the value of "this" to the function that it calls, which is different from ES 3. Evgeny recently made these changes in master.

However, this broke some Node.js code I was testing, and I found the following note, also in section 19.2.3.3:

"NOTE 1 The thisArg value is passed without modification as the this value. This is a change from Edition 3, where an undefined or null thisArg is replaced with the global object and ToObject is applied to all other values and that result is passed as the this value. Even though the thisArg is passed without modification, non-strict functions still perform these transformations upon entry to the function."

My interpretation of "non-strict functions still perform these transformations" is that the "old" behavior should be enabled in ScriptRuntime.applyOrCall, and that the new behavior should only be enabled when strict mode is in force -- and sainen just gave us a PR to enable strict mode.

Sure enough, when I changed "applyOrCall" to only apply the new behavior in strict mode, the legacy Node.js code worked.

However, the "array find" tests broke because they were expecting a TypeError in this case:

Array.prototype.find.call(null, function() { });

and that only happens when running in strict mode -- otherwise, "null" gets converted to the global object, and since that has no "length" property, "find" just does nothing.

(V8 version 3.28.73, under Node.js in "--harmony" mode, seems to use the old behavior.)

@eshepelyuk, do you have any ideas how to resolve this?

Some possibilities:

  1. My interpretation of section 19.2.3.3 is incorrect and we should always pass "null" and "undefined" as per the spec when the language version is > 1.7/
  2. The test262 tests that we are running for Array.find should have been run only in "strict mode".
  3. We have to do some more work to make everything pass.

Sorry about the long issue but it's an interesting one. Thanks!

@eshepelyuk
Copy link
Contributor

Have you tried to play with context feature Context.FEATURE_OLD_UNDEF_NULL_THIS ?
That feature enables / disables ES5 behaviour of Function.prototype.call / Function.prototype.appply.
See org.mozilla.javascript.ScriptRuntime#applyOrCall

@p-bakker p-bakker added the Strict Mode Issues related to non-compliance with the ES5 Strict Mode spec label Jul 5, 2021
@p-bakker
Copy link
Collaborator

p-bakker commented Jul 5, 2021

Related to #940

@p-bakker p-bakker added the Ecma Incompatibility Issues about Rhino being incompatible with the EcmaScript spec label Aug 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ecma Incompatibility Issues about Rhino being incompatible with the EcmaScript spec Strict Mode Issues related to non-compliance with the ES5 Strict Mode spec
Projects
None yet
Development

No branches or pull requests

3 participants