aboutsummaryrefslogtreecommitdiff
path: root/src/zilch/bridge/root.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/zilch/bridge/root.zig')
-rw-r--r--src/zilch/bridge/root.zig40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/zilch/bridge/root.zig b/src/zilch/bridge/root.zig
new file mode 100644
index 0000000..166aadc
--- /dev/null
+++ b/src/zilch/bridge/root.zig
@@ -0,0 +1,40 @@
+const std = @import("std");
+const builtin = @import("builtin");
+
+const mod = @import("../root.zig");
+const Env = mod.Env;
+const Function = Env.Function;
+
+const wasm = @import("wasm.root.zig");
+const server = @import("server.root.zig");
+
+pub fn Bridge(T: type) type {
+ if (builtin.target.cpu.arch.isWasm()) {
+ return wasm.Bridge(T);
+ } else {
+ return server.Bridge(T);
+ }
+}
+
+pub const Context = struct {
+ gpa: std.mem.Allocator,
+ arena: std.mem.Allocator,
+ io: std.Io,
+};
+
+pub const bindings = struct {
+ pub const call: Function = .from(
+ \\async function(ptr, len, result_ptr, len_check) {
+ \\ const response = await fetch("/api", {
+ \\ method: "POST",
+ \\ body: new DateView(wasm.memory.buffer, ptr, len),
+ \\ })
+ \\ const bytes = await response.bytes();
+ \\ if (bytes.length != len_check) {
+ \\ throw "response has incorrect size";
+ \\ }
+ \\ const memory = new Uint8Array(wasm.memory.buffer);
+ \\ memory.set(bytes, result_ptr);
+ \\}
+ );
+};