Skip to content

Commit 003cb7d

Browse files
committed
feat: enhance generate_checked_function to support variadic arguments and update Result handling
1 parent 0c26dc1 commit 003cb7d

1 file changed

Lines changed: 22 additions & 8 deletions

File tree

ci/generate_checked_functions.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22
import os
33

44

5-
# Helper function to determine if a parameter is a pointer
6-
def is_pointer(param):
7-
return isinstance(param.type, c_ast.PtrDecl)
8-
9-
105
# Updated generate_checked_function to dynamically update Result definition for new return types
116

127

@@ -45,10 +40,13 @@ def generate_checked_function(func):
4540
new_func.append(") {")
4641

4742
# Add null checks for pointer parameters
43+
has_variadic = False
4844
for param in params:
4945
if isinstance(param, c_ast.EllipsisParam):
50-
continue # Skip variadic arguments
51-
if is_pointer(param):
46+
# Restructure to use va_list
47+
new_func.append(" va_list args;")
48+
has_variadic = True
49+
elif isinstance(param.type, c_ast.PtrDecl):
5250
new_func.append(f" if ({param.name} == NULL) {{")
5351
new_func.append(f" Result res = {{ .error_code = -1 }};")
5452
new_func.append(f" return res;")
@@ -58,6 +56,19 @@ def generate_checked_function(func):
5856
if return_type == "void":
5957
new_func.append(f" {func_name}({', '.join(param_list)});")
6058
new_func.append(f" Result res = {{ .error_code = 0 }};")
59+
elif has_variadic:
60+
new_func.append(" va_start(args, " + param_list[-2] + ");")
61+
new_func.append(
62+
f" {return_type} original_result = {func_name}({', '.join(param_list[:-1])}, args);"
63+
)
64+
new_func.append(" va_end(args);")
65+
new_func.append(f" Result res;")
66+
new_func.append(f" if (original_result == 0) {{")
67+
new_func.append(f" res.error_code = 0;")
68+
new_func.append(f" res.value.{return_type}_value = original_result;")
69+
new_func.append(f" }} else {{")
70+
new_func.append(f" res.error_code = -2;")
71+
new_func.append(f" }}")
6172
else:
6273
new_func.append(
6374
f" {return_type} original_result = {func_name}({', '.join(param_list)});"
@@ -151,7 +162,10 @@ def process_header():
151162
f.write("#include <stdbool.h>\n")
152163
f.write("#include <stdint.h>\n")
153164
f.write("#include <stdlib.h>\n")
154-
f.write('#include "wasm_export.h"\n\n')
165+
f.write("\n")
166+
f.write('#include "wasm_export.h"\n')
167+
f.write('#include "lib_export.h"\n')
168+
f.write("\n")
155169

156170
# Write the updated Result struct
157171
f.write(RESULT_STRUCT + "\n")

0 commit comments

Comments
 (0)