From b061058065581cb649f12c0f99b3d3ab4c77d86c Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Fri, 8 Jul 2022 14:09:15 -0700 Subject: [PATCH] feat: Extract getElementSegmentInfo into individual functions --- src/js/binaryen.js-post.js | 44 ++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/src/js/binaryen.js-post.js b/src/js/binaryen.js-post.js index 9d874d5b55c..6d6212cbbd1 100644 --- a/src/js/binaryen.js-post.js +++ b/src/js/binaryen.js-post.js @@ -3296,17 +3296,16 @@ Module['getTableInfo'] = function(table) { }; Module['getElementSegmentInfo'] = function(segment) { - var segmentLength = Module['_BinaryenElementSegmentGetLength'](segment); + var segmentLength = Module['ElementSegment']['getLength'](segment); var names = new Array(segmentLength); for (let j = 0; j !== segmentLength; ++j) { - var ptr = Module['_BinaryenElementSegmentGetData'](segment, j); - names[j] = UTF8ToString(ptr); + names[j] = Module['ElementSegment']['getData'](segment, j); } return { - 'name': UTF8ToString(Module['_BinaryenElementSegmentGetName'](segment)), - 'table': UTF8ToString(Module['_BinaryenElementSegmentGetTable'](segment)), - 'offset': Module['_BinaryenElementSegmentGetOffset'](segment), + 'name': Module['ElementSegment']['getName'](segment), + 'table': Module['ElementSegment']['getTable'](segment), + 'offset': Module['ElementSegment']['getOffset'](segment), 'data': names } } @@ -4797,6 +4796,39 @@ Module['I31Get'] = makeExpressionWrapper({ } }); +// ElementSegment wrapper +Module['ElementSegment'] = (() => { + // Closure compiler doesn't allow multiple `Function`s at top-level, so: + function Function(func) { + if (!(this instanceof Function)) { + if (!func) return null; + return new Function(func); + } + if (!func) throw Error("function reference must not be null"); + this[thisPtr] = func; + } + Function['getLength'] = function(segment) { + Module['_BinaryenElementSegmentGetLength'](segment); + }; + Function['getData'] = function(segment, index) { + return UTF8ToString(Module['_BinaryenElementSegmentGetData'](segment, index)); + }; + Function['getName'] = function(segment) { + return UTF8ToString(Module['_BinaryenElementSegmentGetName'](segment)); + }; + Function['getTable'] = function(segment) { + return UTF8ToString(Module['_BinaryenElementSegmentGetTable'](segment)); + }; + Function['getOffset'] = function(segment) { + return Module['_BinaryenElementSegmentGetOffset'](segment); + }; + deriveWrapperInstanceMembers(Function.prototype, Function); + Function.prototype['valueOf'] = function() { + return this[thisPtr]; + }; + return Function; +})(); + // Export wrapper Module['Export'] = (() => { // Closure compiler doesn't allow multiple `Function`s at top-level, so: