summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorHeejin Ahn <aheejin@gmail.com>2019-05-13 10:33:55 -0700
committerGitHub <noreply@github.com>2019-05-13 10:33:55 -0700
commit67019f9a72175bc7a098d72aa14a6f6afeb2efce (patch)
tree83adc3d4a235c00f6bf545fc5aeeeadb8f1a5663 /src
parenta1ff274b6bca0ff8d1635c32a6d206863f0a2fc3 (diff)
downloadbinaryen-67019f9a72175bc7a098d72aa14a6f6afeb2efce.tar.gz
binaryen-67019f9a72175bc7a098d72aa14a6f6afeb2efce.tar.bz2
binaryen-67019f9a72175bc7a098d72aa14a6f6afeb2efce.zip
Add missing methods for globals to binaryen.js (#2099)
- Print `globals` array in the tracing mode like other arrays (`functions`, `exports`, `imports`, ...) - Add accessor functions for globals
Diffstat (limited to 'src')
-rw-r--r--src/binaryen-c.cpp52
-rw-r--r--src/binaryen-c.h15
-rw-r--r--src/js/binaryen.js-post.js12
3 files changed, 69 insertions, 10 deletions
diff --git a/src/binaryen-c.cpp b/src/binaryen-c.cpp
index ffd24d48e..37d2c2142 100644
--- a/src/binaryen-c.cpp
+++ b/src/binaryen-c.cpp
@@ -2699,19 +2699,24 @@ void BinaryenRemoveFunction(BinaryenModuleRef module, const char* name) {
wasm->removeFunction(name);
}
+// Globals
+
BinaryenGlobalRef BinaryenAddGlobal(BinaryenModuleRef module,
const char* name,
BinaryenType type,
int8_t mutable_,
BinaryenExpressionRef init) {
+ auto* wasm = (Module*)module;
+ auto* ret = new Global();
+
if (tracing) {
- std::cout << " BinaryenAddGlobal(the_module, \"" << name << "\", " << type
- << ", " << int(mutable_) << ", expressions[" << expressions[init]
- << "]);\n";
+ auto id = globals.size();
+ globals[ret] = id;
+ std::cout << " globals[" << id << "] = BinaryenAddGlobal(the_module, \""
+ << name << "\", " << type << ", " << int(mutable_)
+ << ", expressions[" << expressions[init] << "]);\n";
}
- auto* wasm = (Module*)module;
- auto* ret = new Global();
ret->name = name;
ret->type = Type(type);
ret->mutable_ = !!mutable_;
@@ -3518,6 +3523,43 @@ void BinaryenFunctionSetDebugLocation(BinaryenFunctionRef func,
}
//
+// =========== Global operations ===========
+//
+
+const char* BinaryenGlobalGetName(BinaryenGlobalRef global) {
+ if (tracing) {
+ std::cout << " BinaryenGlobalGetName(globals[" << globals[global]
+ << "]);\n";
+ }
+
+ return ((Global*)global)->name.c_str();
+}
+BinaryenType BinaryenGlobalGetType(BinaryenGlobalRef global) {
+ if (tracing) {
+ std::cout << " BinaryenGlobalGetType(globals[" << globals[global]
+ << "]);\n";
+ }
+
+ return ((Global*)global)->type;
+}
+int BinaryenGlobalIsMutable(BinaryenGlobalRef global) {
+ if (tracing) {
+ std::cout << " BinaryenGlobalIsMutable(globals[" << globals[global]
+ << "]);\n";
+ }
+
+ return ((Global*)global)->mutable_;
+}
+BinaryenExpressionRef BinaryenGlobalGetInitExpr(BinaryenGlobalRef global) {
+ if (tracing) {
+ std::cout << " BinaryenGlobalGetInitExpr(globals[" << globals[global]
+ << "]);\n";
+ }
+
+ return ((Global*)global)->init;
+}
+
+//
// =========== Import operations ===========
//
diff --git a/src/binaryen-c.h b/src/binaryen-c.h
index 62ffa4348..310ca3a2a 100644
--- a/src/binaryen-c.h
+++ b/src/binaryen-c.h
@@ -1105,6 +1105,21 @@ void BinaryenFunctionSetDebugLocation(BinaryenFunctionRef func,
BinaryenIndex columnNumber);
//
+// ========== Global Operations ==========
+//
+
+// Gets the name of the specified `Global`.
+const char* BinaryenGlobalGetName(BinaryenGlobalRef global);
+// Gets the name of the `GlobalType` associated with the specified `Global`. May
+// be `NULL` if the signature is implicit.
+BinaryenType BinaryenGlobalGetType(BinaryenGlobalRef global);
+// Returns true if the specified `Global` is mutable.
+int BinaryenGlobalIsMutable(BinaryenGlobalRef global);
+// Gets the initialization expression of the specified `Global`.
+BinaryenExpressionRef BinaryenGlobalGetInitExpr(BinaryenGlobalRef global);
+
+//
+//
// ========== Import Operations ==========
//
diff --git a/src/js/binaryen.js-post.js b/src/js/binaryen.js-post.js
index 52f4743bb..8aa28a08c 100644
--- a/src/js/binaryen.js-post.js
+++ b/src/js/binaryen.js-post.js
@@ -2327,12 +2327,14 @@ Module['getFunctionInfo'] = function(func) {
};
// Obtains information about a 'Global'
-Module['getGlobalInfo'] = function(func) {
+Module['getGlobalInfo'] = function(global) {
return {
- 'name': UTF8ToString(Module['_BinaryenGlobalGetName'](func)),
- 'module': UTF8ToString(Module['_BinaryenGlobalImportGetModule'](func)),
- 'base': UTF8ToString(Module['_BinaryenGlobalImportGetBase'](func)),
- 'type': UTF8ToString(Module['_BinaryenGlobalGetType'](func))
+ 'name': UTF8ToString(Module['_BinaryenGlobalGetName'](global)),
+ 'module': UTF8ToString(Module['_BinaryenGlobalImportGetModule'](global)),
+ 'base': UTF8ToString(Module['_BinaryenGlobalImportGetBase'](global)),
+ 'type': Module['_BinaryenGlobalGetType'](global),
+ 'mutable': Boolean(Module['_BinaryenGlobalIsMutable'](global)),
+ 'init': Module['_BinaryenGlobalGetInitExpr'](global)
};
};