feat: do not use Object.defineProperty?

This commit is contained in:
2026-01-17 16:52:58 +08:00
parent 09bfbca64a
commit 513b43965c
2 changed files with 8 additions and 33 deletions

View File

@@ -4,6 +4,7 @@
import type { NixValue, NixAttrs, NixList } from "../types"; import type { NixValue, NixAttrs, NixList } from "../types";
import { forceAttrs, forceString, forceFunction, forceList } from "../type-assert"; import { forceAttrs, forceString, forceFunction, forceList } from "../type-assert";
import { createThunk } from "../thunk";
export const attrNames = (set: NixValue): string[] => Object.keys(forceAttrs(set)).sort(); export const attrNames = (set: NixValue): string[] => Object.keys(forceAttrs(set)).sort();
@@ -22,17 +23,13 @@ export const hasAttr =
export const mapAttrs = export const mapAttrs =
(f: NixValue) => (f: NixValue) =>
(attrs: NixValue): NixAttrs => { (attrs: NixValue): NixAttrs => {
const forced_attrs = forceAttrs(attrs); const forcedAttrs = forceAttrs(attrs);
const forced_f = forceFunction(f); const forcedF = forceFunction(f);
const new_attrs: NixAttrs = {}; const newAttrs: NixAttrs = {};
for (const key in forced_attrs) { for (const key in forcedAttrs) {
Object.defineProperty(new_attrs, key, { newAttrs[key] = createThunk(() => forceFunction(forcedF(key))(forcedAttrs[key]));
get: () => forceFunction(forced_f(key))(forced_attrs[key]),
enumerable: true,
configurable: true,
});
} }
return new_attrs; return newAttrs;
}; };
export const removeAttrs = export const removeAttrs =

View File

@@ -250,27 +250,5 @@ export const op = {
return Array.prototype.concat.call(forceList(a), forceList(b)); return Array.prototype.concat.call(forceList(a), forceList(b));
}, },
update: (a: NixValue, b: NixValue): NixAttrs => { update: (a: NixValue, b: NixValue): NixAttrs => ({ ...forceAttrs(a), ...forceAttrs(b) }),
const forcedA = forceAttrs(a);
const forcedB = forceAttrs(b);
const newAttrs: NixAttrs = {};
for (const key in forcedA) {
Object.defineProperty(newAttrs, key, {
get: () => force(forcedA[key]),
enumerable: true,
configurable: true,
});
}
for (const key in forcedB) {
Object.defineProperty(newAttrs, key, {
get: () => force(forcedB[key]),
enumerable: true,
configurable: true,
});
}
return newAttrs;
},
}; };