Skip to content

Commit f7db9e9

Browse files
dcodeIOkripken
authored andcommitted
Add initial/maximum table size parameters to C/JS API (#1687)
1 parent ad9152e commit f7db9e9

File tree

8 files changed

+18
-16
lines changed

8 files changed

+18
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ Current Trunk
1515
### BREAKING CHANGES
1616

1717
- `BinaryenSetFunctionTable` in the C API no longer accepts an array of functions, instead it accepts an array of function names, `const char** funcNames`. Previously, you could not include imported functions because they are of type `BinaryenImportRef` instead of `BinaryenFunctionRef`. [#1650](https://github.com/WebAssembly/binaryen/pull/1650)
18+
- `BinaryenSetFunctionTable` in the C API now expects the initial and maximum table size as additional parameters, like `BinaryenSetMemory` does for pages, so tables can be grown dynamically. [#1687](https://github.com/WebAssembly/binaryen/pull/1687)

src/binaryen-c.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,7 +1778,7 @@ void BinaryenRemoveExport(BinaryenModuleRef module, const char* externalName) {
17781778

17791779
// Function table. One per module
17801780

1781-
void BinaryenSetFunctionTable(BinaryenModuleRef module, const char** funcNames, BinaryenIndex numFuncNames) {
1781+
void BinaryenSetFunctionTable(BinaryenModuleRef module, BinaryenIndex initial, BinaryenIndex maximum, const char** funcNames, BinaryenIndex numFuncNames) {
17821782
if (tracing) {
17831783
std::cout << " {\n";
17841784
std::cout << " const char* funcNames[] = { ";
@@ -1787,18 +1787,19 @@ void BinaryenSetFunctionTable(BinaryenModuleRef module, const char** funcNames,
17871787
std::cout << "\"" << funcNames[i] << "\"";
17881788
}
17891789
std::cout << " };\n";
1790-
std::cout << " BinaryenSetFunctionTable(the_module, funcNames, " << numFuncNames << ");\n";
1790+
std::cout << " BinaryenSetFunctionTable(the_module, " << initial << ", " << maximum << ", funcNames, " << numFuncNames << ");\n";
17911791
std::cout << " }\n";
17921792
}
17931793

17941794
auto* wasm = (Module*)module;
1795-
wasm->table.exists = true;
17961795
Table::Segment segment(wasm->allocator.alloc<Const>()->set(Literal(int32_t(0))));
17971796
for (BinaryenIndex i = 0; i < numFuncNames; i++) {
17981797
segment.data.push_back(funcNames[i]);
17991798
}
1799+
wasm->table.initial = initial;
1800+
wasm->table.max = maximum;
1801+
wasm->table.exists = true;
18001802
wasm->table.segments.push_back(segment);
1801-
wasm->table.initial = wasm->table.max = numFuncNames;
18021803
}
18031804

18041805
// Memory. One per module

src/binaryen-c.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ BinaryenGlobalRef BinaryenAddGlobal(BinaryenModuleRef module, const char* name,
616616

617617
// Function table. One per module
618618

619-
void BinaryenSetFunctionTable(BinaryenModuleRef module, const char** funcNames, BinaryenIndex numFuncNames);
619+
void BinaryenSetFunctionTable(BinaryenModuleRef module, BinaryenIndex initial, BinaryenIndex maximum, const char** funcNames, BinaryenIndex numFuncNames);
620620

621621
// Memory. One per module
622622

src/js/binaryen.js-post.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,11 +1112,12 @@ Module['Module'] = function(module) {
11121112
return Module['_BinaryenRemoveExport'](module, strToStack(externalName));
11131113
});
11141114
};
1115-
this['setFunctionTable'] = function(funcNames) {
1115+
this['setFunctionTable'] = function(initial, maximum, funcNames) {
11161116
return preserveStack(function() {
1117-
return Module['_BinaryenSetFunctionTable'](module, i32sToStack(
1118-
funcNames.map(strToStack)
1119-
), funcNames.length);
1117+
return Module['_BinaryenSetFunctionTable'](module, initial, maximum,
1118+
i32sToStack(funcNames.map(strToStack)),
1119+
funcNames.length
1120+
);
11201121
});
11211122
};
11221123
this['setMemory'] = function(initial, maximum, exportName, segments) {

test/binaryen.js/kitchen-sink.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
// kitchen sink, tests the full API
32

43
function cleanInfo(info) {
@@ -249,7 +248,7 @@ function test_core() {
249248

250249
// Function table. One per module
251250

252-
module.setFunctionTable([ Binaryen.getFunctionInfo(sinker).name ]);
251+
module.setFunctionTable(1, 0xffffffff, [ Binaryen.getFunctionInfo(sinker).name ]);
253252

254253
// Memory. One per module
255254

test/binaryen.js/kitchen-sink.js.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ getExpressionInfo(f64.const)={"id":14,"type":4,"value":9.5}
4848
(type $3 (func))
4949
(memory $0 1 256)
5050
(data (i32.const 10) "hello, world")
51-
(table 1 1 anyfunc)
51+
(table 1 anyfunc)
5252
(elem (i32.const 0) "$kitchen()sinker")
5353
(import "module" "base" (func $an-imported (param i32 f64) (result f32)))
5454
(export "kitchen_sinker" (func "$kitchen()sinker"))
@@ -1470,7 +1470,7 @@ getExpressionInfo(f64.const)={"id":14,"type":4,"value":9.5}
14701470
BinaryenFunctionGetBody(functions[0]);
14711471
{
14721472
const char* funcNames[] = { "kitchen()sinker" };
1473-
BinaryenSetFunctionTable(the_module, funcNames, 1);
1473+
BinaryenSetFunctionTable(the_module, 1, 4294967295, funcNames, 1);
14741474
}
14751475
expressions[256] = BinaryenConst(the_module, BinaryenLiteralInt32(10));
14761476
{
@@ -1504,7 +1504,7 @@ getExpressionInfo(f64.const)={"id":14,"type":4,"value":9.5}
15041504
(type $3 (func))
15051505
(memory $0 1 256)
15061506
(data (i32.const 10) "hello, world")
1507-
(table 1 1 anyfunc)
1507+
(table 1 anyfunc)
15081508
(elem (i32.const 0) "$kitchen()sinker")
15091509
(import "module" "base" (func $an-imported (param i32 f64) (result f32)))
15101510
(export "kitchen_sinker" (func "$kitchen()sinker"))

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ void test_core() {
254254

255255
// Function table. One per module
256256
const char* funcNames[] = { BinaryenFunctionGetName(sinker) };
257-
BinaryenSetFunctionTable(module, funcNames, 1);
257+
BinaryenSetFunctionTable(module, 1, 1, funcNames, 1);
258258

259259
// Memory. One per module
260260

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1403,7 +1403,7 @@ int main() {
14031403
BinaryenFunctionGetName(functions[0]);
14041404
{
14051405
const char* funcNames[] = { "kitchen()sinker" };
1406-
BinaryenSetFunctionTable(the_module, funcNames, 1);
1406+
BinaryenSetFunctionTable(the_module, 1, 1, funcNames, 1);
14071407
}
14081408
expressions[254] = BinaryenConst(the_module, BinaryenLiteralInt32(10));
14091409
{

0 commit comments

Comments
 (0)