74 lines
2.8 KiB
JavaScript
74 lines
2.8 KiB
JavaScript
/*
|
|
File: std_functions.c
|
|
Author: Taylor Robbins
|
|
Date: 09\01\2025
|
|
Description:
|
|
** Contains all the functions that are required as imports by the C standard library implementation in the std folder
|
|
*/
|
|
|
|
import { WASM_MEMORY_PAGE_SIZE, appGlobals } from './globals.js'
|
|
import { wasmPntrToJsString, wasmPntrAndLengthToJsString } from './wasm_functions.js'
|
|
|
|
export function jsStdPrint(level, messageStrPntr, messageLength)
|
|
{
|
|
let messageStr = wasmPntrAndLengthToJsString(messageStrPntr, messageLength);
|
|
if (level == 0) { console.debug(messageStr); }
|
|
else if (level == 1) { console.info(messageStr); }
|
|
else if (level == 2) { console.warn(messageStr); }
|
|
else if (level == 3) { console.error(messageStr); }
|
|
else { console.log(messageStr); }
|
|
}
|
|
|
|
export function jsStdAbort(messageStrPntr, exitCode)
|
|
{
|
|
let messageStr = wasmPntrToJsString(messageStrPntr);
|
|
let exitStr = "Abort [" + exitCode + "]: " + messageStr;
|
|
console.error(exitStr);
|
|
throw new Error(exitStr);
|
|
}
|
|
|
|
export function jsStdAssertFailure(filePathPntr, fileLineNum, funcNamePntr, conditionStrPntr, messageStrPntr)
|
|
{
|
|
let filePath = wasmPntrToJsString(filePathPntr);
|
|
let funcName = wasmPntrToJsString(funcNamePntr);
|
|
let conditionStr = wasmPntrToJsString(conditionStrPntr);
|
|
let outputMessage = "";
|
|
if (messageStrPntr != 0)
|
|
{
|
|
let messageStr = wasmPntrToJsString(messageStrPntr);
|
|
outputMessage = "Assertion failed, " + messageStr + " (" + conditionStr + ") is not true! In " + filePath + ":" + fileLineNum + " " + funcName + "(...)";
|
|
}
|
|
else
|
|
{
|
|
outputMessage = "Assertion failed! (" + conditionStr + ") is not true! In " + filePath + ":" + fileLineNum + " " + funcName + "(...)";
|
|
}
|
|
console.error(outputMessage);
|
|
throw new Error(outputMessage);
|
|
}
|
|
|
|
export function jsStdDebugBreak()
|
|
{
|
|
//TODO: This is not a proper solution, really. Can we somehow notify the debugger in Firefox/Chrome/Safari/etc.?
|
|
alert("A debug breakpoint has been hit!");
|
|
}
|
|
|
|
//TODO: Can we use these inside the Wasm module rather than calling out to javascript??
|
|
// __builtin_wasm_memory_size(0); // the number of 64Kb pages we have
|
|
// __builtin_wasm_memory_grow(0, blocks); // increases amount of pages
|
|
// __builtin_huge_valf(); // similar to Infinity in JS
|
|
export function jsStdGrowMemory(numPages)
|
|
{
|
|
let currentPageCount = appGlobals.wasmModule.exports.memory.buffer.byteLength / WASM_MEMORY_PAGE_SIZE;
|
|
// console.log("Memory growing by " + numPages + " pages (" + currentPageCount + " -> " + (currentPageCount + numPages) + ")");
|
|
appGlobals.wasmModule.exports.memory.grow(numPages);
|
|
appGlobals.memDataView = new DataView(new Uint8Array(appGlobals.wasmModule.exports.memory.buffer).buffer);
|
|
}
|
|
|
|
export let jsStdFunctions = {
|
|
jsStdPrint: jsStdPrint,
|
|
jsStdAbort: jsStdAbort,
|
|
jsStdAssertFailure: jsStdAssertFailure,
|
|
jsStdDebugBreak: jsStdDebugBreak,
|
|
jsStdGrowMemory: jsStdGrowMemory,
|
|
};
|