Skip to content
This repository has been archived by the owner on Jan 25, 2022. It is now read-only.

Editorial: Phrase private methods as brands, not fields #52

Merged
merged 1 commit into from
Dec 12, 2018

Conversation

littledan
Copy link
Member

Many people attempting to implement private methods, both in
transpilers and native JS engines, have run into similar sources
of confusion about semantics and optimizability:

  • Many have the misconception that each instance of a class with
    private methods has its own copy of that method, as if it were
    written in a field initializer, when actually, the same function
    identity is intended.
  • Private methods are designed to have semantics that they can be
    implemented as a single check that the receiver "has" the private
    method, followed by a call of that method, without taking up
    memory space per-instance. However, many implementers are reaching
    for a direct implementation of them as non-writable private fields,
    which ends up taking a large amount of additional space.

To clear things up, this patch rephrases private methods and accessors'
semantics as being based on a brand check, followed by a function call.
Each object has a list of "private brands" it supports, which are
identified by Klass.prototype, for classes Klass which have private
methods or accessors. Private names are considered to have both a
reference to this brand as well as the actual method linked from them.
My hope is that this will be a reasonable first-pass implementation
strategy; there is a note about how to optimize the brand list further.

With this change, the static class features needs some simple changes,
and the decorators proposal needs some larger changes. See
tc39/proposal-decorators#180 for details.

spec.html Outdated
<emu-alg>
1. If _O_.[[PrivateBrands]] contains an entry _e_ such that SameValue(_e_, _brand_) is *true*,
1. Throw a *TypeError* exception.
1. Append _P_ to _O_.[[PrivateBrands]].
Copy link
Member

Choose a reason for hiding this comment

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

What is P?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, that should say brand, thanks, will fix.

Many people attempting to implement private methods, both in
transpilers and native JS engines, have run into similar sources
of confusion about semantics and optimizability:
- Many have the misconception that each instance of a class with
  private methods has its own copy of that method, as if it were
  written in a field initializer, when actually, the same function
  identity is intended.
- Private methods are designed to have semantics that they can be
  implemented as a single check that the receiver "has" the private
  method, followed by a call of that method, without taking up
  memory space per-instance. However, many implementers are reaching
  for a direct implementation of them as non-writable private fields,
  which ends up taking a large amount of additional space.

To clear things up, this patch rephrases private methods and accessors'
semantics as being based on a brand check, followed by a function call.
Each object has a list of "private brands" it supports, which are
identified by Klass.prototype, for classes Klass which have private
methods or accessors. Private names are considered to have both a
reference to this brand as well as the actual method linked from them.
My hope is that this will be a reasonable first-pass implementation
strategy; there is a note about how to optimize the brand list further.

With this change, the static class features needs some simple changes,
and the decorators proposal needs some larger changes. See
tc39/proposal-decorators#180 for details.
@littledan
Copy link
Member Author

OK, I'm going to land this patch so it's more easy to read this version. If anyone has concerns with it, please say so here, and I can roll it back.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants