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

A problem in Object.defineProperty #1319

Closed
Georgezxh opened this issue Apr 14, 2023 · 2 comments · Fixed by #1426
Closed

A problem in Object.defineProperty #1319

Georgezxh opened this issue Apr 14, 2023 · 2 comments · Fixed by #1426
Labels
bug Issues considered a bug Ecma Incompatibility Issues about Rhino being incompatible with the EcmaScript spec

Comments

@Georgezxh
Copy link

Description

Build version: 1.7.14
OS version: ubuntu20.04

The following code showed that rhino didn't change the length of array and even didn't trigger the get() method in defineProperty, while other js engines(like V8 in Google,Spidermokey in firefox) did.

Testcase

var array = new Array(5)
var proto = {};

Object.setPrototypeOf(array,proto);
Object.defineProperty(
  proto, 1, {
    get() {
      array.length = 1;
      return 1;
    },
});
var a=[];
var res=a.concat(array);
print(array.length);

Output

5

Expected behavior

1

@p-bakker p-bakker added Ecma Incompatibility Issues about Rhino being incompatible with the EcmaScript spec bug Issues considered a bug labels Jun 30, 2023
@gbrail
Copy link
Collaborator

gbrail commented Nov 28, 2023

I haven't dug in too deep, but I bet that the problem here isn't with defineProperty, but with concat. Specifically, Rhino tries to optimize if both sides of an "concat" are instances of NativeArray, which is why it isn't traversing the prototype chain:

if ((newlen <= Integer.MAX_VALUE) && (result instanceof NativeArray)) {

@gbrail
Copy link
Collaborator

gbrail commented Nov 30, 2023

Update -- the root problem is not what I thought, but it is the NativeArray class and its ability to correctly delegate to the prototype. I know how to fix this. Interestingly, fixing it doesn't unblock any test262 tests.

This is not hard to fix and it's indeed a difference from other engines, so I don't mind fixing it. But it raises the question that I often think of when these kinds of issues come up:

Why would someone want to do this? You are essentially creating an array, and then making it behave like an arbitrary object. Is that important?

Is the theory that, by doing this, the array will perform differently than an ordinary object? Or is it just one of the millions of ways to be clever with JavaScript?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issues considered a bug Ecma Incompatibility Issues about Rhino being incompatible with the EcmaScript spec
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants