diff --git a/src/wasm.zig b/src/wasm.zig index d19de15..2297ae2 100644 --- a/src/wasm.zig +++ b/src/wasm.zig @@ -9,9 +9,9 @@ comptime { @export(wasmAllocator.free, .{ .name = "free", .linkage = .Strong }); } -export fn compress(null_terminated: [*:0]const u8) i32 { +export fn compress(ptr: [*]const u8, length: usize) i32 { const allocator = std.heap.page_allocator; - const data: []const u8 = std.mem.span(null_terminated); // FIXME: Should we pass a length? + const data: []const u8 = ptr[0..length]; var output = impl.compress(u16, 0, 0xFFFE, data, allocator) catch { return 0; }; @@ -21,9 +21,9 @@ export fn compress(null_terminated: [*:0]const u8) i32 { return @intCast(@intFromPtr(r.ptr)); } -export fn decompress(null_terminated: [*:0]const u16) i32 { +export fn decompress(ptr: [*]const u16, length: usize) i32 { const allocator = std.heap.page_allocator; - const data: []const u16 = std.mem.span(null_terminated); + const data: []const u16 = ptr[0..length]; var output = impl.decompress(u16, 0, 0xFFFE, data, allocator) catch { return 0; }; diff --git a/ts-lib/src/common.ts b/ts-lib/src/common.ts index f782c0b..40d7e5e 100644 --- a/ts-lib/src/common.ts +++ b/ts-lib/src/common.ts @@ -4,16 +4,15 @@ export function copyToWasmBuffer( ) { // There's also an 'encodeInto' method to avoid a copy, but by encoding first, we con allocate only exactly what we need. const utf8Str = new TextEncoder().encode(str); - const ptrToStr = exports.allocUint8(utf8Str.length + 1); + const ptrToStr = exports.allocUint8(utf8Str.length); const inBuffer = new Uint8Array( exports.memory.buffer, ptrToStr, - utf8Str.length + 1 + utf8Str.length ); inBuffer.set(utf8Str); // Copy to WASM buffer. - inBuffer[utf8Str.length] = 0; // null terminate it. - return { ptr: ptrToStr, length: utf8Str.length + 1 }; + return { ptr: ptrToStr, length: utf8Str.length }; } // Horrible. Surely we can do better... diff --git a/ts-lib/src/module-packed.wasm b/ts-lib/src/module-packed.wasm index e84c96a..3e434e1 100644 Binary files a/ts-lib/src/module-packed.wasm and b/ts-lib/src/module-packed.wasm differ diff --git a/ts-lib/src/module.wasm b/ts-lib/src/module.wasm index ca02499..3665603 100644 Binary files a/ts-lib/src/module.wasm and b/ts-lib/src/module.wasm differ diff --git a/ts-lib/src/smol-string.ts b/ts-lib/src/smol-string.ts index 459e86c..dbde6c9 100644 --- a/ts-lib/src/smol-string.ts +++ b/ts-lib/src/smol-string.ts @@ -4,8 +4,8 @@ import init from "./module.wasm?init"; type exportsType = { allocUint8: (size: number) => number; allocUint16: (size: number) => number; - compress: (ptr: number) => number; - decompress: (ptr: number) => number; + compress: (ptr: number, length: number) => number; + decompress: (ptr: number, length: number) => number; free: (ptr: number, length: number) => void; memory: any; }; @@ -16,7 +16,7 @@ const exports = instance.exports as exportsType; export function compress(str: string) { const { ptr, length } = copyToWasmBuffer(str, exports); - const ptrToCompressed = exports.compress(ptr); + const ptrToCompressed = exports.compress(ptr, length); exports.free(ptr, length); @@ -38,23 +38,21 @@ export function compress(str: string) { } export function decompress(compressedStr: string) { - const ptrToCompressedNullTerminated = exports.allocUint16( - compressedStr.length + 1 - ); + const ptrToCompressed = exports.allocUint16(compressedStr.length); const compressed_buffer = new Uint16Array( exports.memory.buffer, - ptrToCompressedNullTerminated, - compressedStr.length + 1 + ptrToCompressed, + compressedStr.length ); for (let i = 0; i < compressedStr.length; i++) compressed_buffer[i] = compressedStr.charCodeAt(i); - compressed_buffer[compressedStr.length] = 0; const ptrToDecompressedNullTerminated = exports.decompress( - ptrToCompressedNullTerminated + ptrToCompressed, + compressedStr.length ); - exports.free(ptrToCompressedNullTerminated, compressedStr.length + 1); + exports.free(ptrToCompressed, compressedStr.length); const decompressed_buffer = new Uint8Array( exports.memory.buffer.slice(