Skip to content

Commit dc37d7b

Browse files
authored
Add getGlobal to binaryen.js (#2142)
We have `getFunction`, but not `getGlobal` because its name clashed with APIs for the deprecated instruction `get_global`. Now we have reflected instruction renaming in code, we can add it for consistency.
1 parent 7ae3805 commit dc37d7b

6 files changed

Lines changed: 20 additions & 2 deletions

File tree

build-js.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,7 @@ export_function "_BinaryenAddFunction"
754754
export_function "_BinaryenGetFunction"
755755
export_function "_BinaryenRemoveFunction"
756756
export_function "_BinaryenAddGlobal"
757+
export_function "_BinaryenGetGlobal"
757758
export_function "_BinaryenRemoveGlobal"
758759
export_function "_BinaryenAddFunctionImport"
759760
export_function "_BinaryenAddTableImport"

src/binaryen-c.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2751,6 +2751,15 @@ BinaryenGlobalRef BinaryenAddGlobal(BinaryenModuleRef module,
27512751
wasm->addGlobal(ret);
27522752
return ret;
27532753
}
2754+
BinaryenGlobalRef BinaryenGetGlobal(BinaryenModuleRef module,
2755+
const char* name) {
2756+
if (tracing) {
2757+
std::cout << " BinaryenGetGlobal(the_module, \"" << name << "\");\n";
2758+
}
2759+
2760+
auto* wasm = (Module*)module;
2761+
return wasm->getGlobal(name);
2762+
}
27542763
void BinaryenRemoveGlobal(BinaryenModuleRef module, const char* name) {
27552764
if (tracing) {
27562765
std::cout << " BinaryenRemoveGlobal(the_module, \"" << name << "\");\n";

src/binaryen-c.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -853,11 +853,9 @@ BinaryenFunctionRef BinaryenAddFunction(BinaryenModuleRef module,
853853
BinaryenType* varTypes,
854854
BinaryenIndex numVarTypes,
855855
BinaryenExpressionRef body);
856-
857856
// Gets a function reference by name.
858857
BinaryenFunctionRef BinaryenGetFunction(BinaryenModuleRef module,
859858
const char* name);
860-
861859
// Removes a function by name.
862860
void BinaryenRemoveFunction(BinaryenModuleRef module, const char* name);
863861

@@ -913,6 +911,8 @@ BinaryenGlobalRef BinaryenAddGlobal(BinaryenModuleRef module,
913911
BinaryenType type,
914912
int8_t mutable_,
915913
BinaryenExpressionRef init);
914+
// Gets a global reference by name.
915+
BinaryenGlobalRef BinaryenGetGlobal(BinaryenModuleRef module, const char* name);
916916
void BinaryenRemoveGlobal(BinaryenModuleRef module, const char* name);
917917

918918
// Function table. One per module

src/js/binaryen.js-post.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,6 +1776,11 @@ function wrapModule(module, self) {
17761776
return Module['_BinaryenAddGlobal'](module, strToStack(name), type, mutable, init);
17771777
});
17781778
}
1779+
self['getGlobal'] = function(name) {
1780+
return preserveStack(function() {
1781+
return Module['_BinaryenGetGlobal'](module, strToStack(name));
1782+
});
1783+
};
17791784
self['removeGlobal'] = function(name) {
17801785
return preserveStack(function() {
17811786
return Module['_BinaryenRemoveGlobal'](module, strToStack(name));

test/binaryen.js/global.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ var module = new Binaryen.Module();
1313
var initExpr = module.i32.const(1);
1414
var global = module.addGlobal("a-global", Binaryen.i32, false, initExpr);
1515

16+
console.log("GetGlobal is equal: " + (global === module.getGlobal("a-global")));
17+
1618
var globalInfo = Binaryen.getGlobalInfo(global);
1719
console.log("getGlobalInfo=" + JSON.stringify(cleanInfo(globalInfo)));
1820

test/binaryen.js/global.js.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
GetGlobal is equal: true
12
getGlobalInfo={"module":"","base":"","mutable":false}
23
getExpressionInfo(init)={"id":14,"value":1}
34
(i32.const 1)

0 commit comments

Comments
 (0)