~fd/cane-lang

cbacbd4b1e22814981bb96204716df206b463570 — Ersei Saggi 10 months ago 5179dfa
WASM done
3 files changed, 29 insertions(+), 19 deletions(-)

M cane-wasm/Cargo.toml
M cane-wasm/index.html
M cane-wasm/src/lib.rs
M cane-wasm/Cargo.toml => cane-wasm/Cargo.toml +7 -0
@@ 7,6 7,13 @@ description = "A strange programming language"
license = "Apache-2.0"
repository = "https://git.sr.ht/~fd/cane-lang"

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

[dependencies]
cane = { path = "../cane-lang" }
wasm-bindgen = "0.2"

[dependencies.web-sys]
version = "0.3"
features = []

M cane-wasm/index.html => cane-wasm/index.html +15 -5
@@ 2,14 2,24 @@
<html lang="en-US">
  <head>
    <meta charset="utf-8" />
    <title>hello-world example</title>
    <title>Cane Lang Web Playground</title>
  </head>
  <body>
    <button id="run">Run</button>
    <textarea id="code_input">([(`hello, world!\n' print)] 5 repeat)</textarea>
    <pre id="output"></pre>
    <script type="module">
      import init, { interpreter } from "./pkg/cane_lang.js";
      init().then(() => {
        interpreter("(`hello, world!' print)", "");
      });
      import init, { interpreter } from './pkg/cane_wasm.js'
      window.writeOut = function writeOut(text) {
        document.getElementById('output').innerText += text
      }

      document.getElementById('run').onclick = function () {
        document.getElementById('output').innerText = ''
        init().then(() => {
          interpreter(document.getElementById('code_input').value, '')
        })
      }
    </script>
  </body>
</html>

M cane-wasm/src/lib.rs => cane-wasm/src/lib.rs +7 -14
@@ 4,15 4,14 @@ use cane::stdtypes::{Data,Types};

use cane::Interpreter;
use std::collections::HashMap;
use std::io::BufReader;
use std::io::{BufReader, Seek};
use std::io::{Cursor, Write};

#[wasm_bindgen]
extern "C" {
    pub fn alert(s: &str);

    #[wasm_bindgen(js_namespace = console)]
    fn log(s: &str);
    
    pub fn writeOut(s: &str);
}

struct JSOut {}


@@ 25,13 24,10 @@ impl JSOut {

impl Write for JSOut {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        log("writing data...");
        log(std::str::from_utf8(buf).unwrap());
        log("success!");
        writeOut(&std::str::from_utf8(buf).unwrap());
        Ok(buf.len())
    }
    fn flush(&mut self) -> std::io::Result<()> {
        log("flushing...");
        Ok(())
    }
}


@@ 40,10 36,10 @@ impl Write for JSOut {
pub fn interpreter(input: &str, args_str: &str) {
    let args: Vec<_> = args_str.split(" ").collect();
    let mut c = Cursor::new(Vec::new());
    let test_input = input.as_bytes();

    c.write_all(test_input).unwrap();
    let reader = BufReader::new(c);
    c.write_all(input.as_bytes()).unwrap();
    let mut reader = BufReader::new(c);
    reader.rewind().unwrap();

    let mut stdout = JSOut::new();



@@ 58,11 54,8 @@ pub fn interpreter(input: &str, args_str: &str) {

    let mut runner = Interpreter::new(reader, &mut stdout, &mut variables, args_data);

    log("here");

    match runner.execute() {
        Ok(..) => (),
        Err(text) => alert(&text.to_string()),
    }
    log("done");
}