fix: vnode clone compatible with frozen intrinsics#5078
Open
kumavis wants to merge 1 commit intopreactjs:mainfrom
Open
fix: vnode clone compatible with frozen intrinsics#5078kumavis wants to merge 1 commit intopreactjs:mainfrom
kumavis wants to merge 1 commit intopreactjs:mainfrom
Conversation
Object.assign({}, vnode) in renderComponent() (and cloneNode in
src/diff/index.js) tries to copy a vnode's `constructor: undefined`
own property via [[Set]]. Under https://hardenedjs.org/ environment the default
overrideTaming makes Object.prototype.constructor non-writable, so
the assignment fails with:
TypeError: Cannot assign to read only property 'constructor'
Initial render works because createVNode builds vnodes with an
object literal ([[CreateDataProperty]], not Set), but any
setState/forceUpdate-triggered re-render goes through
Object.assign({}, oldVNode) and trips the override mistake.
📊 Tachometer Benchmark ResultsSummaryduration
usedJSHeapSize
Resultscreate10kduration
usedJSHeapSize
filter-listduration
usedJSHeapSize
hydrate1kduration
usedJSHeapSize
many-updatesduration
usedJSHeapSize
replace1kduration
usedJSHeapSize
run-warmup-0
run-warmup-1
run-warmup-2
run-warmup-3
run-warmup-4
run-final
text-updateduration
usedJSHeapSize
tododuration
usedJSHeapSize
update10th1kduration
usedJSHeapSize
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes a compatibility issue when Preact is run in Hardened JavaScript environments such as:
The compatibility issue comes from a javascript quirk known as "the override mistake" related to frozen intrinsics. For this bug, the minimal environment change is just
Object.freeze(Object.prototype).In this environment, assigning the
constructorproperty via assignment syntax (=) orObject.assigncauses an error.Specifically,
Object.assign({}, vnode)inrenderComponent()(andcloneNodeinsrc/diff/index.js) tries to copy a vnode'sconstructor: undefinedown property via[[Set]]. Under environments with a frozenObject.prototypetheObject.prototype.constructoris non-writable, so the assignment fails.This PR introduces a
cloneVNodeutility that ensuresVNodesare cloned safely. It also reorders theconstructorkey for consistent object shape betweenVNodesand their clones.Note: I erred on the side of excessive documentation in comments, let me know if you would like this reduced.