Skip to content

Commit

Permalink
Pass explicit lengths to WASM rather than null terminated strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Senryoku committed Nov 1, 2023
1 parent 7700f83 commit b736d43
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 19 deletions.
8 changes: 4 additions & 4 deletions src/wasm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand All @@ -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;
};
Expand Down
7 changes: 3 additions & 4 deletions ts-lib/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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...
Expand Down
Binary file modified ts-lib/src/module-packed.wasm
Binary file not shown.
Binary file modified ts-lib/src/module.wasm
Binary file not shown.
20 changes: 9 additions & 11 deletions ts-lib/src/smol-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand All @@ -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);

Expand All @@ -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(
Expand Down

0 comments on commit b736d43

Please sign in to comment.