Skip to content

Commit 0750bdb

Browse files
authored
[C API] Add APIs for getting/setting function types, and a CallRef example (#6721)
Fixes #6718
1 parent 081f28b commit 0750bdb

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

src/binaryen-c.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5684,6 +5684,12 @@ void BinaryenFunctionSetBody(BinaryenFunctionRef func,
56845684
assert(body);
56855685
((Function*)func)->body = (Expression*)body;
56865686
}
5687+
BinaryenHeapType BinaryenFunctionGetType(BinaryenFunctionRef func) {
5688+
return ((Function*)func)->type.getID();
5689+
}
5690+
void BinaryenFunctionSetType(BinaryenFunctionRef func, BinaryenHeapType type) {
5691+
((Function*)func)->type = HeapType(type);
5692+
}
56875693
void BinaryenFunctionOptimize(BinaryenFunctionRef func,
56885694
BinaryenModuleRef module) {
56895695
PassRunner passRunner((Module*)module);

src/binaryen-c.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3136,6 +3136,11 @@ BinaryenFunctionGetBody(BinaryenFunctionRef func);
31363136
// Sets the body of the specified `Function`.
31373137
BINARYEN_API void BinaryenFunctionSetBody(BinaryenFunctionRef func,
31383138
BinaryenExpressionRef body);
3139+
// Gets the type of the specified `Function`.
3140+
BINARYEN_API BinaryenHeapType BinaryenFunctionGetType(BinaryenFunctionRef func);
3141+
// Sets the type of the specified `Function`.
3142+
BINARYEN_API void BinaryenFunctionSetType(BinaryenFunctionRef func,
3143+
BinaryenHeapType type);
31393144

31403145
// Runs the standard optimization passes on the function. Uses the currently set
31413146
// global optimize and shrink level.

test/example/c-api-kitchen-sink.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2194,6 +2194,42 @@ void test_typebuilder() {
21942194
BinaryenModuleDispose(module);
21952195
}
21962196

2197+
void test_callref_and_types() {
2198+
BinaryenModuleRef module = BinaryenModuleCreate();
2199+
BinaryenModuleSetFeatures(module, BinaryenFeatureAll());
2200+
2201+
// Create a tiny function.
2202+
BinaryenFunctionRef tiny = BinaryenAddFunction(module,
2203+
"tiny",
2204+
BinaryenTypeNone(),
2205+
BinaryenTypeNone(),
2206+
NULL,
2207+
0,
2208+
BinaryenNop(module));
2209+
2210+
// Get a non-nullable type with that function's heap type.
2211+
BinaryenHeapType funcType =
2212+
BinaryenTypeFromHeapType(BinaryenFunctionGetType(tiny), false);
2213+
2214+
// Add a CallRef with that function and that type. Note that the RefFunc must
2215+
// use that type (and not generic funcref, as in the IR the type must always
2216+
// be precise).
2217+
BinaryenExpressionRef callRef =
2218+
BinaryenCallRef(module,
2219+
BinaryenRefFunc(module, "tiny", funcType),
2220+
NULL,
2221+
0,
2222+
BinaryenTypeNone(),
2223+
false);
2224+
BinaryenFunctionSetBody(tiny, callRef);
2225+
2226+
bool didValidate = BinaryenModuleValidate(module);
2227+
assert(didValidate);
2228+
printf("module with a call_ref:\n");
2229+
BinaryenModulePrint(module);
2230+
BinaryenModuleDispose(module);
2231+
}
2232+
21972233
int main() {
21982234
test_types();
21992235
test_features();
@@ -2207,6 +2243,7 @@ int main() {
22072243
test_for_each();
22082244
test_func_opt();
22092245
test_typebuilder();
2246+
test_callref_and_types();
22102247

22112248
return 0;
22122249
}

test/example/c-api-kitchen-sink.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3053,3 +3053,13 @@ module with recursive GC types:
30533053
(unreachable)
30543054
)
30553055
)
3056+
module with a call_ref:
3057+
(module
3058+
(type $0 (func))
3059+
(elem declare func $tiny)
3060+
(func $tiny (type $0)
3061+
(call_ref $0
3062+
(ref.func $tiny)
3063+
)
3064+
)
3065+
)

0 commit comments

Comments
 (0)