Slide 17
Slide 17 text
AS൛ͷίʔυʢJSଆʣ
// wm = InstantiatedWasmModule.exports
type pointer = number; // 32-bit integer
export function utf8DecodeWasm(bytes: Uint8Array, inputOffset: numbe
byteLength: number): string {
const inputPtr: pointer = wm.malloc(byteLength);
const outputPtr: pointer = wm.malloc(byteLength * 2);
try {
setMemoryU8(inputPtr, bytes.subarray(inputOffset, inputOffset +
byteLength), byteLength);
const outputArraySize = wm.utf8DecodeToUint16Array(outputPtr,
inputPtr, byteLength);
const units = new Uint16Array(wm.memory.buffer, outputPtr,
outputArraySize);
return String.fromCharCode(...units);
} finally {
wm.free(inputPtr);
wm.free(outputPtr);
}
}