object.ts 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { type AstNode, InterpreterRuntimeError } from "../interpreter/model.js"
  2. import { containsOpaqueReference } from "../interpreter/references.js"
  3. import { isBlockedMember } from "../tool-runtime.js"
  4. import { isCodeModeValue, CodeModeMap, CodeModePromise, CodeModeSet, CodeModeURLSearchParams } from "../values.js"
  5. import { boundedData, coerceToString } from "./value.js"
  6. export const objectMethodsPreservingIdentity = new Set(["assign", "values", "entries", "fromEntries"])
  7. export const objectStatics = new Set(["keys", "values", "entries", "hasOwn", "is", "assign", "fromEntries", "groupBy"])
  8. export const invokeObjectMethod = (name: string, args: Array<unknown>, node: AstNode): unknown => {
  9. const requireObject = (): Record<string, unknown> => {
  10. const input = args[0]
  11. if (Array.isArray(input)) return input as unknown as Record<string, unknown>
  12. if (isCodeModeValue(input)) return {}
  13. if (input instanceof CodeModePromise) {
  14. throw new InterpreterRuntimeError(
  15. `Object.${name} received an un-awaited Promise; await it before inspecting the result.`,
  16. node,
  17. "InvalidDataValue",
  18. )
  19. }
  20. if (input === null || typeof input !== "object") {
  21. throw new InterpreterRuntimeError(`Object.${name} expects a data object or array.`, node, "InvalidDataValue")
  22. }
  23. const prototype = Object.getPrototypeOf(input)
  24. if (prototype !== null && prototype !== Object.prototype) {
  25. throw new InterpreterRuntimeError(`Object.${name} expects a data object or array.`, node, "InvalidDataValue")
  26. }
  27. return input as Record<string, unknown>
  28. }
  29. const guardedSet = (out: Record<string, unknown>, key: string, item: unknown): void => {
  30. if (isBlockedMember(key)) throw new InterpreterRuntimeError(`Property '${key}' is not available.`, node)
  31. out[key] = item
  32. }
  33. const addEntry = (out: Record<string, unknown>, key: unknown, item: unknown): void => {
  34. boundedData(key, "Object.fromEntries key")
  35. boundedData(item, "Object.fromEntries value")
  36. guardedSet(out, coerceToString(key), item)
  37. }
  38. switch (name) {
  39. case "keys":
  40. return Object.keys(requireObject())
  41. case "values":
  42. return Object.values(requireObject())
  43. case "entries":
  44. return Object.entries(requireObject()).map(([key, item]) => [key, item])
  45. case "hasOwn":
  46. return Object.hasOwn(requireObject(), String(args[1]))
  47. case "is":
  48. if (containsOpaqueReference(args[0]) || containsOpaqueReference(args[1])) {
  49. throw new InterpreterRuntimeError("Object.is requires data values.", node, "InvalidDataValue")
  50. }
  51. return Object.is(args[0], args[1])
  52. case "assign": {
  53. const target = args[0]
  54. if (target === null || typeof target !== "object" || Array.isArray(target) || isCodeModeValue(target)) {
  55. throw new InterpreterRuntimeError("Object.assign expects a data object target.", node)
  56. }
  57. const out = target as Record<string, unknown>
  58. for (const source of args.slice(1)) {
  59. if (source === null || source === undefined || isCodeModeValue(source)) continue
  60. if (typeof source !== "object" || Array.isArray(source)) {
  61. throw new InterpreterRuntimeError("Object.assign expects data objects.", node)
  62. }
  63. for (const [key, item] of Object.entries(source)) guardedSet(out, key, item)
  64. }
  65. return out
  66. }
  67. case "fromEntries": {
  68. if (args[0] instanceof CodeModeMap) {
  69. const out: Record<string, unknown> = Object.create(null)
  70. for (const [key, item] of args[0].map.entries()) addEntry(out, key, item)
  71. return out
  72. }
  73. if (args[0] instanceof CodeModeURLSearchParams) {
  74. const out: Record<string, unknown> = Object.create(null)
  75. for (const [key, value] of args[0].params.entries()) guardedSet(out, key, value)
  76. return out
  77. }
  78. const pairs = args[0] instanceof CodeModeSet ? Array.from(args[0].set.values()) : args[0]
  79. if (!Array.isArray(pairs)) {
  80. boundedData(args[0], "Object.fromEntries input")
  81. throw new InterpreterRuntimeError("Object.fromEntries expects an array of [key, value] pairs.", node)
  82. }
  83. const out: Record<string, unknown> = Object.create(null)
  84. for (const pair of pairs) {
  85. const validated = boundedData(pair, "Object.fromEntries entry")
  86. if (validated === null || typeof validated !== "object" || isCodeModeValue(validated))
  87. throw new InterpreterRuntimeError("Object.fromEntries expects [key, value] entry objects.", node)
  88. const entry = pair as Record<string, unknown>
  89. addEntry(out, entry[0], entry[1])
  90. }
  91. return out
  92. }
  93. }
  94. throw new InterpreterRuntimeError(`Object.${name} is not available.`, node)
  95. }