Skip to content
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

fix: ensure internal cloning can work circular values #14347

Merged
merged 5 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cool-trains-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: ensure internal cloning can work circular values
15 changes: 13 additions & 2 deletions packages/svelte/src/internal/shared/clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ export function snapshot(value, skip_warning = false) {
* @param {Map<T, Snapshot<T>>} cloned
* @param {string} path
* @param {string[]} paths
* @param {null | T} original The original value, if `value` was produced from a `toJSON` call
* @returns {Snapshot<T>}
*/
function clone(value, cloned, path, paths) {
function clone(value, cloned, path, paths, original = null) {
if (typeof value === 'object' && value !== null) {
const unwrapped = cloned.get(value);
if (unwrapped !== undefined) return unwrapped;
Expand All @@ -63,6 +64,10 @@ function clone(value, cloned, path, paths) {
const copy = /** @type {Snapshot<any>} */ ([]);
cloned.set(value, copy);

if (original !== null) {
cloned.set(original, copy);
}

for (let i = 0; i < value.length; i += 1) {
copy.push(clone(value[i], cloned, DEV ? `${path}[${i}]` : path, paths));
}
Expand All @@ -75,6 +80,10 @@ function clone(value, cloned, path, paths) {
const copy = {};
cloned.set(value, copy);

if (original !== null) {
cloned.set(original, copy);
}

for (var key in value) {
// @ts-expect-error
copy[key] = clone(value[key], cloned, DEV ? `${path}.${key}` : path, paths);
Expand All @@ -92,7 +101,9 @@ function clone(value, cloned, path, paths) {
/** @type {T & { toJSON(): any } } */ (value).toJSON(),
cloned,
DEV ? `${path}.toJSON()` : path,
paths
paths,
// Associate the instance with the toJSON clone
value
);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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]);
}
});
Original file line number Diff line number Diff line change
@@ -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