diff --git a/src/config.zig b/src/config.zig index fd92e2de7..58a90082f 100644 --- a/src/config.zig +++ b/src/config.zig @@ -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()); } diff --git a/src/main_wasm.zig b/src/main_wasm.zig index c9c2069dc..46c77082b 100644 --- a/src/main_wasm.zig +++ b/src/main_wasm.zig @@ -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