// 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;
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);
onyx_docs_emit(&docs, context.options->documentation_file);
}
-#if 0
- types_dump_type_info();
-#endif
-
return ONYX_COMPILER_PROGRESS_SUCCESS;
}