fixed a bug with memory segments initializing every time
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 18 Oct 2021 02:33:20 +0000 (21:33 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 18 Oct 2021 02:33:20 +0000 (21:33 -0500)
bin/onyx-js
core/runtime/js.onyx
src/onyx.c

index cf631947af9413e7a28fc5702700c2ea9ab5e1bb..80c843d242a6e3a96bb495b7919f0cb27f2ef639 100755 (executable)
@@ -53,20 +53,27 @@ if (isMainThread) {
 
     // main thread
     if (multi_threaded) {
-        wasm_memory = new WebAssembly.Memory({ initial: 1024, maximum: 65536, shared: true });
+        wasm_memory = new WebAssembly.Memory({ initial: 1024, maximum: 32768, shared: true });
         ENV.onyx.memory = wasm_memory;
-    }
 
-    WebAssembly.instantiate(new Uint8Array(wasm_bytes), ENV)
-        .then(res => {
-            wasm_instance = res.instance;
-            if (!multi_threaded) {
+        let data_module_bytes = fs.readFileSync(process.argv[2] + ".data");
+        WebAssembly.instantiate(new Uint8Array(data_module_bytes), ENV)
+            .then(_ => {
+                WebAssembly.instantiate(new Uint8Array(wasm_bytes), ENV)
+                    .then(res => {
+                        wasm_instance = res.instance;
+                        wasm_instance.exports._start();
+                    });
+            });
+
+    } else {
+        WebAssembly.instantiate(new Uint8Array(wasm_bytes), ENV)
+            .then(res => {
+                wasm_instance = res.instance;
                 wasm_memory = wasm_instance.exports.memory;
-            }
-
-            const lib = res.instance.exports;
-            lib._start();
-        });
+                wasm_instance.exports._start();
+            });
+    }
 
 } else {
     let { thread_id, memory, wasm_bytes, funcidx, dataptr } = workerData;
index 2ad858d7a896aa604aa4d96ac67c663e34b8aca6..d3bce96d68f65ce55721b80ae924bcc9b3d21138 100644 (file)
@@ -31,7 +31,6 @@ __exit          :: (status: i32) -> void #foreign "host" "exit" ---
     }
 
     #export "_thread_exit" (id: i32) {
-        // raw_free(context.allocator, __stack_top);
         raw_free(alloc.heap_allocator, __tls_base);
 
         thread.__exited(id);
index 0532dd6b7e696ee7ca95e71761fd95aa810a834c..9b3eca832fd22b3a346dd2444c9fc8af1ee7a02b 100644 (file)
@@ -501,7 +501,33 @@ static i32 onyx_compile() {
     if (context.options->verbose_output)
         bh_printf("Outputting to WASM file:   %s\n", output_file.filename);
 
-    onyx_wasm_module_write_to_file(context.wasm_module, output_file);
+    // APPARENTLY... the WebAssembly Threading proposal says that the data segment initializations
+    // in a WASM module are copied into the linear memory EVERY time the module is instantiated, not
+    // just the first time. This means that if we are happily chugging along and modifying global state
+    // and then we spawn a thread, that thread will completely wipe all changes to the global and return
+    // it to its original state. This is horrible obviously, but the only thing that is more horrible is
+    // that the best way around this is to create a second WASM module that simply initializes the given
+    // data section. Then have a section module that is actually your code. For right now, this is going
+    // to be fine since the browser is really the only place that multi-threading can be used to any
+    // degree of competency. But still... This is god awful and I hope that there is some other way to
+    // around this down the line.
+    if (context.options->use_multi_threading) {
+        bh_file data_file;
+        if (bh_file_create(&data_file, bh_aprintf(global_scratch_allocator, "%s.data", context.options->target_file)) != BH_FILE_ERROR_NONE)
+            return ONYX_COMPILER_PROGRESS_FAILED_OUTPUT;
+
+        OnyxWasmModule* data_module = bh_alloc_item(global_heap_allocator, OnyxWasmModule);
+        *data_module = onyx_wasm_module_create(global_heap_allocator);
+
+        data_module->data = context.wasm_module->data;
+        context.wasm_module->data = NULL;
+
+        onyx_wasm_module_write_to_file(data_module, data_file);
+        onyx_wasm_module_write_to_file(context.wasm_module, output_file);
+
+    } else {
+        onyx_wasm_module_write_to_file(context.wasm_module, output_file);
+    }
 
     u64 duration = bh_time_duration(start_time);
     
@@ -520,10 +546,6 @@ static i32 onyx_compile() {
         onyx_docs_emit(&docs, context.options->documentation_file);
     }
 
-#if 0
-    types_dump_type_info();
-#endif
-
     return ONYX_COMPILER_PROGRESS_SUCCESS;
 }