Skip to content

Commit 3e5ce64

Browse files
authored
Improve C and JS API module inspection features (WebAssembly#3464)
* BinaryenGetFunction, BinaryenGetGlobal, BinaryenGetEvent now return NULL if an element does not exist * Adds BinaryenGetExport, BinaryenGetNumGlobals, BinaryenGetGlobalByIndex * Corrects BinaryenGetNumFunctions return type * Adds related descriptions of C API functions
1 parent 3b52424 commit 3e5ce64

File tree

6 files changed

+96
-38
lines changed

6 files changed

+96
-38
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ Current Trunk
2727
This can be revived if needed from git history (#3261).
2828
- Make `NUM_PARAMS` in `FuncCastEmulation` a runtime configuration option named
2929
`max-func-params`. This defaults to the original value of 16.
30+
- `BinaryenGetFunction`, `BinaryenGetGlobal` and `BinaryenGetEvent` now return
31+
`NULL` instead of aborting when the respective element does not yet exist.
3032

3133
v98
3234
---

src/binaryen-c.cpp

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3011,21 +3011,21 @@ BinaryenFunctionRef BinaryenAddFunction(BinaryenModuleRef module,
30113011
}
30123012
BinaryenFunctionRef BinaryenGetFunction(BinaryenModuleRef module,
30133013
const char* name) {
3014-
return ((Module*)module)->getFunction(name);
3014+
return ((Module*)module)->getFunctionOrNull(name);
30153015
}
30163016
void BinaryenRemoveFunction(BinaryenModuleRef module, const char* name) {
30173017
((Module*)module)->removeFunction(name);
30183018
}
3019-
uint32_t BinaryenGetNumFunctions(BinaryenModuleRef module) {
3019+
BinaryenIndex BinaryenGetNumFunctions(BinaryenModuleRef module) {
30203020
return ((Module*)module)->functions.size();
30213021
}
30223022
BinaryenFunctionRef BinaryenGetFunctionByIndex(BinaryenModuleRef module,
3023-
BinaryenIndex id) {
3023+
BinaryenIndex index) {
30243024
const auto& functions = ((Module*)module)->functions;
3025-
if (functions.size() <= id) {
3026-
Fatal() << "invalid function id.";
3025+
if (functions.size() <= index) {
3026+
Fatal() << "invalid function index.";
30273027
}
3028-
return functions[id].get();
3028+
return functions[index].get();
30293029
}
30303030

30313031
// Globals
@@ -3045,11 +3045,22 @@ BinaryenGlobalRef BinaryenAddGlobal(BinaryenModuleRef module,
30453045
}
30463046
BinaryenGlobalRef BinaryenGetGlobal(BinaryenModuleRef module,
30473047
const char* name) {
3048-
return ((Module*)module)->getGlobal(name);
3048+
return ((Module*)module)->getGlobalOrNull(name);
30493049
}
30503050
void BinaryenRemoveGlobal(BinaryenModuleRef module, const char* name) {
30513051
((Module*)module)->removeGlobal(name);
30523052
}
3053+
BinaryenIndex BinaryenGetNumGlobals(BinaryenModuleRef module) {
3054+
return ((Module*)module)->globals.size();
3055+
}
3056+
BinaryenGlobalRef BinaryenGetGlobalByIndex(BinaryenModuleRef module,
3057+
BinaryenIndex index) {
3058+
const auto& globals = ((Module*)module)->globals;
3059+
if (globals.size() <= index) {
3060+
Fatal() << "invalid global index.";
3061+
}
3062+
return globals[index].get();
3063+
}
30533064

30543065
// Events
30553066

@@ -3067,7 +3078,7 @@ BinaryenEventRef BinaryenAddEvent(BinaryenModuleRef module,
30673078
}
30683079

30693080
BinaryenEventRef BinaryenGetEvent(BinaryenModuleRef module, const char* name) {
3070-
return ((Module*)module)->getEvent(name);
3081+
return ((Module*)module)->getEventOrNull(name);
30713082
}
30723083
void BinaryenRemoveEvent(BinaryenModuleRef module, const char* name) {
30733084
((Module*)module)->removeEvent(name);
@@ -3192,9 +3203,24 @@ BinaryenExportRef BinaryenAddEventExport(BinaryenModuleRef module,
31923203
((Module*)module)->addExport(ret);
31933204
return ret;
31943205
}
3206+
BinaryenExportRef BinaryenGetExport(BinaryenModuleRef module,
3207+
const char* externalName) {
3208+
return ((Module*)module)->getExportOrNull(externalName);
3209+
}
31953210
void BinaryenRemoveExport(BinaryenModuleRef module, const char* externalName) {
31963211
((Module*)module)->removeExport(externalName);
31973212
}
3213+
BinaryenIndex BinaryenGetNumExports(BinaryenModuleRef module) {
3214+
return ((Module*)module)->exports.size();
3215+
}
3216+
BinaryenExportRef BinaryenGetExportByIndex(BinaryenModuleRef module,
3217+
BinaryenIndex index) {
3218+
const auto& exports = ((Module*)module)->exports;
3219+
if (exports.size() <= index) {
3220+
Fatal() << "invalid export index.";
3221+
}
3222+
return exports[index].get();
3223+
}
31983224

31993225
// Function table. One per module
32003226

@@ -3812,17 +3838,6 @@ const char* BinaryenExportGetName(BinaryenExportRef export_) {
38123838
const char* BinaryenExportGetValue(BinaryenExportRef export_) {
38133839
return ((Export*)export_)->value.c_str();
38143840
}
3815-
uint32_t BinaryenGetNumExports(BinaryenModuleRef module) {
3816-
return ((Module*)module)->exports.size();
3817-
}
3818-
BinaryenExportRef BinaryenGetExportByIndex(BinaryenModuleRef module,
3819-
BinaryenIndex id) {
3820-
const auto& exports = ((Module*)module)->exports;
3821-
if (exports.size() <= id) {
3822-
Fatal() << "invalid export id.";
3823-
}
3824-
return exports[id].get();
3825-
}
38263841

38273842
//
38283843
// ========= Custom sections =========

src/binaryen-c.h

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,18 +1872,19 @@ BinaryenAddFunction(BinaryenModuleRef module,
18721872
BinaryenType* varTypes,
18731873
BinaryenIndex numVarTypes,
18741874
BinaryenExpressionRef body);
1875-
// Gets a function reference by name.
1875+
// Gets a function reference by name. Returns NULL if the function does not
1876+
// exist.
18761877
BINARYEN_API BinaryenFunctionRef BinaryenGetFunction(BinaryenModuleRef module,
18771878
const char* name);
18781879
// Removes a function by name.
18791880
BINARYEN_API void BinaryenRemoveFunction(BinaryenModuleRef module,
18801881
const char* name);
18811882

18821883
// Gets the number of functions in the module.
1883-
BINARYEN_API uint32_t BinaryenGetNumFunctions(BinaryenModuleRef module);
1884-
// Get function pointer from its index.
1884+
BINARYEN_API BinaryenIndex BinaryenGetNumFunctions(BinaryenModuleRef module);
1885+
// Gets the function at the specified index.
18851886
BINARYEN_API BinaryenFunctionRef
1886-
BinaryenGetFunctionByIndex(BinaryenModuleRef module, BinaryenIndex id);
1887+
BinaryenGetFunctionByIndex(BinaryenModuleRef module, BinaryenIndex index);
18871888

18881889
// Imports
18891890

@@ -1923,47 +1924,72 @@ BINARYEN_REF(Export);
19231924
WASM_DEPRECATED BinaryenExportRef BinaryenAddExport(BinaryenModuleRef module,
19241925
const char* internalName,
19251926
const char* externalName);
1927+
// Adds a function export to the module.
19261928
BINARYEN_API BinaryenExportRef BinaryenAddFunctionExport(
19271929
BinaryenModuleRef module, const char* internalName, const char* externalName);
1930+
// Adds a table export to the module.
19281931
BINARYEN_API BinaryenExportRef BinaryenAddTableExport(BinaryenModuleRef module,
19291932
const char* internalName,
19301933
const char* externalName);
1934+
// Adds a memory export to the module.
19311935
BINARYEN_API BinaryenExportRef BinaryenAddMemoryExport(
19321936
BinaryenModuleRef module, const char* internalName, const char* externalName);
1937+
// Adds a global export to the module.
19331938
BINARYEN_API BinaryenExportRef BinaryenAddGlobalExport(
19341939
BinaryenModuleRef module, const char* internalName, const char* externalName);
1940+
// Adds an event export to the module.
19351941
BINARYEN_API BinaryenExportRef BinaryenAddEventExport(BinaryenModuleRef module,
19361942
const char* internalName,
19371943
const char* externalName);
1944+
// Gets an export reference by external name. Returns NULL if the export does
1945+
// not exist.
1946+
BINARYEN_API BinaryenExportRef BinaryenGetExport(BinaryenModuleRef module,
1947+
const char* externalName);
1948+
// Removes an export by external name.
19381949
BINARYEN_API void BinaryenRemoveExport(BinaryenModuleRef module,
19391950
const char* externalName);
1951+
// Gets the number of exports in the module.
1952+
BINARYEN_API BinaryenIndex BinaryenGetNumExports(BinaryenModuleRef module);
1953+
// Gets the export at the specified index.
1954+
BINARYEN_API BinaryenExportRef
1955+
BinaryenGetExportByIndex(BinaryenModuleRef module, BinaryenIndex index);
19401956

19411957
// Globals
19421958

19431959
BINARYEN_REF(Global);
19441960

1961+
// Adds a global to the module.
19451962
BINARYEN_API BinaryenGlobalRef BinaryenAddGlobal(BinaryenModuleRef module,
19461963
const char* name,
19471964
BinaryenType type,
19481965
int8_t mutable_,
19491966
BinaryenExpressionRef init);
1950-
// Gets a global reference by name.
1967+
// Gets a global reference by name. Returns NULL if the global does not exist.
19511968
BINARYEN_API BinaryenGlobalRef BinaryenGetGlobal(BinaryenModuleRef module,
19521969
const char* name);
1970+
// Removes a global by name.
19531971
BINARYEN_API void BinaryenRemoveGlobal(BinaryenModuleRef module,
19541972
const char* name);
1973+
// Gets the number of globals in the module.
1974+
BINARYEN_API BinaryenIndex BinaryenGetNumGlobals(BinaryenModuleRef module);
1975+
// Gets the global at the specified index.
1976+
BINARYEN_API BinaryenGlobalRef
1977+
BinaryenGetGlobalByIndex(BinaryenModuleRef module, BinaryenIndex index);
19551978

19561979
// Events
19571980

19581981
BINARYEN_REF(Event);
19591982

1983+
// Adds an event to the module.
19601984
BINARYEN_API BinaryenEventRef BinaryenAddEvent(BinaryenModuleRef module,
19611985
const char* name,
19621986
uint32_t attribute,
19631987
BinaryenType params,
19641988
BinaryenType results);
1989+
// Gets an event reference by name. Returns NULL if the event does not exist.
19651990
BINARYEN_API BinaryenEventRef BinaryenGetEvent(BinaryenModuleRef module,
19661991
const char* name);
1992+
// Removes an event by name.
19671993
BINARYEN_API void BinaryenRemoveEvent(BinaryenModuleRef module,
19681994
const char* name);
19691995

@@ -2331,11 +2357,6 @@ BinaryenExportGetKind(BinaryenExportRef export_);
23312357
BINARYEN_API const char* BinaryenExportGetName(BinaryenExportRef export_);
23322358
// Gets the internal name of the specified export.
23332359
BINARYEN_API const char* BinaryenExportGetValue(BinaryenExportRef export_);
2334-
// Gets the number of exports in the module.
2335-
BINARYEN_API uint32_t BinaryenGetNumExports(BinaryenModuleRef module);
2336-
// Get export pointer from its index.
2337-
BINARYEN_API BinaryenExportRef
2338-
BinaryenGetExportByIndex(BinaryenModuleRef module, BinaryenIndex id);
23392360

23402361
//
23412362
// ========= Custom sections =========

src/js/binaryen.js-post.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2340,18 +2340,27 @@ function wrapModule(module, self = {}) {
23402340
Module['_BinaryenAddCustomSection'](module, strToStack(name), i8sToStack(contents), contents.length)
23412341
);
23422342
};
2343+
self['getExport'] = function(externalName) {
2344+
return preserveStack(() => Module['_BinaryenGetExport'](module, strToStack(externalName)));
2345+
};
23432346
self['getNumExports'] = function() {
23442347
return Module['_BinaryenGetNumExports'](module);
2345-
}
2346-
self['getExportByIndex'] = function(id) {
2347-
return Module['_BinaryenGetExportByIndex'](module, id);
2348-
}
2348+
};
2349+
self['getExportByIndex'] = function(index) {
2350+
return Module['_BinaryenGetExportByIndex'](module, index);
2351+
};
23492352
self['getNumFunctions'] = function() {
23502353
return Module['_BinaryenGetNumFunctions'](module);
2351-
}
2352-
self['getFunctionByIndex'] = function(id) {
2353-
return Module['_BinaryenGetFunctionByIndex'](module, id);
2354-
}
2354+
};
2355+
self['getFunctionByIndex'] = function(index) {
2356+
return Module['_BinaryenGetFunctionByIndex'](module, index);
2357+
};
2358+
self['getNumGlobals'] = function() {
2359+
return Module['_BinaryenGetNumGlobals'](module);
2360+
};
2361+
self['getGlobalByIndex'] = function(index) {
2362+
return Module['_BinaryenGetGlobalByIndex'](module, index);
2363+
};
23552364
self['emitText'] = function() {
23562365
const old = out;
23572366
let ret = '';

test/binaryen.js/kitchen-sink.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1035,7 +1035,16 @@ function test_for_each() {
10351035
var expected_data = ["hello, world", "segment data 2"];
10361036
var expected_passive = [false, false];
10371037

1038-
var global = module.addGlobal("a-global", binaryen.i32, false, module.i32.const(expected_offsets[1]))
1038+
var glos = [
1039+
module.addGlobal("a-global", binaryen.i32, false, module.i32.const(expected_offsets[1])),
1040+
module.addGlobal("a-global2", binaryen.i32, false, module.i32.const(2)),
1041+
module.addGlobal("a-global3", binaryen.i32, false, module.i32.const(3))
1042+
];
1043+
1044+
for (i = 0; i < module.getNumGlobals(); i++) {
1045+
assert(module.getGlobalByIndex(i) === glos[i]);
1046+
}
1047+
10391048
module.setMemory(1, 256, "mem", [
10401049
{
10411050
passive: expected_passive[0],

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4432,6 +4432,8 @@ sizeof Literal: 24
44324432
(table $0 1 funcref)
44334433
(elem (i32.const 0) $fn0 $fn1 $fn2)
44344434
(global $a-global i32 (i32.const 125))
4435+
(global $a-global2 i32 (i32.const 2))
4436+
(global $a-global3 i32 (i32.const 3))
44354437
(export "export0" (func $fn0))
44364438
(export "export1" (func $fn1))
44374439
(export "export2" (func $fn2))

0 commit comments

Comments
 (0)