Skip to content

Commit

Permalink
upgrade deps and log to UI
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Mar 19, 2023
1 parent 67a5308 commit 825db5f
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 179 deletions.
20 changes: 7 additions & 13 deletions .github/workflows/upload.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,27 @@ jobs:
steps:
- uses: actions/checkout@v2

- uses: actions-rs/toolchain@v1
- uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
toolchain: nightly
components: clippy

- uses: jetli/[email protected]
with:
version: "latest"

- name: Get yarn cache
id: yarn-cache
run: echo "::set-output name=dir::$(yarn cache dir)"

- uses: actions/cache@v2
name: Cache node modules of yarn
- uses: actions/setup-node@v3
with:
path: ${{ steps.yarn-cache.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
node-version: 16
cache: yarn

- run: wasm-pack build -t web

- run: yarn && yarn vite build --base=./

- name: Deploy to server
id: deploy
uses: Pendect/action-rsyncer@v1.1.0
uses: Pendect/action-rsyncer@v2.0.0
env:
DEPLOY_KEY: ${{secrets.rsync_private_key}}
with:
Expand Down
9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ edition = "2021"

[dependencies]
# calcit = { path="../calcit" }
calcit = "0.6.16"
rpds = "0.12.0"
calcit = "0.6.26"
rpds = "0.13.0"
im_ternary_tree = "0.0.11"
wasm-bindgen = "0.2.83"
console_error_panic_hook = "*"
web-sys = { version="0.3.60", features=[ "console" ] }
web-sys = { version="0.3.61", features=[ "console", 'Window' ] }
js-sys = "0.3.61"

[lib]
crate-type = ["cdylib", "rlib"]

[profile.release]
# debug = true
debug = true
7 changes: 3 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<head>
<meta charset="utf-8">
<title>Play</title>
<script defer type="module" src="./script/main.js"></script>
<script defer type="module" src="./script/main.mjs"></script>
<meta content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no" name="viewport" >
<link rel="icon" type="image/png" href="http://cdn.tiye.me/logo/calcit.png"/>
<link rel="stylesheet" type="text/css" href="http://cdn.tiye.me/favored-fonts/main-fonts.css">
Expand All @@ -22,7 +22,7 @@

</div>
<div class="body">
<textarea class="code" placeholder="code here..." >
<textarea class="code" spellcheck="false" placeholder="code here..." >

println (range 100)

Expand All @@ -34,8 +34,7 @@
recur (* x acc) (dec x)
, acc
println $ fact 1 10

</textarea>
</textarea>
<div class="result">;; logs in Console, open Console to read</div>
</div>

Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
"main": "index.js",
"author": "jiyinyiyong <[email protected]>",
"license": "MIT",
"scripts": {
"watch": "cargo watch -w src -s 'wasm-pack build -t web'"
},
"devDependencies": {
"vite": "^4.0.1"
"vite": "^4.2.0"
},
"dependencies": {
"@mvc-works/codearea": "^0.0.3"
Expand Down
18 changes: 13 additions & 5 deletions script/main.js → script/main.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import init, { run_code } from "../pkg/calcit_wasm_play";
import { codearea } from "@mvc-works/codearea";

let codeEl = document.querySelector(".code");
let resultEl = document.querySelector(".result");

window._calcit_log = (content) => {
resultEl.innerText = resultEl.innerText + "\n" + content;
};
window._calcit_error = (content) => {
resultEl.innerText = resultEl.innerText + "\n" + content;
};

import init, { run_code } from "../pkg/calcit_wasm_play";
import { codearea } from "@mvc-works/codearea";

codearea(codeEl);

init().then((w) => {
Expand All @@ -14,7 +21,7 @@ let run = () => {
let code = codeEl.value;

// console.log("code:", code);
console.clear();
// console.clear();

resultEl.innerText = "";
let start = performance.now();
Expand All @@ -24,7 +31,8 @@ let run = () => {
// console.log("result", result);
let cost = performance.now() - start;

resultEl.innerText = result + "\n\n" + cost + "ms";
resultEl.innerText =
resultEl.innerText + "\n\n" + result + "\n\n" + cost + "ms";
};

document.querySelector(".run").addEventListener("click", run);
Expand Down
22 changes: 20 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// extern crate console_error_panic_hook;
extern crate console_error_panic_hook;
extern crate wasm_bindgen;
extern crate web_sys;

Expand All @@ -13,7 +13,7 @@ use calcit::{
};

pub fn eval_code(snippet: String) -> Result<Calcit, String> {
// panic::set_hook(Box::new(console_error_panic_hook::hook));
panic::set_hook(Box::new(console_error_panic_hook::hook));
program::clear_all_program_evaled_defs("app.main/main!".into(), "app.main/reload!".into(), false)?;

let core_snapshot = load_core_snapshot()?;
Expand Down Expand Up @@ -67,6 +67,15 @@ pub fn console_log(xs: &CalcitItems, _call_stack: &CallStackList) -> Result<Calc
buffer.push_str(&x.turn_string());
}
web_sys::console::log_1(&JsValue::from_str(&buffer));

let args = js_sys::Array::new();
args.push(&JsValue::from_str(&buffer));
// use reflect to get a javascript function
let f: js_sys::Function = js_sys::Reflect::get(&web_sys::window().unwrap(), &JsValue::from_str("_calcit_log"))
.unwrap()
.into();
js_sys::Reflect::apply(&f, &JsValue::null(), &args).unwrap();

Ok(Calcit::Nil)
}

Expand All @@ -79,6 +88,15 @@ pub fn console_error(xs: &CalcitItems, _call_stack: &CallStackList) -> Result<Ca
buffer.push_str(&x.turn_string());
}
web_sys::console::error_1(&JsValue::from_str(&buffer));

let args = js_sys::Array::new();
args.push(&JsValue::from_str(&buffer));
// use reflect to get a javascript function
let f: js_sys::Function = js_sys::Reflect::get(&web_sys::window().unwrap(), &JsValue::from_str("_calcit_error"))
.unwrap()
.into();
js_sys::Reflect::apply(&f, &JsValue::null(), &args).unwrap();

Ok(Calcit::Nil)
}

Expand Down
Loading

0 comments on commit 825db5f

Please sign in to comment.