opencode/packages/codemode-rust/conformance/cases/lang-entry-errors.js
kommander 6c2724c58f fix(codemode): new Map and Object.fromEntries reject a non-entry as the reference does
Both read their input as iteration yields it, reject an entry that is not an object (Object.fromEntries also one holding an opaque reference) with the reference's message, and close the iterator first.
2026-11-18 18:56:11 +01:00

39 lines
1.1 KiB
JavaScript

// new Map and Object.fromEntries reject an entry that is not an object as they reach it, closing the iterator.
const attempt = (f) => {
try {
return f()
} catch (error) {
return String(error)
}
}
const closing = (items) => {
const log = []
const iterable = {
[Symbol.iterator]() {
let i = 0
return {
next: () => (i < items.length ? { done: false, value: items[i++] } : { done: true }),
return() {
log.push("closed")
return {}
},
}
},
}
return [iterable, log]
}
const [entries, mapLog] = closing([[1, 2], null])
const [pairs, objectLog] = closing([["a", 1], "ab"])
return {
map: [attempt(() => new Map([[1, 2], , ])), attempt(() => new Map([1])), attempt(() => new Map(entries)), mapLog],
weakMap: attempt(() => new WeakMap([1])),
fromEntries: [
attempt(() => Object.fromEntries([["a", 1], , ])),
attempt(() => Object.fromEntries(["ab"])),
attempt(() => Object.fromEntries([() => 1])),
attempt(() => Object.fromEntries([[1, () => 2]])),
attempt(() => Object.fromEntries(pairs)),
objectLog,
],
fine: Object.fromEntries(new Map([["b", 2]])),
}