Repository navigation
Expand file tree
/
Copy pathweb.ts
More file actions
118 lines (115 loc) · 4.42 KB
/
Copy pathweb.ts
File metadata and controls
118 lines (115 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { fn, methods } from "../runtime/native.js"
import { typeError } from "../runtime/model.js"
import {
define,
entries,
get,
getOwn,
hasOwn,
hidden,
isRuntimeReference,
Arr,
Bytes,
DateObj,
ErrorObj,
MapObj,
Obj,
RegExpObj,
SetObj,
WeakMapObj,
WeakSetObj,
coerceToString,
type Value,
} from "../runtime/objects.js"
import { createErrorValue, isErrorType } from "../runtime/intrinsics.js"
import { describeValue } from "../runtime/references.js"
import type { Interpreter } from "../runtime/interpreter.js"
import { ToolReference } from "../tool-runtime.js"
// WebIDL DOMString conversion: a missing argument is a TypeError, anything else stringifies. Invalid input is a
// TypeError as well; browsers throw a DOMException named InvalidCharacterError, which CodeMode does not have.
export const base64Global = <R>(ctx: Interpreter<R>, name: "atob" | "btoa") =>
fn(ctx.builtins, name, 1, (_, args) => {
if (args.length === 0) throw typeError(`${name} requires 1 argument (a string)`)
const input = coerceToString(args[0])
try {
return name === "atob" ? atob(input) : btoa(input)
} catch {
throw typeError("The string contains invalid characters.")
}
})
// HTML structured clone over CodeMode's data kinds. Shared references survive through the memo; prototypes and
// frozen state do not, as in JS. Without a DOMException, DataCloneError is a TypeError carrying that prefix.
export const structuredCloneGlobal = <R>(ctx: Interpreter<R>) =>
fn(ctx.builtins, "structuredClone", 1, (_, args) => {
if (args.length === 0) throw typeError("structuredClone requires 1 argument")
const builtins = ctx.builtins
const memo = new Map<Obj, Obj>()
const clone = (value: Value): Value => {
if (typeof value === "symbol" || value instanceof ToolReference) {
throw typeError(`DataCloneError: ${describeValue(value)} could not be cloned.`)
}
if (!(value instanceof Obj)) return value
const seen = memo.get(value)
if (seen !== undefined) return seen
const remember = <T extends Obj>(copy: T): T => {
memo.set(value, copy)
return copy
}
if (value instanceof MapObj) {
const copy = remember(new MapObj(builtins.Map))
for (const [key, item] of value.map) copy.map.set(clone(key), clone(item))
return copy
}
if (value instanceof SetObj) {
const copy = remember(new SetObj(builtins.Set))
for (const item of value.set.liveKeys()) copy.set.add(clone(item))
return copy
}
if (value instanceof DateObj) return remember(new DateObj(builtins.Date, value.time))
if (value instanceof RegExpObj) {
return remember(new RegExpObj(builtins.RegExp, value.source, value.flags))
}
if (value instanceof Bytes) return remember(new Bytes(builtins.Uint8Array, new Uint8Array(value.bytes)))
if (value instanceof ErrorObj) {
const name = get(value, "name")
const message = get(value, "message")
const copy = remember(
createErrorValue(
builtins[typeof name === "string" && isErrorType(name) ? name : "Error"],
message === undefined ? undefined : coerceToString(message),
),
)
const stack = getOwn(value, "stack")
if (typeof stack === "string") define(copy, "stack", stack, hidden)
if (hasOwn(value, "cause")) define(copy, "cause", clone(getOwn(value, "cause")), hidden)
return copy
}
if (isRuntimeReference(value) || value instanceof WeakMapObj || value instanceof WeakSetObj) {
throw typeError(`DataCloneError: ${describeValue(value)} could not be cloned.`)
}
const copy = remember(
value instanceof Arr ? new Arr(builtins.Array, new Array(value.items.length)) : new Obj(builtins.Object),
)
for (const [key, item] of entries(value)) define(copy, key, clone(item))
return copy
}
return clone(args[0])
})
export const cryptoGlobal = <R>(ctx: Interpreter<R>) => {
const object = new Obj(ctx.builtins.Object)
methods(ctx.builtins, object, [
["randomUUID", 0, () => crypto.randomUUID()],
[
"getRandomValues",
1,
(_, args) => {
if (!(args[0] instanceof Bytes)) {
throw typeError(`crypto.getRandomValues expects a Uint8Array, received ${describeValue(args[0])}.`)
}
crypto.getRandomValues(args[0].bytes)
return args[0]
},
],
])
return object
}