date.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. export const dateMethods = new Set([
  2. "getTime",
  3. "valueOf",
  4. "toISOString",
  5. "toJSON",
  6. "toString",
  7. "getFullYear",
  8. "getMonth",
  9. "getDate",
  10. "getDay",
  11. "getHours",
  12. "getMinutes",
  13. "getSeconds",
  14. "getMilliseconds",
  15. "getUTCFullYear",
  16. "getUTCMonth",
  17. "getUTCDate",
  18. "getUTCDay",
  19. "getUTCHours",
  20. "getUTCMinutes",
  21. "getUTCSeconds",
  22. "getUTCMilliseconds",
  23. "getTimezoneOffset",
  24. ])
  25. export const dateStatics = new Set(["now", "parse", "UTC"])
  26. export const invokeDateStatic = (name: string, args: Array<unknown>, node: AstNode): number => {
  27. switch (name) {
  28. case "now":
  29. return Date.now()
  30. case "parse":
  31. return Date.parse(coerceToString(args[0]))
  32. case "UTC":
  33. return Date.UTC(...(args.map((arg) => coerceToNumber(arg)) as Parameters<typeof Date.UTC>))
  34. default:
  35. throw new InterpreterRuntimeError(`Date.${name} is not available in CodeMode.`, node)
  36. }
  37. }
  38. export const invokeDateMethod = (value: SandboxDate, name: string, node: AstNode): unknown => {
  39. const hosted = new Date(value.time)
  40. switch (name) {
  41. case "getTime":
  42. case "valueOf":
  43. return value.time
  44. case "toISOString":
  45. if (!Number.isFinite(value.time)) throw new InterpreterRuntimeError("Invalid time value.", node)
  46. return hosted.toISOString()
  47. case "toJSON":
  48. return Number.isFinite(value.time) ? hosted.toISOString() : null
  49. case "toString":
  50. return coerceToString(value)
  51. case "getFullYear":
  52. return hosted.getFullYear()
  53. case "getMonth":
  54. return hosted.getMonth()
  55. case "getDate":
  56. return hosted.getDate()
  57. case "getDay":
  58. return hosted.getDay()
  59. case "getHours":
  60. return hosted.getHours()
  61. case "getMinutes":
  62. return hosted.getMinutes()
  63. case "getSeconds":
  64. return hosted.getSeconds()
  65. case "getMilliseconds":
  66. return hosted.getMilliseconds()
  67. case "getUTCFullYear":
  68. return hosted.getUTCFullYear()
  69. case "getUTCMonth":
  70. return hosted.getUTCMonth()
  71. case "getUTCDate":
  72. return hosted.getUTCDate()
  73. case "getUTCDay":
  74. return hosted.getUTCDay()
  75. case "getUTCHours":
  76. return hosted.getUTCHours()
  77. case "getUTCMinutes":
  78. return hosted.getUTCMinutes()
  79. case "getUTCSeconds":
  80. return hosted.getUTCSeconds()
  81. case "getUTCMilliseconds":
  82. return hosted.getUTCMilliseconds()
  83. case "getTimezoneOffset":
  84. return hosted.getTimezoneOffset()
  85. default:
  86. throw new InterpreterRuntimeError(`Date method '${name}' is not available in CodeMode.`, node)
  87. }
  88. }
  89. import { type AstNode, InterpreterRuntimeError } from "../interpreter/model.js"
  90. import { SandboxDate } from "../values.js"
  91. import { coerceToNumber, coerceToString } from "./value.js"