From 782cfa9e3372d0cfe13471597968d58deb181e71 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Fri, 23 Feb 2024 11:00:13 +0300 Subject: chore(tvixbolt): move from //corp to //web Assigning copyright to the TVL community (whatever that is), and adding AGPL-3.0-or-later license. I also cleaned up some of the stuff on the landing page. Change-Id: I4dbca19406e00e5105fed50e8fb64e0fcca23e3a Reviewed-on: https://cl.tvl.fyi/c/depot/+/11013 Autosubmit: tazjin Reviewed-by: flokli Tested-by: BuildkiteCI --- web/tvixbolt/src/main.rs | 315 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 315 insertions(+) create mode 100644 web/tvixbolt/src/main.rs (limited to 'web/tvixbolt/src/main.rs') diff --git a/web/tvixbolt/src/main.rs b/web/tvixbolt/src/main.rs new file mode 100644 index 000000000000..2e68e03fb0ba --- /dev/null +++ b/web/tvixbolt/src/main.rs @@ -0,0 +1,315 @@ +// tvixbolt - an online tool for exploring Tvix language evaluation +// +// Copyright (C) The TVL Community +// SPDX-License-Identifier: AGPL-3.0-or-later + +use std::fmt::Write; + +use serde::{Deserialize, Serialize}; +use tvix_eval::observer::{DisassemblingObserver, TracingObserver}; +use web_sys::HtmlDetailsElement; +use web_sys::HtmlTextAreaElement; +use yew::prelude::*; +use yew::TargetCast; +use yew_router::{prelude::*, AnyRoute}; + +#[derive(Clone)] +enum Msg { + CodeChange(String), + ToggleTrace(bool), + ToggleDisplayAst(bool), + + // Required because browsers are stupid and it's easy to get into + // infinite loops with `ontoggle` events. + NoOp, +} + +#[derive(Clone, Serialize, Deserialize)] +struct Model { + code: String, + + // #[serde(skip_serializing)] + trace: bool, + + // #[serde(skip_serializing)] + display_ast: bool, +} + +fn tvixbolt_overview() -> Html { + html! { + <> +

+ {"This page lets you explore the bytecode generated by the "} + {"Tvix"} + {" compiler for the Nix language."} +

+

+ {"Tvix is still "}{"work-in-progress"}{" and we would appreciate "} + {"if you told us about bugs you find."} +

+

+ {"Tvixbolt is a project by "} + + {"TVL"} + + {"."} +

+ + } +} + +fn footer_link(location: &'static str, name: &str) -> Html { + html! { + <> + {name}{" | "} + + } +} + +fn footer() -> Html { + html! { + <> +
+
+ +

{"ಠ_ಠ"}

+
+ + } +} + +impl Component for Model { + type Message = Msg; + type Properties = (); + + fn create(_: &Context) -> Self { + BrowserHistory::new() + .location() + .query::() + .unwrap_or_else(|_| Self { + code: String::new(), + trace: false, + display_ast: false, + }) + } + + fn update(&mut self, _: &Context, msg: Self::Message) -> bool { + match msg { + Msg::ToggleTrace(trace) => { + self.trace = trace; + } + + Msg::ToggleDisplayAst(display_ast) => { + self.display_ast = display_ast; + } + + Msg::CodeChange(new_code) => { + self.code = new_code; + } + + Msg::NoOp => {} + } + + let _ = BrowserHistory::new().replace_with_query(AnyRoute::new("/"), self.clone()); + + true + } + + fn view(&self, ctx: &Context) -> Html { + // This gives us a component's "`Scope`" which allows us to send messages, etc to the component. + let link = ctx.link(); + html! { + <> +
+

{"tvixbolt"}

+ {tvixbolt_overview()} +
+
+ {"Input"} + +
+ + +
+
+
+
+ {self.run(ctx)} + {footer()} +
+ + } + } +} + +impl Model { + fn run(&self, ctx: &Context) -> Html { + if self.code.is_empty() { + return html! { +

+ {"Enter some Nix code above to get started. Don't know Nix yet? "} + {"Check out "} + {"nix-1p"} + {"!"} +

+ }; + } + + html! { + <> +

{"Result:"}

+ {eval(self).display(ctx, self)} + + } + } +} + +#[derive(Default)] +struct Output { + errors: String, + warnings: String, + output: String, + bytecode: Vec, + trace: Vec, + ast: String, +} + +fn maybe_show(title: &str, s: &str) -> Html { + if s.is_empty() { + html! {} + } else { + html! { + <> +

{title}

+
{s}
+ + } + } +} + +fn maybe_details( + ctx: &Context, + title: &str, + s: &str, + display: bool, + toggle: fn(bool) -> Msg, +) -> Html { + let link = ctx.link(); + if display { + let msg = toggle(false); + html! { +
(); + if !details.open() { + msg.clone() + } else { + Msg::NoOp + } + })}> + +

{title}

+
{s}
+
+ } + } else { + let msg = toggle(true); + html! { +
(); + if details.open() { + msg.clone() + } else { + Msg::NoOp + } + })}> +

{title}

+
+ } + } +} + +impl Output { + fn display(self, ctx: &Context, model: &Model) -> Html { + html! { + <> + {maybe_show("Errors:", &self.errors)} + {maybe_show("Warnings:", &self.warnings)} + {maybe_show("Output:", &self.output)} + {maybe_show("Bytecode:", &String::from_utf8_lossy(&self.bytecode))} + {maybe_details(ctx, "Runtime trace:", &String::from_utf8_lossy(&self.trace), model.trace, Msg::ToggleTrace)} + {maybe_details(ctx, "Parsed AST:", &self.ast, model.display_ast, Msg::ToggleDisplayAst)} + + } + } +} + +fn eval(model: &Model) -> Output { + let mut out = Output::default(); + + if model.code.is_empty() { + return out; + } + + let mut eval = tvix_eval::Evaluation::new_pure(); + let source = eval.source_map(); + + let result = { + let mut compiler_observer = DisassemblingObserver::new(source.clone(), &mut out.bytecode); + eval.compiler_observer = Some(&mut compiler_observer); + + let mut runtime_observer = TracingObserver::new(&mut out.trace); + if model.trace { + eval.runtime_observer = Some(&mut runtime_observer); + } + + eval.evaluate(&model.code, Some("/nixbolt".into())) + }; + + if model.display_ast { + if let Some(ref expr) = result.expr { + out.ast = tvix_eval::pretty_print_expr(expr); + } + } + + out.output = match result.value { + Some(val) => val.to_string(), + None => "".to_string(), + }; + + for warning in result.warnings { + writeln!( + &mut out.warnings, + "{}\n", + warning.fancy_format_str(&source).trim(), + ) + .unwrap(); + } + + if !result.errors.is_empty() { + for error in &result.errors { + writeln!(&mut out.errors, "{}\n", error.fancy_format_str().trim(),).unwrap(); + } + + return out; + } + + out +} + +fn main() { + yew::start_app::(); +} -- cgit 1.4.1