@@ -3142,6 +3142,26 @@ void BinaryenRemoveFunction(BinaryenModuleRef module, const char* name) {
31423142 auto * wasm = (Module*)module ;
31433143 wasm->removeFunction (name);
31443144}
3145+ uint32_t BinaryenGetNumFunctions (BinaryenModuleRef module ) {
3146+ if (tracing) {
3147+ std::cout << " BinaryenGetNumFunctions(the_module);\n " ;
3148+ }
3149+
3150+ auto * wasm = (Module*)module ;
3151+ return wasm->functions .size ();
3152+ }
3153+ BinaryenFunctionRef BinaryenGetFunctionByIndex (BinaryenModuleRef module ,
3154+ BinaryenIndex id) {
3155+ if (tracing) {
3156+ std::cout << " BinaryenGetFunctionByIndex(the_module, " << id << " );\n " ;
3157+ }
3158+
3159+ auto * wasm = (Module*)module ;
3160+ if (wasm->functions .size () <= id) {
3161+ Fatal () << " invalid function id." ;
3162+ }
3163+ return wasm->functions [id].get ();
3164+ }
31453165
31463166// Globals
31473167
@@ -3578,6 +3598,83 @@ void BinaryenSetMemory(BinaryenModuleRef module,
35783598 }
35793599}
35803600
3601+ // Memory segments
3602+
3603+ uint32_t BinaryenGetNumMemorySegments (BinaryenModuleRef module ) {
3604+ if (tracing) {
3605+ std::cout << " BinaryenGetNumMemorySegments(the_module);\n " ;
3606+ }
3607+
3608+ auto * wasm = (Module*)module ;
3609+ return wasm->memory .segments .size ();
3610+ }
3611+ int64_t BinaryenGetMemorySegmentByteOffset (BinaryenModuleRef module ,
3612+ BinaryenIndex id) {
3613+ if (tracing) {
3614+ std::cout << " BinaryenGetMemorySegmentByteOffset(the_module, " << id
3615+ << " );\n " ;
3616+ }
3617+
3618+ auto * wasm = (Module*)module ;
3619+ if (wasm->memory .segments .size () <= id) {
3620+ Fatal () << " invalid segment id." ;
3621+ }
3622+
3623+ auto globalOffset = [&](const Expression* const & expr,
3624+ int64_t & result) -> bool {
3625+ if (auto * c = expr->dynCast <Const>()) {
3626+ result = c->value .getInteger ();
3627+ return true ;
3628+ }
3629+ return false ;
3630+ };
3631+
3632+ const Memory::Segment& segment = wasm->memory .segments [id];
3633+
3634+ int64_t ret;
3635+ if (globalOffset (segment.offset , ret)) {
3636+ return ret;
3637+ }
3638+ if (auto * get = segment.offset ->dynCast <GlobalGet>()) {
3639+ Global* global = wasm->getGlobal (get->name );
3640+ if (globalOffset (global->init , ret)) {
3641+ return ret;
3642+ }
3643+ }
3644+
3645+ Fatal () << " non-constant offsets aren't supported yet" ;
3646+ return 0 ;
3647+ }
3648+ size_t BinaryenGetMemorySegmentByteLength (BinaryenModuleRef module ,
3649+ BinaryenIndex id) {
3650+ if (tracing) {
3651+ std::cout << " BinaryenGetMemorySegmentByteLength(the_module, " << id
3652+ << " );\n " ;
3653+ }
3654+
3655+ auto * wasm = (Module*)module ;
3656+ if (wasm->memory .segments .size () <= id) {
3657+ Fatal () << " invalid segment id." ;
3658+ }
3659+ const Memory::Segment& segment = wasm->memory .segments [id];
3660+ return segment.data .size ();
3661+ }
3662+ void BinaryenCopyMemorySegmentData (BinaryenModuleRef module ,
3663+ BinaryenIndex id,
3664+ char * buffer) {
3665+ if (tracing) {
3666+ std::cout << " BinaryenCopyMemorySegmentData(the_module, " << id << " , "
3667+ << static_cast <void *>(buffer) << " );\n " ;
3668+ }
3669+
3670+ auto * wasm = (Module*)module ;
3671+ if (wasm->memory .segments .size () <= id) {
3672+ Fatal () << " invalid segment id." ;
3673+ }
3674+ const Memory::Segment& segment = wasm->memory .segments [id];
3675+ std::copy (segment.data .cbegin (), segment.data .cend (), buffer);
3676+ }
3677+
35813678// Start function. One per module
35823679
35833680void BinaryenSetStart (BinaryenModuleRef module , BinaryenFunctionRef start) {
@@ -4305,6 +4402,26 @@ const char* BinaryenExportGetValue(BinaryenExportRef export_) {
43054402
43064403 return ((Export*)export_)->value .c_str ();
43074404}
4405+ uint32_t BinaryenGetNumExports (BinaryenModuleRef module ) {
4406+ if (tracing) {
4407+ std::cout << " BinaryenGetNumExports(the_module);\n " ;
4408+ }
4409+
4410+ auto * wasm = (Module*)module ;
4411+ return wasm->exports .size ();
4412+ }
4413+ BinaryenExportRef BinaryenGetExportByIndex (BinaryenModuleRef module ,
4414+ BinaryenIndex id) {
4415+ if (tracing) {
4416+ std::cout << " BinaryenGetExportByIndex(the_module, " << id << " );\n " ;
4417+ }
4418+
4419+ auto * wasm = (Module*)module ;
4420+ if (wasm->exports .size () <= id) {
4421+ Fatal () << " invalid export id." ;
4422+ }
4423+ return wasm->exports [id].get ();
4424+ }
43084425
43094426//
43104427// ========= Custom sections =========
0 commit comments