config: API for wasm

This commit is contained in:
Mitchell Hashimoto
2022-12-30 16:19:54 -08:00
parent 58218af2b5
commit 7c291a2c4c
2 changed files with 54 additions and 0 deletions

View File

@ -527,6 +527,59 @@ pub const Keybinds = struct {
}
};
// Wasm API.
pub const Wasm = struct {
const wasm = @import("os/wasm.zig");
const alloc = wasm.alloc;
const cli_args = @import("cli_args.zig");
/// Create a new configuration filled with the initial default values.
export fn config_new() ?*Config {
const result = alloc.create(Config) catch |err| {
log.err("error allocating config err={}", .{err});
return null;
};
result.* = Config.default(alloc) catch |err| {
log.err("error creating config err={}", .{err});
return null;
};
return result;
}
export fn config_free(ptr: ?*Config) void {
if (ptr) |v| {
v.deinit();
alloc.destroy(v);
}
}
/// Load the configuration from a string in the same format as
/// the file-based syntax for the desktop version of the terminal.
export fn config_load_string(
self: *Config,
str: [*]const u8,
len: usize,
) void {
config_load_string_(self, str[0..len]) catch |err| {
log.err("error loading config err={}", .{err});
};
}
fn config_load_string_(self: *Config, str: []const u8) !void {
var fbs = std.io.fixedBufferStream(str);
var iter = cli_args.lineIterator(fbs.reader());
try cli_args.parse(Config, alloc, self, &iter);
}
export fn config_finalize(self: *Config) void {
self.finalize() catch |err| {
log.err("error finalizing config err={}", .{err});
};
}
};
test {
std.testing.refAllDecls(@This());
}

View File

@ -7,6 +7,7 @@ pub usingnamespace @import("os/wasm.zig");
pub usingnamespace @import("os/wasm/log.zig");
pub usingnamespace @import("font/main.zig");
pub usingnamespace @import("terminal/main.zig");
pub usingnamespace @import("config.zig").Wasm;
// Set our log level. We try to get as much logging as possible but in
// ReleaseSmall mode where we're optimizing for space, we elevate the