Skip to content

Commit

Permalink
initial demo of Calcit in WASM
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Oct 4, 2021
0 parents commit cd5d4c0
Show file tree
Hide file tree
Showing 10 changed files with 400 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitattributes
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
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/target
Cargo.lock

/dist
node_modules
yarn-error.log
20 changes: 20 additions & 0 deletions Cargo.toml
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

24 changes: 24 additions & 0 deletions README.md
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
21 changes: 21 additions & 0 deletions index.html
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>
43 changes: 43 additions & 0 deletions main.css
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;
}
28 changes: 28 additions & 0 deletions main.js
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";
});
});
14 changes: 14 additions & 0 deletions package.json
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"
}
}
50 changes: 50 additions & 0 deletions src/lib.rs
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()
}
Loading

0 comments on commit cd5d4c0

Please sign in to comment.