node: fix Buffer.from(arrayBuffer) (#17267)

This commit is contained in:
Meghan Denny
2025-02-11 18:10:43 -08:00
committed by GitHub
parent 0b6d468b74
commit bdccbbc828
2 changed files with 29 additions and 1 deletions

View File

@@ -2281,7 +2281,7 @@ JSC_DEFINE_HOST_FUNCTION(Zig::jsFunctionInherits, (JSC::JSGlobalObject * globalO
switch (id) {
${jsclasses
.map(v => v[0])
.map((v, i) => ` case ${i}: return JSValue::encode(jsBoolean(cell->inherits<WebCore::JS${v}>()));`)
.map((v, i) => ` case ${i}: return JSValue::encode(jsBoolean(jsDynamicCast<WebCore::JS${v}*>(cell) != nullptr));`)
.join("\n")}
}
return JSValue::encode(jsBoolean(false));

View File

@@ -2980,3 +2980,31 @@ it("should not trim utf-8 start bytes at end of string", () => {
const buf2 = Buffer.from("36e1", "hex");
expect(buf2.toString("utf-8")).toEqual("6\uFFFD");
});
it("Buffer.from(arrayBuffer)", () => {
const ab = Buffer.from([10, 11, 12, 13, 14, 15, 16, 17, 18, 19]).buffer;
const buf = Buffer.from(ab);
expect(buf.length).toBe(10);
expect(buf.buffer).toBe(ab);
expect(buf.byteOffset).toBe(0);
expect(buf.byteLength).toBe(10);
expect(buf[Symbol.iterator]().toArray()).toEqual([10, 11, 12, 13, 14, 15, 16, 17, 18, 19]);
});
it("Buffer.from(arrayBuffer, byteOffset)", () => {
const ab = Buffer.from([10, 11, 12, 13, 14, 15, 16, 17, 18, 19]).buffer;
const buf = Buffer.from(ab, 2);
expect(buf.length).toBe(8);
expect(buf.buffer).toBe(ab);
expect(buf.byteOffset).toBe(2);
expect(buf.byteLength).toBe(8);
expect(buf[Symbol.iterator]().toArray()).toEqual([12, 13, 14, 15, 16, 17, 18, 19]);
});
it("Buffer.from(arrayBuffer, byteOffset, length)", () => {
const ab = Buffer.from([10, 11, 12, 13, 14, 15, 16, 17, 18, 19]).buffer;
const buf = Buffer.from(ab, 3, 5);
expect(buf.length).toBe(5);
expect(buf.buffer).toBe(ab);
expect(buf.byteOffset).toBe(3);
expect(buf.byteLength).toBe(5);
expect(buf[Symbol.iterator]().toArray()).toEqual([13, 14, 15, 16, 17]);
});