Skip to content

Commit b3f6791

Browse files
authored
Slightly improve table C API (#3604)
Uses BinaryenIndex instead of int to mirror parameter types in table construction, and adds setters for name, initial and max.
1 parent 54d13b1 commit b3f6791

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

src/binaryen-c.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3923,13 +3923,24 @@ void BinaryenFunctionSetDebugLocation(BinaryenFunctionRef func,
39233923
const char* BinaryenTableGetName(BinaryenTableRef table) {
39243924
return ((Table*)table)->name.c_str();
39253925
}
3926-
int BinaryenTableGetInitial(BinaryenTableRef table) {
3926+
void BinaryenTableSetName(BinaryenTableRef table, const char* name) {
3927+
((Table*)table)->name = name;
3928+
}
3929+
BinaryenIndex BinaryenTableGetInitial(BinaryenTableRef table) {
39273930
return ((Table*)table)->initial;
39283931
}
3932+
void BinaryenTableSetInitial(BinaryenTableRef table, BinaryenIndex initial) {
3933+
((Table*)table)->initial = initial;
3934+
}
39293935
int BinaryenTableHasMax(BinaryenTableRef table) {
39303936
return ((Table*)table)->hasMax();
39313937
}
3932-
int BinaryenTableGetMax(BinaryenTableRef table) { return ((Table*)table)->max; }
3938+
BinaryenIndex BinaryenTableGetMax(BinaryenTableRef table) {
3939+
return ((Table*)table)->max;
3940+
}
3941+
void BinaryenTableSetMax(BinaryenTableRef table, BinaryenIndex max) {
3942+
((Table*)table)->max = max;
3943+
}
39333944

39343945
//
39353946
// =========== Global operations ===========

src/binaryen-c.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2381,10 +2381,23 @@ BINARYEN_API void BinaryenFunctionSetDebugLocation(BinaryenFunctionRef func,
23812381
// ========== Table Operations ==========
23822382
//
23832383

2384+
// Gets the name of the specified `Table`.
23842385
BINARYEN_API const char* BinaryenTableGetName(BinaryenTableRef table);
2385-
BINARYEN_API int BinaryenTableGetInitial(BinaryenTableRef table);
2386+
// Sets the name of the specified `Table`.
2387+
BINARYEN_API void BinaryenTableSetName(BinaryenTableRef table,
2388+
const char* name);
2389+
// Gets the initial number of pages of the specified `Table`.
2390+
BINARYEN_API BinaryenIndex BinaryenTableGetInitial(BinaryenTableRef table);
2391+
// Sets the initial number of pages of the specified `Table`.
2392+
BINARYEN_API void BinaryenTableSetInitial(BinaryenTableRef table,
2393+
BinaryenIndex initial);
2394+
// Tests whether the specified `Table` has a maximum number of pages.
23862395
BINARYEN_API int BinaryenTableHasMax(BinaryenTableRef table);
2387-
BINARYEN_API int BinaryenTableGetMax(BinaryenTableRef table);
2396+
// Gets the maximum number of pages of the specified `Table`.
2397+
BINARYEN_API BinaryenIndex BinaryenTableGetMax(BinaryenTableRef table);
2398+
// Sets the maximum number of pages of the specified `Table`.
2399+
BINARYEN_API void BinaryenTableSetMax(BinaryenTableRef table,
2400+
BinaryenIndex max);
23882401

23892402
//
23902403
// ========== Global Operations ==========

test/example/c-api-multiple-tables.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,15 @@ int main() {
5151
assert(t2 != NULL);
5252

5353
assert(strcmp(BinaryenTableGetName(t2), "t2") == 0);
54+
BinaryenTableSetName(t2, "table2");
55+
assert(strcmp(BinaryenTableGetName(t2), "table2") == 0);
5456
assert(BinaryenTableGetInitial(t2) == 1);
57+
BinaryenTableSetInitial(t2, 2);
58+
assert(BinaryenTableGetInitial(t2) == 2);
5559
assert(BinaryenTableHasMax(t2) == 1);
5660
assert(BinaryenTableGetMax(t2) == 1);
61+
BinaryenTableSetMax(t2, 2);
62+
assert(BinaryenTableGetMax(t2) == 2);
5763
assert(strcmp(BinaryenTableImportGetModule(t2), "") == 0);
5864
assert(strcmp(BinaryenTableImportGetBase(t2), "") == 0);
5965

test/example/c-api-multiple-tables.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
33
(table $tab 1 1 funcref)
44
(elem (table $tab) (i32.const 0) func $adder)
5-
(table $t2 1 1 funcref)
6-
(elem (table $t2) (i32.const 0) func $adder)
5+
(table $table2 2 2 funcref)
6+
(elem (table $table2) (i32.const 0) func $adder)
77
(func $adder (param $0 i32) (param $1 i32) (result i32)
88
(i32.add
99
(local.get $0)

0 commit comments

Comments
 (0)