summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/binaryen-c.cpp6
-rw-r--r--src/binaryen-c.h5
-rw-r--r--test/example/c-api-kitchen-sink.c37
-rw-r--r--test/example/c-api-kitchen-sink.txt10
4 files changed, 58 insertions, 0 deletions
diff --git a/src/binaryen-c.cpp b/src/binaryen-c.cpp
index 9c9a1c54f..be80206f5 100644
--- a/src/binaryen-c.cpp
+++ b/src/binaryen-c.cpp
@@ -5684,6 +5684,12 @@ void BinaryenFunctionSetBody(BinaryenFunctionRef func,
assert(body);
((Function*)func)->body = (Expression*)body;
}
+BinaryenHeapType BinaryenFunctionGetType(BinaryenFunctionRef func) {
+ return ((Function*)func)->type.getID();
+}
+void BinaryenFunctionSetType(BinaryenFunctionRef func, BinaryenHeapType type) {
+ ((Function*)func)->type = HeapType(type);
+}
void BinaryenFunctionOptimize(BinaryenFunctionRef func,
BinaryenModuleRef module) {
PassRunner passRunner((Module*)module);
diff --git a/src/binaryen-c.h b/src/binaryen-c.h
index 45230a117..35f1d8eb8 100644
--- a/src/binaryen-c.h
+++ b/src/binaryen-c.h
@@ -3136,6 +3136,11 @@ BinaryenFunctionGetBody(BinaryenFunctionRef func);
// Sets the body of the specified `Function`.
BINARYEN_API void BinaryenFunctionSetBody(BinaryenFunctionRef func,
BinaryenExpressionRef body);
+// Gets the type of the specified `Function`.
+BINARYEN_API BinaryenHeapType BinaryenFunctionGetType(BinaryenFunctionRef func);
+// Sets the type of the specified `Function`.
+BINARYEN_API void BinaryenFunctionSetType(BinaryenFunctionRef func,
+ BinaryenHeapType type);
// Runs the standard optimization passes on the function. Uses the currently set
// global optimize and shrink level.
diff --git a/test/example/c-api-kitchen-sink.c b/test/example/c-api-kitchen-sink.c
index cd305c69b..802789173 100644
--- a/test/example/c-api-kitchen-sink.c
+++ b/test/example/c-api-kitchen-sink.c
@@ -2194,6 +2194,42 @@ void test_typebuilder() {
BinaryenModuleDispose(module);
}
+void test_callref_and_types() {
+ BinaryenModuleRef module = BinaryenModuleCreate();
+ BinaryenModuleSetFeatures(module, BinaryenFeatureAll());
+
+ // Create a tiny function.
+ BinaryenFunctionRef tiny = BinaryenAddFunction(module,
+ "tiny",
+ BinaryenTypeNone(),
+ BinaryenTypeNone(),
+ NULL,
+ 0,
+ BinaryenNop(module));
+
+ // Get a non-nullable type with that function's heap type.
+ BinaryenHeapType funcType =
+ BinaryenTypeFromHeapType(BinaryenFunctionGetType(tiny), false);
+
+ // Add a CallRef with that function and that type. Note that the RefFunc must
+ // use that type (and not generic funcref, as in the IR the type must always
+ // be precise).
+ BinaryenExpressionRef callRef =
+ BinaryenCallRef(module,
+ BinaryenRefFunc(module, "tiny", funcType),
+ NULL,
+ 0,
+ BinaryenTypeNone(),
+ false);
+ BinaryenFunctionSetBody(tiny, callRef);
+
+ bool didValidate = BinaryenModuleValidate(module);
+ assert(didValidate);
+ printf("module with a call_ref:\n");
+ BinaryenModulePrint(module);
+ BinaryenModuleDispose(module);
+}
+
int main() {
test_types();
test_features();
@@ -2207,6 +2243,7 @@ int main() {
test_for_each();
test_func_opt();
test_typebuilder();
+ test_callref_and_types();
return 0;
}
diff --git a/test/example/c-api-kitchen-sink.txt b/test/example/c-api-kitchen-sink.txt
index 140d8e948..e568cfba0 100644
--- a/test/example/c-api-kitchen-sink.txt
+++ b/test/example/c-api-kitchen-sink.txt
@@ -3053,3 +3053,13 @@ module with recursive GC types:
(unreachable)
)
)
+module with a call_ref:
+(module
+ (type $0 (func))
+ (elem declare func $tiny)
+ (func $tiny (type $0)
+ (call_ref $0
+ (ref.func $tiny)
+ )
+ )
+)