Skip to content

Commit 284e394

Browse files
COFFEETALEStlively
authored andcommitted
uint32_t instead of int64_t as return type for GetMemorySegmentByteOffset (#2432)
`uint32_t` instead of `int64_t` as return type for `GetMemorySegmentByteOffset` and minor fixes on tests.
1 parent 5ee9e0a commit 284e394

File tree

4 files changed

+21
-22
lines changed

4 files changed

+21
-22
lines changed

src/binaryen-c.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3623,8 +3623,8 @@ uint32_t BinaryenGetNumMemorySegments(BinaryenModuleRef module) {
36233623
auto* wasm = (Module*)module;
36243624
return wasm->memory.segments.size();
36253625
}
3626-
int64_t BinaryenGetMemorySegmentByteOffset(BinaryenModuleRef module,
3627-
BinaryenIndex id) {
3626+
uint32_t BinaryenGetMemorySegmentByteOffset(BinaryenModuleRef module,
3627+
BinaryenIndex id) {
36283628
if (tracing) {
36293629
std::cout << " BinaryenGetMemorySegmentByteOffset(the_module, " << id
36303630
<< ");\n";

src/binaryen-c.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1190,7 +1190,7 @@ BINARYEN_API void BinaryenSetMemory(BinaryenModuleRef module,
11901190
// Memory segments. Query utilities.
11911191

11921192
BINARYEN_API uint32_t BinaryenGetNumMemorySegments(BinaryenModuleRef module);
1193-
BINARYEN_API int64_t
1193+
BINARYEN_API uint32_t
11941194
BinaryenGetMemorySegmentByteOffset(BinaryenModuleRef module, BinaryenIndex id);
11951195
BINARYEN_API size_t BinaryenGetMemorySegmentByteLength(BinaryenModuleRef module,
11961196
BinaryenIndex id);

test/example/c-api-hello-world.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11

22
#include <binaryen-c.h>
33

4-
// "hello world" type example: create a function that adds two i32s and returns the result
4+
// "hello world" type example: create a function that adds two i32s and returns
5+
// the result
56

67
int main() {
78
BinaryenModuleRef module = BinaryenModuleCreate();
89

910
// Create a function type for i32 (i32, i32)
10-
BinaryenType params[2] = { BinaryenTypeInt32(), BinaryenTypeInt32() };
11-
BinaryenFunctionTypeRef iii = BinaryenAddFunctionType(module, "iii", BinaryenTypeInt32(), params, 2);
11+
BinaryenType params[2] = {BinaryenTypeInt32(), BinaryenTypeInt32()};
12+
BinaryenFunctionTypeRef iii =
13+
BinaryenAddFunctionType(module, "iii", BinaryenTypeInt32(), params, 2);
1214

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

1820
// Create the add function
1921
// Note: no additional local variables
20-
// Note: no basic blocks here, we are an AST. The function body is just an expression node.
21-
BinaryenFunctionRef adder = BinaryenAddFunction(module, "adder", iii, NULL, 0, add);
22+
// Note: no basic blocks here, we are an AST. The function body is just an
23+
// expression node.
24+
BinaryenFunctionRef adder =
25+
BinaryenAddFunction(module, "adder", iii, NULL, 0, add);
2226

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

2933
return 0;
3034
}
31-

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

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,28 +1033,24 @@ void test_for_each() {
10331033
assert(BinaryenGetExportByIndex(module, i) == exps[i]);
10341034
}
10351035

1036-
BinaryenAddGlobal(module, "a-global", BinaryenTypeInt32(), 0, makeInt32(module, 125));
1037-
10381036
const char* segments[] = { "hello, world", "segment data 2" };
1037+
const uint32_t expected_offsets[] = { 10, 125 };
10391038
int8_t segmentPassive[] = { 0, 0 };
1039+
BinaryenIndex segmentSizes[] = { 12, 14 };
1040+
10401041
BinaryenExpressionRef segmentOffsets[] = {
1041-
BinaryenConst(module, BinaryenLiteralInt32(10)),
1042+
BinaryenConst(module, BinaryenLiteralInt32(expected_offsets[0])),
10421043
BinaryenGlobalGet(module, "a-global", BinaryenTypeInt32())
10431044
};
1044-
BinaryenIndex segmentSizes[] = { 12, 14 };
10451045
BinaryenSetMemory(module, 1, 256, "mem", segments, segmentPassive, segmentOffsets, segmentSizes, 2, 0);
1046+
BinaryenAddGlobal(module, "a-global", BinaryenTypeInt32(), 0, makeInt32(module, expected_offsets[1]));
10461047

10471048
for (i = 0; i < BinaryenGetNumMemorySegments(module) ; i++) {
1048-
char out[15] = {0};
1049-
assert(BinaryenGetMemorySegmentByteOffset(module, i) == (0==i?10:125));
1049+
char out[15] = {};
1050+
assert(BinaryenGetMemorySegmentByteOffset(module, i) == expected_offsets[i]);
10501051
assert(BinaryenGetMemorySegmentByteLength(module, i) == segmentSizes[i]);
1051-
BinaryenCopyMemorySegmentData(module, i, &out[0]);
1052-
if (0 == i) {
1053-
assert(0 == strcmp("hello, world", &out[0]));
1054-
}
1055-
else {
1056-
assert(0 == strcmp("segment data 2", &out[0]));
1057-
}
1052+
BinaryenCopyMemorySegmentData(module, i, out);
1053+
assert(0 == strcmp(segments[i], out));
10581054
}
10591055
}
10601056
BinaryenModulePrint(module);

0 commit comments

Comments
 (0)