"use strict";
//import debug from "debug";
//const log = debug("dylink:util");
Object.defineProperty(exports, "__esModule", { value: true });
exports.MBtoPages = exports.alignMemory = exports.strlen = exports.recvString = exports.nonzeroPositions = void 0;
function nonzeroPositions(table) {
    var v = [];
    for (var i = 0; i < table.length; i++) {
        if (table.get(i) != null) {
            v.push(i);
        }
    }
    return v;
}
exports.nonzeroPositions = nonzeroPositions;
var textDecoder = new TextDecoder(); // utf-8
function recvString(charPtr, memory) {
    var len = strlen(charPtr, memory);
    var slice = memory.buffer.slice(charPtr, charPtr + len);
    return textDecoder.decode(slice);
}
exports.recvString = recvString;
function strlen(charPtr, memory) {
    var mem = new Uint8Array(memory.buffer);
    var i = charPtr;
    while (mem[i]) {
        i += 1;
    }
    return i - charPtr;
}
exports.strlen = strlen;
function alignMemory(size, alignment) {
    return Math.ceil(size / alignment) * alignment;
}
exports.alignMemory = alignMemory;
function MBtoPages(MB) {
    // "Note: A WebAssembly page has a constant size of 65,536 bytes, i.e., 64KiB." from
    // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Memory/Memory
    // There's thus 1025/64 = 16 pages in a MB.
    return MB * 16.;
}
exports.MBtoPages = MBtoPages;
//# sourceMappingURL=util.js.map