summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorAlon Zakai <azakai@google.com>2024-07-09 10:06:15 -0700
committerGitHub <noreply@github.com>2024-07-09 10:06:15 -0700
commit0750bdbc1f7c356a160493acdb8dc314a68f1f12 (patch)
tree982d3597fd2aef1bfa48acf79cf27403de79541f /test
parent081f28b337e50ff6837d580fed61254e92abe76a (diff)
downloadbinaryen-0750bdbc1f7c356a160493acdb8dc314a68f1f12.tar.gz
binaryen-0750bdbc1f7c356a160493acdb8dc314a68f1f12.tar.bz2
binaryen-0750bdbc1f7c356a160493acdb8dc314a68f1f12.zip
[C API] Add APIs for getting/setting function types, and a CallRef example (#6721)
Fixes #6718
Diffstat (limited to 'test')
-rw-r--r--test/example/c-api-kitchen-sink.c37
-rw-r--r--test/example/c-api-kitchen-sink.txt10
2 files changed, 47 insertions, 0 deletions
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)
+ )
+ )
+)