-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit cd5d4c0
Showing
10 changed files
with
400 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
calcit.cirru -diff linguist-generated | ||
yarn.lock -diff linguist-generated | ||
Cargo.lock -diff linguist-generated |
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,6 @@ | ||
/target | ||
Cargo.lock | ||
|
||
/dist | ||
node_modules | ||
yarn-error.log |
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,20 @@ | ||
[package] | ||
name = "calcit-wasm-play" | ||
version = "0.1.0" | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
calcit_runner = { path="../calcit-runner.rs" } | ||
wasm-bindgen = "*" | ||
im = "15.0.0" | ||
console_error_panic_hook = "*" | ||
web-sys = { version="0.3.5", features=[ "console" ] } | ||
|
||
[lib] | ||
crate-type = ["cdylib", "rlib"] | ||
|
||
[profile.release] | ||
debug = true | ||
|
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,24 @@ | ||
## Calcit built into WASM | ||
|
||
Demo http://repo.calcit-lang.org/calcit-wasm-play/ | ||
|
||
### Development | ||
|
||
> Based on local copy of calcit_runner.rs with `libloading` and injections disabled. | ||
Build wasm code: | ||
|
||
```bash | ||
wasm-pack build -t web | ||
``` | ||
|
||
Serve page: | ||
|
||
```bash | ||
yarn | ||
yarn vite | ||
``` | ||
|
||
### License | ||
|
||
MIT |
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,21 @@ | ||
|
||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Play</title> | ||
<script type="module" src="./main.js"></script> | ||
<link rel="stylesheet" href="./main.css"/> | ||
</head> | ||
<body> | ||
|
||
<textarea class="code" placeholder="code here..." ></textarea> | ||
<div class="panel"> | ||
<div> | ||
<button class="run" >Run</button> | ||
</div> | ||
<div class="result"></div> | ||
</div> | ||
|
||
</body> | ||
</html> |
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,43 @@ | ||
body { | ||
position: absolute; | ||
width: 100%; | ||
margin: 0; | ||
height: 100%; | ||
display: flex; | ||
overscroll-behavior-y: none; | ||
overscroll-behavior-x: none; | ||
} | ||
|
||
body * { | ||
box-sizing: border-box; | ||
} | ||
|
||
textarea { | ||
width: 40%; | ||
flex: 1; | ||
height: 100%; | ||
padding: 8px 8px; | ||
font-family: Source Code Pro, Menlo, Consolas, monospace; | ||
border-color: #ddd; | ||
resize: none; | ||
} | ||
|
||
.panel { | ||
flex: 1; | ||
overflow: auto; | ||
display: flex; | ||
flex-direction: column; | ||
} | ||
|
||
button { | ||
margin: 4px; | ||
} | ||
|
||
.result { | ||
padding: 24px 8px 200px 8px; | ||
background-color: #eee; | ||
font-family: Source Code Pro, Menlo, Consolas, monospace; | ||
flex: 1; | ||
overflow: auto; | ||
font-size: 14px; | ||
} |
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,28 @@ | ||
import init, { run_code } from "./pkg/calcit_wasm_play"; | ||
import { codearea } from "@mvc-works/codearea"; | ||
|
||
init().then((w) => { | ||
console.log("loaded", w); | ||
}); | ||
|
||
window.addEventListener("load", (event) => { | ||
let codeEl = document.querySelector(".code"); | ||
let resultEl = document.querySelector(".result"); | ||
codearea(codeEl); | ||
|
||
document.querySelector(".run").addEventListener("click", (event) => { | ||
let code = codeEl.value; | ||
|
||
console.log("code:", code); | ||
|
||
resultEl.innerText = ""; | ||
let start = performance.now(); | ||
|
||
let result = run_code(code); | ||
|
||
// console.log("result", result); | ||
let cost = performance.now() - start; | ||
|
||
resultEl.innerText = result + "\n\n" + cost + "ms"; | ||
}); | ||
}); |
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,14 @@ | ||
{ | ||
"name": "calcit-wasm-play", | ||
"version": "0.0.1", | ||
"description": "Demo of Calcit in WASM", | ||
"main": "index.js", | ||
"author": "jiyinyiyong <[email protected]>", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"vite": "^2.6.2" | ||
}, | ||
"dependencies": { | ||
"@mvc-works/codearea": "^0.0.2" | ||
} | ||
} |
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,50 @@ | ||
// extern crate console_error_panic_hook; | ||
extern crate wasm_bindgen; | ||
extern crate web_sys; | ||
|
||
use std::cell::RefCell; | ||
use std::panic; | ||
|
||
use wasm_bindgen::prelude::*; | ||
|
||
use calcit_runner::{load_core_snapshot, program, runner, snapshot}; | ||
|
||
#[wasm_bindgen] | ||
pub fn run_code(snippet: String) -> String { | ||
// panic::set_hook(Box::new(console_error_panic_hook::hook)); | ||
program::clear_all_program_evaled_defs("app.main/main!", "app.main/reload!", false).unwrap(); | ||
|
||
let core_snapshot = load_core_snapshot().unwrap(); | ||
let mut snapshot = snapshot::gen_default(); // placeholder data | ||
match snapshot::create_file_from_snippet(&snippet) { | ||
Ok(main_file) => { | ||
snapshot.files.insert(String::from("app.main"), main_file); | ||
} | ||
Err(e) => panic!("failed snapshot: {}", e), | ||
} | ||
// attach core | ||
for (k, v) in core_snapshot.files { | ||
snapshot.files.insert(k.to_owned(), v.to_owned()); | ||
} | ||
|
||
let program_code = program::extract_program_data(&snapshot).unwrap(); | ||
let check_warnings: &RefCell<Vec<String>> = &RefCell::new(vec![]); | ||
|
||
// make sure builtin classes are touched | ||
runner::preprocess::preprocess_ns_def( | ||
calcit_runner::primes::CORE_NS, | ||
calcit_runner::primes::BUILTIN_CLASSES_ENTRY, | ||
&program_code, | ||
calcit_runner::primes::BUILTIN_CLASSES_ENTRY, | ||
None, | ||
check_warnings, | ||
) | ||
.unwrap(); | ||
|
||
let v = calcit_runner::run_program("app.main/main!", im::vector![], &program_code).unwrap(); | ||
|
||
// web_sys::console::log_1(&"Hello, world!".into()); | ||
// web_sys::console::log_1(&JsValue::from_str(&format!("Result: {}", v))); | ||
// JsValue::from_str(&format!("Result: {}", v)) | ||
v.to_string() | ||
} |
Oops, something went wrong.