UNPKG

@doc.e.dub/csound-browser

Version:

[![npm (scoped with tag)](https://shields.shivering-isles.com/npm/v/@csound/browser/latest)](https://www.npmjs.com/package/@csound/browser) [![GitHub Workflow Status](https://shields.shivering-isles.com/github/workflow/status/csound/csound/csound_wasm)](h

28 lines (23 loc) 834 B
import { encoder, uint2String } from "@utils/text-encoders"; import { trimNull } from "@utils/trim-null"; export const freeStringPtr = (wasm, ptr) => { wasm.exports.freeStringMem(ptr); }; export const ptr2string = (wasm, stringPtr) => { const { buffer } = wasm.exports.memory; const intArray = new Uint8Array(buffer, stringPtr); const result = uint2String(intArray); return trimNull(result); }; export const string2ptr = (wasm, string) => { if (typeof string !== "string") { console.error("Expected string but got", typeof string); return; } const stringBuf = encoder.encode(string); const offset = wasm.exports.allocStringMem(stringBuf.length); const { buffer } = wasm.exports.memory; const outBuf = new Uint8Array(buffer, offset, stringBuf.length + 1); outBuf.set(stringBuf); return offset; };