( u: unknown, seen = new Map<object, unknown>() )
| 2216 | * Throws when a trap throws or a buffer is detached. |
| 2217 | */ |
| 2218 | const materialize = ( |
| 2219 | u: unknown, |
| 2220 | seen = new Map<object, unknown>() |
| 2221 | ): unknown => { |
| 2222 | if (typeof u === "function") return standIn(u) |
| 2223 | if (typeof u !== "object" || u === null) return u |
| 2224 | if (Equal.symbol in u) return standIn(u) |
| 2225 | const known = seen.get(u) |
| 2226 | if (known !== undefined) return known |
| 2227 | if (ArrayBuffer.isView(u)) { |
| 2228 | const kind: string | undefined = typedArrayTag.call(u) |
| 2229 | if (kind === undefined) { |
| 2230 | // A `DataView`: its getter throws on a detached buffer, skipping the check. |
| 2231 | return new DataView( |
| 2232 | copyBytes( |
| 2233 | dataViewBuffer.call(u), |
| 2234 | dataViewOffset.call(u), |
| 2235 | dataViewLength.call(u) |
| 2236 | ).buffer |
| 2237 | ) |
| 2238 | } |
| 2239 | // `Equal.equals` compares typed array elements with `!==`, which never matches `NaN` and matches `0` with `-0`, |
| 2240 | // and doesn't tell kinds apart. A record under a private key holds the kind and the bytes instead. |
| 2241 | const out: Record<PropertyKey, unknown> = Object.create(null) |
| 2242 | out[typedArrayKey] = [ |
| 2243 | kind, |
| 2244 | copyBytes( |
| 2245 | typedArrayBuffer.call(u), |
| 2246 | typedArrayOffset.call(u), |
| 2247 | typedArrayLength.call(u) |
| 2248 | ).buffer |
| 2249 | ] |
| 2250 | return out |
| 2251 | } |
| 2252 | if (Array.isArray(u)) { |
| 2253 | const length = Object.getOwnPropertyDescriptor(u, "length")!.value as number |
| 2254 | const out: Array<unknown> = new Array(length) |
| 2255 | seen.set(u, out) |
| 2256 | for (let i = 0; i < length; i++) { |
| 2257 | const descriptor = Object.getOwnPropertyDescriptor(u, i) |
| 2258 | out[i] = descriptor === undefined |
| 2259 | ? undefined |
| 2260 | : "value" in descriptor |
| 2261 | ? materialize(descriptor.value, seen) |
| 2262 | : accessorSlot |
| 2263 | } |
| 2264 | return out |
| 2265 | } |
| 2266 | // `instanceof` only walks the prototypes; the slot check then tells a built-in object from a `Proxy` or an |
| 2267 | // object made from its prototype, which are copied as records. |
| 2268 | if (u instanceof Date && hasSlot(dateTime, u)) { |
| 2269 | return new Date(dateTime.call(u)) |
| 2270 | } |
| 2271 | if (u instanceof RegExp && hasSlot(regExpSource, u)) { |
| 2272 | return new RegExp(regExpSource.call(u), regExpFlags(u)) |
| 2273 | } |
| 2274 | if (u instanceof ArrayBuffer && hasSlot(arrayBufferLength, u)) { |
| 2275 | return copyBytes(u as ArrayBuffer, 0, arrayBufferLength.call(u)).buffer |
no test coverage detected