-
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
New integrity level #15
Comments
I would expect such an integrity level to apply to everything - preventing a promise from ever fulfilling, preventing any changes to a Weak Map/Set, etc. Functions and Errors have no internal slots that change, so i'd expect it to either be a noop for these objects, or better, for them to be born "hardened". Your list is missing Dates, which would become immutable, also hardened RegExps would have to fail to be changeable via For Arrays, none of that info is internal slots, so I would not expect hardening an Array to have any effect - you can freeze an array for that. |
@ljharb thanks! I've added the missing The internal state I referred does not the same as an internal slot. It means a necessary part of a data structure to make the data it refers to mutable. For example, in RegExp, I choose to harden the harden(arr)
arr[0] = a // error
arr.length = 0 // error
arr.own = a // OK, define new own property For Functions, maybe we can harden the It's technically ok to harden |
It is very useful to prevent people from clearing a weak map or weak set, or from changing or removing mappings. I don’t think we need a new integrity level for anything to do with non-exotic properties - function length is configurable, and can easily be made not so (also, the name is the same as the length). Array length might be a special case for arrays, though. In other words, for most properties, i don’t think it would be appropriate to harden them - that’s already handled by existing APIs. |
Oh, I only think of adding to the weak map/set. That's reasonable, I have updated the table.
Then what about |
That's already possible to make immutable: var r = /a/g;
r.lastIndex; // 0
r.exec('aaaa')
r.lastIndex; // 1
Object.defineProperty(r, 'lastIndex', { configurable: false, writable: false });
r.exec('aaaa') // throws TypeError and surprisingly, if you continue with the same code, the internal slots for source and flags are also now immutable: Object.freeze(r);
r.source; // 'a'
r.flags; // 'g'
r.compile('b', 'gi'); // throws TypeError because However, making lastIndex immutable makes a global regex largely useless - but if you leave lastIndex mutable, then the source and flags can be changed out from under you at any time. Thus, having a way to make these internal slots immutable WITHOUT making |
Hmm, this RegExp case is very interesting, but that will be strange if harden a Map make the map immutable, but harden a RegExp its lastIndex still might change. |
It wouldn’t make the Map’s properties immutable, just its internal storage. |
Yeah, I know. Map's own properties do not interact with all built-in methods on Map (e.g. |
Right - but it's not internal state, it's just a normal property, so there's no need for a new way to lock it down. |
I have updated the term to |
I am struggling a bit to fully understand what the impact here will be, and have similar concerns to @ljharb . A new integrity level, I would expect to be applied broadly -- but at the moment I only really understand this in relation to map/set and weakmap/set and similar collections. Can you elaborate a bit more about what you intend to communicate with "new integrity level"? I also have some concerns about something so broad without a clear motivation, it may make more sense to scope this. I am discussing this with my team currently, but as it is on the agenda for tomorrow I am trying to give early feedback so this isn't fully thought through. |
I'll expect host data structures (and user-land objects) to opt into the new protocol. Since we only have a few data structures in the language (Map, Set, Date, ...), it's a challenge to clearly define what is it and how it should behave. |
Conclusion:
I may change the conclusion above after I review the meeting notes. |
Re: clearing, |
It doesn't seem that the conclusion is to expose |
I revisited this issue and found the discussion very interesting. @erights talked about
|
Follow-on of discussion in #13, I organized ideas with a new "new integrity level" that freeze the internal state of an object. What do you think?
(cc @erights, @ljharb, @phoddie, @syg)
stabilized state (temporally name)
A new integrity level of object:
Object.freeze
)difference between
harden
harden
is recursive,stabilize
is not.harden
care about the property descriptors,stabilize
only care about the meaningful fields (and internal slots) that play roles in its data structure.New APIs:
isStable(x)
stablilzed(x)
Terms
ECMAScript defined:
1
,true
)new Number(1)
)Array
Refuse to add new array index properties.
ArrayBuffer
[[ArrayBufferData]]
(but allowing detach to transfer).Refuse to modify
[[ArrayBufferByteLength]]
(refuse to expand/shrink size).Date
[[DateValue]]
.EvalError
, ...)FinalizationRegistry
&WeakRef
Function
length
andname
unconfigurable?globalThis
ArrayIterator
, ...)Map
&Set
[[MapData]]
or[[SetData]]
field.Promise
It's either already stabilized (fulfilled/rejected), or useless after hardened (pending).
Proxy
Symbol.harden
.RegExp
lastIndex
property non-writable.Refuse to
compile()
.ShadowRealm
❌ Refuse to be stabilized
✅ Refuse to compile new code (by
evaluate
,eval
,Function
orimportValue
)DataView
It is a view, you must freeze the underlying buffer with the buffer directly.
WeakMap
&WeakSet
[[WeakMapData]]
or[[WeakSetData]]
field.Userland objects:
Symbol.stabilize
protocol:Extra APIs:
Those methods are useful but not directly related to the new integrity level, and not possible to make a unified protocol for all objects. I list them case-by-case.
https://github.com/tc39/proposal-readonly-collections
%TypedArray%.prototype.readOnlyView()
DataView.prototype.readOnlyView()
The text was updated successfully, but these errors were encountered: