Skip to content

Commit 8ee2681

Browse files
committed
feat: Complete binding function
1 parent 59a9f59 commit 8ee2681

File tree

2 files changed

+53
-21
lines changed

2 files changed

+53
-21
lines changed

examples/call_zig_from_js/main.zig

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ pub fn main() !void {
1414
// _ = try nwin.bind("my_function_integer", my_function_integer);
1515
_ = try nwin.bind("my_function_boolean", my_function_boolean);
1616
_ = try nwin.bind("my_function_with_response", my_function_with_response);
17-
_ = try nwin.bind("my_function_raw_binary", my_function_raw_binary);
17+
// _ = try nwin.binding("my_function_raw_binary", my_function_raw_binary);
18+
_ = try nwin.binding("my_function_raw_binary", raw_binary);
19+
// _ = try nwin.bind("my_function_raw_binary", my_function_raw_binary);
1820

1921
try nwin.show(html);
2022

@@ -104,6 +106,33 @@ fn my_function_with_response(e: *webui.Event) void {
104106
e.returnValue(res);
105107
}
106108

109+
fn raw_binary(e: *webui.Event, raw_1: [:0]const u8, raw_2: [*]const u8) void {
110+
// Or e.getSizeAt(0);
111+
const len_1 = e.getSize() catch return;
112+
const len_2 = e.getSizeAt(1) catch return;
113+
114+
// Print raw_1
115+
std.debug.print("my_function_raw_binary 1 ({} bytes): ", .{len_1});
116+
for (0..len_1) |i| {
117+
std.debug.print("0x{x} ", .{raw_1[i]});
118+
}
119+
std.debug.print("\n", .{});
120+
121+
// Check raw_2 (Big)
122+
// [0xA1, 0x00..., 0xA2]
123+
var vaild = false;
124+
125+
if (raw_2[0] == 0xA1 and raw_2[len_2 - 1] == 0xA2) {
126+
vaild = true;
127+
}
128+
129+
// Print raw_2
130+
std.debug.print("my_function_raw_binary 2 big ({} bytes): valid data? {s}\n", .{
131+
len_2,
132+
if (vaild) "Yes" else "No",
133+
});
134+
}
135+
107136
fn my_function_raw_binary(e: *webui.Event) void {
108137
// JavaScript:
109138
// my_function_raw_binary(new Uint8Array([0x41]), new Uint8Array([0x42, 0x43]));

src/webui.zig

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -778,53 +778,56 @@ pub fn binding(self: webui, element: [:0]const u8, comptime callback: anytype) !
778778
fn handle(e: *Event) void {
779779
var param_tup: tup_t = undefined;
780780

781+
var index: usize = 0;
781782
inline for (fnInfo.params, 0..fnInfo.params.len) |param, i| {
782783
if (param.type) |tt| {
783784
const paramTInfo = @typeInfo(tt);
784785
switch (paramTInfo) {
785786
.@"struct" => {
786-
if (tt != Event) {
787+
if (tt == Event) {
788+
param_tup[i] = e.*;
789+
index += 1;
790+
} else {
787791
const err_msg = std.fmt.comptimePrint(
788792
"the struct type is ({}), the struct type you can use only is Event in params!",
789793
.{tt},
790794
);
791795
@compileError(err_msg);
792796
}
793-
param_tup[i] = e;
794797
},
795798
.bool => {
796-
const res = e.getBoolAt(i);
799+
const res = e.getBoolAt(i - index);
797800
param_tup[i] = res;
798801
},
799802
.int => {
800-
const res = e.getIntAt(i);
803+
const res = e.getIntAt(i - index);
801804
param_tup[i] = @intCast(res);
802805
},
803806
.float => {
804-
const res = e.getFloatAt(i);
807+
const res = e.getFloatAt(i - index);
805808
param_tup[i] = res;
806809
},
807810
.pointer => |pointer| {
808-
if (pointer.size != .slice or pointer.child != u8 or pointer.is_const == false) {
811+
if (pointer.size == .slice and pointer.child == u8 and pointer.is_const == true) {
812+
if (pointer.sentinel()) |sentinel| {
813+
if (sentinel == 0) {
814+
const str_ptr = e.getStringAt(i - index);
815+
param_tup[i] = str_ptr;
816+
}
817+
}
818+
} else if (pointer.size == .one and pointer.child == Event) {
819+
param_tup[i] = e;
820+
index += 1;
821+
} else if (pointer.size == .many and pointer.child == u8 and pointer.is_const == true and pointer.sentinel() == null) {
822+
const raw_ptr = e.getRawAt(i - index);
823+
param_tup[i] = raw_ptr;
824+
} else {
809825
const err_msg = std.fmt.comptimePrint(
810-
"the pointer type is ({}), not support other type for pointer param!",
826+
"the pointer type is ({}), now we only support [:0]const u8 or [*]const u8 or *webui.Event !",
811827
.{tt},
812828
);
813829
@compileError(err_msg);
814830
}
815-
if (pointer.sentinel()) |sentinel| {
816-
if (sentinel != 0) {
817-
const err_msg = std.fmt.comptimePrint(
818-
"type is ({}), only support these types: Event, Bool, Int, Float, [:0]const u8, []u8!",
819-
.{tt},
820-
);
821-
@compileError(err_msg);
822-
}
823-
const str_ptr = e.getStringAt(i);
824-
param_tup[i] = str_ptr;
825-
} else {
826-
@compileError("not support []u8");
827-
}
828831
},
829832
else => {
830833
const err_msg = std.fmt.comptimePrint(

0 commit comments

Comments
 (0)