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

Can you stamp a private field on a struct? #42

Open
bakkot opened this issue Oct 8, 2024 · 0 comments
Open

Can you stamp a private field on a struct? #42

bakkot opened this issue Oct 8, 2024 · 0 comments

Comments

@bakkot
Copy link

bakkot commented Oct 8, 2024

Capturing a topic discussed in plenary and on Matrix.

The accursed return override trick lets you put private fields on (almost) any object, even a frozen object:

class ReturnOverride { constructor(o) { return o; } }

class Stamp extends ReturnOverride {
  #x = 1;
  static isStamped(o) {
    return #x in o;
  }
}

let object = Object.freeze({});
console.log(Stamp.isStamped(object)); // false
new Stamp(object);
console.log(Stamp.isStamped(object)); // true

Should you be allowed to do this to a struct? This violates the "fixed layout" goal, under common implementations of private fields.

If no, how exactly is it denied?

Is the failure when you try to instantiate a class with a private field and its super constructor returns a struct? That's one option; that's how it works for WindowProxy.

Another option is to say that classes are forbidden from extending structs: i.e., it is an error if super() returns a struct (either because of class extends Struct or because of a return override). As a consequence you mechanically would not be able add a private field to a struct. I like this option.

cc @erights

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

No branches or pull requests

1 participant