mirror of
https://github.com/bytecodealliance/wasm-micro-runtime.git
synced 2025-11-28 02:20:50 +00:00
refactor: Make Result struct definition locally and remove dynamic type addition
This commit is contained in:
parent
2860ead566
commit
a94ca0b5d3
|
|
@ -1,19 +1,6 @@
|
||||||
from pycparser import c_parser, c_ast, parse_file
|
from pycparser import c_parser, c_ast, parse_file
|
||||||
import os
|
import os
|
||||||
|
|
||||||
# Define the Result struct as a string
|
|
||||||
RESULT_STRUCT = """
|
|
||||||
typedef struct {
|
|
||||||
int error_code; // Error code (0 for success, non-zero for errors)
|
|
||||||
union {
|
|
||||||
bool bool_value;
|
|
||||||
void *ptr_value;
|
|
||||||
int int_value;
|
|
||||||
// Add other types as needed
|
|
||||||
} value;
|
|
||||||
} Result;
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
# Helper function to determine if a parameter is a pointer
|
# Helper function to determine if a parameter is a pointer
|
||||||
def is_pointer(param):
|
def is_pointer(param):
|
||||||
|
|
@ -24,8 +11,6 @@ def is_pointer(param):
|
||||||
|
|
||||||
|
|
||||||
def generate_checked_function(func):
|
def generate_checked_function(func):
|
||||||
global RESULT_STRUCT # Access the global Result definition
|
|
||||||
|
|
||||||
func_name = func.name # Access the name directly from Decl
|
func_name = func.name # Access the name directly from Decl
|
||||||
new_func_name = f"{func_name}_checked"
|
new_func_name = f"{func_name}_checked"
|
||||||
|
|
||||||
|
|
@ -37,14 +22,6 @@ def generate_checked_function(func):
|
||||||
if isinstance(func.type.type, c_ast.TypeDecl):
|
if isinstance(func.type.type, c_ast.TypeDecl):
|
||||||
return_type = " ".join(func.type.type.type.names)
|
return_type = " ".join(func.type.type.type.names)
|
||||||
|
|
||||||
# Check if the return type is already in Result, if not, add it
|
|
||||||
if return_type not in ["bool", "void*", "int", "uint32_t"]:
|
|
||||||
# Add a new field to the Result struct dynamically
|
|
||||||
RESULT_STRUCT = RESULT_STRUCT.replace(
|
|
||||||
"// Add other types as needed",
|
|
||||||
f" {return_type} {return_type}_value;\n // Add other types as needed",
|
|
||||||
)
|
|
||||||
|
|
||||||
# Start building the new function
|
# Start building the new function
|
||||||
new_func = [f"Result {new_func_name}("]
|
new_func = [f"Result {new_func_name}("]
|
||||||
param_list = []
|
param_list = []
|
||||||
|
|
@ -110,7 +87,15 @@ def generate_checked_function(func):
|
||||||
|
|
||||||
|
|
||||||
def process_header():
|
def process_header():
|
||||||
global RESULT_STRUCT # Access the global Result definition
|
# Define the Result struct as a string
|
||||||
|
RESULT_STRUCT = """
|
||||||
|
typedef struct {
|
||||||
|
int error_code; // Error code (0 for success, non-zero for errors)
|
||||||
|
union {
|
||||||
|
// Add other types as needed
|
||||||
|
} value;
|
||||||
|
} Result;
|
||||||
|
"""
|
||||||
|
|
||||||
# Based on current file location, adjust the path to the header file
|
# Based on current file location, adjust the path to the header file
|
||||||
input_header = os.path.join(
|
input_header = os.path.join(
|
||||||
|
|
@ -156,7 +141,6 @@ def process_header():
|
||||||
|
|
||||||
# Update the Result struct with all return types
|
# Update the Result struct with all return types
|
||||||
for return_type in return_types:
|
for return_type in return_types:
|
||||||
if return_type not in ["bool", "void*", "int", "uint32_t"]:
|
|
||||||
RESULT_STRUCT = RESULT_STRUCT.replace(
|
RESULT_STRUCT = RESULT_STRUCT.replace(
|
||||||
"// Add other types as needed",
|
"// Add other types as needed",
|
||||||
f" {return_type} {return_type}_value;\n // Add other types as needed",
|
f" {return_type} {return_type}_value;\n // Add other types as needed",
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user