on master help sign in
Got basic sail working
ec4cb45iko
nock/sail.hoonnew file+5
@@ -0,0 +1,5 @@
1+|= *
2+;div
3+ ;h1: Hello from Hoon!
4+ ;p: text (:
5+==
nock/sail.nocknew file

Binary file not shown.

sail.nocknew file

Binary file not shown.

wock-web/Cargo.toml+2
@@ -21,6 +21,8 @@ nock = { path = "../nock" }
21console_error_panic_hook = { version = "0.1.7", optional = true }
21console_error_panic_hook = { version = "0.1.7", optional = true }
22wasm-bindgen-futures = "0.4.45"
22wasm-bindgen-futures = "0.4.45"
23js-sys = "0.3.72"
23js-sys = "0.3.72"
24+# dodrio = "0.2.0"
25+dodrio = { path = "../../dodrio" }
24
26
25[dependencies.web-sys]
27[dependencies.web-sys]
26version = "0.3.72"
28version = "0.3.72"
wock-web/index.html+1−1
@@ -5,6 +5,6 @@
5 <body>
5 <body>
6 <input id="input"></input>
6 <input id="input"></input>
7 <div id="output"></div>
7 <div id="output"></div>
8− <script type="module" src="script.mjs?src=/hello.nock"></script>
8+ <script type="module" src="script.mjs?src=/sail.nock"></script>
9 </body>
9 </body>
10</html>
10</html>
wock-web/src/lib.rs+79−31
@@ -1,8 +1,12 @@
1+#![feature(iter_collect_into)]
1mod utils;
2mod utils;
2
3
3use std::rc::Rc;
4use std::rc::Rc;
4
5
5−use gloo::events::EventListener;
6+use dodrio::{
7+ bumpalo::{self, Bump},
8+ Attribute, Node, NodeKey, Render, RenderContext,
9+};
6use nock::{
10use nock::{
7 cue::cue_bytes,
11 cue::cue_bytes,
8 interpreter::{generate_interpreter_context, slam},
12 interpreter::{generate_interpreter_context, slam},
@@ -10,7 +14,7 @@ use nock::{
10};
14};
11use wasm_bindgen::prelude::*;
15use wasm_bindgen::prelude::*;
12use wasm_bindgen_futures::JsFuture;
16use wasm_bindgen_futures::JsFuture;
13−use web_sys::{Element, HtmlInputElement};
17+use web_sys::Element;
14
18
15#[wasm_bindgen]
19#[wasm_bindgen]
16extern "C" {
20extern "C" {
@@ -20,45 +24,89 @@ extern "C" {
20 fn log(s: &str);
24 fn log(s: &str);
21}
25}
22
26
23−#[wasm_bindgen]
27+struct WockApp {
24−pub fn greet() {
28+ nock: Rc<Noun>,
25− alert("Hello, wock-web!");
29+}
30+
31+impl<'a> Render<'a> for WockApp {
32+ fn render(&self, cx: &mut dodrio::RenderContext<'a>) -> dodrio::Node<'a> {
33+ let mut ctx = generate_interpreter_context();
34+
35+ let sail = slam(&mut ctx, &self.nock, &Rc::new(Noun::from_u32(0))).unwrap();
36+
37+ render_sail(cx, &sail)
38+ }
39+}
40+
41+fn render_sail<'a>(ctx: &mut RenderContext<'a>, manx: &Rc<Noun>) -> Node<'a> {
42+ let (marx, marl) = manx.as_cell().unwrap();
43+ let (mane, mart) = marx.as_cell().unwrap();
44+
45+ if mane.is_sig() {
46+ let contents = mart
47+ .list_iter()
48+ .map(|attr| attr.as_cell().unwrap())
49+ .find(|(key, _)| key.is_sig())
50+ .map(|(_, value)| tape_to_bump_str(ctx.bump, value))
51+ .unwrap_or("");
52+ Node::text(contents)
53+ } else {
54+ let mut children = bumpalo::collections::Vec::new_in(&ctx.bump);
55+ marl.list_iter()
56+ .map(|child| render_sail(ctx, child))
57+ .collect_into(&mut children);
58+
59+ let mut attributes = bumpalo::collections::Vec::new_in(ctx.bump);
60+ mart.list_iter()
61+ .map(|attr| {
62+ let (name, value) = attr.as_cell().unwrap();
63+ Attribute {
64+ name: cord_to_bump_str(ctx.bump, name),
65+ value: cord_to_bump_str(ctx.bump, value),
66+ }
67+ })
68+ .collect_into(&mut attributes);
69+
70+ let listeners = bumpalo::collections::Vec::new_in(ctx.bump);
71+
72+ Node::element(
73+ &ctx.bump,
74+ NodeKey::NONE,
75+ cord_to_bump_str(ctx.bump, mane),
76+ listeners.into_bump_slice(),
77+ attributes.into_bump_slice(),
78+ children.into_bump_slice(),
79+ None,
80+ )
81+ }
26}
82}
27
83
28−static mut LISTENER: Option<EventListener> = None;
84+fn cord_to_bump_str<'a>(bump: &'a Bump, non: &Rc<Noun>) -> &'a str {
85+ let bytes =
86+ bumpalo::collections::Vec::from_iter_in(non.as_bytes().unwrap().iter().cloned(), bump);
87+ &*bumpalo::collections::String::from_utf8(bytes)
88+ .unwrap()
89+ .into_bump_str()
90+}
91+
92+fn tape_to_bump_str<'a>(bump: &'a Bump, non: &Rc<Noun>) -> &'a str {
93+ let bytes = bumpalo::collections::Vec::from_iter_in(
94+ non.list_iter().map(|char| char.as_u32().unwrap() as u8),
95+ bump,
96+ );
97+ &*bumpalo::collections::String::from_utf8(bytes)
98+ .unwrap()
99+ .into_bump_str()
100+}
29
101
30#[wasm_bindgen]
102#[wasm_bindgen]
31pub async fn main(path: &str) {
103pub async fn main(path: &str) {
32 let nock = load_nock(path).await;
104 let nock = load_nock(path).await;
33
105
34− let mut ctx = generate_interpreter_context();
35−
36 let document = gloo::utils::document();
106 let document = gloo::utils::document();
37− let input_element: web_sys::HtmlInputElement = document
38− .get_element_by_id("input")
39− .unwrap()
40− .dyn_into()
41− .unwrap();
42 let output_el: Element = document.get_element_by_id("output").unwrap();
107 let output_el: Element = document.get_element_by_id("output").unwrap();
43− unsafe {
44− LISTENER = Some(EventListener::new(&input_element, "input", move |ev| {
45− let el: HtmlInputElement = ev.target().unwrap().dyn_into().unwrap();
46−
47− let bar = slam(
48− &mut ctx,
49− &nock,
50− &Rc::new(Noun::from_bytes(el.value().as_bytes())),
51− )
52− .unwrap();
53−
54− let foo = bar.as_bytes().unwrap();
55− let qux = String::from_utf8(foo).unwrap();
56−
57− output_el.set_inner_html(&*qux);
58− }));
59− }
60
108
61− // window.get_eleme
109+ dodrio::Vdom::new(&output_el, WockApp { nock }).forget();
62}
110}
63
111
64pub async fn load_nock(path: &str) -> Rc<Noun> {
112pub async fn load_nock(path: &str) -> Rc<Noun> {