|
| 1 | +import { describe, expect, test } from "vitest"; |
| 2 | +import { SwiftRuntime } from "../src/index.js"; |
| 3 | + |
| 4 | +// `swjs_load_typed_array` must copy only the window a TypedArray view describes, |
| 5 | +// not its whole backing `ArrayBuffer`. The guest sizes the destination from the |
| 6 | +// view's own `length`/`byteLength`, so copying the entire buffer both shifts the |
| 7 | +// bytes (a view with a non-zero `byteOffset` lands offset in the guest) and |
| 8 | +// writes past the end of the destination. |
| 9 | +const DESTINATION = 1024; |
| 10 | + |
| 11 | +function makeRuntime(): { runtime: SwiftRuntime; memory: WebAssembly.Memory } { |
| 12 | + const memory = new WebAssembly.Memory({ initial: 1 }); |
| 13 | + const runtime = new SwiftRuntime(); |
| 14 | + runtime.setInstance({ |
| 15 | + exports: { |
| 16 | + memory, |
| 17 | + swjs_library_version: () => 708, |
| 18 | + }, |
| 19 | + } as unknown as WebAssembly.Instance); |
| 20 | + return { runtime, memory }; |
| 21 | +} |
| 22 | + |
| 23 | +describe("swjs_load_typed_array respects the view's window", () => { |
| 24 | + test("copies a Uint8Array view from its byteOffset", () => { |
| 25 | + const { runtime, memory } = makeRuntime(); |
| 26 | + const backing = new ArrayBuffer(32); |
| 27 | + new Uint8Array(backing).set( |
| 28 | + Array.from({ length: 32 }, (_, i) => 0xa0 + i), |
| 29 | + ); |
| 30 | + const view = new Uint8Array(backing, 8, 8); |
| 31 | + |
| 32 | + const space = (runtime as any).memory; |
| 33 | + const imports = runtime.wasmImports as any; |
| 34 | + imports.swjs_load_typed_array(space.retain(view), DESTINATION); |
| 35 | + |
| 36 | + const guest = new Uint8Array(memory.buffer); |
| 37 | + expect( |
| 38 | + Array.from(guest.subarray(DESTINATION, DESTINATION + 8)), |
| 39 | + ).toEqual(Array.from(view)); |
| 40 | + }); |
| 41 | + |
| 42 | + test("does not write past the end of the view", () => { |
| 43 | + const { runtime, memory } = makeRuntime(); |
| 44 | + const backing = new ArrayBuffer(32); |
| 45 | + new Uint8Array(backing).fill(0xff); |
| 46 | + const view = new Uint8Array(backing, 8, 8); |
| 47 | + |
| 48 | + // Fill the guest memory around the destination with a sentinel so any |
| 49 | + // byte written beyond the view's `byteLength` is visible. |
| 50 | + const guest = new Uint8Array(memory.buffer); |
| 51 | + guest.fill(0x5a, DESTINATION, DESTINATION + 64); |
| 52 | + |
| 53 | + const space = (runtime as any).memory; |
| 54 | + const imports = runtime.wasmImports as any; |
| 55 | + imports.swjs_load_typed_array(space.retain(view), DESTINATION); |
| 56 | + |
| 57 | + expect( |
| 58 | + Array.from(guest.subarray(DESTINATION + 8, DESTINATION + 64)), |
| 59 | + ).toEqual(new Array(56).fill(0x5a)); |
| 60 | + }); |
| 61 | + |
| 62 | + test("copies a multi-byte element view from its byteOffset", () => { |
| 63 | + const { runtime, memory } = makeRuntime(); |
| 64 | + const backing = new ArrayBuffer(32); |
| 65 | + new Int32Array(backing).set([1, 2, 3, 4, 5, 6, 7, 8]); |
| 66 | + const view = new Int32Array(backing, 8, 4); |
| 67 | + |
| 68 | + const space = (runtime as any).memory; |
| 69 | + const imports = runtime.wasmImports as any; |
| 70 | + imports.swjs_load_typed_array(space.retain(view), DESTINATION); |
| 71 | + |
| 72 | + const guest = new Int32Array(memory.buffer, DESTINATION, 4); |
| 73 | + expect(Array.from(guest)).toEqual([3, 4, 5, 6]); |
| 74 | + }); |
| 75 | + |
| 76 | + test("copies a DataView from its byteOffset", () => { |
| 77 | + const { runtime, memory } = makeRuntime(); |
| 78 | + const backing = new ArrayBuffer(32); |
| 79 | + new Uint8Array(backing).set( |
| 80 | + Array.from({ length: 32 }, (_, i) => 0xa0 + i), |
| 81 | + ); |
| 82 | + const view = new DataView(backing, 8, 8); |
| 83 | + |
| 84 | + const space = (runtime as any).memory; |
| 85 | + const imports = runtime.wasmImports as any; |
| 86 | + imports.swjs_load_typed_array(space.retain(view), DESTINATION); |
| 87 | + |
| 88 | + const guest = new Uint8Array(memory.buffer); |
| 89 | + expect( |
| 90 | + Array.from(guest.subarray(DESTINATION, DESTINATION + 8)), |
| 91 | + ).toEqual(Array.from(new Uint8Array(backing, 8, 8))); |
| 92 | + }); |
| 93 | + |
| 94 | + test("still copies a whole-buffer view unchanged", () => { |
| 95 | + const { runtime, memory } = makeRuntime(); |
| 96 | + const view = new Uint8Array([1, 2, 3, 4, 5]); |
| 97 | + |
| 98 | + const space = (runtime as any).memory; |
| 99 | + const imports = runtime.wasmImports as any; |
| 100 | + imports.swjs_load_typed_array(space.retain(view), DESTINATION); |
| 101 | + |
| 102 | + const guest = new Uint8Array(memory.buffer); |
| 103 | + expect( |
| 104 | + Array.from(guest.subarray(DESTINATION, DESTINATION + 5)), |
| 105 | + ).toEqual([1, 2, 3, 4, 5]); |
| 106 | + }); |
| 107 | +}); |
0 commit comments