start making the src more wasm target aware

This commit is contained in:
Mitchell Hashimoto
2022-12-31 08:55:12 -08:00
parent ce490e21ea
commit 8b6128ddfc
2 changed files with 44 additions and 34 deletions

View File

@ -56,8 +56,10 @@ pub const Backend = enum {
/// Returns the default backend for a build environment. This is
/// meant to be called at comptime.
pub fn default() Backend {
// Wasm only supports browser at the moment.
if (builtin.target.isWasm()) return .web_canvas;
const wasm = @import("../os/wasm.zig");
if (wasm.target) |target| return switch (target) {
.browser => .web_canvas,
};
return if (build_options.coretext)
.coretext_freetype

View File

@ -2,39 +2,47 @@ const std = @import("std");
const builtin = @import("builtin");
const wasm = @import("../wasm.zig");
// The function std.log will call.
pub fn log(
comptime level: std.log.Level,
comptime scope: @TypeOf(.EnumLiteral),
comptime format: []const u8,
args: anytype,
) void {
// The buffer for putting our log message. We try to use a stack-allocated
// buffer first because we want to avoid allocation. If we are logging
// an error DUE to an OOM, allocating will of course fail and we'd like
// to see the error message so we prefer to use this.
var buf: [2048]u8 = undefined;
// Use the correct implementation
pub usingnamespace if (wasm.target) |target| switch (target) {
.browser => Browser,
} else struct {};
// Build the string
const level_txt = comptime level.asText();
const prefix = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): ";
const txt = level_txt ++ prefix ++ format;
/// Browser implementation calls an extern "log" function.
pub const Browser = struct {
// The function std.log will call.
pub fn log(
comptime level: std.log.Level,
comptime scope: @TypeOf(.EnumLiteral),
comptime format: []const u8,
args: anytype,
) void {
// The buffer for putting our log message. We try to use a stack-allocated
// buffer first because we want to avoid allocation. If we are logging
// an error DUE to an OOM, allocating will of course fail and we'd like
// to see the error message so we prefer to use this.
var buf: [2048]u8 = undefined;
// Format. We attempt to use a stack-allocated string first and if that
// fails we'll try to allocate.
var allocated: bool = false;
const str = nosuspend std.fmt.bufPrint(&buf, txt, args) catch str: {
allocated = true;
break :str std.fmt.allocPrint(wasm.alloc, txt, args) catch return;
// Build the string
const level_txt = comptime level.asText();
const prefix = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): ";
const txt = level_txt ++ prefix ++ format;
// Format. We attempt to use a stack-allocated string first and if that
// fails we'll try to allocate.
var allocated: bool = false;
const str = nosuspend std.fmt.bufPrint(&buf, txt, args) catch str: {
allocated = true;
break :str std.fmt.allocPrint(wasm.alloc, txt, args) catch return;
};
defer if (allocated) wasm.alloc.free(str);
// Send it over to the JS side
JS.log(str.ptr, str.len);
}
// We wrap our externs in this namespace so we can reuse symbols, otherwise
// "log" would collide.
const JS = struct {
extern "env" fn log(ptr: [*]const u8, len: usize) void;
};
defer if (allocated) wasm.alloc.free(str);
// Send it over to the JS side
JS.log(str.ptr, str.len);
}
// We wrap our externs in this namespace so we can reuse symbols, otherwise
// "log" would collide.
const JS = struct {
extern "env" fn log(ptr: [*]const u8, len: usize) void;
};