aboutsummaryrefslogtreecommitdiff
path: root/src/exports.zig
blob: 132b52a849be71ae5bbe827e76a90eaa0539b069 (plain) (blame)
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
41
42
43
44
const std = @import("std");
const alloc = @import("alloc.zig");
usingnamespace @import("global.zig");

const Root = @import("main_wasm.zig").Root;

pub extern fn init() void {
    alloc.dynamic = default_allocator;
    alloc.static = default_allocator;
}

/// Convert a slice into known memory representation -- enables C ABI
pub const U8Chunk = packed struct {
    const Float = @Type(builtin.TypeInfo{ .Float = .{ .bits = 2 * @bitSizeOf(usize) } });
    const Abi = if (builtin.arch.isWasm()) Float else U8Chunk;

    ptr: [*]u8,
    len: usize,

    pub fn toSlice(raw: Abi) []u8 {
        const self = @bitCast(U8Chunk, raw);
        return self.ptr[0..self.len];
    }

    pub fn fromSlice(slice: []u8) Abi {
        const self = U8Chunk{ .ptr = slice.ptr, .len = slice.len };
        return @bitCast(Abi, self);
    }

    pub fn empty() Abi {
        return U8Chunk.fromSlice(&[0]u8{});
    }
};

export fn fd_create() ?*Root {
    const fd = allocator.create(Root) catch return null;
    fd.* = .{};
    return fd;
}

export fn fd_destroy(fd: *Root) void {
    fd.deinit(allocator);
    allocator.destroy(fd);
}