| 1 |
export function readString8(wasm, ptr, len) { |
| 2 |
const buf = new Uint8Array(wasm.memory.buffer, ptr, len); |
| 3 |
return (new TextDecoder("utf-8")).decode(buf) |
| 4 |
} |
| 5 |
|
| 6 |
export function readString16(wasm, ptr, len) { |
| 7 |
const buf = new Uint16Array(wasm.memory.buffer, ptr, len); |
| 8 |
return String.fromCharCode.apply(null, buf) |
| 9 |
} |
| 10 |
|
| 11 |
export function withWriteable(wasm, callback) { |
| 12 |
const writeable = wasm.diplomat_buffer_writeable_create(0); |
| 13 |
try { |
| 14 |
callback(writeable); |
| 15 |
const outStringPtr = wasm.diplomat_buffer_writeable_get_bytes(writeable); |
| 16 |
const outStringLen = wasm.diplomat_buffer_writeable_len(writeable); |
| 17 |
return readString8(wasm, outStringPtr, outStringLen); |
| 18 |
} finally { |
| 19 |
wasm.diplomat_buffer_writeable_destroy(writeable); |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
export class FFIError extends Error { |
| 24 |
constructor(error_value) { |
| 25 |
super("Error over FFI"); |
| 26 |
this.error_value = error_value; // (2) |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
export function extractCodePoint(str, param) { |
| 31 |
const cp = str.codePointAt?.(0); |
| 32 |
if ((!cp && cp !== 0) || [...str]?.length != 1) { |
| 33 |
throw new TypeError(`Expected single-character string for char parameter ${param}, found ${str}`); |
| 34 |
} |
| 35 |
return cp; |
| 36 |
} |
| 37 |
|
| 38 |
// Get the pointer returned by an FFI function |
| 39 |
// |
| 40 |
// It's tempting to call `(new Uint32Array(wasm.memory.buffer, FFI_func(), 1))[0]`. |
| 41 |
// However, there's a chance that `wasm.memory.buffer` will be resized between |
| 42 |
// the time it's accessed and the time it's used, invalidating the view. |
| 43 |
// This function ensures that the view into wasm memory is fresh. |
| 44 |
// |
| 45 |
// This is used for methods that return multiple types into a wasm buffer, where |
| 46 |
// one of those types is another ptr. Call this method to get access to the returned |
| 47 |
// ptr, so the return buffer can be freed. |
| 48 |
export function ptrRead(wasm, ptr) { |
| 49 |
return (new Uint32Array(wasm.memory.buffer, ptr, 1))[0]; |
| 50 |
} |
| 51 |
|
| 52 |
// Get the flag of a result type. |
| 53 |
export function resultFlag(wasm, ptr, offset) { |
| 54 |
return (new Uint8Array(wasm.memory.buffer, ptr + offset, 1))[0]; |
| 55 |
} |
| 56 |
|
| 57 |
// Get the discriminant of a Rust enum. |
| 58 |
export function enumDiscriminant(wasm, ptr) { |
| 59 |
return (new Int32Array(wasm.memory.buffer, ptr, 1))[0] |
| 60 |
} |
| 61 |
|
| 62 |
// A wrapper around a slice of WASM memory that can be freed manually or |
| 63 |
// automatically by the garbage collector. |
| 64 |
// |
| 65 |
// This type is necessary for Rust functions that take a `&str` or `&[T]`, since |
| 66 |
// they can create an edge to this object if they borrow from the str/slice, |
| 67 |
// or we can manually free the WASM memory if they don't. |
| 68 |
export class DiplomatBuf { |
| 69 |
static str8 = (wasm, string) => { |
| 70 |
var utf8Length = 0; |
| 71 |
for (const codepointString of string) { |
| 72 |
let codepoint = codepointString.codePointAt(0); |
| 73 |
if (codepoint < 0x80) { |
| 74 |
utf8Length += 1 |
| 75 |
} else if (codepoint < 0x800) { |
| 76 |
utf8Length += 2 |
| 77 |
} else if (codepoint < 0x10000) { |
| 78 |
utf8Length += 3 |
| 79 |
} else { |
| 80 |
utf8Length += 4 |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
const ptr = wasm.diplomat_alloc(utf8Length, 1); |
| 85 |
|
| 86 |
const result = (new TextEncoder()).encodeInto(string, new Uint8Array(wasm.memory.buffer, ptr, utf8Length)); |
| 87 |
console.assert(string.length == result.read && utf8Length == result.written, "UTF-8 write error"); |
| 88 |
|
| 89 |
return new DiplomatBuf(ptr, utf8Length, () => wasm.diplomat_free(ptr, utf8Length, 1)); |
| 90 |
} |
| 91 |
|
| 92 |
static str16 = (wasm, string) => { |
| 93 |
const byteLength = string.length * 2; |
| 94 |
const ptr = wasm.diplomat_alloc(byteLength, 2); |
| 95 |
|
| 96 |
const destination = new Uint16Array(wasm.memory.buffer, ptr, byteLength); |
| 97 |
for (var i; i < string.length; i++) { |
| 98 |
destination[i] = string.charCodeAt(i); |
| 99 |
} |
| 100 |
|
| 101 |
return new DiplomatBuf(ptr, string.length, () => wasm.diplomat_free(ptr, byteLength, 2)); |
| 102 |
} |
| 103 |
|
| 104 |
static slice = (wasm, list, rustType) => { |
| 105 |
const elementSize = rustType == "u8" || rustType == "i8" || rustType == "bool" ? 1 : |
| 106 |
rustType == "u16" || rustType == "i16" ? 2 : |
| 107 |
rustType == "u64" || rustType == "i64" || rustType == "f64" ? 8 : |
| 108 |
4; |
| 109 |
|
| 110 |
const byteLength = list.length * elementSize; |
| 111 |
const ptr = wasm.diplomat_alloc(byteLength, elementSize); |
| 112 |
|
| 113 |
// Create an array view of the buffer. This gives us the `set` method which correctly handles untyped values |
| 114 |
const destination = |
| 115 |
rustType == "u8" || rustType == "bool" ? new Uint8Array(wasm.memory.buffer, ptr, byteLength) : |
| 116 |
rustType == "i8" ? new Int8Array(wasm.memory.buffer, ptr, byteLength) : |
| 117 |
rustType == "u16" ? new Uint16Array(wasm.memory.buffer, ptr, byteLength) : |
| 118 |
rustType == "i16" ? new Int16Array(wasm.memory.buffer, ptr, byteLength) : |
| 119 |
rustType == "i32" || rustType == "isize" ? new Int32Array(wasm.memory.buffer, ptr, byteLength) : |
| 120 |
rustType == "u64" ? new BigUint64Array(wasm.memory.buffer, ptr, byteLength) : |
| 121 |
rustType == "i64" ? new BigInt64Array(wasm.memory.buffer, ptr, byteLength) : |
| 122 |
rustType == "f32" ? new Float32Array(wasm.memory.buffer, ptr, byteLength) : |
| 123 |
rustType == "f64" ? new Float64Array(wasm.memory.buffer, ptr, byteLength) : |
| 124 |
new Uint32Array(wasm.memory.buffer, ptr, byteLength); |
| 125 |
destination.set(list); |
| 126 |
|
| 127 |
return new DiplomatBuf(ptr, list.length, () => wasm.diplomat_free(ptr, byteLength, elementSize)); |
| 128 |
} |
| 129 |
|
| 130 |
constructor(ptr, size, free) { |
| 131 |
this.ptr = ptr; |
| 132 |
this.size = size; |
| 133 |
// Generated code calls one of methods these for each allocation, to either |
| 134 |
// free directly after the FFI call, to leak (to create a &'static), or to |
| 135 |
// register the buffer with the garbage collector (to create a &'a). |
| 136 |
this.free = free; |
| 137 |
this.leak = () => { }; |
| 138 |
this.garbageCollect = () => DiplomatBufferFinalizer.register(this, this.free); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
const DiplomatBufferFinalizer = new FinalizationRegistry(free => free()); |