/* File: main.c Author: Taylor Robbins Date: 08\28\2025 Description: ** This file serves as the compilable file for all of CWasm. It may be #included by the app.c file */ #include "cwasm.h" #include "std/src/std_main.c" #include "cwasm_debug.c" #include "cwasm_vectors.c" #include "cwasm_matrices.c" #include "cwasm_arena.c" #if !CWASM_NO_STB_IMAGE #define STB_IMAGE_IMPLEMENTATION #define STBIDEF static #define STBI_NO_STDIO #define STBI_ASSERT(expression) Assert(expression) #define STBI_MALLOC(numBytes) AllocMem(&ScratchArenas[0], numBytes) #define STBI_REALLOC_SIZED(allocPntr, oldSize, newSize) ReallocMem(&ScratchArenas[0], (allocPntr), (oldSize), (newSize)) #define STBI_FREE(allocPntr) //nothing TODO: Once we have a general purpose arena we can implement this! #define STBI_NO_SIMD #define STBI_ONLY_PNG #define STBI_NO_THREAD_LOCALS #include "stb_image.h" #endif #include "cwasm_misc_js_imports.h" #include "cwasm_webgl_js_imports.h" #include "cwasm_webgl_constants.h" void InitializeCWasm(u32 scratchArenasSize) { InitGlobalArenas(scratchArenasSize); } static inline void SetLabel(const char* labelName, const char* displayStr) { jsSetLabel(labelName, displayStr); } void SetLabelPrint(const char* labelName, const char* formatStr, ...) { char* displayStr = nullptr; va_list args; va_start(args, formatStr); int length = vsnprintf(displayStr, 0, formatStr, args); //Measure first Assert(length >= 0); va_end(args); if (length == 0) { SetLabel(labelName, ""); return; } ScratchBegin(scratch); { displayStr = AllocArray(char, scratch, length+1); if (displayStr == nullptr) { SetLabel(labelName, ""); return; } va_start(args, formatStr); vsnprintf(displayStr, (size_t)(length+1), formatStr, args); //Real printf va_end(args); displayStr[length] = '\0'; SetLabel(labelName, displayStr); } ScratchEnd(scratch); } // +--------------------------------------------------------------+ // | Memory Allocation Functions for Javascript | // +--------------------------------------------------------------+ WASM_EXPORT(cAllocMem) void* cAllocMem(Arena* arenaPntr, int numBytes, int alignment) { NotNull(arenaPntr); Assert(numBytes >= 0); Assert(alignment >= 0); return AllocMemAligned(arenaPntr, (u32)numBytes, (u32)alignment); } WASM_EXPORT(cGetScratchArenaPntr) Arena* cGetScratchArenaPntr(Arena* conflictArenaPntr) { ArenaMark scratchMark = GetScratch1(conflictArenaPntr); return scratchMark.arena; } WASM_EXPORT(cGetScratchArenaMark) u32 cGetScratchArenaMark(Arena* conflictArenaPntr) { ArenaMark scratchMark = GetScratch1(conflictArenaPntr); return scratchMark.mark; } WASM_EXPORT(cEndScratchArena) void cEndScratchArena(Arena* arenaPntr, u32 mark) { ResetToMark(arenaPntr, mark); }