33 lines
870 B
JavaScript
33 lines
870 B
JavaScript
|
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;
|
|||
|
}
|