deprived-main-website/static/OpenBirchWasm/OpenBirchWrapper.js

38 lines
1.1 KiB
JavaScript

// This is old code but works, so i won't touch it ¯\_(ツ)_/¯
function str2C(s) {
var size = lengthBytesUTF8(s) + 1;
var ret = _malloc(size);
stringToUTF8Array(s, HEAP8, ret, size);
return ret;
}
window.runEval = function (inputText, stopAssignments = false) {
if (!stopAssignments) console.log("input: " + inputText);
var inputPtr = str2C(inputText);
var resultPointer = Module._eval(inputPtr, stopAssignments);
var resultString = "";
// Convert resultPointer (char*) to JavaScript string
// Here, we treat it as ASCII and convert byte by byte
if (resultPointer) {
var i = 0;
while (true) {
var charCode = Module.getValue(resultPointer + i, 'i8');
if (charCode === 0) break;
resultString += String.fromCharCode(charCode);
i++;
}
}
if (!stopAssignments) console.log("Result from eval:", resultString);
// Free allocated memory
_free(resultPointer);
return resultString;
}
window.resetOpenBirch = function () {
Module._resetState();
}