From 4098b1b4901417993c6f683bc4d33aa34fc8760f Mon Sep 17 00:00:00 2001 From: Mark Date: Sat, 12 May 2018 23:36:01 +0200 Subject: [PATCH] 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 @@ + + + + + + + + + + + + +