@@ -1,12 +1,8 @@
1#![feature(iter_collect_into)]
1#![feature(iter_collect_into)]
4−use std::{
4+use core::panic;
6− cell::RefCell,
5+use std::{cell::RefCell, rc::Rc};
7− ops::{Deref, DerefMut},
11use dodrio::{
7use dodrio::{
12 bumpalo::{self, Bump},
8 bumpalo::{self, Bump},
@@ -16,10 +12,11 @@ use nock::{
16 cue::cue_bytes,
12 cue::cue_bytes,
17 interpreter::{generate_interpreter_context, slam, InterpreterContext},
13 interpreter::{generate_interpreter_context, slam, InterpreterContext},
18 noun::{cell, Noun},
14 noun::{cell, Noun},
20use wasm_bindgen::prelude::*;
17use wasm_bindgen::prelude::*;
21−use wasm_bindgen_futures::JsFuture;
18+use wasm_bindgen_futures::{spawn_local, JsFuture};
22−use web_sys::Element;
19+use web_sys::{console::error_1, Element};
24#[wasm_bindgen]
21#[wasm_bindgen]
25extern "C" {
22extern "C" {
@@ -32,22 +29,51 @@ extern "C" {
32 fn error(s: &str);
29 fn error(s: &str);
35struct WockApp {
33struct WockApp {
36 nock: Rc<Noun>,
34 nock: Rc<Noun>,
37 ctx: RefCell<InterpreterContext>,
35 ctx: RefCell<InterpreterContext>,
38 model: Rc<Noun>,
36 model: Rc<Noun>,
37+ urbit: RefCell<Option<Rc<Noun>>>,
41+ async fn get_urbit(self: &Self) -> Rc<Noun> {
42+ match self.urbit.borrow().clone() {
43+ Some(urbit) => urbit,
45+ let urbit = load_nock("/urbit.nock").await.unwrap_or_else(|err| {
47+ panic!("Could not load urbit.")
50+ self.urbit.replace(Some(urbit.clone()));
41impl<'a> Render<'a> for WockApp {
58impl<'a> Render<'a> for WockApp {
42 fn render(&self, cx: &mut dodrio::RenderContext<'a>) -> dodrio::Node<'a> {
59 fn render(&self, cx: &mut dodrio::RenderContext<'a>) -> dodrio::Node<'a> {
43− let sail = slam(
60+ match slam(
44 &mut *self.ctx.borrow_mut(),
61 &mut *self.ctx.borrow_mut(),
45 &self.nock,
62 &self.nock,
46 &cell(&Rc::new(Noun::from_bytes(b"view")), &self.model),
63 &cell(&Rc::new(Noun::from_bytes(b"view")), &self.model),
50− render_sail(cx, sail)
65+ Ok(sail) => render_sail(cx, sail),
67+ let wock_app = self.clone();
68+ let mut ctx = (*self.ctx.borrow()).clone();
69+ spawn_local(async move {
70+ let urbit = wock_app.get_urbit().await;
71+ let str = wash(&mut ctx, &urbit, tanks);
72+ error_1(&JsValue::from_str(&str));
74+ panic!("Could not render");
@@ -151,7 +177,10 @@ fn tape_to_bump_str<'a>(bump: &'a Bump, non: &Rc<Noun>) -> &'a str {
152#[wasm_bindgen]
178#[wasm_bindgen]
153pub async fn main(path: &str) {
179pub async fn main(path: &str) {
154− let nock = load_nock(path).await;
180+ let nock = load_nock(path).await.unwrap_or_else(|err| {
182+ panic!("Could not load nock.")
156 let document = gloo::utils::document();
185 let document = gloo::utils::document();
157 let output_el: Element = document.get_element_by_id("output").unwrap();
186 let output_el: Element = document.get_element_by_id("output").unwrap();
@@ -162,8 +191,20 @@ pub async fn main(path: &str) {
162 &mut ctx,
191 &mut ctx,
164 &cell(&Rc::new(Noun::from_bytes(b"init")), &Rc::new(Noun::SIG)),
193 &cell(&Rc::new(Noun::from_bytes(b"init")), &Rc::new(Noun::SIG)),
166− .unwrap();
195+ let model = match model {
198+ let urbit = load_nock("/urbit.nock").await.unwrap_or_else(|err| {
200+ panic!("Could not load urbit.")
203+ let str = wash(&mut ctx, &urbit, tanks);
204+ error_1(&JsValue::from_str(&str));
205+ panic!("Could not load initial model.");
168 dodrio::Vdom::new(
209 dodrio::Vdom::new(
169 &output_el,
210 &output_el,
@@ -171,26 +212,26 @@ pub async fn main(path: &str) {
173 ctx: RefCell::new(ctx),
214 ctx: RefCell::new(ctx),
215+ urbit: RefCell::new(None),
176 .forget();
218 .forget();
179−pub async fn load_nock(path: &str) -> Rc<Noun> {
221+pub async fn load_nock(path: &str) -> Result<Rc<Noun>, JsValue> {
180 let window = web_sys::window().unwrap();
222 let window = web_sys::window().unwrap();
182− let resp = JsFuture::from(window.fetch_with_str(path)).await.unwrap();
224+ let resp = JsFuture::from(window.fetch_with_str(path)).await?;
183 assert!(resp.is_instance_of::<web_sys::Response>());
225 assert!(resp.is_instance_of::<web_sys::Response>());
184− let resp: web_sys::Response = resp.dyn_into().unwrap();
226+ let resp: web_sys::Response = resp.dyn_into()?;
185− let blob = JsFuture::from(resp.blob().unwrap()).await.unwrap();
227+ let blob = JsFuture::from(resp.blob()?).await?;
186 assert!(blob.is_instance_of::<web_sys::Blob>());
228 assert!(blob.is_instance_of::<web_sys::Blob>());
187− let blob: web_sys::Blob = blob.dyn_into().unwrap();
229+ let blob: web_sys::Blob = blob.dyn_into()?;
190− js_sys::Uint8Array::new(&JsFuture::from(blob.array_buffer()).await.unwrap()).to_vec();
231+ let nock = js_sys::Uint8Array::new(&JsFuture::from(blob.array_buffer()).await?).to_vec();
192 let nock = cue_bytes(&*nock);
233 let nock = cue_bytes(&*nock);
193 let (_typ, nok) = nock.as_cell().unwrap();
234 let (_typ, nok) = nock.as_cell().unwrap();
195− nok.clone()
236+ Ok(nok.clone())