summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <azakai@google.com>2023-07-21 11:14:25 -0700
committerGitHub <noreply@github.com>2023-07-21 18:14:25 +0000
commit794a7ee41068ffe450bf904de504f2cad36d1f21 (patch)
tree189e38c991dd7273e9cf548923a987fe3f4eecab
parent0d79590340237214bccfca6a73ad11b3728f26fc (diff)
downloadbinaryen-794a7ee41068ffe450bf904de504f2cad36d1f21.tar.gz
binaryen-794a7ee41068ffe450bf904de504f2cad36d1f21.tar.bz2
binaryen-794a7ee41068ffe450bf904de504f2cad36d1f21.zip
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
-rw-r--r--CHANGELOG.md3
-rw-r--r--src/binaryen-c.cpp37
-rw-r--r--src/binaryen-c.h9
-rw-r--r--test/example/c-api-kitchen-sink.c3
-rw-r--r--test/example/c-api-kitchen-sink.txt5
5 files changed, 47 insertions, 10 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 69948d3f9..62a7af564 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -17,6 +17,9 @@ Current Trunk
- Add a pass to reorder functions by name, which can be useful for debugging
(e.g. comparisons after optimizations), `--reorder-functions-by-name`.
+- C API: Add BinaryenAddFunctionWithHeapType() which is like BinaryenAddFunction
+ but takes a heap type. The old function is kept for backwards compatibility
+ and as a convenience.
v114
----
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,
diff --git a/test/example/c-api-kitchen-sink.c b/test/example/c-api-kitchen-sink.c
index cb26c372d..36e6776f7 100644
--- a/test/example/c-api-kitchen-sink.c
+++ b/test/example/c-api-kitchen-sink.c
@@ -2309,6 +2309,9 @@ void test_typebuilder() {
varTypes,
4,
BinaryenNop(module));
+ // Also test adding a function using the HeapType-using API.
+ BinaryenAddFunctionWithHeapType(
+ module, "test2", signatureHeapType, NULL, 0, BinaryenUnreachable(module));
}
bool didValidate = BinaryenModuleValidate(module);
assert(didValidate);
diff --git a/test/example/c-api-kitchen-sink.txt b/test/example/c-api-kitchen-sink.txt
index 75a4d1d8d..1276a5a6a 100644
--- a/test/example/c-api-kitchen-sink.txt
+++ b/test/example/c-api-kitchen-sink.txt
@@ -3167,8 +3167,8 @@ TypeBuilderErrorReasonForwardChildReference: 3
module with recursive GC types:
(module
(type $SomeArray (array (mut (ref null $SomeArray))))
- (type $SomeStruct (struct (field $SomeField (mut (ref null $SomeStruct)))))
(type $SomeSignature (func (param (ref null $SomeSignature) (ref null $SomeArray)) (result (ref null $SomeSignature))))
+ (type $SomeStruct (struct (field $SomeField (mut (ref null $SomeStruct)))))
(type $none_=>_none (func))
(type $SomeSubStruct (sub $SomeStruct (struct (field $SomeField (mut (ref null $SomeStruct))) (field $SomePackedField i8))))
(func $test (type $none_=>_none)
@@ -3178,4 +3178,7 @@ module with recursive GC types:
(local $3 (ref null $SomeSubStruct))
(nop)
)
+ (func $test2 (type $SomeSignature) (param $0 (ref null $SomeSignature)) (param $1 (ref null $SomeArray)) (result (ref null $SomeSignature))
+ (unreachable)
+ )
)