aboutsummaryrefslogtreecommitdiff
path: root/src/z/app/main.js
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2026-10-06 07:53:25 +0200
committerNathan Reiner <nathan@nathanreiner.xyz>2026-10-06 07:53:25 +0200
commit64146d3d4277b34946ab9a294b11e41979d7ac74 (patch)
tree39003d7077e472caf8c012cd24a4ca9d00a1ac56 /src/z/app/main.js
parentec8ab44877368a1ac27dcdd615ad00bd1249407b (diff)
wasm interface add return type string decoding
Diffstat (limited to 'src/z/app/main.js')
-rw-r--r--src/z/app/main.js29
1 files changed, 15 insertions, 14 deletions
diff --git a/src/z/app/main.js b/src/z/app/main.js
index 9747bfe..e15f783 100644
--- a/src/z/app/main.js
+++ b/src/z/app/main.js
@@ -1,8 +1,3 @@
-function decodeString(base, length) {
- const slice = new Uint8Array(wasm.memory.buffer, base, length);
- return new TextDecoder().decode(slice);
-}
-
class ObjectStore {
static id_name = "@id";
@@ -76,6 +71,11 @@ class ObjectStore {
return new target(...args);
}
+ static decodeString(base, len) {
+ const slice = new Uint8Array(wasm.memory.buffer, base, len);
+ return new TextDecoder().decode(slice);
+ }
+
static decodeCall(view, offset) {
const kind = ObjectStore.methods[view.getUint8(offset, true)]
@@ -95,7 +95,7 @@ class ObjectStore {
if (kind === 'string') {
const ptr = view.getUint32(offset + 8, true)
const len = view.getUint32(offset + 12, true)
- return decodeString(ptr, len);
+ return ObjectStore.decodeString(ptr, len);
}
if (kind === 'int') {
@@ -156,20 +156,21 @@ class ObjectStore {
}
if (typeof value === 'number') {
- // float
mem.setUint8(0, ObjectStore.types['float'], true);
mem.setFloat64(8, value, true)
return;
}
if (typeof value === 'string') {
- // string
- throw "unimplemented";
+ const string_ptr = wasm.exports.valueAllocString(ptr, value.length);
+ const encoder = new TextEncoder();
+ const string = encoder.encode(value);
+ const memory = new Uint8Array(wasm.memory.buffer);
+ memory.set(string, string_ptr);
return;
}
if (typeof value === 'undefined') {
- // undefined
mem.setUint8(0, ObjectStore.types['undefined'], true);
return;
}
@@ -216,16 +217,16 @@ const imports = {
},
console: {
log(buffer, length) {
- console.log(decodeString(buffer, length))
+ console.log(ObjectStore.decodeString(buffer, length))
},
warn(buffer, length) {
- console.warn(decodeString(buffer, length))
+ console.warn(ObjectStore.decodeString(buffer, length))
},
err(buffer, length) {
- console.error(decodeString(buffer, length))
+ console.error(ObjectStore.decodeString(buffer, length))
},
debug(buffer, length) {
- console.debug(decodeString(buffer, length))
+ console.debug(ObjectStore.decodeString(buffer, length))
},
}
}