Browse Source

Add initial Emscripten support. (#779)

João Matos 4 months ago
parent
commit
2e983a36d9
4 changed files with 20 additions and 1 deletions
  1. 8 0
      src/gc.c
  2. 8 1
      src/hl.h
  3. 2 0
      src/std/socket.c
  4. 2 0
      src/std/sys.c

+ 8 - 0
src/gc.c

@@ -27,6 +27,10 @@
 #	include <sys/mman.h>
 #endif
 
+#if defined(HL_EMSCRIPTEN)
+#	include <emscripten/heap.h>
+#endif
+
 #if defined(HL_VCC)
 #define DRAM_PREFETCH(addr) _mm_prefetch(p, 1)
 #elif defined(HL_CLANG) || defined (HL_GCC)
@@ -1181,6 +1185,8 @@ static void *gc_alloc_page_memory( int size ) {
 	return ptr;
 #elif defined(HL_CONSOLE)
 	return sys_alloc_align(size, GC_PAGE_SIZE);
+#elif defined(HL_EMSCRIPTEN)
+	return emscripten_builtin_memalign(GC_PAGE_SIZE, size);
 #else
 	static int recursions = 0;
 	int i = 0;
@@ -1233,6 +1239,8 @@ static void gc_free_page_memory( void *ptr, int size ) {
 	VirtualFree(ptr, 0, MEM_RELEASE);
 #elif defined(HL_CONSOLE)
 	sys_free_align(ptr,size);
+#elif defined(HL_EMSCRIPTEN)
+	emscripten_builtin_free(ptr);
 #else
 	pextra *e = extra_pages, *prev = NULL;
 	while( e ) {

+ 8 - 1
src/hl.h

@@ -58,6 +58,13 @@
 #	endif
 #endif
 
+#if defined(__EMSCRIPTEN__)
+#	define HL_EMSCRIPTEN
+#	ifndef _GNU_SOURCE
+#		define _GNU_SOURCE
+#	endif
+#endif
+
 #if defined(HL_IOS) || defined(HL_ANDROID) || defined(HL_TVOS)
 #	define HL_MOBILE
 #endif
@@ -86,7 +93,7 @@
 #	define HL_BSD
 #endif
 
-#if defined(_64BITS) || defined(__x86_64__) || defined(_M_X64) || defined(__LP64__)
+#if defined(_64BITS) || defined(__x86_64__) || defined(_M_X64) || defined(__LP64__) || defined(__wasm64__)
 #	define HL_64
 #endif
 

+ 2 - 0
src/std/socket.c

@@ -41,7 +41,9 @@
 #	include <hl.h>
 #	include <posix/posix.h>
 #else
+#	ifndef _GNU_SOURCE
 #	define _GNU_SOURCE
+#	endif
 #	include <string.h>
 #	include <sys/types.h>
 #	include <sys/socket.h>

+ 2 - 0
src/std/sys.c

@@ -122,6 +122,8 @@ HL_PRIM vbyte *hl_sys_string() {
 	return (vbyte*)USTR("GNU/kFreeBSD");
 #elif defined(HL_LINUX)
 	return (vbyte*)USTR("Linux");
+#elif defined(HL_EMSCRIPTEN)
+	return (vbyte*)USTR("Emscripten");
 #else
 #error Unknown system string
 #endif