From 77cf7819974e8669d950279f2db1a13e2126fabe Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Mon, 12 Oct 2020 00:09:16 +0100 Subject: [PATCH] towards new string arrangements Signed-off-by: Ian Jackson --- templates/script.ts | 6 +----- wasm/wasm.rs | 23 +++++++++-------------- 2 files changed, 10 insertions(+), 19 deletions(-) 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() +} -- 2.30.2