fix: deepSeq

This commit is contained in:
2026-02-08 15:35:37 +08:00
parent d09b84676c
commit bd9eb638af

View File

@@ -2,10 +2,11 @@
* Functional programming builtin functions * Functional programming builtin functions
*/ */
import { CatchableError, HAS_CONTEXT, type NixValue } from "../types"; import { CatchableError, type NixValue } from "../types";
import { force } from "../thunk"; import { force } from "../thunk";
import { coerceToString, StringCoercionMode } from "./conversion"; import { coerceToString, StringCoercionMode } from "./conversion";
import { printValue } from "../print"; import { printValue } from "../print";
import { isAttrs } from "./type-check";
export const seq = export const seq =
(e1: NixValue) => (e1: NixValue) =>
@@ -17,16 +18,25 @@ export const seq =
export const deepSeq = export const deepSeq =
(e1: NixValue) => (e1: NixValue) =>
(e2: NixValue): NixValue => { (e2: NixValue): NixValue => {
const forced = force(e1); const seen: Set<NixValue> = new Set;
const recurse = (e: NixValue) => {
if (!seen.has(e)) {
seen.add(e);
} else {
return
}
const forced = force(e);
if (Array.isArray(forced)) { if (Array.isArray(forced)) {
for (const val of forced) { for (const val of forced) {
deepSeq(val); recurse(val);
} }
} else if (typeof forced === "object" && forced !== null && !(HAS_CONTEXT in forced)) { } else if (isAttrs(forced)) {
for (const [_, val] of Object.entries(forced)) { for (const [_, val] of Object.entries(forced)) {
deepSeq(val); recurse(val);
} }
} }
}
recurse(e1);
return e2; return e2;
}; };