chiark / gitweb /
towards new string arrangements
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 11 Oct 2020 23:09:16 +0000 (00:09 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 11 Oct 2020 23:09:16 +0000 (00:09 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
templates/script.ts
wasm/wasm.rs

index dbfe0406ab957af280ec1092db1f52f52503a77b..9d3ac39fbde0ebd02102d42ceb19345e029d4c13 100644 (file)
@@ -1013,11 +1013,7 @@ function test_swap_stack() {
 
 function startup() {
   console.log('STARTUP');
-//  let mut = wasm_bindgen.mutable('4200000000');
-//  let y = mut.next();
-//  console.log('FOO', mut, y);
-  let x = wasm_bindgen.jsstring(12);
-  console.log(x);
+  console.log(wasm_bindgen.canary("OK"));
 
   var body = document.getElementById("main-body")!;
   ctoken = body.dataset.ctoken!;
index 2540a8033ccdfd5d796fd569e7bf9286c8def71f..b84d815cf2cd5503d5916e9fdee6e5e16fb67d4c 100644 (file)
@@ -2,6 +2,7 @@
 use wasm_bindgen::prelude::*;
 use fehler::throws;
 use std::fmt::Display;
+use js_sys::JsString;
 
 use zcoord::ZCoord;
 
@@ -12,16 +13,10 @@ trait WasmError {
   fn e(self) -> JsValue;
 }
 
-fn mkstr(s: &str) -> JsValue {
-  let s : Vec<u16> = s.encode_utf16().collect();
-  let jss = js_sys::JsString::from_char_code(&s);
-  jss.into()
-}
-
 impl<E> WasmError for E where E: Display {
   fn e(self) -> JsValue {
     let s = format!("{}", self);
-    mkstr(&s)
+    JsValue::from_str(&s)
   }
 }
 
@@ -42,13 +37,6 @@ pub fn check(packed: &JsValue) {
   ZCoord::check_str(&s).ok_or(zcoord::ParseError).e()?;
 }
 
-#[wasm_bindgen]
-pub fn jsstring(x: u32) -> JsValue {
-  let s = format!("hi!{:?}",x);
-  //  JsValue::from_str(&s);
-  mkstr(&s)
-}
-
 //const X : &'static str = "invalid value passed to wasm";
 /*
 #[throws(JsValue)]
@@ -61,3 +49,10 @@ pub fn mutable(s: String) -> ZCoordIterator {
 impl ZCoordIterator {
   pub fn next(&mut self) -> u32 { 42 }
 }
+
+#[wasm_bindgen]
+pub fn canary(s: &str) -> JsString {
+  // returning String produces a wasm-opt error, as here
+  //  https://github.com/WebAssembly/binaryen/issues/3006
+  format!("WASM {}", s).into()
+}