summaryrefslogtreecommitdiff
path: root/src/sketch
diff options
context:
space:
mode:
Diffstat (limited to 'src/sketch')
-rw-r--r--src/sketch/root.zig9
-rw-r--r--src/sketch/web.zig23
2 files changed, 27 insertions, 5 deletions
diff --git a/src/sketch/root.zig b/src/sketch/root.zig
index 316a886..0567839 100644
--- a/src/sketch/root.zig
+++ b/src/sketch/root.zig
@@ -193,16 +193,17 @@ pub fn Glue(comptime App: type) type {
else => App.draw(current, session(ss), &f),
}
}
- pub fn command(ctx: *const abi.Context, s: [*]u8, ss: [*]u8, line: *const []const u8) callconv(.c) bool {
- if (!@hasDecl(App, "command")) return false;
+ pub fn command(ctx: *const abi.Context, s: [*]u8, ss: [*]u8, line: *const []const u8) callconv(.c) abi.Outcome {
+ if (!@hasDecl(App, "command")) return .unknown;
var c: Console = .{ .host = ctx.host, .in = ctx.input };
- return switch (comptime arity(App.command)) {
+ const taken = switch (comptime arity(App.command)) {
3 => App.command(state(s), &c, line.*),
else => App.command(state(s), session(ss), &c, line.*),
} catch |err| {
c.print("{t}. Type help for usage.", .{err});
- return true;
+ return .failed;
};
+ return if (taken) .done else .unknown;
}
};
}
diff --git a/src/sketch/web.zig b/src/sketch/web.zig
index 7246c0d..e91a0ad 100644
--- a/src/sketch/web.zig
+++ b/src/sketch/web.zig
@@ -23,7 +23,8 @@ const js = struct {
};
/// Exports `App` for `web/host.js`: `sk_init`, `sk_event`, `sk_frame`, and
-/// `sk_field` for tests to read state by path.
+/// for tests to read state by path, `sk_field` (a number) and `sk_tag` (an
+/// enum's tag name).
pub fn exportWeb(comptime App: type) void {
const D = Driver(App);
@export(&D.init, .{ .name = "sk_init" });
@@ -31,6 +32,7 @@ pub fn exportWeb(comptime App: type) void {
@export(&D.frame, .{ .name = "sk_frame" });
@export(&D.buffer, .{ .name = "sk_buffer" });
@export(&D.field, .{ .name = "sk_field" });
+ @export(&D.tag, .{ .name = "sk_tag" });
}
fn Driver(comptime App: type) type {
@@ -96,6 +98,7 @@ fn Driver(comptime App: type) type {
input.dt = dt;
input.frame += 1;
input.events = events[0..event_count];
+ input.started = input.frame == 1;
const ctx = context();
G.update(&ctx, std.mem.asBytes(&state));
input.events = &.{};
@@ -131,6 +134,24 @@ fn Driver(comptime App: type) type {
/// The state field at the path written into `sk_buffer`, as a number
/// (enums by value); NaN when there is no such scalar field.
+ /// Writes the tag name of the enum field at the path in `buffer` over
+ /// it, and returns its length; -1 when there is no such enum field.
+ fn tag(len: usize) callconv(.c) i32 {
+ const name = path[0..@min(len, path.len)];
+ for (G.description.state.fields) |f| {
+ if (f.kind != .@"enum" or !std.mem.eql(u8, std.mem.span(f.path), name)) continue;
+ const value: u64 = @intFromFloat(field(len));
+ var pairs = std.mem.tokenizeScalar(u8, std.mem.span(f.tags), ',');
+ while (pairs.next()) |pair| {
+ const eq = std.mem.indexOfScalar(u8, pair, '=') orelse continue;
+ if ((std.fmt.parseInt(u64, pair[eq + 1 ..], 10) catch continue) != value) continue;
+ @memcpy(path[0..eq], pair[0..eq]);
+ return @intCast(eq);
+ }
+ }
+ return -1;
+ }
+
fn field(len: usize) callconv(.c) f64 {
const name = path[0..@min(len, path.len)];
const bytes = std.mem.asBytes(&state);