UNPKG

taglib-wasm

Version:

TagLib-Wasm is the universal tagging library for TypeScript/JavaScript platforms: Browsers, Node.js, Deno, Bun, Cloudflare Workers, and Electron apps

114 lines (113 loc) 3.2 kB
var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); import { WasmerExecutionError } from "../wasmer-sdk-loader/types.js"; import { MemoryError } from "../../errors/classes.js"; import { WasiFileHandle } from "./file-handle.js"; class WasiToTagLibAdapter { constructor(wasiModule) { __publicField(this, "wasi"); __publicField(this, "heap"); __publicField(this, "FileHandle", class { constructor() { throw new WasmerExecutionError( "Use createFileHandle() instead of new FileHandle()" ); } }); this.wasi = wasiModule; this.heap = new Uint8Array(wasiModule.memory.buffer); } get ready() { return Promise.resolve(this); } get HEAP8() { return new Int8Array(this.wasi.memory.buffer); } get HEAP16() { return new Int16Array(this.wasi.memory.buffer); } get HEAPU8() { return new Uint8Array(this.wasi.memory.buffer); } get HEAP32() { return new Int32Array(this.wasi.memory.buffer); } get HEAPU16() { return new Uint16Array(this.wasi.memory.buffer); } get HEAPU32() { return new Uint32Array(this.wasi.memory.buffer); } get HEAPF32() { return new Float32Array(this.wasi.memory.buffer); } get HEAPF64() { return new Float64Array(this.wasi.memory.buffer); } _malloc(size) { return this.wasi.malloc(size); } _free(ptr) { this.wasi.free(ptr); } _realloc(ptr, newSize) { const newPtr = this.wasi.malloc(newSize); if (!newPtr) { throw new MemoryError( `realloc failed. Requested size: ${newSize} bytes` ); } if (ptr) { const oldData = this.heap.slice(ptr, ptr + newSize); this.heap.set(oldData, newPtr); this.wasi.free(ptr); } return newPtr; } UTF8ToString(ptr) { if (!ptr) return ""; let end = ptr; while (this.heap[end] !== 0) end++; return new TextDecoder().decode(this.heap.slice(ptr, end)); } stringToUTF8(str, ptr, maxBytes) { const bytes = new TextEncoder().encode(str); const len = Math.min(bytes.length, maxBytes - 1); this.heap.set(bytes.slice(0, len), ptr); this.heap[ptr + len] = 0; return len; } lengthBytesUTF8(str) { return new TextEncoder().encode(str).length; } createFileHandle() { return new WasiFileHandle(this.wasi); } version() { return this.wasi.tl_version(); } addFunction(_func) { throw new WasmerExecutionError( "addFunction not supported in WASI mode" ); } removeFunction(_ptr) { throw new WasmerExecutionError( "removeFunction not supported in WASI mode" ); } cwrap(_name, _returnType, _argTypes) { throw new WasmerExecutionError( "cwrap not supported in WASI mode - use direct exports" ); } ccall(_name, _returnType, _argTypes, _args) { throw new WasmerExecutionError( "ccall not supported in WASI mode - use direct exports" ); } } export { WasiToTagLibAdapter };