summaryrefslogtreecommitdiff
path: root/src/binaryen-c.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/binaryen-c.cpp')
-rw-r--r--src/binaryen-c.cpp117
1 files changed, 117 insertions, 0 deletions
diff --git a/src/binaryen-c.cpp b/src/binaryen-c.cpp
index 1cb0963a2..6bb6f6e14 100644
--- a/src/binaryen-c.cpp
+++ b/src/binaryen-c.cpp
@@ -3142,6 +3142,26 @@ void BinaryenRemoveFunction(BinaryenModuleRef module, const char* name) {
auto* wasm = (Module*)module;
wasm->removeFunction(name);
}
+uint32_t BinaryenGetNumFunctions(BinaryenModuleRef module) {
+ if (tracing) {
+ std::cout << " BinaryenGetNumFunctions(the_module);\n";
+ }
+
+ auto* wasm = (Module*)module;
+ return wasm->functions.size();
+}
+BinaryenFunctionRef BinaryenGetFunctionByIndex(BinaryenModuleRef module,
+ BinaryenIndex id) {
+ if (tracing) {
+ std::cout << " BinaryenGetFunctionByIndex(the_module, " << id << ");\n";
+ }
+
+ auto* wasm = (Module*)module;
+ if (wasm->functions.size() <= id) {
+ Fatal() << "invalid function id.";
+ }
+ return wasm->functions[id].get();
+}
// Globals
@@ -3578,6 +3598,83 @@ void BinaryenSetMemory(BinaryenModuleRef module,
}
}
+// Memory segments
+
+uint32_t BinaryenGetNumMemorySegments(BinaryenModuleRef module) {
+ if (tracing) {
+ std::cout << " BinaryenGetNumMemorySegments(the_module);\n";
+ }
+
+ auto* wasm = (Module*)module;
+ return wasm->memory.segments.size();
+}
+int64_t BinaryenGetMemorySegmentByteOffset(BinaryenModuleRef module,
+ BinaryenIndex id) {
+ if (tracing) {
+ std::cout << " BinaryenGetMemorySegmentByteOffset(the_module, " << id
+ << ");\n";
+ }
+
+ auto* wasm = (Module*)module;
+ if (wasm->memory.segments.size() <= id) {
+ Fatal() << "invalid segment id.";
+ }
+
+ auto globalOffset = [&](const Expression* const& expr,
+ int64_t& result) -> bool {
+ if (auto* c = expr->dynCast<Const>()) {
+ result = c->value.getInteger();
+ return true;
+ }
+ return false;
+ };
+
+ const Memory::Segment& segment = wasm->memory.segments[id];
+
+ int64_t ret;
+ if (globalOffset(segment.offset, ret)) {
+ return ret;
+ }
+ if (auto* get = segment.offset->dynCast<GlobalGet>()) {
+ Global* global = wasm->getGlobal(get->name);
+ if (globalOffset(global->init, ret)) {
+ return ret;
+ }
+ }
+
+ Fatal() << "non-constant offsets aren't supported yet";
+ return 0;
+}
+size_t BinaryenGetMemorySegmentByteLength(BinaryenModuleRef module,
+ BinaryenIndex id) {
+ if (tracing) {
+ std::cout << " BinaryenGetMemorySegmentByteLength(the_module, " << id
+ << ");\n";
+ }
+
+ auto* wasm = (Module*)module;
+ if (wasm->memory.segments.size() <= id) {
+ Fatal() << "invalid segment id.";
+ }
+ const Memory::Segment& segment = wasm->memory.segments[id];
+ return segment.data.size();
+}
+void BinaryenCopyMemorySegmentData(BinaryenModuleRef module,
+ BinaryenIndex id,
+ char* buffer) {
+ if (tracing) {
+ std::cout << " BinaryenCopyMemorySegmentData(the_module, " << id << ", "
+ << static_cast<void*>(buffer) << ");\n";
+ }
+
+ auto* wasm = (Module*)module;
+ if (wasm->memory.segments.size() <= id) {
+ Fatal() << "invalid segment id.";
+ }
+ const Memory::Segment& segment = wasm->memory.segments[id];
+ std::copy(segment.data.cbegin(), segment.data.cend(), buffer);
+}
+
// Start function. One per module
void BinaryenSetStart(BinaryenModuleRef module, BinaryenFunctionRef start) {
@@ -4305,6 +4402,26 @@ const char* BinaryenExportGetValue(BinaryenExportRef export_) {
return ((Export*)export_)->value.c_str();
}
+uint32_t BinaryenGetNumExports(BinaryenModuleRef module) {
+ if (tracing) {
+ std::cout << " BinaryenGetNumExports(the_module);\n";
+ }
+
+ auto* wasm = (Module*)module;
+ return wasm->exports.size();
+}
+BinaryenExportRef BinaryenGetExportByIndex(BinaryenModuleRef module,
+ BinaryenIndex id) {
+ if (tracing) {
+ std::cout << " BinaryenGetExportByIndex(the_module, " << id << ");\n";
+ }
+
+ auto* wasm = (Module*)module;
+ if (wasm->exports.size() <= id) {
+ Fatal() << "invalid export id.";
+ }
+ return wasm->exports[id].get();
+}
//
// ========= Custom sections =========