-
Notifications
You must be signed in to change notification settings - Fork 3
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
Scope-based privacy #44
Comments
This seems to nearly retain the flexibility of current WeakMap solutions and can be reasoned about in the same terms, which I like very much. One thing I don’t understand: what is the advantage of constraining use of private members to objects which were created in a scope that originally had access to them? While I don’t think there’s any great utility lost on account of that, it’s the one point where it’s less flexible than WeakMap, and it seems like it would add runtime overhead (the need for tracking what was available to each object) without an obvious benefit (it’s no less private if this constraint doesn’t exist). Do I have it backwards maybe? Is this behavior easier for engines to optimize or something? |
It also happens to cover the 99% use case for weak maps, and the transpiled equivalent trivially desugars. The remaining 1% is when you need to expose that weak map to other modules, but 99% of the time there, you don't actually need to care about the presence of the object in that map, just the data associated with it. (This comes up frequently in things like JSDOM.) In my experience, the only times you really need weak maps for anything this doesn't work for is when you need to add expando private data to an object you didn't create.
The advantage is that when you restrict it to in-scope object allocations, you gain the ability to know exactly how many slots to allocate for that object. Then it becomes a matter of tracking a static, per-AST key + offset list, which makes it flat out trivial for an engine to calculate optimal layout for such objects. (In fact, most statically typed languages operate this way, and it's why C/C++ have such well-defined object layouts.) Keep in mind, the class-oriented proposals just allow private fields to be scoped to classes, it amounts to about 90% of the work anyways.
You already have to track that with all the existing proposals, the difference is that it's only checking one scope, where mine needs to check potentially multiple. It's marginally harder for engines to optimize, but only marginally (and they'll do it regardless). Probably the bigger difference is how they would implement each one:
(It's still a pretty cheap check either way, and ICs are fully capable of eliminating it or at least dodging the memory access worst case scenario, in both cases. Also, the rest of the optimization process is roughly the same.) |
Cross-posted from tc39/proposal-class-fields#94 with edits for relevance
Edit: Inner classes and shadowed properties already exist in the class fields proposal. Updated to reflect this.
Repo/explainer: https://github.com/isiahmeadows/private-data-proposal
Since this whole "Classes 1.1" is about thinking from a clean slate, I thought I'd throw another thing at the wall, to see if it'd stick any.
The basic concept for my idea is to use scopes (for a very loose meaning of the term) as the basis of privacy rather than classes. It's mostly compatible with this proposal on the surface, but it avoids most of the gotchas and doesn't require explicit support for
protected
/friend
/etc. The extensibility was pretty easy to spec, and the only complicating factor was making private fields work with subclasses.Here's some of the benefits it offers:
private
can't be used:And yes, it's fully feature-compatible (and mostly syntactically compatible) with the existing class fields proposal, even though it's syntactically and semantically wildly different from its core:
private #x
in the class.#x = 1
/#x: 1
,#x() {}
, etc., it's implicitly declared for free if you didn't already declare it outside of the class/object. (This is where 99% of the syntax compatibility comes from.)Notably absent is this: the ability to guard outside access to certain fields to specific classes/scopes/modules. The proper way to handle this is just consenting-adults style: if they say it's private, just consider it private. Unless it's a password or something similarly sensitive (which should be clearly not exported at all), there's no need to truly care too much about it. If it's "protected" or "friend", the subclass or friend class could just choose to expose what you let it see anyways.
There are some drawbacks:
let
now, but other features likedo
expressions in the pipeline are starting to make meaningful use of curly braces more.)The text was updated successfully, but these errors were encountered: