Compare commits

..

3 Commits

Author SHA1 Message Date
Scott Richmond
1ec60b9362 get commands wired up, probs 2025-07-01 14:35:36 -04:00
Scott Richmond
400bd5864b fix FF event loop bug 2025-07-01 12:54:11 -04:00
Scott Richmond
991705e734 add thoughts 2025-07-01 11:10:50 -04:00
12 changed files with 172 additions and 196 deletions

View File

@ -3,8 +3,6 @@ name = "rudus"
version = "0.0.1"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
crate-type = ["cdylib", "rlib"]
@ -19,4 +17,3 @@ wasm-bindgen-futures = "0.4.50"
serde = {version = "1.0", features = ["derive"]}
serde_json = "1.0"
console_error_panic_hook = "0.1.7"
# talc = "4.4.3"

View File

@ -1034,7 +1034,7 @@ box turtle_state = turtle_init
fn apply_command
fn add_command! (command) -> {
update! (turtle_commands, append (_, command))
update! (turtle_commands, append (_, (:turtle_0, command)))
let prev = unbox (turtle_state)
let curr = apply_command (prev, command)
store! (turtle_state, curr)

View File

@ -1181,6 +1181,30 @@ That leaves the following list:
Happy Canada day!
After a really rough evening, I seem to have the actor model not only working in Ludus, but reasonably debugged in Rust.
We've got one bug to address in Firefox before I continue:
* [x] the event loop isn't returning once something is done, which makes no sense
- What seems to be happening is that the javascript behaviour is subtly different
- Current situation is that synchronous scripts work just fine
- But async scripts work ONCE, and then not again
- In FF, `do_io` doesn't return after `complete_main` in the `world` loop the second time.
- Which is to say, that last call to `io` isn't completing.
- Do I hack around this or do I try to find the source of the problem?
After that:
* [ ] implement other verbs beside `console`:
- [x] `command`
- [ ] `input`
* [ ] js->rust->ludus buffer (in Rust code)
* [ ] ludus abstractions around this buffer (in Ludus code)
- [ ] `fetch`--request & response
* [ ] request: ludus->rust->js->net
* [ ] response: js->rust->ludus
- [ ] `keyboard`
* [ ] still working on how to represent this
* [ ] hook this up to `web.ludus.dev`
* [ ] do some integration testing
- [ ] do synchronous programs still work?
- [ ] animations?
- [ ] read inputs?
- [ ] load url text?

View File

@ -1,82 +1,126 @@
if (window) window.ludus = {run, kill, flush_console, p5, svg, turtle_commands, result, input}
if (window) window.ludus = {run, kill, flush_stdout, stdout, p5, svg, flush_commands, commands, result, input, is_running}
const worker = new Worker("worker.js", {type: "module"})
let outbox = []
let ludus_console = ""
let ludus_commands = []
let ludus_result = null
let code = null
let running = false
let io_interval_id = null
worker.onmessage = async (e) => {
worker.onmessage = handle_messages
function handle_messages (e) {
let msgs
try {
msgs = JSON.parse(e.data)
} catch {
console.log(e.data)
throw Error("bad json from Ludus")
throw Error("Main: bad json from Ludus")
}
for (const msg of msgs) {
switch (msg.verb) {
case "complete": {
console.log("ludus completed with => ", msg.data)
res = msg.data
console.log("Main: ludus completed with => ", msg.data)
ludus_result = msg.data
running = false
break
}
// TODO: do more than report these
case "console": {
console.log("ludus says => ", msg.data)
console.log("Main: ludus says => ", msg.data)
ludus_console = ludus_console + msg.data
break
}
case "commands": {
console.log("Main: ludus commands => ", msg.data)
for (const command of msg.data) {
ludus_commands.push(command)
}
break
}
}
}
}
let res = null
let code = null
let running = false
let io_interval_id = null
const io_poller = () => {
function io_poller () {
if (io_interval_id && !running) {
// flush the outbox one last time
worker.postMessage(outbox)
// cancel the poller
clearInterval(io_interval_id)
return
} else {
worker.postMessage(outbox)
outbox = []
}
worker.postMessage(outbox)
outbox = []
}
function poll_io () {
function start_io_polling () {
io_interval_id = setInterval(io_poller, 10)
}
// runs a ludus script; does not return the result
// the result must be explicitly polled with `result`
export function run (source) {
if (running) "TODO: handle this? should not be running"
running = true
code = source
outbox.push({verb: "run", data: source})
poll_io()
outbox = [{verb: "run", data: source}]
start_io_polling()
}
// tells if the ludus script is still running
export function is_running() {
return running
}
// kills a ludus script
export function kill () {
running = false
outbox.push({verb: "kill"})
}
// sends text into ludus (status: not working)
export function input (text) {
outbox.push({verb: "input", data: text})
}
export function flush_console () {
if (!res) return ""
return res.io.stdout.data
// returns the contents of the ludus console and resets the console
export function flush_stdout () {
let out = ludus_console
ludus_console = ""
out
}
export function turtle_commands () {
if (!res) return []
return res.io.turtle.data
// returns the contents of the ludus console, retaining them
export function stdout () {
return ludus_console
}
// returns the array of turtle commands
export function commands () {
return ludus_commands
}
// returns the array of turtle commands and clears it
export function flush_commands () {
let out = ludus_commands
ludus_commands = []
out
}
// returns the ludus result
// this is effectively Option<String>:
// null if no result has been returned, or
// a string representation of the result
export function result () {
return res
return ludus_result
}
//////////// turtle plumbing below
// TODO: refactor this out into modules
const turtle_init = {
position: [0, 0],
heading: 0,
@ -131,8 +175,9 @@ function unit_of (heading) {
return [Math.cos(radians), Math.sin(radians)]
}
function command_to_state (prev_state, curr_command) {
const verb = curr_command[0]
function command_to_state (prev_state, command) {
const [_target, curr_command] = command
const [verb] = curr_command
switch (verb) {
case "goto": {
const [_, x, y] = curr_command
@ -349,7 +394,7 @@ export function svg (commands) {
return accum
}, {maxX: 0, maxY: 0, minX: 0, minY: 0})
const [r, g, b, a] = resolve_color(background_color)
const [r, g, b] = resolve_color(background_color)
if ((r+g+b)/3 > 128) turtle_color = [0, 0, 0, 150]
const view_width = (maxX - minX) * 1.2
const view_height = (maxY - minY) * 1.2

4
pkg/rudus.d.ts vendored
View File

@ -14,8 +14,8 @@ export interface InitOutput {
readonly __wbindgen_malloc: (a: number, b: number) => number;
readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
readonly __wbindgen_export_6: WebAssembly.Table;
readonly closure327_externref_shim: (a: number, b: number, c: any) => void;
readonly closure340_externref_shim: (a: number, b: number, c: any, d: any) => void;
readonly closure323_externref_shim: (a: number, b: number, c: any) => void;
readonly closure336_externref_shim: (a: number, b: number, c: any, d: any) => void;
readonly __wbindgen_start: () => void;
}

View File

@ -134,71 +134,6 @@ function makeMutClosure(arg0, arg1, dtor, f) {
CLOSURE_DTORS.register(real, state, state);
return real;
}
function debugString(val) {
// primitive types
const type = typeof val;
if (type == 'number' || type == 'boolean' || val == null) {
return `${val}`;
}
if (type == 'string') {
return `"${val}"`;
}
if (type == 'symbol') {
const description = val.description;
if (description == null) {
return 'Symbol';
} else {
return `Symbol(${description})`;
}
}
if (type == 'function') {
const name = val.name;
if (typeof name == 'string' && name.length > 0) {
return `Function(${name})`;
} else {
return 'Function';
}
}
// objects
if (Array.isArray(val)) {
const length = val.length;
let debug = '[';
if (length > 0) {
debug += debugString(val[0]);
}
for(let i = 1; i < length; i++) {
debug += ', ' + debugString(val[i]);
}
debug += ']';
return debug;
}
// Test for built-in
const builtInMatches = /\[object ([^\]]+)\]/.exec(toString.call(val));
let className;
if (builtInMatches && builtInMatches.length > 1) {
className = builtInMatches[1];
} else {
// Failed to match the standard '[object ClassName]'
return toString.call(val);
}
if (className == 'Object') {
// we're a user defined class or Object
// JSON.stringify avoids problems with cycles, and is generally much
// easier than looping through ownProperties of `val`.
try {
return 'Object(' + JSON.stringify(val) + ')';
} catch (_) {
return 'Object';
}
}
// errors
if (val instanceof Error) {
return `${val.name}: ${val.message}\n${val.stack}`;
}
// TODO we could test for more things here, like `Set`s and `Map`s.
return className;
}
/**
* @param {string} src
* @returns {Promise<string>}
@ -210,12 +145,12 @@ export function ludus(src) {
return ret;
}
function __wbg_adapter_22(arg0, arg1, arg2) {
wasm.closure327_externref_shim(arg0, arg1, arg2);
function __wbg_adapter_20(arg0, arg1, arg2) {
wasm.closure323_externref_shim(arg0, arg1, arg2);
}
function __wbg_adapter_52(arg0, arg1, arg2, arg3) {
wasm.closure340_externref_shim(arg0, arg1, arg2, arg3);
function __wbg_adapter_48(arg0, arg1, arg2, arg3) {
wasm.closure336_externref_shim(arg0, arg1, arg2, arg3);
}
async function __wbg_load(module, imports) {
@ -271,7 +206,7 @@ function __wbg_get_imports() {
wasm.__wbindgen_free(deferred0_0, deferred0_1, 1);
}
};
imports.wbg.__wbg_io_4b41f8089de924df = function(arg0, arg1) {
imports.wbg.__wbg_io_5a3c8ea72d8c6ea3 = function() { return handleError(function (arg0, arg1) {
let deferred0_0;
let deferred0_1;
try {
@ -282,7 +217,7 @@ function __wbg_get_imports() {
} finally {
wasm.__wbindgen_free(deferred0_0, deferred0_1, 1);
}
};
}, arguments) };
imports.wbg.__wbg_log_86d603e98cc11395 = function(arg0, arg1) {
let deferred0_0;
let deferred0_1;
@ -294,9 +229,6 @@ function __wbg_get_imports() {
wasm.__wbindgen_free(deferred0_0, deferred0_1, 1);
}
};
imports.wbg.__wbg_log_9e426f8e841e42d3 = function(arg0, arg1) {
console.log(getStringFromWasm0(arg0, arg1));
};
imports.wbg.__wbg_log_edeb598b620f1ba2 = function(arg0, arg1) {
let deferred0_0;
let deferred0_1;
@ -315,7 +247,7 @@ function __wbg_get_imports() {
const a = state0.a;
state0.a = 0;
try {
return __wbg_adapter_52(a, state0.b, arg0, arg1);
return __wbg_adapter_48(a, state0.b, arg0, arg1);
} finally {
state0.a = a;
}
@ -393,17 +325,10 @@ function __wbg_get_imports() {
const ret = false;
return ret;
};
imports.wbg.__wbindgen_closure_wrapper974 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 328, __wbg_adapter_22);
imports.wbg.__wbindgen_closure_wrapper969 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 324, __wbg_adapter_20);
return ret;
};
imports.wbg.__wbindgen_debug_string = function(arg0, arg1) {
const ret = debugString(arg1);
const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
const len1 = WASM_VECTOR_LEN;
getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
};
imports.wbg.__wbindgen_init_externref_table = function() {
const table = wasm.__wbindgen_export_2;
const offset = table.grow(4);

Binary file not shown.

View File

@ -9,6 +9,6 @@ export const __wbindgen_free: (a: number, b: number, c: number) => void;
export const __wbindgen_malloc: (a: number, b: number) => number;
export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
export const __wbindgen_export_6: WebAssembly.Table;
export const closure327_externref_shim: (a: number, b: number, c: any) => void;
export const closure340_externref_shim: (a: number, b: number, c: any, d: any) => void;
export const closure323_externref_shim: (a: number, b: number, c: any) => void;
export const closure336_externref_shim: (a: number, b: number, c: any, d: any) => void;
export const __wbindgen_start: () => void;

View File

@ -1,39 +1,55 @@
import init, {ludus} from "./rudus.js";
let initialized_wasm = false
onmessage = run
// exposed in rust as:
// async fn io (out: String) -> Result<JsValue, JsValue>
// rust calls this to perform io
export function io (out) {
// only send messages if we have some
if (out.length > 0) postMessage(out)
return new Promise((resolve, _) => {
// make an event handler that captures and delivers messages from the main thread
// because our promise resolution isn't about calculating a value but setting a global variable, we can't asyncify it
// explicitly return a promise
return new Promise((resolve, reject) => {
// deliver the response to ludus when we get a response from the main thread
onmessage = (e) => {
// console.log("Worker: from Ludus:", e.data)
resolve(JSON.stringify(e.data))
}
// cancel the response if it takes too long
setTimeout(() => reject("io took too long"), 500)
})
}
let loaded_wasm = false
// set as default event handler from main thread
async function run(e) {
if (!loaded_wasm) {
loaded_wasm = true
// we must NEVER run `await init()` twice
if (!initialized_wasm) {
// this must come before the init call
initialized_wasm = true
await init()
console.log("Worker: Ludus has been initialized.")
}
// the data is always an array; we only really expect one member tho
let msgs = e.data
for (const msg of msgs) {
// evaluate source if we get some
if (msg.verb === "run" && typeof msg.data === 'string') {
// console.log("running ludus!")
// temporarily stash an empty function so we don't keep calling this one if we receive additional messages
onmessage = () => {}
console.log("Worker: Beginning new Ludus run.")
// actually run the ludus--which will call `io`--and replace `run` as the event handler for ipc
await ludus(msg.data)
// once we've returned from `ludus`, make this the event handler again
onmessage = run
} else {
// report and swallow any malformed startup messages
console.log("Worker: Did not get valid startup message. Instead got:")
console.log(e.data)
}
}
}
onmessage = run

View File

@ -8,7 +8,8 @@ use std::rc::Rc;
#[wasm_bindgen(module = "/pkg/worker.js")]
extern "C" {
async fn io (output: String) -> JsValue;
#[wasm_bindgen(catch)]
async fn io (output: String) -> Result<JsValue, JsValue>;
}
#[wasm_bindgen]
@ -117,6 +118,11 @@ impl MsgIn {
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;
// if our request dies, make sure we return back to the event loop
let inbox = match inbox {
Ok(msgs) => msgs,
Err(_) => return vec![]
};
let inbox = inbox.as_string().expect("response should be a string");
let inbox: Vec<MsgIn> = serde_json::from_str(inbox.as_str()).expect("response from js should be valid");
if !inbox.is_empty() {

View File

@ -114,7 +114,6 @@ extern "C" {
#[wasm_bindgen]
pub async fn ludus(src: String) -> String {
log("Ludus: starting ludus run.");
console_error_panic_hook::set_once();
let src = src.to_string().leak();
let (tokens, lex_errs) = lexer().parse(src).into_output_errors();
@ -134,7 +133,6 @@ pub async fn ludus(src: String) -> String {
let parsed: &'static Spanned<Ast> = Box::leak(Box::new(parse_result.unwrap()));
let prelude = prelude();
let postlude = prelude.clone();
// let prelude = imbl::HashMap::new();
let mut validator = Validator::new(&parsed.0, &parsed.1, "user input", src, prelude.clone());
@ -148,7 +146,7 @@ pub async fn ludus(src: String) -> String {
let mut compiler = Compiler::new(
parsed,
"sandbox",
"ludus script",
src,
0,
prelude.clone(),
@ -156,6 +154,7 @@ pub async fn ludus(src: String) -> String {
);
compiler.compile();
if DEBUG_SCRIPT_COMPILE {
println!("=== source code ===");
println!("{src}");
@ -173,63 +172,13 @@ pub async fn ludus(src: String) -> String {
world.run().await;
let result = world.result.clone().unwrap();
let console = postlude.get("console").unwrap();
let Value::Box(console) = console else {
unreachable!()
};
let Value::List(ref lines) = *console.borrow() else {
unreachable!()
};
let mut console = lines
.iter()
.map(|line| line.stringify())
.collect::<Vec<_>>()
.join("\n");
let turtle_commands = postlude.get("turtle_commands").unwrap();
let Value::Box(commands) = turtle_commands else {
unreachable!()
};
let commands = commands.borrow();
let commands = commands.to_json().unwrap();
let output = match result {
Ok(val) => val.show(),
Err(panic) => {
console = format!("{console}\nLudus panicked! {panic}");
"".to_string()
}
Err(panic) => format!("Ludus panicked! {panic}")
};
if DEBUG_SCRIPT_RUN {
// vm.print_stack();
}
// TODO: use serde_json to make this more robust?
format!(
"{{\"result\":\"{output}\",\"io\":{{\"stdout\":{{\"proto\":[\"text-stream\",\"0.1.0\"],\"data\":\"{console}\"}},\"turtle\":{{\"proto\":[\"turtle-graphics\",\"0.1.0\"],\"data\":{commands}}}}}}}"
)
}
pub fn fmt(src: &'static str) -> Result<String, String> {
let (tokens, lex_errs) = lexer().parse(src).into_output_errors();
if !lex_errs.is_empty() {
println!("{:?}", lex_errs);
return Err(format!("{:?}", lex_errs));
}
let tokens = tokens.unwrap();
let (parse_result, parse_errors) = parser()
.parse(Stream::from_iter(tokens).map((0..src.len()).into(), |(t, s)| (t, s)))
.into_output_errors();
if !parse_errors.is_empty() {
return Err(format!("{:?}", parse_errors));
}
// ::sigh:: The AST should be 'static
// This simplifies lifetimes, and
// in any event, the AST should live forever
let parsed: &'static Spanned<Ast> = Box::leak(Box::new(parse_result.unwrap()));
Ok(parsed.0.show())
output
}

View File

@ -212,12 +212,6 @@ impl Zoo {
let mut proc = Status::Nested(proc);
swap(&mut proc, &mut self.procs[*idx]);
}
// Removed because, well, we shouldn't have creatures we don't know about
// And since zoo.next now cleans (and thus kills) before the world releases its active process
// We'll die if we execute this check
// else {
// unreachable!("tried to return a process the world doesn't know about");
// }
}
pub fn is_available(&self) -> bool {
@ -233,6 +227,10 @@ impl Zoo {
let starting_idx = self.active_idx;
self.active_idx = (self.active_idx + 1) % self.procs.len();
while !self.is_available() {
// we've gone round the process queue already
// that means no process is active
// but we may have processes that are alive and asleep
// if nothing is active, yield back to the world's event loop
if self.active_idx == starting_idx {
return ""
}
@ -255,7 +253,7 @@ impl Zoo {
#[derive(Debug, Clone, PartialEq)]
pub struct Buffers {
console: Value,
// commands: Value,
commands: Value,
// fetch_outbox: Value,
// fetch_inbox: Value,
input: Value,
@ -265,7 +263,7 @@ impl Buffers {
pub fn new (prelude: imbl::HashMap<&'static str, Value>) -> Buffers {
Buffers {
console: prelude.get("console").unwrap().clone(),
// commands: prelude.get("commands").unwrap().clone(),
commands: prelude.get("turtle_commands").unwrap().clone(),
// fetch_outbox: prelude.get("fetch_outbox").unwrap().clone(),
// fetch_inbox: prelude.get("fetch_inbox").unwrap().clone(),
input: prelude.get("input").unwrap().clone(),
@ -280,9 +278,10 @@ impl Buffers {
self.input.as_box()
}
// pub fn commands (&self) -> Rc<RefCell<Value>> {
// self.commands.as_box()
// }
pub fn commands (&self) -> Rc<RefCell<Value>> {
self.commands.as_box()
}
// pub fn fetch_outbox (&self) -> Rc<RefCell<Value>> {
// self.fetch_outbox.as_box()
// }
@ -367,6 +366,9 @@ impl World {
if let Some(console) = self.flush_console() {
outbox.push(console);
}
if let Some(commands) = self.flush_commands() {
outbox.push(commands);
}
outbox
}
@ -379,6 +381,18 @@ impl World {
Some(MsgOut::Console(working_value.clone()))
}
fn flush_commands(&self) -> Option<MsgOut> {
let commands = self.buffers.commands();
let working_copy = RefCell::new(Value::new_list());
commands.swap(&working_copy);
let commands = working_copy.borrow();
if commands.as_list().is_empty() {
None
} else {
Some(MsgOut::Commands(commands.clone()))
}
}
fn complete_main(&mut self) -> Vec<MsgOut> {
let mut outbox = self.flush_buffers();
// TODO: if we have a panic, actually add the panic message to the console