From 22e9c1a4198554fa99b422e31bcef71e649459d0 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 10 Oct 2020 19:13:36 +0100 Subject: [PATCH] wip wasm Signed-off-by: Ian Jackson --- Cargo.lock.example | 1 + Makefile | 27 +++++++++++++++++++++------ wasm/Cargo.toml | 1 + wasm/wasm.rs | 11 +++++++++++ 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/Cargo.lock.example b/Cargo.lock.example index ad78b5a6..19de0148 100644 --- a/Cargo.lock.example +++ b/Cargo.lock.example @@ -1127,6 +1127,7 @@ dependencies = [ name = "otter-wasm" version = "0.0.1" dependencies = [ + "fehler", "otter-zcoord", "wasm-bindgen", ] diff --git a/Makefile b/Makefile index d227d495..86f65be6 100644 --- a/Makefile +++ b/Makefile @@ -22,6 +22,9 @@ CARGO_TARGET_DIR ?= target USVG ?= usvg USVG_OPTIONS = "--sans-serif-family=DejaVu Sans" +WASM_PACK ?= wasm-pack +WASM_PACK_OPTIONS = --cargo-path=/bin/echo + BUNDLE_SOURCES ?= bundle-rust-sources DEPLOY_ARCH=x86_64-unknown-linux-musl @@ -58,6 +61,7 @@ endef $(eval $(call lp,BUNDLE_SOURCES,bundle-sources,debug,bundle-rust-sources)) $(eval $(call lp,USVG,resvg,release,usvg)) +$(eval $(call lp,WASM_PACK,wasm-pack,debug,wasm-pack)) #---------- variables defining bits of source etc. ---------- @@ -81,15 +85,26 @@ extra-release: bundled-sources #---------- cargo ---------- -CARGOES=$(foreach t, / /wasm- , \ - $(addprefix $t, , \ - check debug release)) +DR=debug release +CARGOES=$(foreach t,/ /wasm-,$(addprefix $t,check $(DR))) + +X := $(shell echo >&2 PHONY $(addprefix cargo, $(CARGOES))) .PHONY: $(addprefix cargo, $(CARGOES)) -cargo/check cargo/debug:: cargo/%:; $(CARGO) test $(call cr,$*) -cargo/wasm-%:; $(CARGO) -TWASM build -p otter-wasm $(call cr,$*) -cargo/deploy-build: $(CARGO) -T$(DEPLOY_ARCH) build $(call cr,$(DEPLOY_RELEASE)) +$(addprefix cargo/,$(DR)):: cargo/%: + $(CARGO) test $(call cr,$*) + +$(addprefix cargo/wasm-,$(DR)):: cargo/wasm-%: + $(CARGO) -TWASM build -p otter-wasm $(call cr,$*) + +cargo/deploy-build: + $(CARGO) -T$(DEPLOY_ARCH) build $(call cr,$(DEPLOY_RELEASE)) + +#---------- wasm ---------- + +#WASM_ASSETS := $(addprefix otter_wasm,.js _bg.wasm) +#WASM_OUTPUTS := $(addprefix otter_wasm,.d.ts #---------- bundle-sources ---------- diff --git a/wasm/Cargo.toml b/wasm/Cargo.toml index 715c03c6..e2f066e9 100644 --- a/wasm/Cargo.toml +++ b/wasm/Cargo.toml @@ -15,3 +15,4 @@ crate-type = ["cdylib"] [dependencies] otter-zcoord = { path = "../zcoord" } wasm-bindgen = "0.2" +fehler = "1" diff --git a/wasm/wasm.rs b/wasm/wasm.rs index d6c22dc9..347ec4b3 100644 --- a/wasm/wasm.rs +++ b/wasm/wasm.rs @@ -1,5 +1,16 @@ use wasm_bindgen::prelude::*; +use fehler::throws; + +use zcoord::ZCoord; #[wasm_bindgen] pub struct ZCoordIterator (zcoord::Mutable); + +const X : &'static str = "invalid value passed to wasm"; + +#[throws(JsValue)] +#[wasm_bindgen] +pub fn mutable(s: String) -> ZCoordIterator { + ZCoordIterator(ZCoord::from_str(&s).ok_or(X)?.clone_mut()) +} -- 2.30.2