1#![feature(iter_collect_into)]
5use std::{borrow::BorrowMut, cell::RefCell, rc::Rc};
9 Attribute, Listener, Node, NodeKey, Render, RenderContext, RootRender, VdomWeak,
11use gloo::net::{self, http::Request};
14 interpreter::{generate_interpreter_context, ram_ttanks, slam, InterpreterContext},
17use wasm_bindgen::prelude::*;
19 console::{error_1, log_1},
27 #[wasm_bindgen(js_namespace = console)]
30 #[wasm_bindgen(js_namespace = console)]
37 ctx: RefCell<InterpreterContext>,
41impl<'a> Render<'a> for WockApp {
42 fn render(&self, cx: &mut dodrio::RenderContext<'a>) -> dodrio::Node<'a> {
44 &mut *self.ctx.borrow_mut(),
46 &cell(&Rc::new(Noun::from_bytes(b"view")), &self.model),
48 Ok(sail) => render_sail(cx, sail),
50 let str = ram_ttanks(&mut (*self).ctx.borrow_mut(), tanks);
51 error_1(&JsValue::from_str(&str));
53 panic!("Could not render");
59fn render_sail<'a>(ctx: &mut RenderContext<'a>, manx: Rc<Noun>) -> Node<'a> {
60 let (marx, marl) = manx.as_cell().unwrap();
61 let (mane, mart) = marx.as_cell().unwrap();
66 .map(|attr| attr.as_cell().unwrap())
67 .find(|(key, _)| key.is_sig())
68 .map(|(_, value)| tape_to_bump_str(ctx.bump, value))
72 let mut children = bumpalo::collections::Vec::new_in(&ctx.bump);
74 .map(|child| render_sail(ctx, child.clone()))
75 .collect_into(&mut children);
77 let mut attributes = bumpalo::collections::Vec::new_in(ctx.bump);
78 let mut listeners = bumpalo::collections::Vec::new_in(ctx.bump);
80 mart.list_iter().for_each(|attr| {
81 let (name, value) = attr.as_cell().unwrap();
82 match &*name.as_bytes().unwrap() {
83 b"on-click" => listeners.push(Listener {
86 let value = value.clone();
88 move |root: &mut dyn RootRender,
90 _event: web_sys::Event| {
91 let app = root.unwrap_mut::<WockApp>();
93 let binding = app.ctx.clone();
94 let mut ctx = binding.borrow_mut();
95 let sig = ctx.nouns.sig.clone();
97 let step: Rc<Noun> = Rc::new((value.clone(), sig).into());
104 Noun::from_bytes(b"move").rc(),
112 app.model = new_model;
113 vdom.schedule_render();
116 let mut ctx = ctx.borrow_mut().clone();
118 let str = ram_ttanks(&mut ctx, tanks);
119 error_1(&JsValue::from_str(&str));
122 "Application failed to process event: {}",
131 _ => attributes.push(Attribute {
132 name: cord_to_bump_str(ctx.bump, name),
133 value: tape_to_bump_str(ctx.bump, value),
141 cord_to_bump_str(ctx.bump, mane),
142 listeners.into_bump_slice(),
143 attributes.into_bump_slice(),
144 children.into_bump_slice(),
150fn cord_to_bump_str<'a>(bump: &'a Bump, non: &Rc<Noun>) -> &'a str {
152 bumpalo::collections::Vec::from_iter_in(non.as_bytes().unwrap().iter().cloned(), bump);
153 &*bumpalo::collections::String::from_utf8(bytes)
158fn tape_to_bump_str<'a>(bump: &'a Bump, non: &Rc<Noun>) -> &'a str {
159 let bytes = bumpalo::collections::Vec::from_iter_in(
160 non.list_iter().map(|char| char.as_u32().unwrap() as u8),
163 &*bumpalo::collections::String::from_utf8(bytes)
169pub async fn main(path: &str) {
170 let nock = load_nock(path).await.unwrap_or_else(|err| {
171 error(&*err.to_string());
172 panic!("Could not load nock.")
175 let document = gloo::utils::document();
176 let output_el: Element = document.get_element_by_id("output").unwrap();
178 let mut ctx = InterpreterContext {
179 slog: |str: &String| log_1(&JsValue::from_str(&*str)),
180 ..generate_interpreter_context()
186 &cell(&Rc::new(Noun::from_bytes(b"init")), &Rc::new(Noun::SIG)),
188 let model = match model {
191 let str = ram_ttanks(&mut ctx, tanks);
192 error_1(&JsValue::from_str(&str));
193 panic!("Could not load initial model.");
202 ctx: RefCell::new(ctx),
208pub async fn load_nock(path: &str) -> Result<Rc<Noun>, net::Error> {
209 let blob = Request::get(path).send().await?.binary().await?;
211 let nock = cue_bytes(&*blob);
212 let (_typ, nok) = nock.as_cell().unwrap();
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list