From fa3c2a572f403578bc2d87b8d37bba5695468855 Mon Sep 17 00:00:00 2001 From: Mark Date: Sun, 13 May 2018 16:22:01 +0200 Subject: [PATCH] Switch to bindgen without emscripten for library-mode #47 --- Cargo.toml | 11 +++++++---- README.rst | 7 ++++--- Web.toml | 13 ------------ src/lib.rs | 2 ++ src/mango/ui/webi/compile_str.rs | 34 +++++++++++++++++--------------- static/index.html | 2 +- 6 files changed, 32 insertions(+), 37 deletions(-) delete mode 100644 Web.toml diff --git a/Cargo.toml b/Cargo.toml index a1bbd5fc..8cddeaab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,18 +16,21 @@ regex = "0.2.*" string-interner = "0.6.*" lazy_static = "1.0.*" derive-new = "0.5.*" -cargo-wasm = "0.4.*" +#cargo-wasm = "0.4.*" +wasm-bindgen = "0.2.*" [profile.release] lto = true +opt-level = 's' [lib] +crate-type = ["cdylib"] name = "mango" path = "src/lib.rs" -[[bin]] -name = "mango" -path = "src/mango/ui/cli/main.rs" +#[[bin]] +#name = "mangoc" +#path = "src/mango/ui/cli/main.rs" #[[bin]] #name = "mango-webi" diff --git a/README.rst b/README.rst index 2c822695..335cec2b 100644 --- a/README.rst +++ b/README.rst @@ -38,14 +38,14 @@ These instructions were tested on Ubuntu 18.4 (using Bash). It should also work rustup toolchain install nightly rustup override set nightly # make sure you are in the mango directory - rustup target add wasm32-unknown-emscripten # emscritem for now + rustup target add wasm32-unknown-unknown --toolchain nightly * We need a few packages: .. code:: bash rustup component add rustfmt-preview - cargo install cargo-web + cargo install wasm-bindgen-cli * There are git commit hooks that you can use. They test that code is formatted well and compiles and commit messages are correctly formatted. You don't have to use them if you ensure these things yourself. If you want to use them: @@ -69,7 +69,8 @@ These instructions were tested on Ubuntu 18.4 (using Bash). It should also work .. code:: bash - cargo web deploy --release + cargo +nightly build --target wasm32-unknown-unknown --release + wasm-bindgen target/wasm32-unknown-unknown/release/mango.wasm --out-dir target/deploy/ # then open target/deploy/index.html in a browser * You're now ready to make changes! If you want to help, you're very welcome! Have a glance at CONTRIBUTING.rst_ if you have a minute. diff --git a/Web.toml b/Web.toml deleted file mode 100644 index 3a86e880..00000000 --- a/Web.toml +++ /dev/null @@ -1,13 +0,0 @@ -default-target = "wasm32-unknown-emscripten" -#prepend-js = ["src/runtime.js", "src/mango/ui/webi/static/prepend-general.js"] - -[cargo-web] -minimum-version = "0.6.0" - -[target.wasm32-unknown-emscripten] -prepend-js = ["src/mango/ui/webi/static/prepend-general.js", "src/mango/ui/webi/static/prepend-emscripten.js"] -link-args = ["-s", "USE_SDL=2", "-s", "EXPORTED_FUNCTIONS=['_compile_string_to_wat']", "-s", "EXTRA_EXPORTED_RUNTIME_METHODS=['cwrap','addOnInit']"] - -## You can also specify the target by its full name. -#[target.wasm32-unknown-unknown] -#prepend-js = "src/native_runtime.js" diff --git a/src/lib.rs b/src/lib.rs index afd84d13..6d88dc87 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,6 @@ +#![feature(proc_macro, wasm_custom_section, wasm_import_module)] extern crate core; +extern crate wasm_bindgen; #[macro_use] extern crate lazy_static; extern crate regex; diff --git a/src/mango/ui/webi/compile_str.rs b/src/mango/ui/webi/compile_str.rs index ef6a4e41..09e80416 100644 --- a/src/mango/ui/webi/compile_str.rs +++ b/src/mango/ui/webi/compile_str.rs @@ -1,21 +1,23 @@ -use std::ffi::CStr; -use std::ffi::CString; -use std::os::raw::c_char; +//use std::ffi::CStr; +//use std::ffi::CString; +//use std::os::raw::c_char; +use wasm_bindgen::prelude::*; -// Convert a c-string, e.g. from javascript, into a Rust String. -fn str_to_rust(external_str: *const c_char) -> String { - unsafe { CStr::from_ptr(external_str).to_string_lossy().into_owned() } -} - -// Convert a Rust string into a c-string. -fn str_from_rust(internal_str: String) -> *mut c_char { - CString::new(internal_str.as_str()).unwrap().into_raw() -} +//// Convert a c-string, e.g. from javascript, into a Rust String. +//fn str_to_rust(external_str: *const c_char) -> String { +// unsafe { CStr::from_ptr(external_str).to_string_lossy().into_owned() } +//} +// +//// Convert a Rust string into a c-string. +//fn str_from_rust(internal_str: String) -> *mut c_char { +// CString::new(internal_str.as_str()).unwrap().into_raw() +//} -#[no_mangle] -pub fn compile_string_to_wat(codechrs: *const c_char) -> *mut c_char { - let code = str_to_rust(codechrs); +#[wasm_bindgen] +pub fn compile_string_to_wat(code: &str) -> String { + // let code = str_to_rust(codechrs); // TODO println!("compiling {}", code); - str_from_rust(code) + format!("compiled: {}", code).to_owned() + // str_from_rust(code) } diff --git a/static/index.html b/static/index.html index 87770899..7e53c811 100644 --- a/static/index.html +++ b/static/index.html @@ -74,7 +74,7 @@

Mango (wasm)

- +