Skip to content

Commit 65effa0

Browse files
committed
Add typed call helpers
1 parent 08e7047 commit 65effa0

3 files changed

Lines changed: 27 additions & 7 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ The current phase provides:
1212
- embedded HTML, static directories, custom resources, external URLs, and a
1313
built-in JavaScript bridge;
1414
- JavaScript calls to Zig bindings with return values;
15+
- typed integer, float, and boolean call arguments and replies;
1516
- window and targeted `Call.client` calls to JavaScript with results, errors,
1617
timeouts, and stale-client detection;
1718
- targeted client navigation, close, and raw binary delivery;

docs/PURE_ZIG_REFACTOR.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,6 @@ implementations.
296296
| Upstream API | Current gap |
297297
|---|---|
298298
| `webui_bind()` | `Window.bind` supports explicit `webui.call()` calls, but the bridge does not automatically dispatch DOM events from an element with the same ID to that binding. |
299-
| `webui_get_float()`, `webui_get_float_at()` | `Call.float()` is not implemented. |
300-
| `webui_return_float()`, `webui_return_bool()` | `Call.reply()` can encode these values as text, but typed `replyFloat()` and `replyBool()` helpers are not implemented. |
301299
| `webui_show()`, `webui_set_root_folder()`, `webui_set_file_handler()`, `webui_set_file_handler_window()` | Content and resource handling can only be selected when creating a window; replacing them at runtime is not implemented. |
302300
| `webui_show_client()` | `Client` cannot replace the content of only one connected browser. |
303301
| `webui_is_shown()` | There is no window-level connected/shown query. |
@@ -350,8 +348,8 @@ not implementation gaps:
350348
| `webui_close()`, `webui_destroy()`, `webui_exit()`, `webui_clean()` | `Window.close()`, `Running.stop()`, and `App.deinit()`. |
351349
| `webui_set_context()`, `webui_get_context()` | Binding and event-handler `user_data`. |
352350
| `webui_get_count()`, `webui_get_size()`, `webui_get_size_at()` | `Call.arguments.len` and `Call.bytes(index).len`. |
353-
| `webui_get_string()`, `webui_get_string_at()`, `webui_get_int()`, `webui_get_int_at()`, `webui_get_bool()`, `webui_get_bool_at()` | `Call.string()`, `Call.int()`, and `Call.boolean()`. |
354-
| `webui_return_string()`, `webui_return_int()` | `Call.reply()` and `Call.replyInt()`. |
351+
| `webui_get_string()`, `webui_get_string_at()`, `webui_get_int()`, `webui_get_int_at()`, `webui_get_float()`, `webui_get_float_at()`, `webui_get_bool()`, `webui_get_bool_at()` | `Call.string()`, `Call.int()`, `Call.float()`, and `Call.boolean()`. |
352+
| `webui_return_string()`, `webui_return_int()`, `webui_return_float()`, `webui_return_bool()` | `Call.reply()`, `Call.replyInt()`, `Call.replyFloat()`, and `Call.replyBool()`. |
355353
| `webui_run()`, `webui_script()` | `Window.run()` and `Window.eval()`. |
356354
| `webui_run_client()`, `webui_script_client()` | `Client.run()` and `Client.eval()`. |
357355
| `webui_close_client()`, `webui_navigate_client()`, `webui_send_raw_client()` | `Client.close()`, `Client.navigate()`, and `Client.sendRaw()`. |
@@ -386,7 +384,6 @@ This completes the behavior represented by `webui_set_public()`,
386384

387385
### Calls, bindings, and browser bridge
388386

389-
- Add `Call.float()`, `Call.replyFloat()`, and `Call.replyBool()`.
390387
- Make element-name bindings dispatch the same binding for DOM events while
391388
preserving explicit `webui.call()` support.
392389
- Implement bridge `setLogging()`, `encode()`, `decode()`,
@@ -508,5 +505,5 @@ zig build -Dtarget=aarch64-macos
508505

509506
Continue capability parity:
510507

511-
1. Add `Call.float()`, `Call.replyFloat()`, and `Call.replyBool()`.
508+
1. Add element-name DOM binding dispatch.
512509
2. Complete the public browser bridge API.

src/app.zig

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,10 @@ pub const Call = struct {
636636
return std.fmt.parseInt(i64, try self.string(index), 10);
637637
}
638638

639+
pub fn float(self: *const Call, index: usize) !f64 {
640+
return std.fmt.parseFloat(f64, try self.string(index));
641+
}
642+
639643
pub fn boolean(self: *const Call, index: usize) !bool {
640644
const value = try self.string(index);
641645
if (std.mem.eql(u8, value, "true")) return true;
@@ -654,6 +658,15 @@ pub const Call = struct {
654658
var buffer: [64]u8 = undefined;
655659
try self.reply(try std.fmt.bufPrint(&buffer, "{d}", .{value}));
656660
}
661+
662+
pub fn replyFloat(self: *Call, value: f64) !void {
663+
var buffer: [64]u8 = undefined;
664+
try self.reply(try std.fmt.bufPrint(&buffer, "{d}", .{value}));
665+
}
666+
667+
pub fn replyBool(self: *Call, value: bool) !void {
668+
try self.reply(if (value) "true" else "false");
669+
}
657670
};
658671

659672
pub const Client = struct {
@@ -1718,13 +1731,22 @@ test "call accessors, window creation, and routes" {
17181731
var call: Call = .{
17191732
.gpa = gpa,
17201733
.client = .{ .state = window.state, .client_id = 1 },
1721-
.arguments = &.{ "42", "true" },
1734+
.arguments = &.{ "42", "true", "1.25", "invalid" },
17221735
};
17231736
defer call.deinit();
17241737
try std.testing.expectEqual(@as(i64, 42), try call.int(0));
17251738
try std.testing.expect(try call.boolean(1));
1739+
try std.testing.expectEqual(@as(f64, 1.25), try call.float(2));
1740+
try std.testing.expectError(error.InvalidCharacter, call.float(3));
1741+
try std.testing.expectError(error.MissingArgument, call.float(4));
17261742
try call.replyInt(84);
17271743
try std.testing.expectEqualStrings("84", call.response.items);
1744+
try call.replyFloat(1.25);
1745+
try std.testing.expectEqualStrings("1.25", call.response.items);
1746+
try call.replyBool(true);
1747+
try std.testing.expectEqualStrings("true", call.response.items);
1748+
try call.replyBool(false);
1749+
try std.testing.expectEqualStrings("false", call.response.items);
17281750
}
17291751

17301752
fn integrationHandler(call: *Call, user_data: ?*anyopaque) !void {

0 commit comments

Comments
 (0)