-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from mangolang/web_compile
Compiler in wasm
- Loading branch information
Showing
13 changed files
with
175 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#!/usr/bin/env bash | ||
|
||
pth="$(dirname $(dirname $(realpath -s $0)))" | ||
echo $pth | ||
|
||
set -e # stop on error | ||
set -o xtrace # print commands | ||
|
||
# Compile rust to wasm | ||
# TODO: need release as workaround for a compile error in dependency; remove when that is resolved | ||
cargo +nightly build --target wasm32-unknown-unknown --release | ||
|
||
# Copy the static files | ||
outdir="$pth/target/deploy" | ||
mkdir -p "$outdir" | ||
cp -rf "$pth/static"/* "$outdir" | ||
|
||
# Prepare the wasm for use from js | ||
wasm-bindgen --debug --no-modules --no-typescript "$pth/target/wasm32-unknown-unknown/release/mango.wasm" --out-dir "$outdir" | ||
|
||
# Start a simple webserver | ||
( | ||
cd "$outdir" | ||
python -m SimpleHTTPServer | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use wasm_bindgen::prelude::*; | ||
|
||
#[wasm_bindgen] | ||
pub fn compile_string_to_wat(code: &str) -> String { | ||
// TODO | ||
format!("compiled: {}", code).to_owned() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_to_wat; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/** | ||
* Mango: Javascript prefix for emscripten target. | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/** | ||
* Mango: General javascript prefix. | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<!-- TODO: NOTICE: --> | ||
<!-- TODO: This is a simple wasm demo of the compiler. --> | ||
<!-- TODO: The 'real' version should be in github.com/mangolang/website. --> | ||
|
||
<!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"/> | ||
<style type="text/css" media="screen"> | ||
body { | ||
background-color: orange; | ||
} | ||
|
||
#content { | ||
background-color: #EEE; | ||
max-width: 1000px; | ||
margin: 0 auto; | ||
} | ||
|
||
#editor { | ||
width: 100%; | ||
height: 600px; | ||
} | ||
|
||
#output { | ||
background-color: #DDD; | ||
border: 1px solid black; | ||
min-height: 150px; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<main> | ||
<section id="content"> | ||
<h1>Mango (wasm)</h1> | ||
<div id="editor">// Type your Mango code here! | ||
|
||
</div> | ||
<button id="compile_wat" onclick="alert('Not ready yet, please wait or check console');" | ||
title="Compile the code to the ascii text representation of WebAssembly">Compile to wat | ||
</button> | ||
<button onclick="alert('Not implemented');" title="Compile the code to binary, executable WebAssembly">Compile to wasm | ||
</button> | ||
<button onclick="alert('Not implemented');" | ||
title="Compile the code to binary WebAssembly and run it, showing only the output">Compile and run | ||
</button> | ||
<div id="output"></div> | ||
</section> | ||
</main> | ||
|
||
<!-- Editor --> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.3.3/ace.js"></script> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.3.3/theme-crimson_editor.js"></script> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.3.3/mode-javascript.js"></script> | ||
|
||
<script type="application/javascript"> | ||
window.editor = ace.edit("editor"); | ||
window.editor.setTheme("ace/theme/crimson_editor"); | ||
window.editor.session.setMode("ace/mode/javascript"); | ||
// window.editor.resize(); | ||
</script> | ||
|
||
<!--Wasm Mango compiler--> | ||
<script src="./mango.js" type="module"></script> | ||
<script type="module"> | ||
(async function () { | ||
/** | ||
* Show a string in the output area, replacing any previous results. | ||
*/ | ||
function show_output(txt) { | ||
let output = document.getElementById("output"); | ||
output.innerHTML = ""; | ||
let pre = document.createElement("pre"); | ||
pre.innerHTML = txt; | ||
output.appendChild(pre); | ||
} | ||
|
||
/** | ||
* Load the Mango compiler wasm code (async). | ||
*/ | ||
let _ = await window.wasm_bindgen("./mango_bg.wasm"); | ||
let mango = window.wasm_bindgen; | ||
|
||
/** | ||
* Bind the "compile to wat" button. | ||
*/ | ||
document.getElementById("compile_wat").onclick = function (event) { | ||
let mangocode = window.editor.getValue(); | ||
console.log("Compiling Mango:\n", mangocode); | ||
let watcode = mango.compile_string_to_wat(mangocode); | ||
console.log("Resulting WebAssembly text:\n", watcode); | ||
show_output(watcode); | ||
}; | ||
})(); | ||
</script> | ||
</body> | ||
</html> |