refactor(codemode): move collection constructors, groupBy, forEach, getOrInsertComputed, and Set methods to the prelude

This commit is contained in:
James Long
2026-11-05 15:20:57 +00:00
Unverified
parent 0bff057313
commit d8504a97b0
5 changed files with 210 additions and 449 deletions
+70 -4
View File
@@ -1,4 +1,7 @@
import { requireCallback } from "../interpreter/callback.js"
import { isSupportedCallback, requireCallback } from "../interpreter/callback.js"
import type { Builtins } from "../interpreter/intrinsics.js"
import { iterateDirect } from "../machine/iteration.js"
import { weakKey } from "../stdlib/collections.js"
import type { Interpreter } from "../interpreter/interpreter.js"
import { checkArrayLength } from "../interpreter/limits.js"
import { invalidData, IteratorSymbol, PendingThrow } from "../interpreter/model.js"
@@ -6,6 +9,11 @@ import { native, receiver } from "../interpreter/native.js"
import {
Arr,
Bytes,
coerceToString,
define,
getOwn,
IteratorObj,
positionIterator,
coerceToInteger,
coerceToNumber,
GeneratorObj,
@@ -22,7 +30,7 @@ import {
WeakSetObj,
} from "../interpreter/objects.js"
import { builtinCursor } from "../interpreter/operations.js"
import { describeValue } from "../interpreter/references.js"
import { describeValue, isOpaque } from "../interpreter/references.js"
import { Invoke } from "../interpreter/signals.js"
// The prelude's privileged helpers: leaf built-ins (they never call program code) bound only in the prelude's scope.
@@ -42,7 +50,7 @@ const classes = {
export const helpers = (ctx: Interpreter<unknown>): Array<readonly [string, Value]> => {
const builtins = ctx.builtins
const table: Record<string, Helper> = {
const helpers: Record<string, Helper> = {
/** Throws a program error of `type` ("TypeError", "RangeError", or "InvalidData") with `message`. */
$Throw: [
2,
@@ -61,9 +69,67 @@ export const helpers = (ctx: Interpreter<unknown>): Array<readonly [string, Valu
([value, kind, method]) => receiver<Obj>(classes[kind as keyof typeof classes], value, String(method)),
],
$IsObject: [1, ([value]) => value instanceof Obj],
$IsCallable: [1, ([value]) => isSupportedCallback(value)],
/** An iterator object as `for...of` steps it, reading `next` once (GetIteratorDirect); an array's values. */
$Direct: [
1,
([value]) =>
value instanceof Arr
? positionIterator(builtins, value, "values")
: new IteratorObj(builtins.Iterator, iterateDirect(value, undefined)),
],
/** What the collection built-ins need from a Map, Set, WeakMap, or WeakSet's table; see `table`. */
$Table: [4, ([op, target, key, value]) => table(builtins, String(op), target, key, value)],
}
return Object.entries(table).map(([name, [length, call]]) => [
return Object.entries(helpers).map(([name, [length, call]]) => [
name,
native(builtins, { name, length, call: (_, args) => call(args), callback: false }),
])
}
const table = (builtins: Builtins, op: string, target: Value, key: Value, value: Value): Value => {
switch (op) {
case "add":
if (target instanceof MapObj) target.map.set(key, value)
else if (target instanceof SetObj) target.set.add(key)
else if (target instanceof WeakMapObj) target.map.set(weakKey(key, "as weak map key"), value)
else (target as WeakSetObj).set.add(weakKey(key, "in weak set"))
return undefined
case "delete":
return (target as SetObj).set.delete(key)
case "has":
return (target as SetObj).set.has(key)
case "size":
return (target as SetObj).set.size
case "copy": {
const copy = new SetObj(builtins.Set)
for (const item of (target as SetObj).set.liveKeys()) copy.set.add(item)
return copy
}
case "values":
return positionIterator(builtins, target as SetObj, "values")
case "entries":
return positionIterator(builtins, target as MapObj | SetObj, "entries")
case "groups":
return target === "Map" ? new MapObj(builtins.Map) : new Obj(null)
case "groupKey":
if (target instanceof PromiseObj) return coerceToString(target)
if (isOpaque(target)) {
throw invalidData(`Object.groupBy callback must return a data value, received ${describeValue(target)}.`)
}
return target
case "group": {
if (target instanceof MapObj) {
const group = target.map.get(key)
if (group === undefined) target.map.set(key, new Arr(builtins.Array, [value]))
else (group as Arr).items.push(value)
return undefined
}
const group = getOwn(target as Obj, key as string)
if (group === undefined) define(target as Obj, key as string, new Arr(builtins.Array, [value]))
else (group as Arr).items.push(value)
return undefined
}
}
throw new Error(`Unknown table operation ${op}.`)
}
+112
View File
@@ -24,4 +24,116 @@ function Array$prototype$toLocaleString() {
}
return result
}
// new Map, Set, WeakMap, and WeakSet from an iterable.
function fillCollection(target, items, kind) {
const pairs = kind === "Map" || kind === "WeakMap"
for (const item of items) {
if (!pairs) $Table("add", target, item)
else if (!$IsObject(item)) $Throw("TypeError", "new " + kind + "(...) expects [key, value] pairs as entry objects.")
else $Table("add", target, item[0], item[1])
}
return target
}
function Map$groupBy(items, callback) {
return groupBy(items, callback, "Map")
}
function Object$groupBy(items, callback) {
return groupBy(items, callback, "Object")
}
// Object.groupBy's groups have a null prototype, so group names never collide with inherited methods.
function groupBy(items, callback, kind) {
if (items === null || items === undefined) $Throw("TypeError", kind + ".groupBy expects an iterable collection.")
$Callback(callback, kind + ".groupBy")
const groups = $Table("groups", kind)
let index = 0
let key
for (const item of items) {
key = callback(item, index++)
if (kind === "Object") key = String($Table("groupKey", key))
$Table("group", groups, key, item)
}
return groups
}
// getOrInsertComputed when the key is missing: the callback's result wins over anything it inserted itself.
function insertComputed(target, key, callback) {
const value = callback(key)
$Table("add", target, key, value)
return value
}
function Map$prototype$forEach(callback, thisArg = undefined) {
collectionForEach($Receiver(this, "Map", "Map.prototype.forEach"), callback, thisArg, "Map.forEach")
}
function Set$prototype$forEach(callback, thisArg = undefined) {
collectionForEach($Receiver(this, "Set", "Set.prototype.forEach"), callback, thisArg, "Set.forEach")
}
// Visits entries live, as in JS.
function collectionForEach(target, callback, thisArg, label) {
$Callback(callback, label)
for (const entry of $Table("entries", target)) $Call(callback, thisArg, entry[1], entry[0], target)
}
function Set$prototype$union(other) {
return setMethod($Receiver(this, "Set", "Set.prototype.union"), other, "union")
}
function Set$prototype$intersection(other) {
return setMethod($Receiver(this, "Set", "Set.prototype.intersection"), other, "intersection")
}
function Set$prototype$difference(other) {
return setMethod($Receiver(this, "Set", "Set.prototype.difference"), other, "difference")
}
function Set$prototype$symmetricDifference(other) {
return setMethod($Receiver(this, "Set", "Set.prototype.symmetricDifference"), other, "symmetricDifference")
}
function Set$prototype$isSubsetOf(other) {
return setMethod($Receiver(this, "Set", "Set.prototype.isSubsetOf"), other, "isSubsetOf")
}
function Set$prototype$isSupersetOf(other) {
return setMethod($Receiver(this, "Set", "Set.prototype.isSupersetOf"), other, "isSupersetOf")
}
function Set$prototype$isDisjointFrom(other) {
return setMethod($Receiver(this, "Set", "Set.prototype.isDisjointFrom"), other, "isDisjointFrom")
}
// A Set method over the other set's size, has, and keys (GetSetRecord): it either asks the other set whether it has
// each of this set's values, or takes all of the other set's values, whichever is smaller.
function setMethod(target, other, name) {
if (!$IsObject(other)) $Throw("TypeError", "Set." + name + " expects a Set-like object.")
const size = +other.size
if (size !== size) $Throw("TypeError", "Set." + name + " received a Set-like object with an invalid size.")
if (size < 0) $Throw("RangeError", "Set." + name + " received a Set-like object with a negative size.")
const has = other.has
const keys = other.keys
if (!$IsCallable(has) || !$IsCallable(keys)) $Throw("TypeError", "Set." + name + " expects callable 'has' and 'keys' methods.")
const own = $Table("size", target)
if (name === "isSubsetOf" && own > size) return false
if (name === "isSupersetOf" && own < size) return false
const result = name === "intersection" ? new Set() : name === "union" || name === "difference" || name === "symmetricDifference" ? $Table("copy", target) : undefined
const asks = name === "isSubsetOf" || ((name === "intersection" || name === "difference" || name === "isDisjointFrom") && own <= size)
let found
if (asks) {
for (const item of $Table("values", name === "difference" ? result : target)) {
found = !!$Call(has, other, item)
if (name === "intersection") {
if (found) $Table("add", result, item)
} else if (name === "difference") {
if (found) $Table("delete", result, item)
} else if (found === (name === "isDisjointFrom")) return false
}
return result === undefined ? true : result
}
const iterator = $Call(keys, other)
if (!$IsObject(iterator)) $Throw("TypeError", "Set." + name + " expects 'keys' to return an iterator object.")
for (const item of $Direct(iterator)) {
found = $Table("has", target, item)
if (name === "union") $Table("add", result, item)
else if (name === "intersection") {
if (found) $Table("add", result, item)
} else if (name === "difference") $Table("delete", result, item)
else if (name === "symmetricDifference") $Table(found ? "delete" : "add", result, item)
else if (found === (name === "isDisjointFrom")) return false
}
return result === undefined ? true : result
}
`
+28 -442
View File
@@ -1,225 +1,50 @@
import { consumer, nativeKind, STATE, SUB } from "../machine/native-kind.js"
import { Push } from "../interpreter/signals.js"
import type { Builtins } from "../interpreter/intrinsics.js"
import { locate } from "../interpreter/errors.js"
import type { AstNode } from "../interpreter/model.js"
import type { TableCursor } from "../interpreter/table.js"
import { PENDING } from "../machine/frame.js"
import { toPrimitive } from "../machine/calls.js"
import { iterateDirect, iteratorStep } from "../machine/iteration.js"
import { Drain, drain } from "../machine/natives.js"
import { constructor, fn, type Method, methods, prototypeFrom, receiver, requiresNew } from "../interpreter/native.js"
import { invalidData, IteratorSymbol, typeError } from "../interpreter/model.js"
import { Invoke } from "../interpreter/signals.js"
import { constructor, methods, prototypeFrom, receiver, requiresNew } from "../interpreter/native.js"
import { IteratorSymbol, typeError } from "../interpreter/model.js"
import {
define,
defineAccessor,
get,
getOwn,
hidden,
Arr,
type Callable,
coerceToNumber,
coerceToString,
type Cursor,
MapObj,
Position,
positionIterator,
Obj,
PromiseObj,
SetObj,
type Step,
type Value,
WeakMapObj,
WeakSetObj,
} from "../interpreter/objects.js"
import { describeValue, isOpaque } from "../interpreter/references.js"
import { isSupportedCallback, requireCallback } from "../interpreter/callback.js"
import { describeValue } from "../interpreter/references.js"
import { requireCallback } from "../interpreter/callback.js"
import type { Interpreter } from "../interpreter/interpreter.js"
/** `Map.groupBy` and `Object.groupBy`: the same iteration, keyed into a Map or a data object. */
export const groupBy = <R>(ctx: Interpreter<R>, namespace: "Map" | "Object") =>
fn(ctx.builtins, "groupBy", 2, (_, args) => {
const source = args[0]
if (source === null || source === undefined) {
throw typeError(`${namespace}.groupBy expects an iterable collection.`)
}
const callback = requireCallback(args[1], `${namespace}.groupBy`)
// Object.groupBy returns a null-prototype object, so group names never collide with inherited methods.
const result = namespace === "Map" ? new MapObj(ctx.builtins.Map) : new Obj(null)
return new Push(Drain, drain(GroupBy, source, [namespace, callback, result, undefined]))
})
// State: the namespace, the callback, the groups, and the item being grouped. A value's work: call the callback,
// then (Object.groupBy) convert its result to a property key.
const GroupBy = consumer({
name: "groupBy",
notIterable: (_task, frame) => {
throw typeError(`${frame.slots[STATE] as string}.groupBy expects an iterable collection.`)
},
each: (task, frame, value, index) => {
const slots = frame.slots
slots[STATE + 3] = value
const key = task.call(slots[STATE + 1] as Callable, undefined, [value, index], frame.node)
if (key === PENDING) return PENDING
return GroupBy.resume!(task, frame, key, index)
},
resume: (task, frame, input) => {
const slots = frame.slots
const item = slots[STATE + 3] as Value
const builtins = task.ctx.builtins
const result = slots[STATE + 2] as Obj
if (result instanceof MapObj) {
const group = result.map.get(input as Value)
if (group === undefined) result.map.set(input as Value, new Arr(builtins.Array, [item]))
else (group as Arr).items.push(item)
return undefined
}
let key = input as Value
if (slots[SUB] === 0) {
if (key instanceof PromiseObj) key = coerceToString(key)
else if (isOpaque(key)) {
throw invalidData(`Object.groupBy callback must return a data value, received ${describeValue(key)}.`)
}
slots[SUB] = 1
const converted = toPrimitive(task, key, "string", frame.node as AstNode)
if (converted === PENDING) return PENDING
key = converted
}
const name = coerceToString(key)
const group = getOwn(result, name)
if (group === undefined) define(result, name, new Arr(builtins.Array, [item]))
else (group as Arr).items.push(item)
return undefined
},
done: (_task, frame) => frame.slots[STATE + 2] as Obj,
})
const constructMap = (init: Value, proto: Obj) => {
const target = new MapObj(proto)
if (init === undefined || init === null) return target
return new Push(Drain, drain(MapEntries, init, [target]))
}
// State: the collection being filled.
const MapEntries = consumer({
name: "new Map",
notIterable: (_task, _frame, init) => {
throw typeError(`new Map(...) expects an iterable of [key, value] pairs, received ${describeValue(init)}.`)
},
each: (_task, frame, entry) => {
if (!(entry instanceof Obj)) {
throw typeError("new Map(...) expects [key, value] pairs as entry objects.")
}
;(frame.slots[STATE] as MapObj).map.set(getOwn(entry, 0), getOwn(entry, 1))
},
done: (_task, frame) => frame.slots[STATE] as Value,
})
const constructSet = (init: Value, proto: Obj) => {
const target = new SetObj(proto)
if (init === undefined || init === null) return target
return new Push(Drain, drain(SetValues, init, [target]))
}
const SetValues = consumer({
name: "new Set",
notIterable: (_task, _frame, init) => {
throw typeError(`new Set(...) expects a synchronous iterable, received ${describeValue(init)}.`)
},
take: (frame, value) => {
;(frame.slots[STATE] as SetObj).set.add(value)
},
done: (_task, frame) => frame.slots[STATE] as Value,
})
const WeakMapEntries = consumer({
name: "new WeakMap",
notIterable: (_task, _frame, init) => {
throw typeError(`new WeakMap(...) expects an iterable of [key, value] pairs, received ${describeValue(init)}.`)
},
each: (_task, frame, entry) => {
if (!(entry instanceof Obj)) {
throw typeError("new WeakMap(...) expects [key, value] pairs as entry objects.")
}
;(frame.slots[STATE] as WeakMapObj).map.set(weakKey(getOwn(entry, 0), "as weak map key"), getOwn(entry, 1))
},
done: (_task, frame) => frame.slots[STATE] as Value,
})
const WeakSetValues = consumer({
name: "new WeakSet",
notIterable: (_task, _frame, init) => {
throw typeError(`new WeakSet(...) expects a synchronous iterable, received ${describeValue(init)}.`)
},
each: (_task, frame, value) => {
;(frame.slots[STATE] as WeakSetObj).set.add(weakKey(value, "in weak set"))
},
done: (_task, frame) => frame.slots[STATE] as Value,
})
// A collection from an iterable is filled by the prelude, which iterates it.
const filled = <R>(ctx: Interpreter<R>, target: Obj, init: Value, kind: string) =>
init === undefined || init === null
? target
: new Invoke(ctx.prelude.fn("fillCollection"), undefined, [target, init, kind], undefined)
// The callback sees the stored key (-0 is +0) and its result wins over anything it inserted itself.
const getOrInsertComputed = (table: MapObj["map"] | WeakMapObj["map"], key: Value, callback: Callable) => {
if (table.has(key as never)) return table.get(key as never)
return new Push(GetOrInsertComputedCall, [table, key, callback])
const getOrInsertComputed = <R>(
ctx: Interpreter<R>,
owner: MapObj | WeakMapObj,
key: Value,
callback: Callable,
): Value | Invoke => {
const table = owner.map as Map<Value, Value>
if (table.has(key)) return table.get(key)
return new Invoke(ctx.prelude.fn("insertComputed"), undefined, [owner, key === 0 ? 0 : key, callback], undefined)
}
const GetOrInsertComputedCall = nativeKind("getOrInsertComputed callback", (task, frame, input) => {
const slots = frame.slots
let value = input as Value
if (frame.phase === 0) {
const key = slots[1] as Value
const called = task.call(slots[2] as Callable, undefined, [key === 0 ? 0 : key], frame.node)
if (called === PENDING) {
frame.phase = 1
return PENDING
}
value = called
}
;(slots[0] as MapObj["map"]).set(slots[1] as Value, value)
return value
})
// Slots: the collection, its table cursor, the callback, its `this`. `forEach` visits entries live, as in JS.
const CollectionForEach = nativeKind(
"collection forEach",
(task, frame) => {
const slots = frame.slots
const target = slots[0] as MapObj | SetObj
const table = target instanceof MapObj ? target.map : target.set
const cursor = slots[1] as TableCursor
for (let at = table.advance(cursor); at >= 0; at = table.advance(cursor)) {
const key = table.keys[at] as Value
const value = target instanceof MapObj ? table.values[at] : key
if (task.call(slots[2] as Callable, slots[3] as Value, [value, key, target], frame.node) === PENDING)
return PENDING
}
return undefined
},
{
unwind: (_task, frame, error) => {
;(frame.slots[1] as TableCursor).done = true
throw locate(error, frame.node)
},
},
)
const forEach = (target: MapObj | SetObj, callback: Callable, thisArg: Value) =>
new Push(CollectionForEach, [
target,
(target instanceof MapObj ? target.map : target.set).cursor(),
callback,
thisArg,
])
export const mapGlobal = <R>(ctx: Interpreter<R>) => {
const builtins = ctx.builtins
const proto = builtins.Map
const map = constructor(builtins, proto, {
name: "Map",
call: requiresNew("Map"),
construct: (args, newTarget) => constructMap(args[0], prototypeFrom(newTarget, proto)),
construct: (args, newTarget) => filled(ctx, new MapObj(prototypeFrom(newTarget, proto)), args[0], "Map"),
})
define(map, "groupBy", groupBy(ctx, "Map"), hidden)
const self = (thisValue: Value, name: string) => receiver(MapObj, thisValue, `Map.prototype.${name}`)
defineAccessor(proto, "size", (thisValue) => receiver(MapObj, thisValue, "Map.prototype.size").map.size)
methods(builtins, proto, [
@@ -247,9 +72,8 @@ export const mapGlobal = <R>(ctx: Interpreter<R>) => {
"getOrInsertComputed",
2,
(thisValue, args) => {
const target = self(thisValue, "getOrInsertComputed").map
const callback = requireCallback(args[1], "Map.getOrInsertComputed")
return getOrInsertComputed(target, args[0], callback)
const target = self(thisValue, "getOrInsertComputed")
return getOrInsertComputed(ctx, target, args[0], requireCallback(args[1], "Map.getOrInsertComputed"))
},
],
["delete", 1, (thisValue, args) => self(thisValue, "delete").map.delete(args[0])],
@@ -264,235 +88,20 @@ export const mapGlobal = <R>(ctx: Interpreter<R>) => {
["keys", 0, (thisValue) => positionIterator(builtins, self(thisValue, "keys"), "keys")],
["values", 0, (thisValue) => positionIterator(builtins, self(thisValue, "values"), "values")],
["entries", 0, (thisValue) => positionIterator(builtins, self(thisValue, "entries"), "entries")],
[
"forEach",
1,
(thisValue, args) => {
const target = self(thisValue, "forEach")
return forEach(target, requireCallback(args[0], "Map.forEach"), args[1])
},
],
])
define(proto, IteratorSymbol, get(proto, "entries"), hidden)
return map
}
type SetOp =
| "union"
| "intersection"
| "difference"
| "symmetricDifference"
| "isSubsetOf"
| "isSupersetOf"
| "isDisjointFrom"
/**
* How a Set method uses the other set: asking it whether it has each of this set's values (`has`), or taking all of
* its values (`keys`). The result set, when the method makes one, starts as a copy or empty.
*/
const plan = (name: SetOp, size: number, otherSize: number) => {
switch (name) {
case "union":
case "symmetricDifference":
return "keys"
case "intersection":
case "difference":
case "isDisjointFrom":
return size <= otherSize ? "has" : "keys"
case "isSubsetOf":
return size > otherSize ? false : "has"
case "isSupersetOf":
return size < otherSize ? false : "keys"
}
}
const startResult = (builtins: Builtins, name: SetOp, target: SetObj) => {
if (name === "intersection") return new SetObj(builtins.Set)
if (name !== "union" && name !== "difference" && name !== "symmetricDifference") return undefined
const result = new SetObj(builtins.Set)
for (const item of target.set.liveKeys()) result.set.add(item)
return result
}
// One answer from the other set's `has` for `item`: the method's result once it is decided, or undefined to go on.
const hasAnswer = (name: SetOp, result: SetObj | undefined, item: Value, has: boolean): Value | undefined => {
if (name === "intersection") {
if (has) result!.set.add(item)
return undefined
}
if (name === "difference") {
if (has) result!.set.delete(item)
return undefined
}
if (name === "isSubsetOf") return has ? undefined : false
return has ? false : undefined
}
// The method's result from all the other set's values.
const fromKeys = (name: SetOp, target: SetObj, result: SetObj | undefined, items: Iterable<Value>): Value => {
for (const item of items) {
if (name === "union") result!.set.add(item)
else if (name === "intersection") {
if (target.set.has(item)) result!.set.add(item)
} else if (name === "difference") result!.set.delete(item)
else if (name === "symmetricDifference") {
if (target.set.has(item)) result!.set.delete(item)
else result!.set.add(item)
} else if (name === "isSupersetOf") {
if (!target.set.has(item)) return false
} else if (target.set.has(item)) return false
}
return finish(name, result)
}
const finish = (name: SetOp, result: SetObj | undefined): Value =>
result ?? (name === "isSubsetOf" || name === "isSupersetOf" || name === "isDisjointFrom")
// A Set or Map as the other set answers without running program code.
const withTable = (builtins: Builtins, name: SetOp, target: SetObj, other: SetObj | MapObj): Value => {
const table = other instanceof SetObj ? other.set : other.map
const mode = plan(name, target.set.size, table.size)
if (mode === false) return false
const result = startResult(builtins, name, target)
if (mode === "keys") return fromKeys(name, target, result, [...table.liveKeys()])
for (const item of (result !== undefined && name === "difference" ? result : target).set.liveKeys()) {
const decided = hasAnswer(name, result, item, table.has(item))
if (decided !== undefined) return decided
}
return finish(name, result)
}
const setOperation = (builtins: Builtins, target: SetObj, name: SetOp, source: Value): Value | Push => {
if (source instanceof SetObj || source instanceof MapObj) return withTable(builtins, name, target, source)
if (!(source instanceof Obj)) {
throw typeError(`Set.${name} expects a Set-like object.`)
}
return new Push(SetOperation, [name, target, source])
}
// The phases of a Set method over a Set-like object: convert its size, ask `has` for each value, or call `keys` and
// step the iterator it returns.
const SIZED = 1
const HAS = 2
const KEYS = 3
const STEPPED = 4
// Slots: the method, this set, the other set, its size, its `has` and `keys`, the result set, the cursor over the set
// whose values `has` is asked about (or the other set's iterator), the value asked about, and the values taken.
const SetOperation = nativeKind(
"Set method",
(task, frame, input) => {
const slots = frame.slots
const name = slots[0] as SetOp
const target = slots[1] as SetObj
const source = slots[2] as Obj
let phase = frame.phase
if (phase === 0) {
const size = toPrimitive(task, get(source, "size"), "number", frame.node as AstNode)
if (size === PENDING) {
frame.phase = SIZED
return PENDING
}
input = size
phase = SIZED
}
if (phase === SIZED) {
const size = coerceToNumber(input as Value)
if (Number.isNaN(size)) {
throw typeError(`Set.${name} received a Set-like object with an invalid size.`)
}
const has = get(source, "has")
const keys = get(source, "keys")
if (!isSupportedCallback(has) || !isSupportedCallback(keys)) {
throw typeError(`Set.${name} expects callable 'has' and 'keys' methods.`)
}
slots[3] = Math.max(Math.trunc(size), 0)
slots[4] = has
slots[5] = keys
const mode = plan(name, target.set.size, slots[3] as number)
if (mode === false) return false
const result = startResult(task.ctx.builtins, name, target)
slots[6] = result
if (mode === "has") {
slots[7] = (result !== undefined && name === "difference" ? result : target).set.cursor()
phase = HAS
} else {
frame.phase = KEYS
const iterator = task.call(keys, source, [], frame.node)
if (iterator === PENDING) return PENDING
input = iterator
phase = KEYS
}
}
const result = slots[6] as SetObj | undefined
if (phase === HAS) {
const asked = (result !== undefined && name === "difference" ? result : target).set
const cursor = slots[7] as TableCursor
if (frame.phase === HAS) {
const decided = hasAnswer(name, result, slots[8] as Value, Boolean(input))
if (decided !== undefined) {
cursor.done = true
return decided
}
}
frame.phase = HAS
for (let at = asked.advance(cursor); at >= 0; at = asked.advance(cursor)) {
const item = asked.keys[at] as Value
slots[8] = item
const has = task.call(slots[4] as Value, source, [item], frame.node)
if (has === PENDING) return PENDING
const decided = hasAnswer(name, result, item, Boolean(has))
if (decided !== undefined) {
cursor.done = true
return decided
}
}
return finish(name, result)
}
if (phase === KEYS) {
const iterator = input as Value
slots[7] =
iterator instanceof Arr
? new Position(iterator, "values", task.ctx.builtins.Array)
: iterateDirect(iterator, undefined)
slots[9] = []
}
const items = slots[9] as Array<Value>
if (frame.phase === STEPPED) {
const step = input as Step
if (step.done) return fromKeys(name, target, result, items)
items.push(step.value)
}
frame.phase = STEPPED
while (true) {
const step = iteratorStep(task, slots[7] as Cursor, frame.node as AstNode)
if (step === PENDING) return PENDING
if (step.done) return fromKeys(name, target, result, items)
items.push(step.value)
}
},
{
unwind: (_task, frame, error) => {
if (frame.phase === HAS) (frame.slots[7] as TableCursor).done = true
throw locate(error, frame.node)
},
},
)
export const setGlobal = <R>(ctx: Interpreter<R>) => {
const builtins = ctx.builtins
const proto = builtins.Set
const set = constructor(builtins, proto, {
name: "Set",
call: requiresNew("Set"),
construct: (args, newTarget) => constructSet(args[0], prototypeFrom(newTarget, proto)),
construct: (args, newTarget) => filled(ctx, new SetObj(prototypeFrom(newTarget, proto)), args[0], "Set"),
})
const self = (thisValue: Value, name: string) => receiver(SetObj, thisValue, `Set.prototype.${name}`)
const operation = (name: SetOp): Method => [
name,
1,
(thisValue, args) => setOperation(builtins, self(thisValue, name), name, args[0]),
]
defineAccessor(proto, "size", (thisValue) => receiver(SetObj, thisValue, "Set.prototype.size").set.size)
methods(builtins, proto, [
["has", 1, (thisValue, args) => self(thisValue, "has").set.has(args[0])],
@@ -517,28 +126,13 @@ export const setGlobal = <R>(ctx: Interpreter<R>) => {
["keys", 0, (thisValue) => positionIterator(builtins, self(thisValue, "keys"), "keys")],
["values", 0, (thisValue) => positionIterator(builtins, self(thisValue, "values"), "values")],
["entries", 0, (thisValue) => positionIterator(builtins, self(thisValue, "entries"), "entries")],
[
"forEach",
1,
(thisValue, args) => {
const target = self(thisValue, "forEach")
return forEach(target, requireCallback(args[0], "Set.forEach"), args[1])
},
],
operation("union"),
operation("intersection"),
operation("difference"),
operation("symmetricDifference"),
operation("isSubsetOf"),
operation("isSupersetOf"),
operation("isDisjointFrom"),
])
define(proto, IteratorSymbol, get(proto, "values"), hidden)
return set
}
// CanBeHeldWeakly: only program objects; tool references are rebuilt on every access, so they could never be found again.
const weakKey = (value: Value, label: string) => {
export const weakKey = (value: Value, label: string) => {
if (value instanceof Obj) return value
throw typeError(`Invalid value used ${label}: ${describeValue(value)} cannot be held weakly.`)
}
@@ -549,11 +143,7 @@ export const weakMapGlobal = <R>(ctx: Interpreter<R>) => {
const weakMap = constructor(builtins, proto, {
name: "WeakMap",
call: requiresNew("WeakMap"),
construct: (args, newTarget) => {
const target = new WeakMapObj(prototypeFrom(newTarget, proto))
if (args[0] === undefined || args[0] === null) return target
return new Push(Drain, drain(WeakMapEntries, args[0], [target]))
},
construct: (args, newTarget) => filled(ctx, new WeakMapObj(prototypeFrom(newTarget, proto)), args[0], "WeakMap"),
})
const self = (thisValue: Value, name: string) => receiver(WeakMapObj, thisValue, `WeakMap.prototype.${name}`).map
// Lookups pass any key through: the host collection answers false for a non-object, as the spec requires.
@@ -591,9 +181,9 @@ export const weakMapGlobal = <R>(ctx: Interpreter<R>) => {
"getOrInsertComputed",
2,
(thisValue, args) => {
const target = self(thisValue, "getOrInsertComputed")
const target = receiver(WeakMapObj, thisValue, "WeakMap.prototype.getOrInsertComputed")
const k = key(args[0])
return getOrInsertComputed(target, k, requireCallback(args[1], "WeakMap.getOrInsertComputed"))
return getOrInsertComputed(ctx, target, k, requireCallback(args[1], "WeakMap.getOrInsertComputed"))
},
],
])
@@ -606,11 +196,7 @@ export const weakSetGlobal = <R>(ctx: Interpreter<R>) => {
const weakSet = constructor(builtins, proto, {
name: "WeakSet",
call: requiresNew("WeakSet"),
construct: (args, newTarget) => {
const target = new WeakSetObj(prototypeFrom(newTarget, proto))
if (args[0] === undefined || args[0] === null) return target
return new Push(Drain, drain(WeakSetValues, args[0], [target]))
},
construct: (args, newTarget) => filled(ctx, new WeakSetObj(prototypeFrom(newTarget, proto)), args[0], "WeakSet"),
})
const self = (thisValue: Value, name: string) => receiver(WeakSetObj, thisValue, `WeakSet.prototype.${name}`).set
methods(builtins, proto, [
-2
View File
@@ -27,7 +27,6 @@ import { containsOpaqueReference, describeValue, rejectCircularInsertion } from
import { invokeMethod } from "../interpreter/callback.js"
import type { Interpreter } from "../interpreter/interpreter.js"
import { ToolReference } from "../tool-runtime.js"
import { groupBy } from "./collections.js"
// ToObject for enumeration.
export const enumerableSource = <R>(ctx: Interpreter<R>, label: string, value: Value, node?: AstNode): Obj => {
@@ -208,7 +207,6 @@ export const objectGlobal = <R>(ctx: Interpreter<R>) => {
},
],
])
define(object, "groupBy", groupBy(ctx, "Object"), hidden)
methods(builtins, builtins.Object, [
[
"hasOwnProperty",
-1
View File
@@ -417,7 +417,6 @@ describe("built-ins as data", () => {
"Array sort",
"PromiseReactionJob",
"JSON.parse reviver",
"Set method",
"search",
]) {
expect(seenKinds).toContain(name)