From 4098b1b4901417993c6f683bc4d33aa34fc8760f Mon Sep 17 00:00:00 2001 From: Mark Date: Sat, 12 May 2018 23:36:01 +0200 Subject: [PATCH 1/6] Working on having the compiler in wasm, it compiles and loads #47 --- Cargo.toml | 15 +++++++++ Web.toml | 13 ++++++++ src/lib.rs | 2 +- src/mango/token/collect/stream.rs | 3 +- src/mango/ui/cli/main.rs | 3 ++ src/mango/{ => ui}/cli/mod.rs | 0 src/mango/ui/mod.rs | 5 +++ src/mango/ui/webi/compile_str.rs | 21 +++++++++++++ src/mango/ui/webi/mod.rs | 2 ++ .../ui/webi/static/prepend-emscripten.js | 3 ++ src/mango/ui/webi/static/prepend-general.js | 3 ++ static/index.html | 31 +++++++++++++++++++ 12 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 Web.toml create mode 100644 src/mango/ui/cli/main.rs rename src/mango/{ => ui}/cli/mod.rs (100%) create mode 100644 src/mango/ui/mod.rs create mode 100644 src/mango/ui/webi/compile_str.rs create mode 100644 src/mango/ui/webi/mod.rs create mode 100644 src/mango/ui/webi/static/prepend-emscripten.js create mode 100644 src/mango/ui/webi/static/prepend-general.js create mode 100644 static/index.html diff --git a/Cargo.toml b/Cargo.toml index 43b395e2..a1bbd5fc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,3 +17,18 @@ string-interner = "0.6.*" lazy_static = "1.0.*" derive-new = "0.5.*" cargo-wasm = "0.4.*" + +[profile.release] +lto = true + +[lib] +name = "mango" +path = "src/lib.rs" + +[[bin]] +name = "mango" +path = "src/mango/ui/cli/main.rs" + +#[[bin]] +#name = "mango-webi" +#path = "src/mango/ui/webi/compile_str.rs" diff --git a/Web.toml b/Web.toml new file mode 100644 index 00000000..400e53a0 --- /dev/null +++ b/Web.toml @@ -0,0 +1,13 @@ +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']"] + +## 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 c7fe180e..afd84d13 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,8 +8,8 @@ extern crate derive_new; pub mod mango { // Utilities - pub mod cli; pub mod jit; + pub mod ui; pub mod util; // Types diff --git a/src/mango/token/collect/stream.rs b/src/mango/token/collect/stream.rs index 10ab1825..1b34486c 100644 --- a/src/mango/token/collect/stream.rs +++ b/src/mango/token/collect/stream.rs @@ -20,11 +20,12 @@ pub struct MemoryTokenStream { } impl MemoryTokenStream { - #[allow(dead_code)] + #[allow(dead_code)] // TODO: for now pub fn new(tokens: Vec) -> MemoryTokenStream { MemoryTokenStream { index: 0, tokens } } + #[allow(dead_code)] // TODO: for now pub fn to_text(&self) -> String { format!( " {}", diff --git a/src/mango/ui/cli/main.rs b/src/mango/ui/cli/main.rs new file mode 100644 index 00000000..eb941355 --- /dev/null +++ b/src/mango/ui/cli/main.rs @@ -0,0 +1,3 @@ +pub fn main() { + println!("welcome to Mango CLI!"); +} diff --git a/src/mango/cli/mod.rs b/src/mango/ui/cli/mod.rs similarity index 100% rename from src/mango/cli/mod.rs rename to src/mango/ui/cli/mod.rs diff --git a/src/mango/ui/mod.rs b/src/mango/ui/mod.rs new file mode 100644 index 00000000..2e8134b1 --- /dev/null +++ b/src/mango/ui/mod.rs @@ -0,0 +1,5 @@ +pub mod cli; +pub use self::cli::*; + +pub mod webi; +pub use self::webi::*; diff --git a/src/mango/ui/webi/compile_str.rs b/src/mango/ui/webi/compile_str.rs new file mode 100644 index 00000000..afd27a96 --- /dev/null +++ b/src/mango/ui/webi/compile_str.rs @@ -0,0 +1,21 @@ +use std::ffi::CStr; +use std::ffi::CString; +use std::os::raw::c_char; + +// 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(codechrs: *const c_char) -> *mut c_char { + let code = str_to_rust(codechrs); + // TODO + println!("compiling {}", code); + str_from_rust(code) +} diff --git a/src/mango/ui/webi/mod.rs b/src/mango/ui/webi/mod.rs new file mode 100644 index 00000000..4657804c --- /dev/null +++ b/src/mango/ui/webi/mod.rs @@ -0,0 +1,2 @@ +pub mod compile_str; +pub use self::compile_str::compile_string; diff --git a/src/mango/ui/webi/static/prepend-emscripten.js b/src/mango/ui/webi/static/prepend-emscripten.js new file mode 100644 index 00000000..21219ff1 --- /dev/null +++ b/src/mango/ui/webi/static/prepend-emscripten.js @@ -0,0 +1,3 @@ +/** + * Mango: Javascript prefix for emscripten target. + */ diff --git a/src/mango/ui/webi/static/prepend-general.js b/src/mango/ui/webi/static/prepend-general.js new file mode 100644 index 00000000..2168946e --- /dev/null +++ b/src/mango/ui/webi/static/prepend-general.js @@ -0,0 +1,3 @@ +/** + * Mango: General javascript prefix. + */ \ No newline at end of file diff --git a/static/index.html b/static/index.html new file mode 100644 index 00000000..28daa055 --- /dev/null +++ b/static/index.html @@ -0,0 +1,31 @@ + + + + + + + + + + + + + From f3a932fdb2ecfbdd1e4900ac4b3e09f8d96e0c02 Mon Sep 17 00:00:00 2001 From: Mark Date: Sun, 13 May 2018 14:26:03 +0200 Subject: [PATCH 2/6] Create a simple editor #47 --- Web.toml | 2 +- src/mango/ui/webi/compile_str.rs | 2 +- src/mango/ui/webi/mod.rs | 2 +- static/index.html | 59 ++++++++++++++++++++++++++++++-- 4 files changed, 59 insertions(+), 6 deletions(-) diff --git a/Web.toml b/Web.toml index 400e53a0..3a86e880 100644 --- a/Web.toml +++ b/Web.toml @@ -6,7 +6,7 @@ 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']"] +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] diff --git a/src/mango/ui/webi/compile_str.rs b/src/mango/ui/webi/compile_str.rs index afd27a96..ef6a4e41 100644 --- a/src/mango/ui/webi/compile_str.rs +++ b/src/mango/ui/webi/compile_str.rs @@ -13,7 +13,7 @@ fn str_from_rust(internal_str: String) -> *mut c_char { } #[no_mangle] -pub fn compile_string(codechrs: *const c_char) -> *mut c_char { +pub fn compile_string_to_wat(codechrs: *const c_char) -> *mut c_char { let code = str_to_rust(codechrs); // TODO println!("compiling {}", code); diff --git a/src/mango/ui/webi/mod.rs b/src/mango/ui/webi/mod.rs index 4657804c..bd370a72 100644 --- a/src/mango/ui/webi/mod.rs +++ b/src/mango/ui/webi/mod.rs @@ -1,2 +1,2 @@ pub mod compile_str; -pub use self::compile_str::compile_string; +pub use self::compile_str::compile_string_to_wat; diff --git a/static/index.html b/static/index.html index 28daa055..87770899 100644 --- a/static/index.html +++ b/static/index.html @@ -1,9 +1,33 @@ + + + + + + +
+
+

