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

34 lines
932 B
JavaScript
Raw Normal View History

2024-07-29 04:15:54 +02:00
// 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) {
console.log("input: " + inputText);
var inputPtr = str2C(inputText);
var resultPointer = Module2._eval(inputPtr);
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 = Module2.getValue(resultPointer + i, 'i8');
if (charCode === 0) break;
resultString += String.fromCharCode(charCode);
i++;
}
}
console.log("Result from eval:", resultString);
// Free allocated memory
_free(resultPointer);
return resultString;
}