Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
60040c3
update c++ api
ericvergnaud Jan 30, 2024
5c66694
Use segment names instead of indices
ericvergnaud Jan 30, 2024
53cd9a6
use segment names instead of segment indices
ericvergnaud Jan 30, 2024
2599ea8
fix formatting
ericvergnaud Jan 30, 2024
2ef578e
fix failing js tests
ericvergnaud Jan 30, 2024
19d841a
fix failing js tests
ericvergnaud Jan 30, 2024
3471b35
fix failing tests
ericvergnaud Jan 30, 2024
081f3f0
fix failing tests
ericvergnaud Jan 30, 2024
de0f397
fix failing tests
ericvergnaud Jan 30, 2024
efc7fd4
troubleshoot failing tests
ericvergnaud Jan 30, 2024
47ea12b
fix failing tests
ericvergnaud Jan 30, 2024
19dc106
Revert "troubleshoot failing tests"
ericvergnaud Jan 30, 2024
d42e44c
allow segment names to be null
ericvergnaud Jan 30, 2024
b42756d
something slipped...
ericvergnaud Jan 30, 2024
7e03f19
support anonymous segments in js too
ericvergnaud Jan 30, 2024
960bea5
long day...
ericvergnaud Jan 30, 2024
bf35b05
make names explicit only when provided
ericvergnaud Jan 30, 2024
25a67e8
make names explicit only when provided
ericvergnaud Jan 30, 2024
c35cdf9
bad day, switching between languages...
ericvergnaud Jan 30, 2024
c9ab883
fix indent
ericvergnaud Jan 31, 2024
f9ebb93
print JS stack trace in emcc tests
ericvergnaud Jan 31, 2024
c839752
print JS stack trace in emcc tests
ericvergnaud Jan 31, 2024
b6bc3c8
fix failing tests
ericvergnaud Jan 31, 2024
a1f89f5
add comment
ericvergnaud Jan 31, 2024
74e7f31
fix failing tests
ericvergnaud Jan 31, 2024
7e2cb37
fix lint issue
ericvergnaud Jan 31, 2024
9d6249e
fix formatting and update comments
ericvergnaud Jan 31, 2024
2293db2
Update scripts/test/binaryenjs.py
kripken Jan 31, 2024
4a787d5
force update of txt files
ericvergnaud Jan 31, 2024
aa4a907
reverse forced update
ericvergnaud Jan 31, 2024
ed97acb
force update
ericvergnaud Jan 31, 2024
d15d3c8
last attempt...
ericvergnaud Jan 31, 2024
e219adf
Revert "fix failing tests"
ericvergnaud Jan 31, 2024
a90fb38
remove to recreate
ericvergnaud Jan 31, 2024
32b1d92
recreate
ericvergnaud Jan 31, 2024
58fb2e6
remove and push ?
ericvergnaud Jan 31, 2024
24f923f
restore
ericvergnaud Jan 31, 2024
4f420f1
add test file
ericvergnaud Jan 31, 2024
81f94b5
Update kitchen-sink.js.txt
ericvergnaud Jan 31, 2024
deb4694
revert changes in test harness
ericvergnaud Jan 31, 2024
0ae7ac4
Merge branch 'use-segment-names' of https://github.com/ericvergnaud/b…
ericvergnaud Jan 31, 2024
0904749
Update kitchen-sink.js.txt
ericvergnaud Jan 31, 2024
416d80c
restore incorrectly reverted change
ericvergnaud Feb 1, 2024
f7f44b9
remove test wrapper
ericvergnaud Feb 1, 2024
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
56 changes: 30 additions & 26 deletions src/binaryen-c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5329,8 +5329,9 @@ void BinaryenSetMemory(BinaryenModuleRef module,
BinaryenIndex initial,
BinaryenIndex maximum,
const char* exportName,
const char** segments,
bool* segmentPassive,
const char** segmentNames,
const char** segmentDatas,
bool* segmentPassives,
BinaryenExpressionRef* segmentOffsets,
BinaryenIndex* segmentSizes,
BinaryenIndex numSegments,
Expand All @@ -5354,13 +5355,15 @@ void BinaryenSetMemory(BinaryenModuleRef module,
return true;
});
for (BinaryenIndex i = 0; i < numSegments; i++) {
auto curr = Builder::makeDataSegment(Name::fromInt(i),
auto explicitName = segmentNames && segmentNames[i];
auto name = explicitName ? Name(segmentNames[i]) : Name::fromInt(i);
auto curr = Builder::makeDataSegment(name,
memory->name,
segmentPassive[i],
segmentPassives[i],
(Expression*)segmentOffsets[i],
segments[i],
segmentDatas[i],
segmentSizes[i]);
curr->hasExplicitName = false;
curr->hasExplicitName = explicitName;
((Module*)module)->addDataSegment(std::move(curr));
}
((Module*)module)->removeMemories([&](Memory* curr) { return true; });
Expand All @@ -5373,10 +5376,11 @@ uint32_t BinaryenGetNumMemorySegments(BinaryenModuleRef module) {
return ((Module*)module)->dataSegments.size();
}
uint32_t BinaryenGetMemorySegmentByteOffset(BinaryenModuleRef module,
BinaryenIndex id) {
const char* segmentName) {
auto* wasm = (Module*)module;
if (wasm->dataSegments.size() <= id) {
Fatal() << "invalid segment id.";
const auto* segment = wasm->getDataSegmentOrNull(Name(segmentName));
if (segment == NULL) {
Fatal() << "invalid segment name.";
}

auto globalOffset = [&](const Expression* const& expr,
Expand All @@ -5388,8 +5392,6 @@ uint32_t BinaryenGetMemorySegmentByteOffset(BinaryenModuleRef module,
return false;
};

const auto& segment = wasm->dataSegments[id];

int64_t ret;
if (globalOffset(segment->offset, ret)) {
return ret;
Expand Down Expand Up @@ -5496,29 +5498,31 @@ bool BinaryenMemoryIs64(BinaryenModuleRef module, const char* name) {
return memory->is64();
}
size_t BinaryenGetMemorySegmentByteLength(BinaryenModuleRef module,
BinaryenIndex id) {
const auto& segments = ((Module*)module)->dataSegments;
if (segments.size() <= id) {
Fatal() << "invalid segment id.";
const char* segmentName) {
auto* wasm = (Module*)module;
const auto* segment = wasm->getDataSegmentOrNull(Name(segmentName));
if (segment == NULL) {
Fatal() << "invalid segment name.";
}
return segments[id]->data.size();
return segment->data.size();
}
bool BinaryenGetMemorySegmentPassive(BinaryenModuleRef module,
BinaryenIndex id) {
const auto& segments = ((Module*)module)->dataSegments;
if (segments.size() <= id) {
Fatal() << "invalid segment id.";
const char* segmentName) {
auto* wasm = (Module*)module;
const auto* segment = wasm->getDataSegmentOrNull(Name(segmentName));
if (segment == NULL) {
Fatal() << "invalid segment name.";
}
return segments[id]->isPassive;
return segment->isPassive;
}
void BinaryenCopyMemorySegmentData(BinaryenModuleRef module,
BinaryenIndex id,
const char* segmentName,
char* buffer) {
const auto& segments = ((Module*)module)->dataSegments;
if (segments.size() <= id) {
Fatal() << "invalid segment id.";
auto* wasm = (Module*)module;
const auto* segment = wasm->getDataSegmentOrNull(Name(segmentName));
if (segment == NULL) {
Fatal() << "invalid segment name.";
}
const auto& segment = segments[id];
std::copy(segment->data.cbegin(), segment->data.cend(), buffer);
}

Expand Down
29 changes: 16 additions & 13 deletions src/binaryen-c.h
Original file line number Diff line number Diff line change
Expand Up @@ -2028,11 +2028,11 @@ BINARYEN_API bool BinaryenSIMDLoadStoreLaneIsStore(BinaryenExpressionRef expr);

// MemoryInit

// Gets the index of the segment being initialized by a `memory.init`
// Gets the name of the segment being initialized by a `memory.init`
// expression.
BINARYEN_API const char*
BinaryenMemoryInitGetSegment(BinaryenExpressionRef expr);
// Sets the index of the segment being initialized by a `memory.init`
// Sets the name of the segment being initialized by a `memory.init`
// expression.
BINARYEN_API void BinaryenMemoryInitSetSegment(BinaryenExpressionRef expr,
const char* segment);
Expand All @@ -2057,9 +2057,9 @@ BINARYEN_API void BinaryenMemoryInitSetSize(BinaryenExpressionRef expr,

// DataDrop

// Gets the index of the segment being dropped by a `data.drop` expression.
// Gets the name of the segment being dropped by a `data.drop` expression.
BINARYEN_API const char* BinaryenDataDropGetSegment(BinaryenExpressionRef expr);
// Sets the index of the segment being dropped by a `data.drop` expression.
// Sets the name of the segment being dropped by a `data.drop` expression.
BINARYEN_API void BinaryenDataDropSetSegment(BinaryenExpressionRef expr,
const char* segment);

Expand Down Expand Up @@ -2906,14 +2906,17 @@ BINARYEN_API BinaryenElementSegmentRef
BinaryenGetElementSegmentByIndex(BinaryenModuleRef module, BinaryenIndex index);

// This will create a memory, overwriting any existing memory
// Each memory has data in segments, a start offset in segmentOffsets, and a
// size in segmentSizes. exportName can be NULL
// Each memory segment has a name in segmentNames, data in segmentDatas,
// 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
BINARYEN_API void BinaryenSetMemory(BinaryenModuleRef module,
BinaryenIndex initial,
BinaryenIndex maximum,
const char* exportName,
const char** segments,
bool* segmentPassive,
const char** segmentNames,
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps we could allow segmentNames to be NULL, and then we autogenerate the names? That's much simpler for people that don't need the names, equally as simple as before.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed, done.

Copy link
Member

Choose a reason for hiding this comment

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

Please document this change in the comment before this declaration.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

const char** segmentDatas,
bool* segmentPassives,
BinaryenExpressionRef* segmentOffsets,
BinaryenIndex* segmentSizes,
BinaryenIndex numSegments,
Expand All @@ -2940,14 +2943,14 @@ BINARYEN_API bool BinaryenMemoryIs64(BinaryenModuleRef module,
// Memory segments. Query utilities.

BINARYEN_API uint32_t BinaryenGetNumMemorySegments(BinaryenModuleRef module);
BINARYEN_API uint32_t
BinaryenGetMemorySegmentByteOffset(BinaryenModuleRef module, BinaryenIndex id);
BINARYEN_API uint32_t BinaryenGetMemorySegmentByteOffset(
BinaryenModuleRef module, const char* segmentName);
BINARYEN_API size_t BinaryenGetMemorySegmentByteLength(BinaryenModuleRef module,
BinaryenIndex id);
const char* segmentName);
BINARYEN_API bool BinaryenGetMemorySegmentPassive(BinaryenModuleRef module,
BinaryenIndex id);
const char* segmentName);
BINARYEN_API void BinaryenCopyMemorySegmentData(BinaryenModuleRef module,
BinaryenIndex id,
const char* segmentName,
char* buffer);

// Start function. One per module
Expand Down
53 changes: 28 additions & 25 deletions src/js/binaryen.js-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -2565,31 +2565,34 @@ function wrapModule(module, self = {}) {
// segments are assumed to be { passive: bool, offset: expression ref, data: array of 8-bit data }
return preserveStack(() => {
const segmentsLen = segments.length;
const segmentData = new Array(segmentsLen);
const segmentDataLen = new Array(segmentsLen);
const segmentPassive = new Array(segmentsLen);
const segmentOffset = new Array(segmentsLen);
const names = new Array(segmentsLen);
const datas = new Array(segmentsLen);
const lengths = new Array(segmentsLen);
const passives = new Array(segmentsLen);
const offsets = new Array(segmentsLen);
for (let i = 0; i < segmentsLen; i++) {
const { data, offset, passive } = segments[i];
segmentData[i] = _malloc(data.length);
HEAP8.set(data, segmentData[i]);
segmentDataLen[i] = data.length;
segmentPassive[i] = passive;
segmentOffset[i] = offset;
const { name, data, offset, passive } = segments[i];
names[i] = name ? strToStack(name) : null;
datas[i] = _malloc(data.length);
HEAP8.set(data, datas[i]);
lengths[i] = data.length;
passives[i] = passive;
offsets[i] = offset;
}
const ret = Module['_BinaryenSetMemory'](
module, initial, maximum, strToStack(exportName),
i32sToStack(segmentData),
i8sToStack(segmentPassive),
i32sToStack(segmentOffset),
i32sToStack(segmentDataLen),
segmentsLen,
shared,
memory64,
strToStack(internalName)
);
i32sToStack(names),
i32sToStack(datas),
i8sToStack(passives),
i32sToStack(offsets),
i32sToStack(lengths),
segmentsLen,
shared,
memory64,
strToStack(internalName)
);
for (let i = 0; i < segmentsLen; i++) {
_free(segmentData[i]);
_free(datas[i]);
}
return ret;
});
Expand All @@ -2613,18 +2616,18 @@ function wrapModule(module, self = {}) {
self['getNumMemorySegments'] = function() {
return Module['_BinaryenGetNumMemorySegments'](module);
};
self['getMemorySegmentInfoByIndex'] = function(id) {
const passive = Boolean(Module['_BinaryenGetMemorySegmentPassive'](module, id));
self['getMemorySegmentInfo'] = function(name) {
const passive = Boolean(Module['_BinaryenGetMemorySegmentPassive'](module, strToStack(name)));
let offset = null;
if (!passive) {
offset = Module['_BinaryenGetMemorySegmentByteOffset'](module, id);
offset = Module['_BinaryenGetMemorySegmentByteOffset'](module, strToStack(name));
}
return {
'offset': offset,
'data': (function(){
const size = Module['_BinaryenGetMemorySegmentByteLength'](module, id);
const size = Module['_BinaryenGetMemorySegmentByteLength'](module, strToStack(name));
const ptr = _malloc(size);
Module['_BinaryenCopyMemorySegmentData'](module, id, ptr);
Module['_BinaryenCopyMemorySegmentData'](module, strToStack(name), ptr);
const res = new Uint8Array(size);
res.set(HEAP8.subarray(ptr, ptr + size));
_free(ptr);
Expand Down
12 changes: 9 additions & 3 deletions test/binaryen.js/kitchen-sink.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,13 @@ function test_core() {
// Memory
module.setMemory(1, 256, "mem", [
{
name: "x0",
passive: false,
offset: module.i32.const(10),
data: "hello, world".split('').map(function(x) { return x.charCodeAt(0) })
},
{
name: "y1",
passive: true,
offset: null,
data: "I am passive".split('').map(function(x) { return x.charCodeAt(0) })
Expand Down Expand Up @@ -555,8 +557,8 @@ function test_core() {
module.i8x16.shuffle(module.v128.const(v128_bytes), module.v128.const(v128_bytes), v128_bytes),
module.v128.bitselect(module.v128.const(v128_bytes), module.v128.const(v128_bytes), module.v128.const(v128_bytes)),
// Bulk memory
module.memory.init("0", makeInt32(1024), makeInt32(0), makeInt32(12)),
module.data.drop("0"),
module.memory.init("x0", makeInt32(1024), makeInt32(0), makeInt32(12)),
module.data.drop("x0"),
module.memory.copy(makeInt32(2048), makeInt32(1024), makeInt32(12)),
module.memory.fill(makeInt32(0), makeInt32(42), makeInt32(1024)),
// All the rest
Expand Down Expand Up @@ -1090,6 +1092,7 @@ function test_for_each() {
}

var expected_offsets = [10, 125, null];
var expected_names = ["x0", "y1", "z2"];
var expected_data = ["hello, world", "segment data 2", "hello, passive"];
var expected_passive = [false, false, true];

Expand All @@ -1105,23 +1108,26 @@ function test_for_each() {

module.setMemory(1, 256, "mem", [
{
name: expected_names[0],
passive: expected_passive[0],
offset: module.i32.const(expected_offsets[0]),
data: expected_data[0].split('').map(function(x) { return x.charCodeAt(0) })
},
{
name: expected_names[1],
passive: expected_passive[1],
offset: module.global.get("a-global"),
data: expected_data[1].split('').map(function(x) { return x.charCodeAt(0) })
},
{
name: expected_names[2],
passive: expected_passive[2],
offset: expected_offsets[2],
data: expected_data[2].split('').map(function(x) { return x.charCodeAt(0) })
}
], false);
for (i = 0; i < module.getNumMemorySegments(); i++) {
var segment = module.getMemorySegmentInfoByIndex(i);
var segment = module.getMemorySegmentInfo(expected_names[i]);
assert(expected_offsets[i] === segment.offset);
var data8 = new Uint8Array(segment.data);
var str = String.fromCharCode.apply(null, data8);
Expand Down
22 changes: 11 additions & 11 deletions test/binaryen.js/kitchen-sink.js.txt
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ getExpressionInfo(tuple[3])={"id":14,"type":5,"value":3.7}
(import "module" "base" (tag $a-tag-imp (param i32)))
(global $a-global i32 (i32.const 1))
(memory $0 1 256 shared)
(data $0 (i32.const 10) "hello, world")
(data $1 "I am passive")
(data $x0 (i32.const 10) "hello, world")
(data $y1 "I am passive")
(table $t0 1 funcref)
(elem $e0 (i32.const 0) $"kitchen()sinker")
(tag $a-tag (param i32))
Expand Down Expand Up @@ -1914,12 +1914,12 @@ getExpressionInfo(tuple[3])={"id":14,"type":5,"value":3.7}
(v128.const i32x4 0x04030201 0x08070605 0x0c0b0a09 0x100f0e0d)
)
)
(memory.init $0
(memory.init $x0
(i32.const 1024)
(i32.const 0)
(i32.const 12)
)
(data.drop $0)
(data.drop $x0)
(memory.copy
(i32.const 2048)
(i32.const 1024)
Expand Down Expand Up @@ -2250,8 +2250,8 @@ getExpressionInfo(tuple[3])={"id":14,"type":5,"value":3.7}
(import "module" "base" (tag $a-tag-imp (param i32)))
(global $a-global i32 (i32.const 1))
(memory $0 1 256 shared)
(data $0 (i32.const 10) "hello, world")
(data $1 "I am passive")
(data $x0 (i32.const 10) "hello, world")
(data $y1 "I am passive")
(table $t0 1 funcref)
(elem $e0 (i32.const 0) $"kitchen()sinker")
(tag $a-tag (param i32))
Expand Down Expand Up @@ -4024,12 +4024,12 @@ getExpressionInfo(tuple[3])={"id":14,"type":5,"value":3.7}
(v128.const i32x4 0x04030201 0x08070605 0x0c0b0a09 0x100f0e0d)
)
)
(memory.init $0
(memory.init $x0
(i32.const 1024)
(i32.const 0)
(i32.const 12)
)
(data.drop $0)
(data.drop $x0)
(memory.copy
(i32.const 2048)
(i32.const 1024)
Expand Down Expand Up @@ -4922,9 +4922,9 @@ sizeof Literal: 24
(global $a-global2 i32 (i32.const 2))
(global $a-global3 i32 (i32.const 3))
(memory $0 1 256)
(data $0 (i32.const 10) "hello, world")
(data $1 (global.get $a-global) "segment data 2")
(data $2 "hello, passive")
(data $x0 (i32.const 10) "hello, world")
(data $y1 (global.get $a-global) "segment data 2")
(data $z2 "hello, passive")
(table $t0 1 funcref)
(elem $e0 (i32.const 0) $fn0 $fn1 $fn2)
(export "export0" (func $fn0))
Expand Down
1 change: 1 addition & 0 deletions test/binaryen.js/reloc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var module = new binaryen.Module();
module.addGlobalImport("memory_base", "env", "memory_base", binaryen.i32, false);
module.setMemory(1, -1, null, [
{
name: "x0",
offset: module.global.get("memory_base", binaryen.i32),
data: "data data".split('').map(function(x) { return x.charCodeAt(0) })
}
Expand Down
2 changes: 1 addition & 1 deletion test/binaryen.js/reloc.js.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(import "env" "memory_base" (global $memory_base i32))
(import "env" "table_base" (global $table_base i32))
(memory $0 1)
(data $0 (global.get $memory_base) "data data")
(data $x0 (global.get $memory_base) "data data")
(table $0 1 funcref)
(elem $0 (global.get $table_base) $func $func)
(func $func
Expand Down
Loading