Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/codemode/interpreter-support.md
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,22 @@ with a hint to encode as text first (`TextDecoder`, `toBase64`, `toHex`).
- [x] `sort` in place, numeric ascending by default (`[10, 9, 1]` sorts to `[1, 9, 10]`) or by comparator.
- [ ] `ArrayBuffer`, `DataView`, and other typed arrays.

## Buffer

Node's `Buffer`, where the host has one (Node and Bun; not browsers). It is a `Uint8Array`, so every `Uint8Array`
method works on it, and its text conversions default to UTF-8.

- [x] `Buffer.from(string, encoding)`, `Buffer.from(bytes | array | array-like | { type: "Buffer", data })`,
`alloc`, `allocUnsafe`, `concat`, `byteLength`, `compare`, `isBuffer`, and `isEncoding`, with the encodings
utf8, hex, base64, base64url, latin1/binary, ascii, and utf16le/ucs2. An unknown encoding is a `TypeError` with
code `ERR_UNKNOWN_ENCODING`.
- [x] `toString(encoding, start, end)` (so string coercion and templates give UTF-8 text), `toJSON` as
`{ type: "Buffer", data }`, `slice` and `subarray` as Buffer views on the same bytes, `equals`, `compare`,
`indexOf`, `lastIndexOf`, `includes`, `write`, `fill`, `copy`, and the `read`/`write` integer and float methods.
`console.log` prints `<Buffer ..>`.
- [x] A host Buffer crosses into the program as a Buffer, and a program Buffer crosses out as one; snapshots keep it.
- [ ] The 64-bit `readBigInt64*`/`writeBigInt64*` methods, since programs have no BigInt.

## Web platform helpers

- [x] `atob` and `btoa` with forgiving-base64 decoding and WebIDL string conversion; invalid input throws a
Expand Down
3 changes: 3 additions & 0 deletions packages/codemode/src/runtime/extensions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Effect } from "effect"
import { type Extension, isAsync, StreamingResponse } from "../extension.js"
import { BufferObj } from "../stdlib/buffer.js"
import { hooked } from "../tool-runtime.js"
import { errorMessage, errorType } from "./errors.js"
import type { Interpreter } from "./interpreter.js"
Expand Down Expand Up @@ -164,6 +165,8 @@ export const extensionGlobals = <R>(
return result
}
if (value instanceof RegExp) return new RegExpObj(builtins.RegExp, value.source, value.flags)
if (typeof Buffer !== "undefined" && Buffer.isBuffer(value))
return new BufferObj(builtins.Buffer, new Uint8Array(value))
if (value instanceof Uint8Array) return new Bytes(builtins.Uint8Array, new Uint8Array(value))
if (value instanceof ArrayBuffer) {
const view = new Uint8Array(value)
Expand Down
3 changes: 3 additions & 0 deletions packages/codemode/src/runtime/globals.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Arr, BoundFn, Callable, coerceToInteger, get, Obj, type Value } from "./objects.js"
import { arrayGlobal } from "../stdlib/array.js"
import { textDecoderGlobal, textEncoderGlobal, uint8ArrayGlobal } from "../stdlib/bytes.js"
import { bufferGlobal } from "../stdlib/buffer.js"
import { mapGlobal, setGlobal, weakMapGlobal, weakSetGlobal } from "../stdlib/collections.js"
import { consoleGlobal } from "../stdlib/console.js"
import { dateGlobal } from "../stdlib/date.js"
Expand Down Expand Up @@ -120,6 +121,8 @@ const table: Record<string, Factory> = {
URLSearchParams: (ctx) => urlSearchParamsGlobal(ctx),
Headers: (ctx) => headersGlobal(ctx),
Uint8Array: (ctx) => uint8ArrayGlobal(ctx),
// Built on the host's Buffer, so only where there is one (Node and Bun, not browsers).
...(typeof Buffer === "undefined" ? {} : { Buffer: <R>(ctx: Interpreter<R>) => bufferGlobal(ctx) }),
TextEncoder: (ctx) => textEncoderGlobal(ctx),
TextDecoder: (ctx) => textDecoderGlobal(ctx),
encodeURI: (ctx) => uriGlobal(ctx, "encodeURI"),
Expand Down
6 changes: 5 additions & 1 deletion packages/codemode/src/runtime/intrinsics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const builtins = [
"URLSearchParams",
"Headers",
"Uint8Array",
"Buffer",
"TextEncoder",
"TextDecoder",
"Promise",
Expand Down Expand Up @@ -80,6 +81,7 @@ export const createBuiltins = (): Builtins => {
}
const iterator = plain()
const asyncIterator = plain()
const uint8Array = plain()
return {
Object: object,
Function: fn,
Expand All @@ -96,7 +98,9 @@ export const createBuiltins = (): Builtins => {
URL: plain(),
URLSearchParams: plain(),
Headers: plain(),
Uint8Array: plain(),
Uint8Array: uint8Array,
/** Node's Buffer: a Uint8Array whose text conversions default to UTF-8. */
Buffer: new Obj(uint8Array),
TextEncoder: plain(),
TextDecoder: plain(),
Promise: plain(),
Expand Down
2 changes: 2 additions & 0 deletions packages/codemode/src/snapshot/codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { Convert, Interruption, Invoke } from "../runtime/signals.js"
import { deleted, OrderedTable } from "../runtime/table.js"
import { AsyncFromSyncIterator } from "../generators.js"
import { HostCall, Resolving } from "../scheduler.js"
import { BufferObj } from "../stdlib/buffer.js"
import { TextDecoderObj } from "../stdlib/bytes.js"
import { ToolError, toolError } from "../tool-error.js"
import { ToolReference, ToolRuntimeError } from "../tool-runtime.js"
Expand Down Expand Up @@ -225,6 +226,7 @@ const builtinSpecs = (): ReadonlyArray<ClassSpec> => [
{ name: "HeadersObj", ctor: HeadersObj, fields: ["headers"], obj: true, constants: { tag: "Headers" } },
{ name: "URLObj", ctor: URLObj, fields: ["searchParams", "url"], obj: true, constants: { tag: "URL" } },
{ name: "Bytes", ctor: Bytes, fields: ["bytes"], obj: true, constants: { tag: "Uint8Array" } },
{ name: "BufferObj", ctor: BufferObj, fields: ["bytes"], obj: true, constants: { tag: "Uint8Array" } },
{
name: "TextDecoderObj",
ctor: TextDecoderObj,
Expand Down
Loading
Loading