Mango (wasm)

+
// Type your Mango code here! + +
+ + + +
+
+
+ + + + + + + + + + - + From fa3c2a572f403578bc2d87b8d37bba5695468855 Mon Sep 17 00:00:00 2001 From: Mark Date: Sun, 13 May 2018 16:22:01 +0200 Subject: [PATCH 3/6] 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)

- + - + + + + -
-
-

Mango (wasm)

-
// Type your Mango code here! +
+
+

Mango (wasm)

+
// Type your Mango code here!
- - - -
-
-
- - - - - - - - - - - - + + + +
+
+
+ + + + + + + + + + + + From 19c84db2243f5cc11c52a907c3ffe89598af40c1 Mon Sep 17 00:00:00 2001 From: Mark Date: Sun, 13 May 2018 20:36:35 +0200 Subject: [PATCH 6/6] Use async syntax and IIFE #47 --- static/index.html | 59 +++++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 27 deletions(-) diff --git a/static/index.html b/static/index.html index 16acacc5..bf0f00cf 100644 --- a/static/index.html +++ b/static/index.html @@ -1,4 +1,3 @@ - @@ -37,11 +36,16 @@

Mango (wasm)

// Type your Mango code here! - -
- - - + + + + +
@@ -60,36 +64,37 @@

Mango (wasm)

- +