aboutsummaryrefslogtreecommitdiff
path: root/src/global.zig
blob: 5a48d1b9c59288008e8b4614e802c3eb09aaf584 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const std = @import("std");
pub usingnamespace @import("strings.zig");

pub const BuildTarget = enum { native, wasm, wasi };
pub const build_target: BuildTarget = comptime {
    if (std.Target.current.isWasm() and std.Target.current.getOsTag() == .wasi) {
        return BuildTarget.wasi;
    } else if (std.Target.current.isWasm()) {
        return BuildTarget.wasm;
    } else {
        return BuildTarget.native;
    }
};

pub const isWasm = build_target == .wasm;
pub const isNative = build_target == .native;
pub const isWasi = build_target == .wasi;
pub const isBrowser = !isWasi and isWasm;

pub const isDebug = std.builtin.Mode.Debug == std.builtin.mode;

pub const Output = struct {
    var source: *Source = undefined;
    pub const Source = struct {
        const StreamType = comptime {
            if (isWasm) {
                return std.io.FixedBufferStream([]u8);
            } else {
                return std.fs.File;
            }
        };
        stream: StreamType,
        error_stream: StreamType,
        out_buffer: []u8 = &([_]u8{}),
        err_buffer: []u8 = &([_]u8{}),

        pub fn init(
            stream: StreamType,
            err: StreamType,
        ) Source {
            return Source{ .stream = stream, .error_stream = err };
        }

        pub fn set(_source: *Source) void {
            source = _source;
        }
    };

    pub fn printErrorable(comptime fmt: string, args: anytype) !void {
        if (isWasm) {
            try source.stream.seekTo(0);
            try source.stream.writer().print(fmt, args);
            const root = @import("root");
            root.console_log(root.Uint8Array.fromSlice(source.out_buffer[0..source.stream.pos]));
        } else {
            std.fmt.format(source.stream.writer(), fmt, args) catch unreachable;
        }
    }

    pub fn print(comptime fmt: string, args: anytype) void {
        if (isWasm) {
            source.stream.seekTo(0) catch return;
            source.stream.writer().print(fmt, args) catch return;
            const root = @import("root");
            root.console_log(root.Uint8Array.fromSlice(source.out_buffer[0..source.stream.pos]));
        } else {
            std.fmt.format(source.stream.writer(), fmt, args) catch unreachable;
        }
    }
    pub fn printError(comptime fmt: string, args: anytype) void {
        if (isWasm) {
            source.error_stream.seekTo(0) catch return;
            source.error_stream.writer().print(fmt, args) catch unreachable;
            const root = @import("root");
            root.console_error(root.Uint8Array.fromSlice(source.err_buffer[0..source.error_stream.pos]));
        } else {
            std.fmt.format(source.error_stream.writer(), fmt, args) catch unreachable;
        }
    }
};

pub const Global = struct {
    pub fn panic(comptime fmt: string, args: anytype) noreturn {
        if (isWasm) {
            Output.print(fmt, args);
            @panic(fmt);
        } else {
            std.debug.panic(fmt, args);
        }
    }

    pub fn notimpl() noreturn {
        Global.panic("Not implemented yet!!!!!", .{});
    }
};