2025-06-30 16:49:07 +00:00
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
use serde::{Serialize, Deserialize};
|
|
|
|
use crate::value::Value;
|
|
|
|
use crate::vm::Panic;
|
|
|
|
use imbl::Vector;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
|
|
|
|
|
|
#[wasm_bindgen(module = "/pkg/worker.js")]
|
|
|
|
extern "C" {
|
|
|
|
async fn io (output: String) -> JsValue;
|
|
|
|
}
|
|
|
|
|
2025-06-30 22:59:59 +00:00
|
|
|
#[wasm_bindgen]
|
|
|
|
extern "C" {
|
|
|
|
#[wasm_bindgen(js_namespace = console)]
|
|
|
|
fn log(s: String);
|
|
|
|
}
|
|
|
|
|
2025-06-30 16:49:07 +00:00
|
|
|
type Lines = Value; // expect a list of values
|
|
|
|
type Commands = Value; // expect a list of values
|
|
|
|
type Url = Value; // expect a string representing a URL
|
|
|
|
type FinalValue = Result<Value, Panic>;
|
|
|
|
|
|
|
|
fn make_json_payload(verb: &'static str, data: String) -> String {
|
|
|
|
format!("{{\"verb\":\"{verb}\",\"data\":{data}}}")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub enum MsgOut {
|
|
|
|
Console(Lines),
|
|
|
|
Commands(Commands),
|
|
|
|
SlurpRequest(Url),
|
2025-06-30 22:59:59 +00:00
|
|
|
Complete(FinalValue),
|
2025-06-30 16:49:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Display for MsgOut {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
|
|
write!(f, "{}", self.to_json())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MsgOut {
|
|
|
|
pub fn to_json(&self) -> String {
|
|
|
|
match self {
|
|
|
|
MsgOut::Complete(value) => match value {
|
2025-06-30 22:59:59 +00:00
|
|
|
Ok(value) => {
|
|
|
|
log(format!("value is: {}", value.show()));
|
|
|
|
make_json_payload("complete", serde_json::to_string(&value.show()).unwrap())
|
|
|
|
},
|
2025-06-30 16:49:07 +00:00
|
|
|
Err(_) => make_json_payload("complete", "\"null\"".to_string())
|
|
|
|
},
|
|
|
|
MsgOut::Commands(commands) => {
|
|
|
|
let commands = commands.as_list();
|
|
|
|
let vals_json = commands.iter().map(|v| v.to_json().unwrap()).collect::<Vec<_>>().join(",");
|
|
|
|
let vals_json = format!("[{vals_json}]");
|
|
|
|
make_json_payload("commands", vals_json)
|
|
|
|
}
|
|
|
|
MsgOut::SlurpRequest(value) => {
|
|
|
|
// TODO: do parsing here?
|
|
|
|
// Right now, defer to fetch
|
|
|
|
let url = value.to_json().unwrap();
|
|
|
|
make_json_payload("slurp", url)
|
|
|
|
}
|
|
|
|
MsgOut::Console(lines) => {
|
|
|
|
let lines = lines.as_list();
|
2025-06-30 22:59:59 +00:00
|
|
|
let json_lines = lines.iter().map(|line| line.stringify()).collect::<Vec<_>>().join("\\n");
|
2025-06-30 16:49:07 +00:00
|
|
|
let json_lines = format!("\"{json_lines}\"");
|
|
|
|
make_json_payload("console", json_lines)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
|
|
|
#[serde(tag = "verb", content = "data")]
|
|
|
|
pub enum MsgIn {
|
|
|
|
Input(String),
|
|
|
|
SlurpResponse(String, String, String),
|
|
|
|
Kill,
|
|
|
|
Keyboard(Vec<String>),
|
|
|
|
}
|
|
|
|
|
2025-06-30 22:59:59 +00:00
|
|
|
impl std::fmt::Display for MsgIn {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
MsgIn::Input(str) => write!(f, "input: {str}"),
|
|
|
|
MsgIn::Kill => write!(f, "kill"),
|
|
|
|
_ => todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-30 16:49:07 +00:00
|
|
|
impl MsgIn {
|
|
|
|
pub fn to_value(self) -> Value {
|
|
|
|
match self {
|
|
|
|
MsgIn::Input(str) => Value::string(str),
|
|
|
|
MsgIn::SlurpResponse(url, status, string) => {
|
|
|
|
let url = Value::string(url);
|
|
|
|
let status = Value::keyword(status);
|
|
|
|
let string = Value::string(string);
|
|
|
|
let result_tuple = Value::tuple(vec![status, string]);
|
|
|
|
Value::tuple(vec![url, result_tuple])
|
|
|
|
}
|
|
|
|
MsgIn::Kill => Value::Nothing,
|
|
|
|
MsgIn::Keyboard(downkeys) => {
|
|
|
|
let mut vector = Vector::new();
|
|
|
|
for key in downkeys {
|
|
|
|
vector.push_back(Value::String(Rc::new(key)));
|
|
|
|
}
|
|
|
|
Value::List(Box::new(vector))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn do_io (msgs: Vec<MsgOut>) -> Vec<MsgIn> {
|
|
|
|
let outbox = format!("[{}]", msgs.iter().map(|msg| msg.to_json()).collect::<Vec<_>>().join(","));
|
|
|
|
let inbox = io (outbox).await;
|
|
|
|
let inbox = inbox.as_string().expect("response should be a string");
|
2025-06-30 22:59:59 +00:00
|
|
|
log(format!("response is: {inbox}"));
|
2025-06-30 16:49:07 +00:00
|
|
|
let inbox: Vec<MsgIn> = serde_json::from_str(inbox.as_str()).expect("response from js should be valid");
|
2025-06-30 22:59:59 +00:00
|
|
|
if !inbox.is_empty() {
|
|
|
|
log("got messages in ludus!".to_string());
|
|
|
|
for msg in inbox.iter() {
|
|
|
|
log(format!("{}", msg));
|
|
|
|
}
|
|
|
|
}
|
2025-06-30 16:49:07 +00:00
|
|
|
inbox
|
|
|
|
}
|
|
|
|
|