-
Notifications
You must be signed in to change notification settings - Fork 5
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
Initial API design #13
Conversation
I have updated the design based on feedback from @hax:
|
This will create a breaking change, as |
Oh, I didn't notice that. We can select another name. |
Ok, no reviews since then. I may merge this and make a non-advance presentation to notify this change. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @Jack-Works , I like this approach.
LGTM , thanks!
Why not change the spec of |
@erights that sounds like it might not be web compatible. |
I'll make a presentation in the future and hope I can get feedback from implementors. |
I strongly support a path to immutable ArrayBuffers in JavaScript and welcome this discussion. Immutable ArrayBuffers have long been supported in the XS engine as part of preparing a virtual machine to run from read-only flash storage. That said, Moddable's view is that immutable ArrayBuffers should done as part of addressing immutability in the language in a consistent manner, not on an object-by-object basis. This proposal isn't freeze. As @ljharb notes, freeze has well defined behavior with respect to the IntegrityLevel. This is a new IntegrityLevel beyond frozen and should have a different name to make that clear. Having just been through implementation of resizable ArrayBuffer, the engine overhead for an inescapable slice seems high. That could be acceptable if the value is also high. I'd want to understand the need more clearly than is described in #11 before exploring solutions. |
Is the idea that |
For what it's worth, adding a true immutable integrity level, beyond "frozen" - one that worked on Maps, Sets, Promises, RegExps, Dates, as well as ArrayBuffers - would be a wonderful proposal. |
I completely agree. And we do "petrify" for all of those in the Moddable SDK. I've considered proposing it here, but the impression I get from discussions is that it wouldn't fly yet. I would be happy to be wrong about that.; ) |
Yes, this API is intended to match the readonly collection proposal. I would expect the common use case of this API is no changes to the snapshot at all so a Copy-on-Write would be a better way to implement this.
A new integrity level is interesting, maybe the Host APIs can get benefits from it too. I'm curious how Promises can be frozen, once it is frozen, it becomes useless (if it's pending, it's forever pending, otherwise they can not make a state transition already). Should we try to merge all tries on this route and make a unified proposal to define a new integrity level and a unified API? |
@Jack-Works yes, a truly immutable Promise would either be already-fulfilled (and thus immutable anyways) or forever-pending (and thus be useless). I think a unified proposal would be great. |
Peter and I have talked alot about Indeed, anything that did reach through an encapsulation barrier to freeze internals that should have been protected would be an attack we must not enable. |
On this arraybuffer proposal, @ljharb is indeed correct, and I was confused. I was confusing ArrayBuffers with TypedArrays. TypedArrays currently cannot be frozen either. However, unlike Array, applying Object.freeze to a non-empty TypedArray throws. It cannot do otherwise without freezing the backing ArrayBuffer. Which leads to an interesting idea:
|
@erights I want to clarify a couple points specifically about |
@phoddie that is correct. As you say, we've gone through some changes over time ;).
On the naming, I thought we settled on something other than However, as with the TypedArray / ArrayBuffer above, we can consider making Were hardened a distinct integrity level, we could also entertain the possibility that properties of hardened objects do not trigger the override mistake. Though I realize this step will be more controversial. |
Why would the verb be an attack? I want to be able to make builtin instances immutable. |
If your class C creates an instance, I'd certainly love it if C could make the instance immutable. But having made, say, a defensive instance guarding mutable state, and giving me access to the instance, I can subvert the purpose of the instance --- cause it to misbehave --- by petrifying it in ways that violate the assumption made by the author of C. |
For instances of most builtins, like Date and ArrayBuffer, in the rest of my message above I propose that locking down their state should not be considered an attack, and so we can consider stretching the behavior of Promises, WeakMaps, Proxies are examples of builtin instances that encapsulate. Making them immutable from the outside should be prohibited, as it would subvert the way these are used to encapsulate state. |
@ljharb I suspect you're also thinking of a non-transitive operation. The transitive |
Oh certainly, just like freeze and everything else in the language, it’d need to be shallow. |
I don't think the COW and snapshot API will be a good fit for WebAssembly and lower-level web APIs, as normal usage for this kind of API promotes making new ArrayBuffers. |
About new integrity level, please move the discussion to #15 |
I tried to design the API based on the feedback from the stage-1 presentation on tc39. And I also try to match the API with readonly collection proposal.
Hope to get early feedback from the following people so I can iterate on the API design to resolve concerns. Thanks!
I want to have feedback, especially from implementors because this proposal will create new paths on ArrayBuffers.