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: 2 additions & 2 deletions src/binaryen-c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3623,8 +3623,8 @@ uint32_t BinaryenGetNumMemorySegments(BinaryenModuleRef module) {
auto* wasm = (Module*)module;
return wasm->memory.segments.size();
}
int64_t BinaryenGetMemorySegmentByteOffset(BinaryenModuleRef module,
BinaryenIndex id) {
uint32_t BinaryenGetMemorySegmentByteOffset(BinaryenModuleRef module,
BinaryenIndex id) {
if (tracing) {
std::cout << " BinaryenGetMemorySegmentByteOffset(the_module, " << id
<< ");\n";
Expand Down
2 changes: 1 addition & 1 deletion src/binaryen-c.h
Original file line number Diff line number Diff line change
Expand Up @@ -1190,7 +1190,7 @@ BINARYEN_API void BinaryenSetMemory(BinaryenModuleRef module,
// Memory segments. Query utilities.

BINARYEN_API uint32_t BinaryenGetNumMemorySegments(BinaryenModuleRef module);
BINARYEN_API int64_t
BINARYEN_API uint32_t
BinaryenGetMemorySegmentByteOffset(BinaryenModuleRef module, BinaryenIndex id);
BINARYEN_API size_t BinaryenGetMemorySegmentByteLength(BinaryenModuleRef module,
BinaryenIndex id);
Expand Down
15 changes: 9 additions & 6 deletions test/example/c-api-hello-world.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@

#include <binaryen-c.h>

// "hello world" type example: create a function that adds two i32s and returns the result
// "hello world" type example: create a function that adds two i32s and returns
// the result

int main() {
BinaryenModuleRef module = BinaryenModuleCreate();

// Create a function type for i32 (i32, i32)
BinaryenType params[2] = { BinaryenTypeInt32(), BinaryenTypeInt32() };
BinaryenFunctionTypeRef iii = BinaryenAddFunctionType(module, "iii", BinaryenTypeInt32(), params, 2);
BinaryenType params[2] = {BinaryenTypeInt32(), BinaryenTypeInt32()};
BinaryenFunctionTypeRef iii =
BinaryenAddFunctionType(module, "iii", BinaryenTypeInt32(), params, 2);

// Get the 0 and 1 arguments, and add them
BinaryenExpressionRef x = BinaryenLocalGet(module, 0, BinaryenTypeInt32()),
Expand All @@ -17,8 +19,10 @@ int main() {

// Create the add function
// Note: no additional local variables
// Note: no basic blocks here, we are an AST. The function body is just an expression node.
BinaryenFunctionRef adder = BinaryenAddFunction(module, "adder", iii, NULL, 0, add);
// Note: no basic blocks here, we are an AST. The function body is just an
// expression node.
BinaryenFunctionRef adder =
BinaryenAddFunction(module, "adder", iii, NULL, 0, add);

// Print it out
BinaryenModulePrint(module);
Expand All @@ -28,4 +32,3 @@ int main() {

return 0;
}

22 changes: 9 additions & 13 deletions test/example/c-api-kitchen-sink.c
Original file line number Diff line number Diff line change
Expand Up @@ -1033,28 +1033,24 @@ void test_for_each() {
assert(BinaryenGetExportByIndex(module, i) == exps[i]);
}

BinaryenAddGlobal(module, "a-global", BinaryenTypeInt32(), 0, makeInt32(module, 125));

const char* segments[] = { "hello, world", "segment data 2" };
const uint32_t expected_offsets[] = { 10, 125 };
int8_t segmentPassive[] = { 0, 0 };
BinaryenIndex segmentSizes[] = { 12, 14 };

BinaryenExpressionRef segmentOffsets[] = {
BinaryenConst(module, BinaryenLiteralInt32(10)),
BinaryenConst(module, BinaryenLiteralInt32(expected_offsets[0])),
BinaryenGlobalGet(module, "a-global", BinaryenTypeInt32())
};
BinaryenIndex segmentSizes[] = { 12, 14 };
BinaryenSetMemory(module, 1, 256, "mem", segments, segmentPassive, segmentOffsets, segmentSizes, 2, 0);
BinaryenAddGlobal(module, "a-global", BinaryenTypeInt32(), 0, makeInt32(module, expected_offsets[1]));

for (i = 0; i < BinaryenGetNumMemorySegments(module) ; i++) {
char out[15] = {0};
assert(BinaryenGetMemorySegmentByteOffset(module, i) == (0==i?10:125));
char out[15] = {};
assert(BinaryenGetMemorySegmentByteOffset(module, i) == expected_offsets[i]);
assert(BinaryenGetMemorySegmentByteLength(module, i) == segmentSizes[i]);
BinaryenCopyMemorySegmentData(module, i, &out[0]);
if (0 == i) {
assert(0 == strcmp("hello, world", &out[0]));
}
else {
assert(0 == strcmp("segment data 2", &out[0]));
}
BinaryenCopyMemorySegmentData(module, i, out);
assert(0 == strcmp(segments[i], out));
}
}
BinaryenModulePrint(module);
Expand Down