Skip to content

Commit

Permalink
Switch to bindgen without emscripten for library-mode #47
Browse files Browse the repository at this point in the history
  • Loading branch information
mverleg committed May 13, 2018
1 parent f3a932f commit fa3c2a5
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 37 deletions.
11 changes: 7 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 4 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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.
Expand Down
13 changes: 0 additions & 13 deletions Web.toml

This file was deleted.

2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
34 changes: 18 additions & 16 deletions src/mango/ui/webi/compile_str.rs
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 1 addition & 1 deletion static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ <h1>Mango (wasm)</h1>
</script>

<!--Wasm Mango compiler-->
<script src="mango.js"></script>
<script src="mango.js" type="module"></script>
<script type="text/javascript">
Module.addOnInit(function () {
let code = Module.cwrap('compile_string', 'string', ['string']);
Expand Down

0 comments on commit fa3c2a5

Please sign in to comment.