Skip to content
Merged
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
26 changes: 20 additions & 6 deletions src/runtime_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@
* SPDX-License-Identifier: MIT
*/

// This gives correct answers for everything less than 2^{14} = 16384
// I hope nobody is contemplating functions with 16384 arguments...
function uleb128Encode(n) {
#if ASSERTIONS
assert(n < 16384);
#endif
if (n < 128) {
return [n];
}
return [(n % 128) | 128, n >> 7];
}


// Wraps a JS function as a wasm function with a given signature.
function convertJsFunctionToWasm(func, sig) {
#if WASM2JS
Expand Down Expand Up @@ -34,8 +47,6 @@ function convertJsFunctionToWasm(func, sig) {
// The module is static, with the exception of the type section, which is
// generated based on the signature passed in.
var typeSection = [
0x01, // id: section,
0x00, // length: 0 (placeholder)
0x01, // count: 1
0x60, // form: func
];
Expand All @@ -49,7 +60,7 @@ function convertJsFunctionToWasm(func, sig) {
};

// Parameters, length + signatures
typeSection.push(sigParam.length);
typeSection = typeSection.concat(uleb128Encode(sigParam.length));
for (var i = 0; i < sigParam.length; ++i) {
typeSection.push(typeCodes[sigParam[i]]);
}
Expand All @@ -62,9 +73,12 @@ function convertJsFunctionToWasm(func, sig) {
typeSection = typeSection.concat([0x01, typeCodes[sigRet]]);
}

// Write the overall length of the type section back into the section header
// (excepting the 2 bytes for the section id and length)
typeSection[1] = typeSection.length - 2;
// Write the section code and overall length of the type section into the
// section header
typeSection = [0x01 /* Type section code */].concat(
uleb128Encode(typeSection.length),
typeSection
);

// Rest of the module is static
var bytes = new Uint8Array([
Expand Down