roc/crates/repl_wasm/src/repl_platform.c
Brian Carroll c50925240d
Merge pull request #5921 from roc-lang/wasm-repl-crash
report roc_panic to the user in the web repl
2023-10-25 19:22:10 +01:00

75 lines
1.6 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
A bare-bones Roc "platform" for REPL code, providing heap allocation for builtins.
*/
// Enable/disable printf debugging. Leave disabled to avoid bloating .wasm files and slowing down tests.
#define ENABLE_PRINTF 0
//--------------------------
void *roc_alloc(size_t size, unsigned int alignment)
{
void *allocated = malloc(size);
#if ENABLE_PRINTF
if (!allocated)
{
fprintf(stderr, "roc_alloc failed\n");
exit(1);
}
else
{
printf("roc_alloc allocated %d bytes with alignment %d at %p\n", size, alignment, allocated);
}
#endif
return allocated;
}
//--------------------------
void *roc_realloc(void *ptr, size_t new_size, size_t old_size,
unsigned int alignment)
{
#if ENABLE_PRINTF
printf("roc_realloc reallocated %p from %d to %d with alignment %zd\n",
ptr, old_size, new_size, alignment);
#endif
return realloc(ptr, new_size);
}
//--------------------------
void roc_dealloc(void *ptr, unsigned int alignment)
{
#if ENABLE_PRINTF
printf("roc_dealloc deallocated %p with alignment %zd\n", ptr, alignment);
#endif
free(ptr);
}
//--------------------------
extern void send_panic_msg_to_js(void *ptr, unsigned int panic_tag);
void roc_panic(void *ptr, unsigned int panic_tag)
{
send_panic_msg_to_js(ptr, panic_tag);
#if ENABLE_PRINTF
char *msg = (char *)ptr;
fprintf(stderr,
"Application crashed with message\n\n %s\n\nShutting down\n", msg);
#endif
abort();
}
//--------------------------
void *roc_memset(void *str, int c, size_t n)
{
return memset(str, c, n);
}