From 28c16ac6e6955b98f6485564d2c15f639bef5802 Mon Sep 17 00:00:00 2001 From: natrixaeria Date: Wed, 22 May 2019 22:00:23 +0200 Subject: Load wasm into the browser and execute it --- WebInterface/wasm/asm-paint/Cargo.toml | 27 --------------------------- WebInterface/wasm/asm-paint/deploy.py | 4 ++-- WebInterface/wasm/asm-paint/index.html | 2 +- WebInterface/wasm/asm-paint/loader.js | 18 ------------------ WebInterface/wasm/asm-paint/src/lib.rs | 19 ------------------- WebInterface/wasm/asm-paint/src/main.rs | 3 +++ WebInterface/wasm/asm-paint/src/utils.rs | 10 ---------- WebInterface/wasm/asm-paint/tests/web.rs | 13 ------------- 8 files changed, 6 insertions(+), 90 deletions(-) delete mode 100644 WebInterface/wasm/asm-paint/loader.js delete mode 100644 WebInterface/wasm/asm-paint/src/lib.rs create mode 100644 WebInterface/wasm/asm-paint/src/main.rs delete mode 100644 WebInterface/wasm/asm-paint/src/utils.rs delete mode 100644 WebInterface/wasm/asm-paint/tests/web.rs diff --git a/WebInterface/wasm/asm-paint/Cargo.toml b/WebInterface/wasm/asm-paint/Cargo.toml index 619630b..8a4cc92 100644 --- a/WebInterface/wasm/asm-paint/Cargo.toml +++ b/WebInterface/wasm/asm-paint/Cargo.toml @@ -4,31 +4,4 @@ version = "0.1.0" authors = ["Dennis Kobert "] edition = "2018" -[lib] -crate-type = ["cdylib", "rlib"] - -[features] -default = ["console_error_panic_hook"] - [dependencies] -wasm-bindgen = "0.2" - -# The `console_error_panic_hook` crate provides better debugging of panics by -# logging them with `console.error`. This is great for development, but requires -# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for -# code size when deploying. -console_error_panic_hook = { version = "0.1.1", optional = true } - -# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size -# compared to the default allocator's ~10K. It is slower than the default -# allocator, however. -# -# Unfortunately, `wee_alloc` requires nightly Rust when targeting wasm for now. -wee_alloc = { version = "0.4.2", optional = true } - -[dev-dependencies] -wasm-bindgen-test = "0.2" - -[profile.release] -# Tell `rustc` to optimize for small code size. -opt-level = "s" diff --git a/WebInterface/wasm/asm-paint/deploy.py b/WebInterface/wasm/asm-paint/deploy.py index f81ebb8..ed4e0dd 100644 --- a/WebInterface/wasm/asm-paint/deploy.py +++ b/WebInterface/wasm/asm-paint/deploy.py @@ -9,8 +9,8 @@ HTML_MIME = 'text/html' REQUESTS = { '/': ('index.html', HTML_MIME), - '/loader.js': ('loader.js', JS_MIME), - '/asm_paint_bg.wasm': ('pkg/asm_paint_bg.wasm', WASM_MIME) + '/asm-paint.js': ('target/wasm32-unknown-emscripten/release/asm-paint.js', JS_MIME), + '/asm_paint.wasm': ('target/wasm32-unknown-emscripten/release/asm_paint.wasm', WASM_MIME), } PAGE_404 = ''' diff --git a/WebInterface/wasm/asm-paint/index.html b/WebInterface/wasm/asm-paint/index.html index a0264cf..b03a295 100644 --- a/WebInterface/wasm/asm-paint/index.html +++ b/WebInterface/wasm/asm-paint/index.html @@ -3,7 +3,7 @@ Scribblio - + diff --git a/WebInterface/wasm/asm-paint/loader.js b/WebInterface/wasm/asm-paint/loader.js deleted file mode 100644 index 7313fa0..0000000 --- a/WebInterface/wasm/asm-paint/loader.js +++ /dev/null @@ -1,18 +0,0 @@ -console.log('js> create import object'); -let importObject = { imports: { my_alert: arg => alert(arg) } }; -//let importObject = {}; - -console.log('js> create fetch object'); - -let asm_paint_bg = fetch('asm_paint_bg.wasm'); - -console.log('js> instantiate streaming'); - -function and_then(obj) { - console.log('js> reached instantiate streaming\'s then'); - return obj.instance.exports.greet(); -} - -WebAssembly.instantiateStreaming(asm_paint_bg, importObject) - .then(and_then); - diff --git a/WebInterface/wasm/asm-paint/src/lib.rs b/WebInterface/wasm/asm-paint/src/lib.rs deleted file mode 100644 index a4f4f54..0000000 --- a/WebInterface/wasm/asm-paint/src/lib.rs +++ /dev/null @@ -1,19 +0,0 @@ -mod utils; - -use wasm_bindgen::prelude::*; - -// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global -// allocator. -#[cfg(feature = "wee_alloc")] -#[global_allocator] -static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; - -#[wasm_bindgen] -extern { - fn my_alert(s: &str); -} - -#[wasm_bindgen] -pub fn greet() { - my_alert("Hello, asm-paint!"); -} diff --git a/WebInterface/wasm/asm-paint/src/main.rs b/WebInterface/wasm/asm-paint/src/main.rs new file mode 100644 index 0000000..0d01b18 --- /dev/null +++ b/WebInterface/wasm/asm-paint/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("hello wasm world!"); +} diff --git a/WebInterface/wasm/asm-paint/src/utils.rs b/WebInterface/wasm/asm-paint/src/utils.rs deleted file mode 100644 index b1d7929..0000000 --- a/WebInterface/wasm/asm-paint/src/utils.rs +++ /dev/null @@ -1,10 +0,0 @@ -pub fn set_panic_hook() { - // When the `console_error_panic_hook` feature is enabled, we can call the - // `set_panic_hook` function at least once during initialization, and then - // we will get better error messages if our code ever panics. - // - // For more details see - // https://github.com/rustwasm/console_error_panic_hook#readme - #[cfg(feature = "console_error_panic_hook")] - console_error_panic_hook::set_once(); -} diff --git a/WebInterface/wasm/asm-paint/tests/web.rs b/WebInterface/wasm/asm-paint/tests/web.rs deleted file mode 100644 index de5c1da..0000000 --- a/WebInterface/wasm/asm-paint/tests/web.rs +++ /dev/null @@ -1,13 +0,0 @@ -//! Test suite for the Web and headless browsers. - -#![cfg(target_arch = "wasm32")] - -extern crate wasm_bindgen_test; -use wasm_bindgen_test::*; - -wasm_bindgen_test_configure!(run_in_browser); - -#[wasm_bindgen_test] -fn pass() { - assert_eq!(1 + 1, 2); -} -- cgit v1.2.3-54-g00ecf