@@ -5329,8 +5329,9 @@ void BinaryenSetMemory(BinaryenModuleRef module,
53295329 BinaryenIndex initial,
53305330 BinaryenIndex maximum,
53315331 const char * exportName,
5332- const char ** segments,
5333- bool * segmentPassive,
5332+ const char ** segmentNames,
5333+ const char ** segmentDatas,
5334+ bool * segmentPassives,
53345335 BinaryenExpressionRef* segmentOffsets,
53355336 BinaryenIndex* segmentSizes,
53365337 BinaryenIndex numSegments,
@@ -5354,13 +5355,15 @@ void BinaryenSetMemory(BinaryenModuleRef module,
53545355 return true ;
53555356 });
53565357 for (BinaryenIndex i = 0 ; i < numSegments; i++) {
5357- auto curr = Builder::makeDataSegment (Name::fromInt (i),
5358+ auto explicitName = segmentNames && segmentNames[i];
5359+ auto name = explicitName ? Name (segmentNames[i]) : Name::fromInt (i);
5360+ auto curr = Builder::makeDataSegment (name,
53585361 memory->name ,
5359- segmentPassive [i],
5362+ segmentPassives [i],
53605363 (Expression*)segmentOffsets[i],
5361- segments [i],
5364+ segmentDatas [i],
53625365 segmentSizes[i]);
5363- curr->hasExplicitName = false ;
5366+ curr->hasExplicitName = explicitName ;
53645367 ((Module*)module )->addDataSegment (std::move (curr));
53655368 }
53665369 ((Module*)module )->removeMemories ([&](Memory* curr) { return true ; });
@@ -5373,10 +5376,11 @@ uint32_t BinaryenGetNumMemorySegments(BinaryenModuleRef module) {
53735376 return ((Module*)module )->dataSegments .size ();
53745377}
53755378uint32_t BinaryenGetMemorySegmentByteOffset (BinaryenModuleRef module ,
5376- BinaryenIndex id ) {
5379+ const char * segmentName ) {
53775380 auto * wasm = (Module*)module ;
5378- if (wasm->dataSegments .size () <= id) {
5379- Fatal () << " invalid segment id." ;
5381+ const auto * segment = wasm->getDataSegmentOrNull (Name (segmentName));
5382+ if (segment == NULL ) {
5383+ Fatal () << " invalid segment name." ;
53805384 }
53815385
53825386 auto globalOffset = [&](const Expression* const & expr,
@@ -5388,8 +5392,6 @@ uint32_t BinaryenGetMemorySegmentByteOffset(BinaryenModuleRef module,
53885392 return false ;
53895393 };
53905394
5391- const auto & segment = wasm->dataSegments [id];
5392-
53935395 int64_t ret;
53945396 if (globalOffset (segment->offset , ret)) {
53955397 return ret;
@@ -5496,29 +5498,31 @@ bool BinaryenMemoryIs64(BinaryenModuleRef module, const char* name) {
54965498 return memory->is64 ();
54975499}
54985500size_t BinaryenGetMemorySegmentByteLength (BinaryenModuleRef module ,
5499- BinaryenIndex id) {
5500- const auto & segments = ((Module*)module )->dataSegments ;
5501- if (segments.size () <= id) {
5502- Fatal () << " invalid segment id." ;
5501+ const char * segmentName) {
5502+ auto * wasm = (Module*)module ;
5503+ const auto * segment = wasm->getDataSegmentOrNull (Name (segmentName));
5504+ if (segment == NULL ) {
5505+ Fatal () << " invalid segment name." ;
55035506 }
5504- return segments[id] ->data .size ();
5507+ return segment ->data .size ();
55055508}
55065509bool BinaryenGetMemorySegmentPassive (BinaryenModuleRef module ,
5507- BinaryenIndex id) {
5508- const auto & segments = ((Module*)module )->dataSegments ;
5509- if (segments.size () <= id) {
5510- Fatal () << " invalid segment id." ;
5510+ const char * segmentName) {
5511+ auto * wasm = (Module*)module ;
5512+ const auto * segment = wasm->getDataSegmentOrNull (Name (segmentName));
5513+ if (segment == NULL ) {
5514+ Fatal () << " invalid segment name." ;
55115515 }
5512- return segments[id] ->isPassive ;
5516+ return segment ->isPassive ;
55135517}
55145518void BinaryenCopyMemorySegmentData (BinaryenModuleRef module ,
5515- BinaryenIndex id ,
5519+ const char * segmentName ,
55165520 char * buffer) {
5517- const auto & segments = ((Module*)module )->dataSegments ;
5518- if (segments.size () <= id) {
5519- Fatal () << " invalid segment id." ;
5521+ auto * wasm = (Module*)module ;
5522+ const auto * segment = wasm->getDataSegmentOrNull (Name (segmentName));
5523+ if (segment == NULL ) {
5524+ Fatal () << " invalid segment name." ;
55205525 }
5521- const auto & segment = segments[id];
55225526 std::copy (segment->data .cbegin (), segment->data .cend (), buffer);
55235527}
55245528
0 commit comments