Skip to content

Commit 2e1a449

Browse files
committed
feat(example): Update call_zig_from_js using binding
1 parent c98a50e commit 2e1a449

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

examples/call_zig_from_js/main.zig

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,40 @@ const webui = @import("webui");
66
const html = @embedFile("index.html");
77

88
pub fn main() !void {
9+
// Create a new WebUI window object
910
var nwin = webui.newWindow();
1011

12+
// Use binding function instead of standard bind function
13+
// binding is an advanced API that automatically handles parameter type conversion and function signature adaptation
14+
// It allows using native Zig function signatures without needing to handle Event pointers directly
15+
// Here we bind the HTML/JS "my_function_string" to Zig function getString
1116
_ = try nwin.binding("my_function_string", getString);
17+
// Equivalent using traditional bind function which requires manual Event handling
1218
// _ = try nwin.bind("my_function_string", my_function_string);
19+
20+
// Bind integer handler function, binding automatically converts JS parameters to corresponding Zig types
1321
_ = try nwin.binding("my_function_integer", getInteger);
1422
// _ = try nwin.bind("my_function_integer", my_function_integer);
15-
_ = try nwin.bind("my_function_boolean", my_function_boolean);
16-
_ = try nwin.bind("my_function_with_response", my_function_with_response);
17-
// _ = try nwin.binding("my_function_raw_binary", my_function_raw_binary);
23+
24+
// Bind boolean handler function, also with automatic type conversion
25+
_ = try nwin.binding("my_function_boolean", getBool);
26+
// _ = try nwin.bind("my_function_boolean", my_function_boolean);
27+
28+
// Bind function with response, binding supports using event object directly for responses
29+
_ = try nwin.binding("my_function_with_response", getResponse);
30+
// _ = try nwin.bind("my_function_with_response", my_function_with_response);
31+
32+
// Bind function for handling binary data, binding supports raw binary data processing
1833
_ = try nwin.binding("my_function_raw_binary", raw_binary);
1934
// _ = try nwin.bind("my_function_raw_binary", my_function_raw_binary);
2035

36+
// Show the window with embedded HTML content
2137
try nwin.show(html);
2238

39+
// Wait for all windows to close, this will block the current thread
2340
webui.wait();
2441

42+
// Clean up all resources
2543
webui.clean();
2644
}
2745

@@ -77,6 +95,12 @@ fn my_function_integer(e: *webui.Event) void {
7795
std.debug.print("my_function_integer 4: {}\n", .{float_1});
7896
}
7997

98+
fn getBool(b1: bool, b2: bool) void {
99+
std.debug.print("boolean is {},{}", .{
100+
b1, b2,
101+
});
102+
}
103+
80104
fn my_function_boolean(e: *webui.Event) void {
81105
// JavaScript:
82106
// my_function_boolean(true, false);
@@ -91,6 +115,13 @@ fn my_function_boolean(e: *webui.Event) void {
91115
std.debug.print("my_function_bool 2: {}\n", .{status_2});
92116
}
93117

118+
fn getResponse(e: *webui.Event,n1: i64, n2: i64) void {
119+
const res = n1 * n2;
120+
std.debug.print("my_function_with_response: {} * {} = {}\n", .{ n1, n2, res });
121+
// Send back the response to JavaScript
122+
e.returnValue(res);
123+
}
124+
94125
fn my_function_with_response(e: *webui.Event) void {
95126
// JavaScript:
96127
// my_function_with_response(number, 2).then(...)

0 commit comments

Comments
 (0)