Skip to content

Commit

Permalink
Working on having the compiler in wasm, it compiles and loads #47
Browse files Browse the repository at this point in the history
  • Loading branch information
mverleg committed May 12, 2018
1 parent bfc94fd commit 4098b1b
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 2 deletions.
15 changes: 15 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
13 changes: 13 additions & 0 deletions Web.toml
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/mango/token/collect/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ pub struct MemoryTokenStream {
}

impl MemoryTokenStream {
#[allow(dead_code)]
#[allow(dead_code)] // TODO: for now
pub fn new(tokens: Vec<Tokens>) -> MemoryTokenStream {
MemoryTokenStream { index: 0, tokens }
}

#[allow(dead_code)] // TODO: for now
pub fn to_text(&self) -> String {
format!(
" {}",
Expand Down
3 changes: 3 additions & 0 deletions src/mango/ui/cli/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn main() {
println!("welcome to Mango CLI!");
}
File renamed without changes.
5 changes: 5 additions & 0 deletions src/mango/ui/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub mod cli;
pub use self::cli::*;

pub mod webi;
pub use self::webi::*;
21 changes: 21 additions & 0 deletions src/mango/ui/webi/compile_str.rs
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 2 additions & 0 deletions src/mango/ui/webi/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod compile_str;
pub use self::compile_str::compile_string;
3 changes: 3 additions & 0 deletions src/mango/ui/webi/static/prepend-emscripten.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/**
* Mango: Javascript prefix for emscripten target.
*/
3 changes: 3 additions & 0 deletions src/mango/ui/webi/static/prepend-general.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/**
* Mango: General javascript prefix.
*/
31 changes: 31 additions & 0 deletions static/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=1" name="viewport" />
<script>
var Module = {};
var __cargo_web = {};
Object.defineProperty( Module, 'canvas', {
get: function() {
if( __cargo_web.canvas ) {
return __cargo_web.canvas;
}

var canvas = document.createElement( 'canvas' );
document.querySelector( 'body' ).appendChild( canvas );
__cargo_web.canvas = canvas;

return canvas;
}
});
</script>
</head>
<body>
<script src="mango.js"></script>
<script>

</script>
</body>
</html>

0 comments on commit 4098b1b

Please sign in to comment.