Browse Source

xxx: Add fallback for safari

Fabian 3 years ago
parent
commit
8107f26f10
2 changed files with 29 additions and 2 deletions
  1. 8 2
      Makefile
  2. 21 0
      src/browser/starter.js

+ 8 - 2
Makefile

@@ -65,16 +65,17 @@ CLOSURE_FLAGS=\
 		--language_in ECMASCRIPT_2017\
 		--language_out ECMASCRIPT_2017
 
-CARGO_FLAGS=\
+CARGO_FLAGS_SAFE=\
 		--target wasm32-unknown-unknown \
 		-- \
-		-C target-feature=+bulk-memory \
 		-C linker=tools/rust-lld-wrapper \
 		-C link-args="--import-table --global-base=262144 $(STRIP_DEBUG_FLAG)" \
 		-C link-args="build/softfloat.o" \
 		-C link-args="build/zstddeclib.o" \
 		--verbose
 
+CARGO_FLAGS=$(CARGO_FLAGS_SAFE) -C target-feature=+bulk-memory
+
 CORE_FILES=const.js config.js io.js main.js lib.js ide.js pci.js floppy.js \
 	   memory.js dma.js pit.js vga.js ps2.js pic.js rtc.js uart.js hpet.js \
 	   acpi.js apic.js ioapic.js \
@@ -179,6 +180,11 @@ build/v86-debug.wasm: $(RUST_FILES) build/softfloat.o build/zstddeclib.o Cargo.t
 	mv build/wasm32-unknown-unknown/debug/v86.wasm build/v86-debug.wasm
 	ls -lh build/v86-debug.wasm
 
+build/v86-fallback.wasm: $(RUST_FILES) build/softfloat.o build/zstddeclib.o Cargo.toml
+	mkdir -p build/
+	cargo +nightly rustc --release $(CARGO_FLAGS_SAFE)
+	mv build/wasm32-unknown-unknown/release/v86.wasm build/v86-fallback.wasm || true
+
 debug-with-profiler: $(RUST_FILES) build/softfloat.o build/zstddeclib.o Cargo.toml
 	mkdir -p build/
 	cargo +nightly rustc --features profiler $(CARGO_FLAGS)

+ 21 - 0
src/browser/starter.js

@@ -164,6 +164,7 @@ function V86Starter(options)
     };
 
     let v86_bin = DEBUG ? "v86-debug.wasm" : "v86.wasm";
+    let v86_bin_fallback = "v86-fallback.wasm";
 
     if(options["wasm_path"])
     {
@@ -172,10 +173,12 @@ function V86Starter(options)
     else if(typeof window === "undefined" && typeof __dirname === "string")
     {
         v86_bin = __dirname + "/" + v86_bin;
+        v86_bin_fallback = __dirname + "/" + v86_bin_fallback;
     }
     else
     {
         v86_bin = "build/" + v86_bin;
+        v86_bin_fallback = "build/" + v86_bin_fallback;
     }
 
     v86util.load_file(v86_bin, {
@@ -193,6 +196,24 @@ function V86Starter(options)
                     cpu = emulator.cpu;
 
                     this.continue_init(emulator, options);
+                }, err => {
+                    v86util.load_file(v86_bin_fallback, {
+                        done: bytes => {
+                            WebAssembly
+                                .instantiate(bytes, { "env": wasm_shared_funcs })
+                                .then(({ instance }) => {
+                                    const imports = wasm_shared_funcs;
+                                    const exports = instance["exports"];
+                                    wasm_memory = exports.memory;
+                                    exports["rust_init"]();
+
+                                    const emulator = this.v86 = new v86(this.emulator_bus, { exports, wasm_table });
+                                    cpu = emulator.cpu;
+
+                                    this.continue_init(emulator, options);
+                                });
+                        },
+                    });
                 });
         },
         progress: e =>