From 5b76a73b8c5d2ccb492d282597665eb737913727 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 10 Oct 2020 22:31:42 +0100 Subject: [PATCH] wasm seems to work now Signed-off-by: Ian Jackson --- .gitignore | 1 + Makefile | 16 +++++++++++++++- server.toml | 1 + src/bin/daemon-otter.rs | 35 ++++++++++++++++++++++++----------- templates/loading.tera | 3 ++- templates/script.ts | 20 ++++++++++++++++++-- wasm/wasm.rs | 2 +- 7 files changed, 62 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index 4ad5bc61..df16a802 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ templates/LICENCE templates/AGPLv3 templates/CC-BY-SA-3.0 templates/CC-BY-SA-4.0 +templates/otter_wasm.ns.d.ts save/lock /library/*/*.usvg *.tmp diff --git a/Makefile b/Makefile index c80eeea1..94188124 100644 --- a/Makefile +++ b/Makefile @@ -135,6 +135,13 @@ stamp/wasm-pack: stamp/cargo.wasm-release --out-dir=../target/packed-wasm wasm -t no-modules --release $(stamp) +# There is some kind of bug in wasm-pack's version of wasm-opt, which +# can be avoided by running --dev instead (but also then using the +# debug rust built). IME this happened when trying to return a u64, +# so maybe bigints are affected? (Which I don't want to use anyway.) +# See +# https://github.com/WebAssembly/binaryen/issues/3006 + #---------- bundle-sources ---------- bundled-sources: $(TARGET_DIR)/bundled-sources @@ -158,7 +165,7 @@ $(LIBRARY_FILES): $(USVG_PROCESSOR) $(USVG_BINARY) Makefile TS_SRCS= script bigfloat TS_SRC_FILES= \ ../webassembly-types/webassembly.d.ts \ - $(addprefix $(WASM_PACKED)/,otter_wasm.d.ts) \ + templates/otter_wasm.ns.d.ts \ $(addprefix templates/,$(addsuffix .ts,$(TS_SRCS))) LITFILES= LICENCE AGPLv3 @@ -178,6 +185,13 @@ js-check: templates/bigfloat-tests.js nodejs <$< @echo 'nodejs check $< ok' +templates/otter_wasm.ns.d.ts: $(WASM_PACKED)/otter_wasm.d.ts Makefile + set -e; exec >$@.tmp; \ + echo 'declare namespace wasm_bindgen {'; \ + sed 's/^export default function init/export function init/' <$<; \ + echo '}' + mv -v $@.tmp $@ + #---------- other templates ---------- $(addprefix templates/,$(LITFILES)): templates/%: %; diff --git a/server.toml b/server.toml index 2339d31f..316bca4c 100644 --- a/server.toml +++ b/server.toml @@ -10,6 +10,7 @@ save_directory = "/home/rustcargo/Rustup/Game/server" template_dir = "/home/ian/Rustup/Game/server/templates" bundled_sources = "/home/rustcargo/Rustup/Game/server/target/bundled-sources" +wasm_dir = "/home/rustcargo/Rustup/Game/server/target/packed-wasm" shapelibs = [ "/home/ian/Rustup/Game/server/library/*.toml" ] diff --git a/src/bin/daemon-otter.rs b/src/bin/daemon-otter.rs index 6f789fb0..6d3c91ab 100644 --- a/src/bin/daemon-otter.rs +++ b/src/bin/daemon-otter.rs @@ -20,19 +20,26 @@ fn index() -> Template { Template::render("test",&c) } -const RESOURCES : &[(&'static str, ContentType)] = &[ - ("script.js", ContentType::JavaScript), - ("style.css", ContentType::JavaScript), - ("LICENCE", ContentType::Plain), - ("libre", ContentType::HTML), - ("AGPLv3", ContentType::Plain), - ("CC-BY-SA-3.0", ContentType::Plain), - ("CC-BY-SA-4.0", ContentType::Plain), +#[derive(Copy,Clone,Debug)] +enum ResourceLocation { Main, Wasm(&'static str), } +type RL = ResourceLocation; + +const RESOURCES : &[(&'static str, ResourceLocation, ContentType)] = &[ + ("script.js", RL::Main, ContentType::JavaScript), + ("style.css", RL::Main, ContentType::JavaScript), + ("LICENCE", RL::Main, ContentType::Plain), + ("libre", RL::Main, ContentType::HTML), + ("AGPLv3", RL::Main, ContentType::Plain), + ("CC-BY-SA-3.0", RL::Main, ContentType::Plain), + ("CC-BY-SA-4.0", RL::Main, ContentType::Plain), + ("wasm.wasm", RL::Wasm("otter_wasm_bg.wasm"), ContentType::WASM), + ("wasm.js", RL::Wasm("otter_wasm.js"), ContentType::JavaScript), ]; #[derive(Debug)] struct CheckedResourceLeaf { safe_leaf: &'static str, + locn: ResourceLocation, ctype: ContentType, } @@ -43,9 +50,12 @@ struct UnknownResource{} impl<'r> FromParam<'r> for CheckedResourceLeaf { type Error = UnknownResource; fn from_param(param: &'r RawStr) -> Result { - for &(safe_leaf, ref ctype) in RESOURCES { + for &(safe_leaf, locn, ref ctype) in RESOURCES { if safe_leaf == param.as_str() { - return Ok(CheckedResourceLeaf { safe_leaf, ctype: ctype.clone() }) + return Ok(CheckedResourceLeaf { + safe_leaf, locn, + ctype: ctype.clone(), + }) } } Err(UnknownResource{}) @@ -83,7 +93,10 @@ fn updates<'r>(ctoken : InstanceAccess, gen: u64, #[get("/_/")] #[throws(io::Error)] fn resource<'r>(leaf : CheckedResourceLeaf) -> impl Responder<'r> { - let path = format!("{}/{}", config().template_dir, leaf.safe_leaf); + let path = match leaf.locn { + RL::Main => format!("{}/{}", config().template_dir, leaf.safe_leaf), + RL::Wasm(s) => format!("{}/{}", config().wasm_dir, s), + }; Content(leaf.ctype, NamedFile::open(path)?) } diff --git a/templates/loading.tera b/templates/loading.tera index 46c6f0e4..003641cf 100644 --- a/templates/loading.tera +++ b/templates/loading.tera @@ -4,7 +4,8 @@ - + + diff --git a/templates/script.ts b/templates/script.ts index 93592aa8..c812b67e 100644 --- a/templates/script.ts +++ b/templates/script.ts @@ -74,6 +74,8 @@ type PieceInfo = { last_seen_moved : DOMHighResTimeStamp | null, // non-0 means halo'd } +let wasm : wasm_bindgen.InitOutput; + let pieces : { [piece: string]: PieceInfo } = Object.create(null); type MessageHandler = (op: Object) => void; @@ -1008,6 +1010,11 @@ function test_swap_stack() { } function startup() { + console.log('STARTUP'); + let mut = wasm_bindgen.mutable('4200000000'); + let y = mut.next(); + console.log('FOO', mut, y); + var body = document.getElementById("main-body")!; ctoken = body.dataset.ctoken!; us = body.dataset.us!; @@ -1079,6 +1086,9 @@ function startup() { document.addEventListener('keydown', some_keydown); } +declare var wasm_input : any; +var wasm_promise : Promise;; + function doload(){ console.log('DOLOAD'); var elem = document.getElementById('loading_token')!; @@ -1086,13 +1096,19 @@ function doload(){ xhr_post_then('/_/session', JSON.stringify({ ptoken : ptoken }), loaded); + + wasm_promise = wasm_input + .then(wasm_bindgen); } function loaded(xhr: XMLHttpRequest){ console.log('LOADED'); var body = document.getElementById('loading_body')!; - body.outerHTML = xhr.response; - startup(); + wasm_promise.then((got_wasm) => { + wasm = got_wasm; + body.outerHTML = xhr.response; + startup(); + }); } // xxx scroll of log messages to bottom does not always work somehow diff --git a/wasm/wasm.rs b/wasm/wasm.rs index 53922406..819cba58 100644 --- a/wasm/wasm.rs +++ b/wasm/wasm.rs @@ -17,5 +17,5 @@ pub fn mutable(s: String) -> ZCoordIterator { #[wasm_bindgen] impl ZCoordIterator { - pub fn next(&mut self) -> () { } + pub fn next(&mut self) -> u32 { 42 } } -- 2.30.2