aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/app/root.zig13
-rw-r--r--src/z/Env/js.zig13
-rw-r--r--src/z/app/main.js29
3 files changed, 40 insertions, 15 deletions
diff --git a/src/app/root.zig b/src/app/root.zig
index 757e1ca..730eb50 100644
--- a/src/app/root.zig
+++ b/src/app/root.zig
@@ -15,10 +15,21 @@ fn run_wrapped() !void {
const console = (try env.getProp(z.Env.window, "console")).await(&env, .object);
const object = (try env.getProp(z.Env.window, "Object")).await(&env, .object);
+ const location = (try env.getProp(z.Env.window, "location")).await(&env, .object);
const obj = try env.new(object, .{});
+ const hostname = (try env.getProp(location, "hostname")).await(&env, .string);
+ defer std.heap.wasm_allocator.free(hostname);
- const call = try env.callMethod(console, "log", .{ "hello, wasm!", obj.await(&env, .object) });
+ const call = try env.callMethod(
+ console,
+ "log",
+ .{
+ "hello, wasm!",
+ obj.await(&env, .object),
+ hostname,
+ },
+ );
call.await(&env, .undefined);
diff --git a/src/z/Env/js.zig b/src/z/Env/js.zig
index b35f6ed..c1ff85d 100644
--- a/src/z/Env/js.zig
+++ b/src/z/Env/js.zig
@@ -7,3 +7,16 @@ pub extern "object_store" fn process(
[*]*Env.Value,
length: usize,
) void;
+
+export fn valueAllocString(value: *Env.Value, len: usize) [*]u8 {
+ const ptr: [*]u8 = @ptrCast(std.heap.wasm_allocator.alloc(u8, len) catch @panic("unable to allocate string"));
+ value.kind = .string;
+ value.value = .{
+ .string = .{
+ .ptr = ptr,
+ .len = len,
+ },
+ };
+
+ return @constCast(value.value.string.ptr);
+}
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))
},
}
}