Skip to content

C/JS accessors for function table segments #2565

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
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
83 changes: 83 additions & 0 deletions src/binaryen-c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3561,6 +3561,89 @@ void BinaryenSetFunctionTable(BinaryenModuleRef module,
wasm->table.segments.push_back(segment);
}

// Function table segments. Query utilities.

uint32_t BinaryenGetNumFunctionTableSegments(BinaryenModuleRef module) {
if (tracing) {
std::cout << " BinaryenGetNumFunctionTableSegments(the_module);\n";
}

auto* wasm = (Module*)module;
return wasm->table.segments.size();
}

uint32_t BinaryenGetFunctionTableSegmentOffset(BinaryenModuleRef module,
BinaryenIndex id) {
if (tracing) {
std::cout << " BinaryenGetFunctionTableSegmentOffset(the_module, " << id
<< ");\n";
}

auto* wasm = (Module*)module;
if (wasm->table.segments.size() <= id) {
Fatal() << "invalid segment id.";
}

auto globalOffset = [&](const Expression* const& expr,
int64_t& result) -> bool {
if (auto* c = expr->dynCast<Const>()) {
result = c->value.getInteger();
return true;
}
return false;
};

const Table::Segment& segment = wasm->table.segments[id];

int64_t ret;
if (globalOffset(segment.offset, ret)) {
return ret;
}
if (auto* get = segment.offset->dynCast<GlobalGet>()) {
Global* global = wasm->getGlobal(get->name);
if (globalOffset(global->init, ret)) {
return ret;
}
}

Fatal() << "non-constant offsets aren't supported yet";
return 0;
}

size_t BinaryenGetFunctionTableSegmentLength(BinaryenModuleRef module,
BinaryenIndex id) {
if (tracing) {
std::cout << " BinaryenGetFunctionTableSegmentLength(the_module, " << id
<< ");\n";
}

auto* wasm = (Module*)module;
if (wasm->table.segments.size() <= id) {
Fatal() << "invalid segment id.";
}
const Table::Segment& segment = wasm->table.segments[id];
return segment.data.size();
}

const char* BinaryenGetFunctionTableSegmentEntry(BinaryenModuleRef module,
BinaryenIndex id,
size_t entry) {
if (tracing) {
std::cout << " BinaryenGetFunctionTableSegmentEntry(the_module, " << id
<< ", " << entry << ");\n";
}

auto* wasm = (Module*)module;
if (wasm->memory.segments.size() <= id) {
Fatal() << "invalid segment id.";
}
const Table::Segment& segment = wasm->table.segments[id];
if (segment.data.size() <= entry) {
Fatal() << "invalid segment entry index.";
}
return segment.data[entry].c_str();
}

// Memory. One per module

void BinaryenSetMemory(BinaryenModuleRef module,
Expand Down
13 changes: 13 additions & 0 deletions src/binaryen-c.h
Original file line number Diff line number Diff line change
Expand Up @@ -1198,6 +1198,19 @@ BINARYEN_API void BinaryenSetFunctionTable(BinaryenModuleRef module,
BinaryenIndex numFuncNames,
BinaryenExpressionRef offset);

// Function table segments. Query utilities.

BINARYEN_API uint32_t
BinaryenGetNumFunctionTableSegments(BinaryenModuleRef module);
BINARYEN_API uint32_t BinaryenGetFunctionTableSegmentOffset(
BinaryenModuleRef module, BinaryenIndex id);
BINARYEN_API size_t BinaryenGetFunctionTableSegmentLength(
BinaryenModuleRef module, BinaryenIndex id);
BINARYEN_API
const char* BinaryenGetFunctionTableSegmentEntry(BinaryenModuleRef module,
BinaryenIndex id,
size_t entry);

// Memory. One per module

// Each segment has data in segments, a start offset in segmentOffsets, and a
Expand Down
16 changes: 16 additions & 0 deletions src/js/binaryen.js-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -2158,6 +2158,22 @@ function wrapModule(module, self) {
);
});
};
self['getNumFunctionTableSegments'] = function() {
return Module['_BinaryenGetNumFunctionTableSegments'](module);
};
self['getFunctionTableSegmentInfoByIndex'] = function(id) {
return {
'offset': Module['_BinaryenGetFunctionTableSegmentOffset'](module, id),
'functions': (function(){
var size = Module['_BinaryenGetFunctionTableSegmentLength'](module, id);
var res = new Array(size);
for (var i = 0; i < size; i++) {
res[i] = UTF8ToString(Module['_BinaryenGetFunctionTableSegmentEntry'](module, id, i));
}
return res;
})()
};
};
self['setMemory'] = function(initial, maximum, exportName, segments, shared) {
// segments are assumed to be { passive: bool, offset: expression ref, data: array of 8-bit data }
if (!segments) segments = [];
Expand Down
15 changes: 15 additions & 0 deletions test/binaryen.js/kitchen-sink.js
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,21 @@ function test_for_each() {
assert(expected_data[i] === str);
}

var expected_table_offsets = [0];
var expected_table_functions = [
['fn0', 'fn1', 'fn2']
];
module.setFunctionTable(3, 3, ['fn0', 'fn1', 'fn2'])
assert(expected_table_offsets.length === module.getNumFunctionTableSegments());
for (i = 0 ; i < module.getNumFunctionTableSegments() ; i++) {
var segment = module.getFunctionTableSegmentInfoByIndex(i);
assert(expected_table_offsets[i] === segment.offset);
assert(expected_table_functions[i].length === segment.functions.length);
for (var j = 0; j < segment.functions.length; j++) {
assert(expected_table_functions[i][j] === segment.functions[j]);
}
}

console.log(module.emitText());
module.dispose();
}
Expand Down
2 changes: 2 additions & 0 deletions test/binaryen.js/kitchen-sink.js.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10317,6 +10317,8 @@ sizeof Literal: 24
(memory $0 1 256)
(data (i32.const 10) "hello, world")
(data (global.get $a-global) "segment data 2")
(table $0 3 3 funcref)
(elem (i32.const 0) $fn0 $fn1 $fn2)
(global $a-global i32 (i32.const 125))
(export "export0" (func $fn0))
(export "export1" (func $fn1))
Expand Down