chiark / gitweb /
wasm seems to work now
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 10 Oct 2020 21:31:42 +0000 (22:31 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 10 Oct 2020 21:31:42 +0000 (22:31 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
.gitignore
Makefile
server.toml
src/bin/daemon-otter.rs
templates/loading.tera
templates/script.ts
wasm/wasm.rs

index 4ad5bc614bf6f78da31a2206615feed39d20ba5d..df16a802d99a19e19e86f1dffe77f6cbcc770d7b 100644 (file)
@@ -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
index c80eeea126139a9990df83b3d707a74661303cac..9418812485359a142a46aec10629d5d3c9ead68b 100644 (file)
--- 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/%: %;
index 2339d31f08fd0cba90730bba1fe7ba0206a51595..316bca4cd0ad74b305fb8271b077dcd881a00049 100644 (file)
@@ -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" ]
 
index 6f789fb0809fbe30ba48174fc4c65c7563f71d8f..6d3c91ab16dd0d91dc11b5f96f6e2b28030f35aa 100644 (file)
@@ -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<Self, Self::Error> {
-    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<ClientId>, gen: u64,
 #[get("/_/<leaf>")]
 #[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)?)
 }  
 
index 46c6f0e4ceeb80e1fdb506c18f376c53afaf6ce6..003641cf49275c2f52122a186b07d7fafbc0acef 100644 (file)
@@ -4,7 +4,8 @@
 <html>
 <head>
 <link rel="license" href="/_/libre.html">
-<script defer>let wasm_input = fetch('/_/wasm');</script>
+<script defer>let wasm_input = fetch('/_/wasm.wasm');</script>
+<script src="/_/wasm.js" defer></script>
 <script src="/_/script.js" defer></script>
 </head>
 <body id="loading_body">
index 93592aa8602e5366801a666216452e0b46695c0f..c812b67ec8ba1b47ff20eea87777f54ecf99a391 100644 (file)
@@ -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<any>;;
+
 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
index 539224067f03bff2effb2749f63b6ebfb43c779d..819cba58643072a924b2766b0b9fdc0accc6db45 100644 (file)
@@ -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 }
 }