From: Ian Jackson Date: Sun, 11 Oct 2020 23:09:16 +0000 (+0100) Subject: towards new string arrangements X-Git-Tag: otter-0.2.0~671 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=77cf7819974e8669d950279f2db1a13e2126fabe;p=otter.git towards new string arrangements Signed-off-by: Ian Jackson --- diff --git a/templates/script.ts b/templates/script.ts index dbfe0406..9d3ac39f 100644 --- a/templates/script.ts +++ b/templates/script.ts @@ -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!; diff --git a/wasm/wasm.rs b/wasm/wasm.rs index 2540a803..b84d815c 100644 --- a/wasm/wasm.rs +++ b/wasm/wasm.rs @@ -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 = s.encode_utf16().collect(); - let jss = js_sys::JsString::from_char_code(&s); - jss.into() -} - impl 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() +}