aboutsummaryrefslogtreecommitdiff
path: root/src/zilch/bridge/root.zig
blob: 166aadccefe50cfa28345136d68585165da19b93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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);
        \\}
    );
};