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
4 changes: 4 additions & 0 deletions src/binaryen-c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5878,6 +5878,10 @@ BinaryenType BinaryenFunctionGetVar(BinaryenFunctionRef func,
assert(index < vars.size());
return vars[index].getID();
}
BinaryenIndex BinaryenFunctionAddVar(BinaryenFunctionRef func,
BinaryenType type) {
return Builder::addVar((Function*)func, (Type)type);
}
BinaryenIndex BinaryenFunctionGetNumLocals(BinaryenFunctionRef func) {
return ((Function*)func)->getNumLocals();
}
Expand Down
4 changes: 4 additions & 0 deletions src/binaryen-c.h
Original file line number Diff line number Diff line change
Expand Up @@ -3197,6 +3197,10 @@ BINARYEN_API BinaryenIndex BinaryenFunctionGetNumVars(BinaryenFunctionRef func);
// specified `Function`.
BINARYEN_API BinaryenType BinaryenFunctionGetVar(BinaryenFunctionRef func,
BinaryenIndex index);
// Appends a local variable to the specified `Function`, returning its
// index.
BINARYEN_API BinaryenIndex BinaryenFunctionAddVar(BinaryenFunctionRef func,
BinaryenType type);
// Gets the number of locals within the specified function. Includes parameters.
BINARYEN_API BinaryenIndex
BinaryenFunctionGetNumLocals(BinaryenFunctionRef func);
Expand Down
10 changes: 10 additions & 0 deletions test/example/c-api-kitchen-sink.c
Original file line number Diff line number Diff line change
Expand Up @@ -1417,6 +1417,16 @@ void test_core() {
BinaryenFunctionRef sinker = BinaryenAddFunction(
module, "kitchen()sinker", iIfF, BinaryenTypeInt32(), localTypes, 2, body);

BinaryenIndex numLocals = BinaryenFunctionGetNumLocals(sinker);
BinaryenIndex numParams =
BinaryenTypeArity(BinaryenFunctionGetParams(sinker));
BinaryenIndex newLocalIdx =
BinaryenFunctionAddVar(sinker, BinaryenTypeFloat32());
assert(newLocalIdx == numLocals);
assert(BinaryenFunctionGetNumLocals(sinker) == numLocals + 1);
assert(BinaryenFunctionGetVar(sinker, newLocalIdx - numParams) ==
BinaryenTypeFloat32());

Copy link
Member

Choose a reason for hiding this comment

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

We can also check the number of vars was incremented properly.

Suggested change
assert(BinaryenFunctionGetNumLocals(sinker) == numLocals + 1);

// Globals

BinaryenAddGlobal(
Expand Down
1 change: 1 addition & 0 deletions test/example/c-api-kitchen-sink.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ BinaryenFeatureAll: 131071
(func $"kitchen()sinker" (type $2) (param $0 i32) (param $1 i64) (param $2 f32) (param $3 f64) (result i32)
(local $4 i32)
(local $5 externref)
(local $6 f32)
(block $the-body (result i32)
(block $the-nothing
(drop
Expand Down