Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/binaryen-c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5682,6 +5682,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);
Expand Down
5 changes: 5 additions & 0 deletions src/binaryen-c.h
Original file line number Diff line number Diff line change
Expand Up @@ -3134,6 +3134,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.
Expand Down
37 changes: 37 additions & 0 deletions test/example/c-api-kitchen-sink.c
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would you think about putting this test in a gtest file? Compilation errors in the gtests are caught during the normal build and the tests run much faster, so they seem strictly better than the example tests.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree this file might make sense as a gtest, yeah. There was the idea of having special "example" tests that are helpful for people to see code samples of the public API (as opposed to testing of internals, or corner cases of the public API), which is what "example" was meant to invoke, but I suppose we could make gtest/example and document that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, gtest/example would be great.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I started to do this but then realized a big missing feature of our gtests atm, which is that they don't work with the auto updater script. So tests like this that match the text format output would need to be manually updated if we ever change the text format. Before we move any significant amount of such tests to gtest we should probably add auto-updating.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Which I agree is worth doing, but not trivial.)

Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -2207,6 +2243,7 @@ int main() {
test_for_each();
test_func_opt();
test_typebuilder();
test_callref_and_types();

return 0;
}
10 changes: 10 additions & 0 deletions test/example/c-api-kitchen-sink.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
)
)