From 794a7ee41068ffe450bf904de504f2cad36d1f21 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Fri, 21 Jul 2023 11:14:25 -0700 Subject: C API: Add BinaryenAddFunctionWithHeapType which takes a heap type (#5829) This is necessary for WasmGC producers using the C API, so that they can set the heap type of functions. Otherwise the heap type is set structurally using params and results in the old API. The old API is kept for backwards compatibility and convenience (for the structural case, which is all code before WasmGC basically). Fixes #5826 --- src/binaryen-c.cpp | 37 ++++++++++++++++++++++++++++--------- src/binaryen-c.h | 9 +++++++++ 2 files changed, 37 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/binaryen-c.cpp b/src/binaryen-c.cpp index d075115e7..dd2ad5c4a 100644 --- a/src/binaryen-c.cpp +++ b/src/binaryen-c.cpp @@ -4909,17 +4909,15 @@ void BinaryenStringSliceIterSetNum(BinaryenExpressionRef expr, // Functions -BinaryenFunctionRef BinaryenAddFunction(BinaryenModuleRef module, - const char* name, - BinaryenType params, - BinaryenType results, - BinaryenType* varTypes, - BinaryenIndex numVarTypes, - BinaryenExpressionRef body) { +static BinaryenFunctionRef addFunctionInternal(BinaryenModuleRef module, + const char* name, + HeapType type, + BinaryenType* varTypes, + BinaryenIndex numVarTypes, + BinaryenExpressionRef body) { auto* ret = new Function; ret->setExplicitName(name); - // TODO: Take a HeapType rather than params and results. - ret->type = Signature(Type(params), Type(results)); + ret->type = type; for (BinaryenIndex i = 0; i < numVarTypes; i++) { ret->vars.push_back(Type(varTypes[i])); } @@ -4934,6 +4932,27 @@ BinaryenFunctionRef BinaryenAddFunction(BinaryenModuleRef module, return ret; } + +BinaryenFunctionRef BinaryenAddFunction(BinaryenModuleRef module, + const char* name, + BinaryenType params, + BinaryenType results, + BinaryenType* varTypes, + BinaryenIndex numVarTypes, + BinaryenExpressionRef body) { + HeapType type = Signature(Type(params), Type(results)); + return addFunctionInternal(module, name, type, varTypes, numVarTypes, body); +} +BinaryenFunctionRef +BinaryenAddFunctionWithHeapType(BinaryenModuleRef module, + const char* name, + BinaryenHeapType type, + BinaryenType* varTypes, + BinaryenIndex numVarTypes, + BinaryenExpressionRef body) { + return addFunctionInternal( + module, name, HeapType(type), varTypes, numVarTypes, body); +} BinaryenFunctionRef BinaryenGetFunction(BinaryenModuleRef module, const char* name) { return ((Module*)module)->getFunctionOrNull(name); diff --git a/src/binaryen-c.h b/src/binaryen-c.h index 07c116aec..75acc20b0 100644 --- a/src/binaryen-c.h +++ b/src/binaryen-c.h @@ -2723,6 +2723,15 @@ BinaryenAddFunction(BinaryenModuleRef module, BinaryenType* varTypes, BinaryenIndex numVarTypes, BinaryenExpressionRef body); +// As BinaryenAddFunction, but takes a HeapType rather than params and results. +// This lets you set the specific type of the function. +BINARYEN_API BinaryenFunctionRef +BinaryenAddFunctionWithHeapType(BinaryenModuleRef module, + const char* name, + BinaryenHeapType type, + BinaryenType* varTypes, + BinaryenIndex numVarTypes, + BinaryenExpressionRef body); // Gets a function reference by name. Returns NULL if the function does not // exist. BINARYEN_API BinaryenFunctionRef BinaryenGetFunction(BinaryenModuleRef module, -- cgit v1.2.3