fix(codemode): make Buffer map, filter, slice and subarray return a Buffer (#64933)

This commit is contained in:
Aiden Cline 2026-11-19 15:26:31 -06:00 • committed by GitHub
parent 53212a6125
commit 6fbc0af624
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 40 additions and 6 deletions

View file

@ -620,6 +620,8 @@ method works on it, and its text conversions default to UTF-8.
`{ 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] The inherited `Uint8Array` methods that make new bytes (`map`, `filter`, `slice`, `subarray`, also called
through `Uint8Array.prototype`) make a Buffer from a Buffer, as JS's species does.
- [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.

File diff suppressed because one or more lines are too long

View file

@ -134,6 +134,15 @@ export const helpers = (ctx: Interpreter<unknown>): Array<readonly [string, Valu
return finishBytes(Uint8Array.from(items, coerceToNumber), into as Obj, offset as number)
},
],
/** A Uint8Array of the values, of the same kind as the Uint8Array `source`: a Buffer's map makes a Buffer. */
$DerivedBytes: [
2,
([values, source]) => {
const items = (values as Arr).items
checkArrayLength(items.length)
return (source as Bytes).derive(builtins, Uint8Array.from(items, coerceToNumber))
},
],
/** Headers or URLSearchParams from the pairs `readPairs` read, or from a record. */
$Pairs: [
4,

View file

@ -30,7 +30,7 @@ function Array$prototype$map(callback, thisArg = undefined) {
}
function Uint8Array$prototype$map(callback, thisArg = undefined) {
const bytes = $Receiver(this, "Uint8Array", "Uint8Array.prototype.map")
return $Bytes(mapItems(bytes, callback, thisArg, "Uint8Array.map"), Uint8Array.prototype, 0)
return $DerivedBytes(mapItems(bytes, callback, thisArg, "Uint8Array.map"), bytes)
}
function mapItems(array, callback, thisArg, label) {
$Callback(callback, label)
@ -49,7 +49,7 @@ function Array$prototype$filter(callback, thisArg = undefined) {
}
function Uint8Array$prototype$filter(callback, thisArg = undefined) {
const bytes = $Receiver(this, "Uint8Array", "Uint8Array.prototype.filter")
return $Bytes(filterItems(bytes, callback, thisArg, "Uint8Array.filter"), Uint8Array.prototype, 0)
return $DerivedBytes(filterItems(bytes, callback, thisArg, "Uint8Array.filter"), bytes)
}
function filterItems(array, callback, thisArg, label) {
$Callback(callback, label)

View file

@ -806,6 +806,10 @@ export class Bytes extends Wrapper {
override iterator(builtins: Builtins) {
return new Position(this, "values", builtins.Array)
}
/** New bytes of this one's kind, as `slice`, `subarray`, `map` and `filter` make them (a Buffer's are a Buffer). */
derive(builtins: Builtins, bytes: Uint8Array): Bytes {
return new Bytes(builtins.Uint8Array, bytes)
}
}
/**

View file

@ -1,4 +1,4 @@
import { createErrorValue } from "../runtime/intrinsics.js"
import { type Builtins, createErrorValue } from "../runtime/intrinsics.js"
import { readsPending, withReads } from "../runtime/callback.js"
import type { Interpreter } from "../runtime/interpreter.js"
import { Throw, typeError } from "../runtime/model.js"
@ -26,6 +26,9 @@ export class BufferObj extends Bytes {
override toHost() {
return Buffer.from(this.bytes)
}
override derive(builtins: Builtins, bytes: Uint8Array) {
return new BufferObj(builtins.Buffer, bytes)
}
override inspect() {
const shown = Array.from(this.bytes.subarray(0, 50), (byte) => byte.toString(16).padStart(2, "0")).join(" ")
const more = this.bytes.length > 50 ? ` ... ${this.bytes.length - 50} more bytes` : ""

View file

@ -98,13 +98,19 @@ export const uint8ArrayGlobal = <R>(ctx: Interpreter<R>) => {
[
"slice",
2,
(thisValue, args) => wrap(self(thisValue, "slice").bytes.slice(optNumber(args[0]), optNumber(args[1]))),
(thisValue, args) => {
const target = self(thisValue, "slice")
return target.derive(builtins, target.bytes.slice(optNumber(args[0]), optNumber(args[1])))
},
],
// A view on the same bytes, as in JS: writes through one are visible through the other.
[
"subarray",
2,
(thisValue, args) => wrap(self(thisValue, "subarray").bytes.subarray(optNumber(args[0]), optNumber(args[1]))),
(thisValue, args) => {
const target = self(thisValue, "subarray")
return target.derive(builtins, target.bytes.subarray(optNumber(args[0]), optNumber(args[1])))
},
],
[
"set",

View file

@ -35,6 +35,16 @@ describe("Buffer", () => {
).toEqual(["axcd", "xc", true, -1, true, 2, "010101", "abab"])
})
test("makes a Buffer from a Buffer through the inherited Uint8Array methods, like Bun", async () => {
expect(
await value(`const b = Buffer.from("abc")
const made = [b.map((x) => x + 1), b.filter((x) => x > 97), Uint8Array.prototype.slice.call(b, 1),
Uint8Array.prototype.subarray.call(b, 2)]
return [...made.map((x) => [Buffer.isBuffer(x), x.toString()]), Buffer.isBuffer(new Uint8Array(1).map((x) => x)),
String(new Uint8Array([1, 2]).slice(1))]`),
).toEqual([[true, "bcd"], [true, "bc"], [true, "bc"], [true, "c"], false, "2"])
})
test("reads and writes numbers", async () => {
expect(
await value(`const b = Buffer.alloc(4)