Skip to content

Commit f564245

Browse files
committed
fixes for BinaryenSetFunctionTable taking array of names
1 parent b1b0180 commit f564245

File tree

6 files changed

+40
-32
lines changed

6 files changed

+40
-32
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ Current Trunk
1414

1515
### BREAKING CHANGES
1616

17-
- `BinaryenSetFunctionTable` 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)
17+
- `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)

src/binaryen-c.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ static PassOptions globalPassOptions = PassOptions::getWithDefaultOptimizationOp
7777

7878
static int tracing = 0;
7979

80-
void traceNameOrNULL(const char *name) {
80+
void traceNameOrNULL(const char* name) {
8181
if (name) std::cout << "\"" << name << "\"";
8282
else std::cout << "NULL";
8383
}
@@ -459,7 +459,7 @@ BinaryenExpressionRef BinaryenBreak(BinaryenModuleRef module, const char* name,
459459

460460
return static_cast<Expression*>(ret);
461461
}
462-
BinaryenExpressionRef BinaryenSwitch(BinaryenModuleRef module, const char **names, BinaryenIndex numNames, const char* defaultName, BinaryenExpressionRef condition, BinaryenExpressionRef value) {
462+
BinaryenExpressionRef BinaryenSwitch(BinaryenModuleRef module, const char** names, BinaryenIndex numNames, const char* defaultName, BinaryenExpressionRef condition, BinaryenExpressionRef value) {
463463
auto* ret = ((Module*)module)->allocator.alloc<Switch>();
464464

465465
if (tracing) {
@@ -485,7 +485,7 @@ BinaryenExpressionRef BinaryenSwitch(BinaryenModuleRef module, const char **name
485485
ret->finalize();
486486
return static_cast<Expression*>(ret);
487487
}
488-
BinaryenExpressionRef BinaryenCall(BinaryenModuleRef module, const char *target, BinaryenExpressionRef* operands, BinaryenIndex numOperands, BinaryenType returnType) {
488+
BinaryenExpressionRef BinaryenCall(BinaryenModuleRef module, const char* target, BinaryenExpressionRef* operands, BinaryenIndex numOperands, BinaryenType returnType) {
489489
auto* ret = ((Module*)module)->allocator.alloc<Call>();
490490

491491
if (tracing) {
@@ -510,7 +510,7 @@ BinaryenExpressionRef BinaryenCall(BinaryenModuleRef module, const char *target,
510510
ret->finalize();
511511
return static_cast<Expression*>(ret);
512512
}
513-
BinaryenExpressionRef BinaryenCallImport(BinaryenModuleRef module, const char *target, BinaryenExpressionRef* operands, BinaryenIndex numOperands, BinaryenType returnType) {
513+
BinaryenExpressionRef BinaryenCallImport(BinaryenModuleRef module, const char* target, BinaryenExpressionRef* operands, BinaryenIndex numOperands, BinaryenType returnType) {
514514
auto* ret = ((Module*)module)->allocator.alloc<CallImport>();
515515

516516
if (tracing) {
@@ -603,7 +603,7 @@ BinaryenExpressionRef BinaryenTeeLocal(BinaryenModuleRef module, BinaryenIndex i
603603
ret->finalize();
604604
return static_cast<Expression*>(ret);
605605
}
606-
BinaryenExpressionRef BinaryenGetGlobal(BinaryenModuleRef module, const char *name, BinaryenType type) {
606+
BinaryenExpressionRef BinaryenGetGlobal(BinaryenModuleRef module, const char* name, BinaryenType type) {
607607
auto* ret = ((Module*)module)->allocator.alloc<GetGlobal>();
608608

609609
if (tracing) {
@@ -616,7 +616,7 @@ BinaryenExpressionRef BinaryenGetGlobal(BinaryenModuleRef module, const char *na
616616
ret->finalize();
617617
return static_cast<Expression*>(ret);
618618
}
619-
BinaryenExpressionRef BinaryenSetGlobal(BinaryenModuleRef module, const char *name, BinaryenExpressionRef value) {
619+
BinaryenExpressionRef BinaryenSetGlobal(BinaryenModuleRef module, const char* name, BinaryenExpressionRef value) {
620620
auto* ret = ((Module*)module)->allocator.alloc<SetGlobal>();
621621

622622
if (tracing) {
@@ -1704,10 +1704,10 @@ BinaryenGlobalRef BinaryenAddGlobal(BinaryenModuleRef module, const char* name,
17041704

17051705
// Imports
17061706

1707-
WASM_DEPRECATED BinaryenImportRef BinaryenAddImport(BinaryenModuleRef module, const char* internalName, const char* externalModuleName, const char *externalBaseName, BinaryenFunctionTypeRef type) {
1707+
WASM_DEPRECATED BinaryenImportRef BinaryenAddImport(BinaryenModuleRef module, const char* internalName, const char* externalModuleName, const char* externalBaseName, BinaryenFunctionTypeRef type) {
17081708
return BinaryenAddFunctionImport(module, internalName, externalModuleName, externalBaseName, type);
17091709
}
1710-
BinaryenImportRef BinaryenAddFunctionImport(BinaryenModuleRef module, const char* internalName, const char* externalModuleName, const char *externalBaseName, BinaryenFunctionTypeRef functionType) {
1710+
BinaryenImportRef BinaryenAddFunctionImport(BinaryenModuleRef module, const char* internalName, const char* externalModuleName, const char* externalBaseName, BinaryenFunctionTypeRef functionType) {
17111711
auto* ret = new Import();
17121712
auto* wasm = (Module*)module;
17131713

@@ -1882,7 +1882,7 @@ void BinaryenRemoveExport(BinaryenModuleRef module, const char* externalName) {
18821882

18831883
// Function table. One per module
18841884

1885-
void BinaryenSetFunctionTable(BinaryenModuleRef module, const char **funcNames, BinaryenIndex numFuncNames) {
1885+
void BinaryenSetFunctionTable(BinaryenModuleRef module, const char** funcNames, BinaryenIndex numFuncNames) {
18861886
if (tracing) {
18871887
std::cout << " {\n";
18881888
std::cout << " const char* funcNames[] = { ";
@@ -1907,7 +1907,7 @@ void BinaryenSetFunctionTable(BinaryenModuleRef module, const char **funcNames,
19071907

19081908
// Memory. One per module
19091909

1910-
void BinaryenSetMemory(BinaryenModuleRef module, BinaryenIndex initial, BinaryenIndex maximum, const char* exportName, const char **segments, BinaryenExpressionRef* segmentOffsets, BinaryenIndex* segmentSizes, BinaryenIndex numSegments) {
1910+
void BinaryenSetMemory(BinaryenModuleRef module, BinaryenIndex initial, BinaryenIndex maximum, const char* exportName, const char** segments, BinaryenExpressionRef* segmentOffsets, BinaryenIndex* segmentSizes, BinaryenIndex numSegments) {
19111911
if (tracing) {
19121912
std::cout << " {\n";
19131913
for (BinaryenIndex i = 0; i < numSegments; i++) {
@@ -2087,7 +2087,7 @@ void BinaryenSetDebugInfo(int on) {
20872087
globalPassOptions.debugInfo = on != 0;
20882088
}
20892089

2090-
void BinaryenModuleRunPasses(BinaryenModuleRef module, const char **passes, BinaryenIndex numPasses) {
2090+
void BinaryenModuleRunPasses(BinaryenModuleRef module, const char** passes, BinaryenIndex numPasses) {
20912091
if (tracing) {
20922092
std::cout << " {\n";
20932093
std::cout << " const char* passes[] = { ";
@@ -2346,7 +2346,7 @@ void BinaryenFunctionOptimize(BinaryenFunctionRef func, BinaryenModuleRef module
23462346
passRunner.addDefaultOptimizationPasses();
23472347
passRunner.runOnFunction((Function*)func);
23482348
}
2349-
void BinaryenFunctionRunPasses(BinaryenFunctionRef func, BinaryenModuleRef module, const char **passes, BinaryenIndex numPasses) {
2349+
void BinaryenFunctionRunPasses(BinaryenFunctionRef func, BinaryenModuleRef module, const char** passes, BinaryenIndex numPasses) {
23502350
if (tracing) {
23512351
std::cout << " {\n";
23522352
std::cout << " const char* passes[] = { ";

src/binaryen-c.h

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -340,16 +340,16 @@ BinaryenExpressionRef BinaryenLoop(BinaryenModuleRef module, const char* in, Bin
340340
// Break: value and condition can be NULL
341341
BinaryenExpressionRef BinaryenBreak(BinaryenModuleRef module, const char* name, BinaryenExpressionRef condition, BinaryenExpressionRef value);
342342
// Switch: value can be NULL
343-
BinaryenExpressionRef BinaryenSwitch(BinaryenModuleRef module, const char **names, BinaryenIndex numNames, const char* defaultName, BinaryenExpressionRef condition, BinaryenExpressionRef value);
343+
BinaryenExpressionRef BinaryenSwitch(BinaryenModuleRef module, const char** names, BinaryenIndex numNames, const char* defaultName, BinaryenExpressionRef condition, BinaryenExpressionRef value);
344344
// Call, CallImport: Note the 'returnType' parameter. You must declare the
345345
// type returned by the function being called, as that
346346
// function might not have been created yet, so we don't
347347
// know what it is.
348348
// Also note that WebAssembly does not differentiate
349349
// between Call and CallImport, but Binaryen does, so you
350350
// must use CallImport if calling an import, and vice versa.
351-
BinaryenExpressionRef BinaryenCall(BinaryenModuleRef module, const char *target, BinaryenExpressionRef* operands, BinaryenIndex numOperands, BinaryenType returnType);
352-
BinaryenExpressionRef BinaryenCallImport(BinaryenModuleRef module, const char *target, BinaryenExpressionRef* operands, BinaryenIndex numOperands, BinaryenType returnType);
351+
BinaryenExpressionRef BinaryenCall(BinaryenModuleRef module, const char* target, BinaryenExpressionRef* operands, BinaryenIndex numOperands, BinaryenType returnType);
352+
BinaryenExpressionRef BinaryenCallImport(BinaryenModuleRef module, const char* target, BinaryenExpressionRef* operands, BinaryenIndex numOperands, BinaryenType returnType);
353353
BinaryenExpressionRef BinaryenCallIndirect(BinaryenModuleRef module, BinaryenExpressionRef target, BinaryenExpressionRef* operands, BinaryenIndex numOperands, const char* type);
354354
// GetLocal: Note the 'type' parameter. It might seem redundant, since the
355355
// local at that index must have a type. However, this API lets you
@@ -368,8 +368,8 @@ BinaryenExpressionRef BinaryenCallIndirect(BinaryenModuleRef module, BinaryenExp
368368
BinaryenExpressionRef BinaryenGetLocal(BinaryenModuleRef module, BinaryenIndex index, BinaryenType type);
369369
BinaryenExpressionRef BinaryenSetLocal(BinaryenModuleRef module, BinaryenIndex index, BinaryenExpressionRef value);
370370
BinaryenExpressionRef BinaryenTeeLocal(BinaryenModuleRef module, BinaryenIndex index, BinaryenExpressionRef value);
371-
BinaryenExpressionRef BinaryenGetGlobal(BinaryenModuleRef module, const char *name, BinaryenType type);
372-
BinaryenExpressionRef BinaryenSetGlobal(BinaryenModuleRef module, const char *name, BinaryenExpressionRef value);
371+
BinaryenExpressionRef BinaryenGetGlobal(BinaryenModuleRef module, const char* name, BinaryenType type);
372+
BinaryenExpressionRef BinaryenSetGlobal(BinaryenModuleRef module, const char* name, BinaryenExpressionRef value);
373373
// Load: align can be 0, in which case it will be the natural alignment (equal to bytes)
374374
BinaryenExpressionRef BinaryenLoad(BinaryenModuleRef module, uint32_t bytes, int8_t signed_, uint32_t offset, uint32_t align, BinaryenType type, BinaryenExpressionRef ptr);
375375
// Store: align can be 0, in which case it will be the natural alignment (equal to bytes)
@@ -608,11 +608,11 @@ void BinaryenRemoveFunction(BinaryenModuleRef module, const char* name);
608608

609609
typedef void* BinaryenImportRef;
610610

611-
WASM_DEPRECATED BinaryenImportRef BinaryenAddImport(BinaryenModuleRef module, const char* internalName, const char* externalModuleName, const char *externalBaseName, BinaryenFunctionTypeRef type);
612-
BinaryenImportRef BinaryenAddFunctionImport(BinaryenModuleRef module, const char* internalName, const char* externalModuleName, const char *externalBaseName, BinaryenFunctionTypeRef functionType);
613-
BinaryenImportRef BinaryenAddTableImport(BinaryenModuleRef module, const char* internalName, const char* externalModuleName, const char *externalBaseName);
614-
BinaryenImportRef BinaryenAddMemoryImport(BinaryenModuleRef module, const char* internalName, const char* externalModuleName, const char *externalBaseName);
615-
BinaryenImportRef BinaryenAddGlobalImport(BinaryenModuleRef module, const char* internalName, const char* externalModuleName, const char *externalBaseName, BinaryenType globalType);
611+
WASM_DEPRECATED BinaryenImportRef BinaryenAddImport(BinaryenModuleRef module, const char* internalName, const char* externalModuleName, const char* externalBaseName, BinaryenFunctionTypeRef type);
612+
BinaryenImportRef BinaryenAddFunctionImport(BinaryenModuleRef module, const char* internalName, const char* externalModuleName, const char* externalBaseName, BinaryenFunctionTypeRef functionType);
613+
BinaryenImportRef BinaryenAddTableImport(BinaryenModuleRef module, const char* internalName, const char* externalModuleName, const char* externalBaseName);
614+
BinaryenImportRef BinaryenAddMemoryImport(BinaryenModuleRef module, const char* internalName, const char* externalModuleName, const char* externalBaseName);
615+
BinaryenImportRef BinaryenAddGlobalImport(BinaryenModuleRef module, const char* internalName, const char* externalModuleName, const char* externalBaseName, BinaryenType globalType);
616616
void BinaryenRemoveImport(BinaryenModuleRef module, const char* internalName);
617617

618618
// Exports
@@ -634,13 +634,13 @@ BinaryenGlobalRef BinaryenAddGlobal(BinaryenModuleRef module, const char* name,
634634

635635
// Function table. One per module
636636

637-
void BinaryenSetFunctionTable(BinaryenModuleRef module, const char **funcNames, BinaryenIndex numFuncNames);
637+
void BinaryenSetFunctionTable(BinaryenModuleRef module, const char** funcNames, BinaryenIndex numFuncNames);
638638

639639
// Memory. One per module
640640

641641
// Each segment has data in segments, a start offset in segmentOffsets, and a size in segmentSizes.
642642
// exportName can be NULL
643-
void BinaryenSetMemory(BinaryenModuleRef module, BinaryenIndex initial, BinaryenIndex maximum, const char* exportName, const char **segments, BinaryenExpressionRef* segmentOffsets, BinaryenIndex* segmentSizes, BinaryenIndex numSegments);
643+
void BinaryenSetMemory(BinaryenModuleRef module, BinaryenIndex initial, BinaryenIndex maximum, const char* exportName, const char** segments, BinaryenExpressionRef* segmentOffsets, BinaryenIndex* segmentSizes, BinaryenIndex numSegments);
644644

645645
// Start function. One per module
646646

@@ -693,7 +693,7 @@ void BinaryenSetDebugInfo(int on);
693693

694694
// Runs the specified passes on the module. Uses the currently set global
695695
// optimize and shrink level.
696-
void BinaryenModuleRunPasses(BinaryenModuleRef module, const char **passes, BinaryenIndex numPasses);
696+
void BinaryenModuleRunPasses(BinaryenModuleRef module, const char** passes, BinaryenIndex numPasses);
697697

698698
// Auto-generate drop() operations where needed. This lets you generate code without
699699
// worrying about where they are needed. (It is more efficient to do it yourself,
@@ -783,7 +783,7 @@ void BinaryenFunctionOptimize(BinaryenFunctionRef func, BinaryenModuleRef module
783783

784784
// Runs the specified passes on the function. Uses the currently set global
785785
// optimize and shrink level.
786-
void BinaryenFunctionRunPasses(BinaryenFunctionRef func, BinaryenModuleRef module, const char **passes, BinaryenIndex numPasses);
786+
void BinaryenFunctionRunPasses(BinaryenFunctionRef func, BinaryenModuleRef module, const char** passes, BinaryenIndex numPasses);
787787

788788
// Sets the debug location of the specified `Expression` within the specified `Function`.
789789
void BinaryenFunctionSetDebugLocation(BinaryenFunctionRef func, BinaryenExpressionRef expr, BinaryenIndex fileIndex, BinaryenIndex lineNumber, BinaryenIndex columnNumber);

src/js/binaryen.js-post.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,9 +1129,17 @@ Module['Module'] = function(module) {
11291129
return Module['_BinaryenRemoveExport'](module, strToStack(externalName));
11301130
});
11311131
};
1132-
this['setFunctionTable'] = function(funcs) {
1132+
this['setFunctionTable'] = function(funcNames) {
11331133
return preserveStack(function() {
1134-
return Module['_BinaryenSetFunctionTable'](module, i32sToStack(funcs), funcs.length);
1134+
return Module['_BinaryenSetFunctionTable'](
1135+
module,
1136+
i32sToStack(
1137+
funcNames.map(function(funcName) {
1138+
return allocate(funcName, 'i8', ALLOC_STACK);
1139+
})
1140+
),
1141+
funcNames.length
1142+
);
11351143
});
11361144
};
11371145
this['setMemory'] = function(initial, maximum, exportName, segments) {

test/binaryen.js/kitchen-sink.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ function test_core() {
250250

251251
// Function table. One per module
252252

253-
module.setFunctionTable([ module.getFunctionInfo(sinker).name ]);
253+
module.setFunctionTable([ Binaryen.getFunctionInfo(sinker).name ]);
254254

255255
// Memory. One per module
256256

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,12 +253,12 @@ void test_core() {
253253
BinaryenAddFunctionExport(module, "kitchen()sinker", "kitchen_sinker");
254254

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

259259
// Memory. One per module
260260

261-
const char *segments[] = { "hello, world" };
261+
const char* segments[] = { "hello, world" };
262262
BinaryenExpressionRef segmentOffsets[] = { BinaryenConst(module, BinaryenLiteralInt32(10)) };
263263
BinaryenIndex segmentSizes[] = { 12 };
264264
BinaryenSetMemory(module, 1, 256, "mem", segments, segmentOffsets, segmentSizes, 1);

0 commit comments

Comments
 (0)