From: Brendan Hansen Date: Mon, 18 Oct 2021 02:33:20 +0000 (-0500) Subject: fixed a bug with memory segments initializing every time X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=4a5b508129531c7f0e49ea15a1ce556e04b952c8;p=onyx.git fixed a bug with memory segments initializing every time --- diff --git a/bin/onyx-js b/bin/onyx-js index cf631947..80c843d2 100755 --- a/bin/onyx-js +++ b/bin/onyx-js @@ -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; diff --git a/core/runtime/js.onyx b/core/runtime/js.onyx index 2ad858d7..d3bce96d 100644 --- a/core/runtime/js.onyx +++ b/core/runtime/js.onyx @@ -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); diff --git a/src/onyx.c b/src/onyx.c index 0532dd6b..9b3eca83 100644 --- a/src/onyx.c +++ b/src/onyx.c @@ -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; }