Skip to content

Commit

Permalink
feat: Extract memorySegment functions into individual functions
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Jun 24, 2022
1 parent 85ee0fb commit a077c7a
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions src/js/binaryen.js-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -2518,23 +2518,34 @@ function wrapModule(module, self = {}) {
self['getNumMemorySegments'] = function() {
return Module['_BinaryenGetNumMemorySegments'](module);
};

self['memorySegment'] = {
'offset'(id) {
return Module['_BinaryenGetMemorySegmentByteOffset'](module, id);
},
'data'(id) {
const size = Module['_BinaryenGetMemorySegmentByteLength'](module, id);
const ptr = _malloc(size);
Module['_BinaryenCopyMemorySegmentData'](module, id, ptr);
const res = new Uint8Array(size);
res.set(new Uint8Array(buffer, ptr, size));
_free(ptr);
return res.buffer;
},
'passive'() {
return Boolean(Module['_BinaryenGetMemorySegmentPassive'](module, id));
}
}

self['getMemorySegmentInfoByIndex'] = function(id) {
const passive = Boolean(Module['_BinaryenGetMemorySegmentPassive'](module, id));
const passive = self['memorySegment']['passive'](id);
let offset = null;
if (!passive) {
offset = Module['_BinaryenGetMemorySegmentByteOffset'](module, id);
offset = self['memorySegment']['offset'](id)
}
return {
'offset': offset,
'data': (function(){
const size = Module['_BinaryenGetMemorySegmentByteLength'](module, id);
const ptr = _malloc(size);
Module['_BinaryenCopyMemorySegmentData'](module, id, ptr);
const res = new Uint8Array(size);
res.set(new Uint8Array(buffer, ptr, size));
_free(ptr);
return res.buffer;
})(),
'data': self['memorySegment']['data'](id),
'passive': passive
};
};
Expand Down

0 comments on commit a077c7a

Please sign in to comment.