use core {package, *}
use simd {*}
-#if runtime.compiler_os == .Linux {
- #if #defined (runtime.vars.CC) {
- Linux_Compiler :: runtime.vars.CC
- } else {
- Linux_Compiler :: "/usr/bin/gcc"
+#if #defined (runtime.vars.CC) {
+ #local Compiler :: runtime.vars.CC
+} else {
+ #if runtime.compiler_os == .Linux {
+ #local Compiler :: "/usr/bin/gcc"
+ #local Library_Suffix :: "so"
+ }
+ #if runtime.compiler_os == .MacOS {
+ #local Compiler :: "clang"
+ #local Library_Suffix :: "dylib"
}
}
includes: [] str = .[],
libraries: [] str = .[],
flags := "") -> bool {
- #if runtime.compiler_os == .Linux {
+ #if runtime.compiler_os == .Linux || runtime.compiler_os == .MacOS {
args: [..] str;
args << "-shared";
args << "-fPIC";
args << path;
- args << "-I";
- args << "/usr/share/onyx/include";
+ onyx_path := os.env("ONYX_PATH");
+ if !onyx_path {
+ logf(.Warning, "ONYX_PATH environment variable was not declared. This likely won't compile.");
+ } else {
+ args << "-I";
+ args << tprintf("{}/include", onyx_path->unwrap());
+ }
for includes {
args << "-I";
}
args << "-o";
- args << tprintf("{}.so", dest);
+ args << tprintf("{}.{}", dest, Library_Suffix);
for libraries {
args << aprintf("-l{}", it);
}
- proc := os.process_spawn(Linux_Compiler, args);
+ proc := os.process_spawn(Compiler, args);
defer os.process_destroy(&proc);
proc_reader := io.reader_make(&proc);
}
#local {
-
write_file_introduction :: (writer: &io.Writer, preamble: [] str, name: str) {
io.write_format(writer, "//\n");
io.write_format(writer, "// THIS FILE WAS AUTOMATICALLY GENERATED.\n");
#define ONYX_UNPTR(p) ((int) (p != NULL ? ((char *) p - runtime->wasm_memory_data(runtime->wasm_memory)) : 0))
-#ifdef ONYX_HEAP_FUNCTIONS
-
-static wasm_func_t* __onyx_heap_resize_function = NULL;
-static void *__onyx_heap_resize(void* ptr, int size) {
- if (__onyx_heap_resize_function == NULL) {
- wasm_extern_t *__extern = runtime->wasm_extern_lookup_by_name(runtime->wasm_module, runtime->wasm_instance, "__heap_resize");
- __onyx_heap_resize_function = runtime->wasm_extern_as_func(__extern);
- }
-
- int onyx_ptr = ONYX_UNPTR(ptr);
-
- wasm_val_t args[] = { WASM_I32_VAL(onyx_ptr), WASM_I32_VAL(size) };
- wasm_val_t results[1];
- wasm_val_vec_t args_arr = WASM_ARRAY_VEC(args);
- wasm_val_vec_t results_arr = WASM_ARRAY_VEC(results);
-
- runtime->wasm_func_call(__onyx_heap_resize_function, &args_arr, &results_arr);
- return ONYX_PTR(results[0].of.i32);
-}
-
-static wasm_func_t* __onyx_heap_free_function = NULL;
-static void __onyx_heap_free(void *ptr) {
- if (__onyx_heap_free_function == NULL) {
- wasm_extern_t *__extern = runtime->wasm_extern_lookup_by_name(runtime->wasm_module, runtime->wasm_instance, "__heap_free");
- __onyx_heap_free_function = runtime->wasm_extern_as_func(__extern);
- }
-
- if (ptr == NULL) return;
- int onyx_ptr = ONYX_UNPTR(ptr);
-
- wasm_val_t args[] = { WASM_I32_VAL(onyx_ptr) };
- wasm_val_vec_t results = {0,0};
- wasm_val_vec_t args_arr = WASM_ARRAY_VEC(args);
-
- runtime->wasm_func_call(__onyx_heap_free_function, &args_arr, &results);
-}
-
-
-#endif
+//
+// Below are definitions that allow you to use the heap_resize and heap_free
+// functions to allocate and free memory inside Onyx's memory space, but from
+// a C-library. These functions were very useful, but they had some nasty
+// problems that cannot be easily addressed.
+//
+// The largest problem was that the WASM embedder made no guarantees about how
+// the underlying memory used would be positioned or when it could relocate.
+// If a C-library were to invoke heap_resize, and that resize caused the
+// memory_grow intrinsic to be invoked, the embedder could move the memory to
+// a new address. This would mean that ALL existing pointers into Onyx's memory
+// given to the C-library would be invalidated, almost always causing a segfault.
+//
+// There isn't a great way around this, since the idea of wrapping a C library
+// isn't necessarily supported by WASM at any level. The current work around
+// is to have the C library allocate using malloc(), then return the pointer
+// as a cptr(T), and then have Onyx copy that to a new buffer, and immediately
+// free the cptr(T), generally using a destructor of some kind from the C library.
+//
+
+// #ifdef ONYX_HEAP_FUNCTIONS
+
+// static wasm_func_t* __onyx_heap_resize_function = NULL;
+// static void *__onyx_heap_resize(void* ptr, int size) {
+// if (__onyx_heap_resize_function == NULL) {
+// wasm_extern_t *__extern = runtime->wasm_extern_lookup_by_name(runtime->wasm_module, runtime->wasm_instance, "__heap_resize");
+// __onyx_heap_resize_function = runtime->wasm_extern_as_func(__extern);
+// }
+
+// int onyx_ptr = ONYX_UNPTR(ptr);
+
+// wasm_val_t args[] = { WASM_I32_VAL(onyx_ptr), WASM_I32_VAL(size) };
+// wasm_val_t results[1];
+// wasm_val_vec_t args_arr = WASM_ARRAY_VEC(args);
+// wasm_val_vec_t results_arr = WASM_ARRAY_VEC(results);
+
+// runtime->wasm_func_call(__onyx_heap_resize_function, &args_arr, &results_arr);
+// return ONYX_PTR(results[0].of.i32);
+// }
+
+// static wasm_func_t* __onyx_heap_free_function = NULL;
+// static void __onyx_heap_free(void *ptr) {
+// if (__onyx_heap_free_function == NULL) {
+// wasm_extern_t *__extern = runtime->wasm_extern_lookup_by_name(runtime->wasm_module, runtime->wasm_instance, "__heap_free");
+// __onyx_heap_free_function = runtime->wasm_extern_as_func(__extern);
+// }
+
+// if (ptr == NULL) return;
+// int onyx_ptr = ONYX_UNPTR(ptr);
+
+// wasm_val_t args[] = { WASM_I32_VAL(onyx_ptr) };
+// wasm_val_vec_t results = {0,0};
+// wasm_val_vec_t args_arr = WASM_ARRAY_VEC(args);
+
+// runtime->wasm_func_call(__onyx_heap_free_function, &args_arr, &results);
+// }
+
+
+// #endif
#endif