-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
feat: use state proxy ancestry for ownership validation #11184
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
Merged
Merged
Changes from 12 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
4c16847
rename metadata.o to metadata.owners, tweak check_ownership implement…
Rich-Harris 14b0be3
track parent relationships
Rich-Harris 8bfef74
update
Rich-Harris 7a5918a
changeset
Rich-Harris fab34ab
adjust test to reflect new semantics
Rich-Harris 8e515b1
prevent component using bind: with object it does not own
Rich-Harris 5440862
failing test
Rich-Harris a638a07
fix #11060
Rich-Harris f6b5553
add explanatory comment
Rich-Harris ed78fed
Merge branch 'main' into proxy-parent
dummdidumm e1c79ba
don't accidentally narrow global ownership, fix has_owner method
dummdidumm abeb31a
widen ownership if assigning different state proxies to one another
dummdidumm 084aabf
don't set owners to null when parent exists
dummdidumm d5791fb
fix
Rich-Harris 47e35cf
Merge branch 'main' into proxy-parent
trueadm 60b24de
only recurse into POJOs
Rich-Harris 32183d1
handle cycles on context
Rich-Harris 05856f3
Merge branch 'proxy-parent' of github.com:sveltejs/svelte into proxy-…
Rich-Harris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'svelte': patch | ||
--- | ||
|
||
feat: use state proxy ancestry for ownership validation |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
/** @typedef {{ file: string, line: number, column: number }} Location */ | ||
|
||
import { STATE_SYMBOL } from '../constants.js'; | ||
import { untrack } from '../runtime.js'; | ||
import { render_effect } from '../reactivity/effects.js'; | ||
import { current_component_context, untrack } from '../runtime.js'; | ||
|
||
/** @type {Record<string, Array<{ start: Location, end: Location, component: Function }>>} */ | ||
const boundaries = {}; | ||
|
@@ -63,6 +64,8 @@ function get_component() { | |
return null; | ||
} | ||
|
||
export const ADD_OWNER = Symbol('ADD_OWNER'); | ||
|
||
/** | ||
* Together with `mark_module_end`, this function establishes the boundaries of a `.svelte` file, | ||
* such that subsequent calls to `get_component` can tell us which component is responsible | ||
|
@@ -98,60 +101,115 @@ export function mark_module_end(component) { | |
} | ||
|
||
/** | ||
* | ||
* @param {any} object | ||
* @param {any} owner | ||
* @param {boolean} [global] | ||
*/ | ||
export function add_owner(object, owner) { | ||
untrack(() => { | ||
add_owner_to_object(object, owner); | ||
}); | ||
export function add_owner(object, owner, global = false) { | ||
if (object && !global) { | ||
// @ts-expect-error | ||
const component = current_component_context.function; | ||
const metadata = object[STATE_SYMBOL]; | ||
if (metadata && !has_owner(metadata, component)) { | ||
let original = get_owner(metadata); | ||
|
||
if (owner.filename !== component.filename) { | ||
let message = `${component.filename} passed a value to ${owner.filename} with \`bind:\`, but the value is owned by ${original.filename}. Consider creating a binding between ${original.filename} and ${component.filename}`; | ||
|
||
// eslint-disable-next-line no-console | ||
console.warn(message); | ||
} | ||
} | ||
} | ||
|
||
add_owner_to_object(object, owner); | ||
} | ||
|
||
/** | ||
* @param {import('#client').ProxyMetadata | null} from | ||
* @param {import('#client').ProxyMetadata} to | ||
*/ | ||
export function widen_ownership(from, to) { | ||
if (to.owners === null) { | ||
return; | ||
} | ||
|
||
while (from) { | ||
if (from.owners === null) { | ||
to.owners = null; | ||
break; | ||
} | ||
for (const owner of from.owners) { | ||
to.owners.add(owner); | ||
} | ||
from = from.parent; | ||
} | ||
} | ||
|
||
/** | ||
* @param {any} object | ||
* @param {Function} owner | ||
*/ | ||
function add_owner_to_object(object, owner) { | ||
if (object?.[STATE_SYMBOL]?.o && !object[STATE_SYMBOL].o.has(owner)) { | ||
object[STATE_SYMBOL].o.add(owner); | ||
const metadata = /** @type {import('#client').ProxyMetadata} */ (object?.[STATE_SYMBOL]); | ||
|
||
for (const key in object) { | ||
add_owner_to_object(object[key], owner); | ||
if (metadata) { | ||
// this is a state proxy, add owner directly, if not globally shared | ||
if (metadata.owners !== null) { | ||
metadata.owners.add(owner); | ||
} | ||
} else if (object && typeof object === 'object') { | ||
if (object[ADD_OWNER]) { | ||
// this is a class with state fields. we put this in a render effect | ||
// so that if state is replaced (e.g. `instance.name = { first, last }`) | ||
// the new state is also co-owned by the caller of `getContext` | ||
render_effect(() => { | ||
object[ADD_OWNER](owner); | ||
}); | ||
} else { | ||
// recurse until we find a state proxy | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we bail on non-POJOs here? IIUC this would also recurse into DOM elements, for example There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah yeah, probably a good idea |
||
for (const key in object) { | ||
add_owner_to_object(object[key], owner); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param {any} object | ||
* @param {import('#client').ProxyMetadata} metadata | ||
* @param {Function} component | ||
* @returns {boolean} | ||
*/ | ||
export function strip_owner(object) { | ||
untrack(() => { | ||
strip_owner_from_object(object); | ||
}); | ||
function has_owner(metadata, component) { | ||
if (metadata.owners === null) { | ||
return true; | ||
} | ||
|
||
return ( | ||
metadata.owners.has(component) || | ||
(metadata.parent !== null && has_owner(metadata.parent, component)) | ||
); | ||
} | ||
|
||
/** | ||
* @param {any} object | ||
* @param {import('#client').ProxyMetadata} metadata | ||
* @returns {any} | ||
*/ | ||
function strip_owner_from_object(object) { | ||
if (object?.[STATE_SYMBOL]?.o) { | ||
object[STATE_SYMBOL].o = null; | ||
|
||
for (const key in object) { | ||
strip_owner(object[key]); | ||
} | ||
} | ||
function get_owner(metadata) { | ||
return ( | ||
metadata?.owners?.values().next().value ?? | ||
get_owner(/** @type {import('#client').ProxyMetadata} */ (metadata.parent)) | ||
); | ||
} | ||
|
||
/** | ||
* @param {Set<Function>} owners | ||
* @param {import('#client').ProxyMetadata} metadata | ||
*/ | ||
export function check_ownership(owners) { | ||
export function check_ownership(metadata) { | ||
const component = get_component(); | ||
|
||
if (component && !owners.has(component)) { | ||
let original = [...owners][0]; | ||
if (component && !has_owner(metadata, component)) { | ||
let original = get_owner(metadata); | ||
|
||
let message = | ||
// @ts-expect-error | ||
|
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
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
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
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
39 changes: 39 additions & 0 deletions
39
packages/svelte/tests/runtime-runes/samples/non-local-mutation-global/_config.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { tick } from 'svelte'; | ||
import { test } from '../../test'; | ||
|
||
/** @type {typeof console.warn} */ | ||
let warn; | ||
|
||
/** @type {any[]} */ | ||
let warnings = []; | ||
|
||
export default test({ | ||
html: `<button>clicks: 0</button>`, | ||
|
||
compileOptions: { | ||
dev: true | ||
}, | ||
|
||
before_test: () => { | ||
warn = console.warn; | ||
|
||
console.warn = (...args) => { | ||
warnings.push(...args); | ||
}; | ||
}, | ||
|
||
after_test: () => { | ||
console.warn = warn; | ||
warnings = []; | ||
}, | ||
|
||
async test({ assert, target }) { | ||
const btn = target.querySelector('button'); | ||
btn?.click(); | ||
await tick(); | ||
|
||
assert.htmlEqual(target.innerHTML, `<button>clicks: 1</button>`); | ||
|
||
assert.deepEqual(warnings, []); | ||
} | ||
}); |
3 changes: 3 additions & 0 deletions
3
packages/svelte/tests/runtime-runes/samples/non-local-mutation-global/child.svelte
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<script> | ||
let { a = $bindable() } = $props(); | ||
</script> |
Oops, something went wrong.
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.
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.
why is a render effect necessary here?
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.
added a comment