summaryrefslogtreecommitdiff
path: root/runtime.c
diff options
context:
space:
mode:
Diffstat (limited to 'runtime.c')
-rw-r--r--runtime.c173
1 files changed, 145 insertions, 28 deletions
diff --git a/runtime.c b/runtime.c
index a00e301..144fbc2 100644
--- a/runtime.c
+++ b/runtime.c
@@ -730,6 +730,7 @@ void resize_dynamic_array_exact(i64 *num, void **data, i64 element_size, i64 new
void resize_dynamic_array_capacity(i64 *num, i64 *capacity, void **data, i64 element_size, i64 new_num);
#ifdef RUNTIME_HEADER
+// NOTE: Platform is a global object because we can't create multiple windows on Web.
extern Platform g_platform;
#endif
@@ -776,14 +777,21 @@ enum {
//
// ================================================================
-static void mem_set_(void *dst, u8 x, i64 size) {
- for (i64 i = 0; i < size; ++i)
- ((u8 *) dst)[i] = x;
+static void mem_set_(void *dst, u8 x, i64 len) {
+ u8 *d = (u8 *) dst;
+ u8 *d_end = d + len;
+
+ for (; d < d_end; ++d)
+ *d = x;
}
-static void mem_cpy_(void *dst, void *src, i64 size) {
- for (i64 i = 0; i < size; ++i)
- ((u8 *) dst)[i] = ((u8 *) src)[i];
+static void mem_cpy_(void *dst, void *src, i64 len) {
+ u8 *s = (u8 *) src;
+ u8 *d = (u8 *) dst;
+ u8 *d_end = d + len;
+
+ for (; d < d_end; ++s, ++d)
+ *d = *s;
}
static i32 min2_i32_(i32 x, i32 y) {
@@ -926,6 +934,10 @@ void log_impl(i32 mode, i32 file_len, c8 const *file, i32 line, i32 func_len, c8
//
// BLAKE2B
//
+// TODO:
+// - Check correctness for arguments.
+// - Change API to be similar to other procs.
+//
// ================================================================
enum blake2b_constant {
@@ -967,17 +979,17 @@ static u64 blake2b_IV[8] = {
};
static u8 blake2b_sigma[12][16] = {
- { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } ,
- { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 } ,
- { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 } ,
- { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 } ,
- { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 } ,
- { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 } ,
- { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 } ,
- { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 } ,
- { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 } ,
- { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0 } ,
- { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } ,
+ { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
+ { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 },
+ { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 },
+ { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 },
+ { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 },
+ { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 },
+ { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 },
+ { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 },
+ { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 },
+ { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0 },
+ { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
{ 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 }
};
@@ -2088,13 +2100,14 @@ static void store_clipboard_text_(i64 size, c8 *data) {
// ================================================================
//
+// PLATFORM-SPECIFIC CODE
+//
+// ================================================================
+//
// Dynamic libraries
//
// ================================================================
-#if defined(__unix__)
-#include <dlfcn.h>
-
typedef struct {
void *handle;
} Dynamic_Library_Slot_;
@@ -2106,6 +2119,9 @@ typedef struct {
static Dynamic_Libraries_ _dynamic_libraries = {0};
+#if defined(__unix__)
+#include <dlfcn.h>
+
void dynamic_library_open(u16 slot, c8 *name) {
if (name == NULL) {
LOG_error("Sanity");
@@ -2155,7 +2171,7 @@ void *dynamic_library_get_proc_address(u16 slot, c8 *proc) {
void *proc_address = dlsym(_dynamic_libraries.slots[slot].handle, proc);
if (proc_address == NULL) {
- LOG_error("Failed to get: %s", proc);
+ LOG_error("Failed to get proc: %s", proc);
LOG_error("%s", dlerror());
}
@@ -2168,10 +2184,100 @@ static void close_all_dynamic_libraries_(void) {
dlclose(_dynamic_libraries.slots[i].handle);
resize_dynamic_array_exact(&_dynamic_libraries.num_slots, (void **) &_dynamic_libraries.slots, sizeof *_dynamic_libraries.slots, 0);
}
-#endif // defined(__unix__)
-#if defined(__wasm__)
+#elif defined(_WIN32)
+
+// Windows definitions
+#if !defined(_INC_WINDOWS)
+ #ifndef WINAPI
+ #if defined(_ARM_)
+ #define WINAPI
+ #else
+ #define WINAPI __stdcall
+ #endif
+ #endif
+
+ typedef c8 const *LPCSTR;
+ typedef void ** HMODULE;
+ typedef int WINBOOL;
+ typedef iptr (WINAPI *FARPROC)();
+
+ // Kernel32.dll
+ HMODULE WINAPI LoadLibraryA (LPCSTR lpLibFileName);
+ WINBOOL WINAPI FreeLibrary (HMODULE hLibModule);
+ FARPROC WINAPI GetProcAddress (HMODULE hModule, LPCSTR lpProcName);
+ DWORD WINAPI GetLastError (void);
+#endif
+
void dynamic_library_open(u16 slot, c8 *name) {
+ if (name == NULL) {
+ LOG_error("Sanity");
+ return;
+ }
+
+ if ((i64) (u64) slot >= _dynamic_libraries.num_slots)
+ resize_dynamic_array_exact(&_dynamic_libraries.num_slots, (void **) &_dynamic_libraries.slots, sizeof *_dynamic_libraries.slots, (i64) slot + 1);
+
+ if ((i64) (u64) slot >= _dynamic_libraries.num_slots) {
+ LOG_error("Invalid slot: %d", (i32) slot);
+ return;
+ }
+
+ if (_dynamic_libraries.slots[slot].handle != NULL) {
+ FreeLibrary((HMODULE) _dynamic_libraries.slots[slot].handle);
+ _dynamic_libraries.slots[slot].handle = NULL;
+ }
+
+ void *h = (void *) LoadLibraryA(name);
+
+ if (h == NULL) {
+ LOG_error("Failed to open: %s", name);
+ LOG_error("%s", dlerror());
+ return;
+ }
+
+ _dynamic_libraries.slots[slot].handle = h;
+}
+
+void *dynamic_library_get_proc_address(u16 slot, c8 *proc) {
+ if ((i64) (u64) slot >= _dynamic_libraries.num_slots) {
+ LOG_error("Invalid slot: %d", (i32) slot);
+ return NULL;
+ }
+
+ if (proc == NULL) {
+ LOG_error("Sanity");
+ return NULL;
+ }
+
+ if (_dynamic_libraries.slots[slot].handle == NULL) {
+ LOG_error("Slot is closed: %d", (i32) slot);
+ return NULL;
+ }
+
+ void *proc_address = GetProcAddress((HMODULE) _dynamic_libraries.slots[slot].handle, proc);
+
+ if (proc_address == NULL)
+ LOG_error("Failed to get proc: %s (error code %d)", proc, GetLastError());
+
+ return proc_address;
+}
+
+static void close_all_dynamic_libraries_(void) {
+ for (i64 i = 0; i < _dynamic_libraries.num_slots; ++i)
+ if (_dynamic_libraries.slots[i].handle != NULL)
+ FreeLibrary((HMODULE) _dynamic_libraries.slots[i].handle);
+ resize_dynamic_array_exact(&_dynamic_libraries.num_slots, (void **) &_dynamic_libraries.slots, sizeof *_dynamic_libraries.slots, 0);
+}
+
+#elif defined(__wasm__)
+
+void dynamic_library_open(u16 slot, c8 *name) {
+ if (name == NULL) {
+ LOG_error("Sanity");
+ return;
+ }
+
(void) slot;
(void) name;
@@ -2179,18 +2285,29 @@ void dynamic_library_open(u16 slot, c8 *name) {
}
void *dynamic_library_get_proc_address(u16 slot, c8 *proc) {
+ if ((i64) (u64) slot >= _dynamic_libraries.num_slots) {
+ LOG_error("Invalid slot: %d", (i32) slot);
+ return NULL;
+ }
+
+ if (proc == NULL) {
+ LOG_error("Sanity");
+ return NULL;
+ }
+
(void) slot;
(void) proc;
LOG_error("Proc address not found: %s", proc);
return NULL;
}
-#endif // defined(__wasm__)
-// ================================================================
-//
-// PLATFORM-SPECIFIC CODE
-//
+static void close_all_dynamic_libraries_(void) {
+ resize_dynamic_array_exact(&_dynamic_libraries.num_slots, (void **) &_dynamic_libraries.slots, sizeof *_dynamic_libraries.slots, 0);
+}
+
+#endif
+
// ================================================================
//
// Time