From 90bfd394da9d5f6275ea45031d8d840b1ad4e92e Mon Sep 17 00:00:00 2001 From: "liang.he@intel.com" Date: Thu, 16 Oct 2025 02:36:36 +0000 Subject: [PATCH] feat: enhance generate_checked_function to support variadic arguments and update Result handling --- ci/generate_checked_functions.py | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/ci/generate_checked_functions.py b/ci/generate_checked_functions.py index 5310822e6..bc6383285 100644 --- a/ci/generate_checked_functions.py +++ b/ci/generate_checked_functions.py @@ -2,11 +2,6 @@ from pycparser import c_parser, c_ast, parse_file import os -# Helper function to determine if a parameter is a pointer -def is_pointer(param): - return isinstance(param.type, c_ast.PtrDecl) - - # Updated generate_checked_function to dynamically update Result definition for new return types @@ -45,10 +40,13 @@ def generate_checked_function(func): new_func.append(") {") # Add null checks for pointer parameters + has_variadic = False for param in params: if isinstance(param, c_ast.EllipsisParam): - continue # Skip variadic arguments - if is_pointer(param): + # Restructure to use va_list + new_func.append(" va_list args;") + has_variadic = True + elif isinstance(param.type, c_ast.PtrDecl): new_func.append(f" if ({param.name} == NULL) {{") new_func.append(f" Result res = {{ .error_code = -1 }};") new_func.append(f" return res;") @@ -58,6 +56,19 @@ def generate_checked_function(func): if return_type == "void": new_func.append(f" {func_name}({', '.join(param_list)});") new_func.append(f" Result res = {{ .error_code = 0 }};") + elif has_variadic: + new_func.append(" va_start(args, " + param_list[-2] + ");") + new_func.append( + f" {return_type} original_result = {func_name}({', '.join(param_list[:-1])}, args);" + ) + new_func.append(" va_end(args);") + new_func.append(f" Result res;") + new_func.append(f" if (original_result == 0) {{") + new_func.append(f" res.error_code = 0;") + new_func.append(f" res.value.{return_type}_value = original_result;") + new_func.append(f" }} else {{") + new_func.append(f" res.error_code = -2;") + new_func.append(f" }}") else: new_func.append( f" {return_type} original_result = {func_name}({', '.join(param_list)});" @@ -151,7 +162,10 @@ def process_header(): f.write("#include \n") f.write("#include \n") f.write("#include \n") - f.write('#include "wasm_export.h"\n\n') + f.write("\n") + f.write('#include "wasm_export.h"\n') + f.write('#include "lib_export.h"\n') + f.write("\n") # Write the updated Result struct f.write(RESULT_STRUCT + "\n")