Skip to content
Merged
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
19 changes: 19 additions & 0 deletions src/binaryen-c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5525,6 +5525,25 @@ void BinaryenCopyMemorySegmentData(BinaryenModuleRef module,
}
std::copy(segment->data.cbegin(), segment->data.cend(), buffer);
}
void BinaryenAddDataSegment(BinaryenModuleRef module,
const char* segmentName,
const char* memoryName,
bool segmentPassive,
BinaryenExpressionRef segmentOffset,
const char* segmentData,
BinaryenIndex segmentSize) {
auto* wasm = (Module*)module;
auto name =
segmentName ? Name(segmentName) : Name::fromInt(wasm->dataSegments.size());
auto curr = Builder::makeDataSegment(name,
memoryName ? memoryName : "0",
segmentPassive,
(Expression*)segmentOffset,
segmentData,
segmentSize);
curr->hasExplicitName = segmentName ? true : false;
wasm->addDataSegment(std::move(curr));
}

// Start function. One per module

Expand Down
7 changes: 7 additions & 0 deletions src/binaryen-c.h
Original file line number Diff line number Diff line change
Expand Up @@ -2952,6 +2952,13 @@ BINARYEN_API bool BinaryenGetMemorySegmentPassive(BinaryenModuleRef module,
BINARYEN_API void BinaryenCopyMemorySegmentData(BinaryenModuleRef module,
const char* segmentName,
char* buffer);
BINARYEN_API void BinaryenAddDataSegment(BinaryenModuleRef module,
const char* segmentName,
const char* memoryName,
bool segmentPassive,
BinaryenExpressionRef segmentOffset,
const char* segmentData,
BinaryenIndex segmentSize);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps it would be clearer to reorder segmentData next to segmentSize? In general matching the order of wasm-builder's makeDataSegment seems the most predictable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps it would be clearer to reorder segmentData next to segmentSize? In general matching the order of wasm-builder's makeDataSegment seems the most predictable.

@kripken, I have made corresponding changes and you can check that out :)


// Start function. One per module

Expand Down
8 changes: 8 additions & 0 deletions test/example/c-api-kitchen-sink.c
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,14 @@ void test_core() {
1,
0,
"0");
BinaryenAddDataSegment(module, NULL, NULL, true, NULL, "data segment 2", 14);
BinaryenAddDataSegment(module,
"seg",
"0",
false,
BinaryenConst(module, BinaryenLiteralInt32(0)),
"data segment 3",
14);

BinaryenExpressionRef valueList[] = {
// Unary
Expand Down
2 changes: 2 additions & 0 deletions test/example/c-api-kitchen-sink.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ BinaryenFeatureAll: 131071
(memory $0 1 256 shared)
(data $0 (i32.const 10) "hello, world")
(data $1 "I am passive")
(data $2 "data segment 2")
(data $seg (i32.const 0) "data segment 3")
(table $tab 0 100 funcref)
(table $0 1 1 funcref)
(elem $0 (table $0) (i32.const 0) func $"kitchen()sinker")
Expand Down