fix: ensure internal cloning can work circular values (#14347)

* fix: ensure internal cloning can work circular values

* better fixc

* 'original' feels slightly clearer than 'json_instance'

* use an optional parameter, so we can omit it in most cases

* Update packages/svelte/src/internal/shared/clone.js

Co-authored-by: Rich Harris <rich.harris@vercel.com>

---------

Co-authored-by: Rich Harris <rich.harris@vercel.com>
pull/14363/head
Dominic Gannaway 1 month ago committed by GitHub
parent f6117bb328
commit 741106879b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: ensure internal cloning can work circular values

@ -49,9 +49,10 @@ export function snapshot(value, skip_warning = false) {
* @param {Map<T, Snapshot<T>>} cloned * @param {Map<T, Snapshot<T>>} cloned
* @param {string} path * @param {string} path
* @param {string[]} paths * @param {string[]} paths
* @param {null | T} original The original value, if `value` was produced from a `toJSON` call
* @returns {Snapshot<T>} * @returns {Snapshot<T>}
*/ */
function clone(value, cloned, path, paths) { function clone(value, cloned, path, paths, original = null) {
if (typeof value === 'object' && value !== null) { if (typeof value === 'object' && value !== null) {
const unwrapped = cloned.get(value); const unwrapped = cloned.get(value);
if (unwrapped !== undefined) return unwrapped; if (unwrapped !== undefined) return unwrapped;
@ -63,6 +64,10 @@ function clone(value, cloned, path, paths) {
const copy = /** @type {Snapshot<any>} */ ([]); const copy = /** @type {Snapshot<any>} */ ([]);
cloned.set(value, copy); cloned.set(value, copy);
if (original !== null) {
cloned.set(original, copy);
}
for (let i = 0; i < value.length; i += 1) { for (let i = 0; i < value.length; i += 1) {
copy.push(clone(value[i], cloned, DEV ? `${path}[${i}]` : path, paths)); copy.push(clone(value[i], cloned, DEV ? `${path}[${i}]` : path, paths));
} }
@ -75,6 +80,10 @@ function clone(value, cloned, path, paths) {
const copy = {}; const copy = {};
cloned.set(value, copy); cloned.set(value, copy);
if (original !== null) {
cloned.set(original, copy);
}
for (var key in value) { for (var key in value) {
// @ts-expect-error // @ts-expect-error
copy[key] = clone(value[key], cloned, DEV ? `${path}.${key}` : path, paths); copy[key] = clone(value[key], cloned, DEV ? `${path}.${key}` : path, paths);
@ -92,7 +101,9 @@ function clone(value, cloned, path, paths) {
/** @type {T & { toJSON(): any } } */ (value).toJSON(), /** @type {T & { toJSON(): any } } */ (value).toJSON(),
cloned, cloned,
DEV ? `${path}.toJSON()` : path, DEV ? `${path}.toJSON()` : path,
paths paths,
// Associate the instance with the toJSON clone
value
); );
} }
} }

@ -0,0 +1,23 @@
import { test } from '../../test';
export default test({
compileOptions: {
dev: true
},
async test({ assert, logs }) {
var a = {
a: {}
};
a.a = a;
var b = {
a: {
b: {}
}
};
b.a.b = b;
assert.deepEqual(logs, ['init', a, 'init', b]);
}
});

@ -0,0 +1,23 @@
<script>
class A {
toJSON(){
return {
a: this
}
}
}
const state = $state(new A());
$inspect(state);
class B {
toJSON(){
return {
a: {
b: this
}
}
}
}
const state2 = $state(new B());
$inspect(state2);
</script>
Loading…
Cancel
Save