Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions src/binaryen-c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5337,6 +5337,7 @@ void BinaryenSetMemory(BinaryenModuleRef module,
BinaryenIndex numSegments,
bool shared,
bool memory64,
bool keepSegment,
const char* name) {
auto memory = std::make_unique<Memory>();
memory->name = name ? name : "0";
Expand All @@ -5351,20 +5352,22 @@ void BinaryenSetMemory(BinaryenModuleRef module,
memoryExport->kind = ExternalKind::Memory;
((Module*)module)->addExport(memoryExport.release());
}
((Module*)module)->removeDataSegments([&](DataSegment* curr) {
return true;
});
for (BinaryenIndex i = 0; i < numSegments; i++) {
auto explicitName = segmentNames && segmentNames[i];
auto name = explicitName ? Name(segmentNames[i]) : Name::fromInt(i);
auto curr = Builder::makeDataSegment(name,
memory->name,
segmentPassives[i],
(Expression*)segmentOffsets[i],
segmentDatas[i],
segmentSizes[i]);
curr->hasExplicitName = explicitName;
((Module*)module)->addDataSegment(std::move(curr));
if (!keepSegment) {
((Module*)module)->removeDataSegments([&](DataSegment* curr) {
return true;
});
for (BinaryenIndex i = 0; i < numSegments; i++) {
auto explicitName = segmentNames && segmentNames[i];
auto name = explicitName ? Name(segmentNames[i]) : Name::fromInt(i);
auto curr = Builder::makeDataSegment(name,
memory->name,
segmentPassives[i],
(Expression*)segmentOffsets[i],
segmentDatas[i],
segmentSizes[i]);
curr->hasExplicitName = explicitName;
((Module*)module)->addDataSegment(std::move(curr));
}
}
((Module*)module)->removeMemories([&](Memory* curr) { return true; });
((Module*)module)->addMemory(std::move(memory));
Expand Down
3 changes: 3 additions & 0 deletions src/binaryen-c.h
Original file line number Diff line number Diff line change
Expand Up @@ -2910,6 +2910,8 @@ BinaryenGetElementSegmentByIndex(BinaryenModuleRef module, BinaryenIndex index);
// a start offset in segmentOffsets, a passive flag in segmentPassives
// and a size in segmentSizes. segmentNames and exportName can be NULL
// If segmentNames is null, BinaryenSetMemory creates names from indices
// If keepSegment is true, BinaryenSetMemory will not operate on memory
// segments.
BINARYEN_API void BinaryenSetMemory(BinaryenModuleRef module,
BinaryenIndex initial,
BinaryenIndex maximum,
Expand All @@ -2922,6 +2924,7 @@ BINARYEN_API void BinaryenSetMemory(BinaryenModuleRef module,
BinaryenIndex numSegments,
bool shared,
bool memory64,
bool keepSegment,
const char* name);

BINARYEN_API bool BinaryenHasMemory(BinaryenModuleRef module);
Expand Down
3 changes: 2 additions & 1 deletion src/js/binaryen.js-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -2561,7 +2561,7 @@ function wrapModule(module, self = {}) {
self['removeExport'] = function(externalName) {
return preserveStack(() => Module['_BinaryenRemoveExport'](module, strToStack(externalName)));
};
self['setMemory'] = function(initial, maximum, exportName, segments = [], shared = false, memory64 = false, internalName = null) {
self['setMemory'] = function(initial, maximum, exportName, segments = [], shared = false, memory64 = false, keepSegment = false, internalName = null) {
// segments are assumed to be { passive: bool, offset: expression ref, data: array of 8-bit data }
return preserveStack(() => {
const segmentsLen = segments.length;
Expand Down Expand Up @@ -2589,6 +2589,7 @@ function wrapModule(module, self = {}) {
segmentsLen,
shared,
memory64,
keepSegment,
strToStack(internalName)
);
for (let i = 0; i < segmentsLen; i++) {
Expand Down
7 changes: 7 additions & 0 deletions test/example/c-api-kitchen-sink.c
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,7 @@ void test_core() {
2,
1,
0,
0,
"0");

BinaryenExpressionRef valueList[] = {
Expand Down Expand Up @@ -2108,6 +2109,7 @@ void test_for_each() {
2,
0,
0,
0,
"0");
BinaryenAddGlobal(module,
"a-global",
Expand All @@ -2124,6 +2126,11 @@ void test_for_each() {
BinaryenCopyMemorySegmentData(module, segmentNames[i], out);
assert(0 == strcmp(segmentDatas[i], out));
}

BinaryenIndex numSegments = BinaryenGetNumMemorySegments(module);
BinaryenSetMemory(
module, 1, 256, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 1, "0");
assert(numSegments == BinaryenGetNumMemorySegments(module));
}
{
const char* funcNames[] = {BinaryenFunctionGetName(fns[0]),
Expand Down
2 changes: 2 additions & 0 deletions test/example/c-api-relooper-unreachable-if.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ int main() {
0,
0,
0,
0,
"0");
}
the_relooper = RelooperCreate(the_module);
Expand Down Expand Up @@ -604,6 +605,7 @@ int main() {
0,
0,
0,
0,
"0");
}
expressions[157] = BinaryenConst(the_module, BinaryenLiteralInt32(65535));
Expand Down
2 changes: 2 additions & 0 deletions test/example/c-api-unused-mem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ int main() {
0,
0,
0,
0,
"0");
}
the_relooper = RelooperCreate(the_module);
Expand Down Expand Up @@ -100,6 +101,7 @@ int main() {
0,
0,
0,
0,
"0");
}
expressions[11] = BinaryenConst(the_module, BinaryenLiteralInt32(65535));
Expand Down
2 changes: 1 addition & 1 deletion test/example/relooper-fuzz.c
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ int main() {

// memory
BinaryenSetMemory(
module, 1, 1, "mem", NULL, NULL, NULL, NULL, NULL, 0, 0, 0, "0");
module, 1, 1, "mem", NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, "0");

assert(BinaryenModuleValidate(module));

Expand Down
2 changes: 1 addition & 1 deletion test/example/relooper-fuzz1.c
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ int main() {

// memory
BinaryenSetMemory(
module, 1, 1, "mem", NULL, NULL, NULL, NULL, NULL, 0, 0, 0, "0");
module, 1, 1, "mem", NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, "0");

assert(BinaryenModuleValidate(module));

Expand Down
2 changes: 1 addition & 1 deletion test/example/relooper-fuzz2.c
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ int main() {

// memory
BinaryenSetMemory(
module, 1, 1, "mem", NULL, NULL, NULL, NULL, NULL, 0, 0, 0, "0");
module, 1, 1, "mem", NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, "0");

// optionally, optimize
if (0)
Expand Down
2 changes: 1 addition & 1 deletion test/example/relooper-merge1.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ int main() {

// memory
BinaryenSetMemory(
module, 1, 1, "mem", NULL, NULL, NULL, NULL, NULL, 0, 0, 0, "0");
module, 1, 1, "mem", NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, "0");

// optionally, optimize
if (0)
Expand Down
2 changes: 1 addition & 1 deletion test/example/relooper-merge2.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ int main() {

// memory
BinaryenSetMemory(
module, 1, 1, "mem", NULL, NULL, NULL, NULL, NULL, 0, 0, 0, "0");
module, 1, 1, "mem", NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, "0");

// optionally, optimize
if (0)
Expand Down
2 changes: 1 addition & 1 deletion test/example/relooper-merge3.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ int main() {

// memory
BinaryenSetMemory(
module, 1, 1, "mem", NULL, NULL, NULL, NULL, NULL, 0, 0, 0, "0");
module, 1, 1, "mem", NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, "0");

// optionally, optimize
if (0)
Expand Down
2 changes: 1 addition & 1 deletion test/example/relooper-merge4.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ int main() {

// memory
BinaryenSetMemory(
module, 1, 1, "mem", NULL, NULL, NULL, NULL, NULL, 0, 0, 0, "0");
module, 1, 1, "mem", NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, "0");

// optionally, optimize
if (0)
Expand Down
2 changes: 1 addition & 1 deletion test/example/relooper-merge5.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ int main() {

// memory
BinaryenSetMemory(
module, 1, 1, "mem", NULL, NULL, NULL, NULL, NULL, 0, 0, 0, "0");
module, 1, 1, "mem", NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, "0");

// optionally, optimize
if (0)
Expand Down
2 changes: 1 addition & 1 deletion test/example/relooper-merge6.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ int main() {

// memory
BinaryenSetMemory(
module, 1, 1, "mem", NULL, NULL, NULL, NULL, NULL, 0, 0, 0, "0");
module, 1, 1, "mem", NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, "0");

// optionally, optimize
if (0)
Expand Down