debug multiturtles?

This commit is contained in:
Scott Richmond 2025-07-05 18:36:01 -04:00
parent 50da642758
commit 8e75713cd7
8 changed files with 129 additions and 60 deletions

View File

@ -420,8 +420,8 @@ fn to_number {
fn print! {
"Sends a text representation of Ludus values to the console."
(...args) -> {
base :print! (args)
let line = do args > map (string, _) > join (_, " ")
& base :print! (args)
update! (console, append (_, line))
:ok
}
@ -1167,6 +1167,8 @@ fn add_command! (turtle_id, command) -> {
update! (command_id, inc)
update! (turtle_commands, append (_, (turtle_id, idx, command)))
let prev = do turtle_states > unbox > turtle_id
print!("previous state: {turtle_id}", prev)
print!("applying command", command)
let curr = apply_command (prev, command)
update! (turtle_states, assoc (_, turtle_id, curr))
:ok
@ -1283,12 +1285,13 @@ fn loadstate! {
}
fn turtle_listener () -> {
print!("listening in", self())
receive {
(:forward!, steps as :number) -> add_command! (self (), (:forward, steps))
(:back!, steps as :number) -> add_command! (self (), (:back, steps))
(:left!, turns as :number) -> add_command! (self (), (:left, turns))
(:right!, turns as :number) -> add_command! (self (), (:right, turns))
(:penup!) -> add_command!(self (), (:penup))
(:penup!) -> add_command! (self (), (:penup))
(:pendown!) -> add_command! (self (), (:pendown))
(:pencolor!, color as :keyword) -> add_command! (self (), (:pencolor, color))
(:pencolor!, gray as :number) -> add_command! (self (), (:pencolor, (gray, gray, gray, 255)))
@ -1314,12 +1317,17 @@ fn turtle_listener () -> {
panic! "{pid} does not understand message: {does_not_understand}"
}
}
print! ("lisening from:", self ())
turtle_listener ()
}
fn spawn_turtle! {
"Spawns a new turtle in a new process. Methods on the turtle process mirror those of turtle graphics functions in prelude. Returns the pid of the new turtle."
() -> spawn! (fn () -> turtle_listener ())
() -> {
let pid = spawn! (fn () -> turtle_listener ())
update! (turtle_states, assoc (_, pid, turtle_init))
pid
}
}
fn apply_command {

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 closure338_externref_shim: (a: number, b: number, c: any) => void;
readonly closure351_externref_shim: (a: number, b: number, c: any, d: any) => void;
readonly closure353_externref_shim: (a: number, b: number, c: any) => void;
readonly closure376_externref_shim: (a: number, b: number, c: any, d: any) => void;
readonly __wbindgen_start: () => void;
}

View File

@ -17,6 +17,22 @@ function handleError(f, args) {
}
}
function logError(f, args) {
try {
return f.apply(this, args);
} catch (e) {
let error = (function () {
try {
return e instanceof Error ? `${e.message}\n\nStack:\n${e.stack}` : e.toString();
} catch(_) {
return "<failed to stringify thrown value>";
}
}());
console.error("wasm-bindgen: imported JS function that was not marked as `catch` threw an error:", error);
throw e;
}
}
const cachedTextDecoder = (typeof TextDecoder !== 'undefined' ? new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }) : { decode: () => { throw Error('TextDecoder not available') } } );
if (typeof TextDecoder !== 'undefined') { cachedTextDecoder.decode(); };
@ -54,6 +70,8 @@ const encodeString = (typeof cachedTextEncoder.encodeInto === 'function'
function passStringToWasm0(arg, malloc, realloc) {
if (typeof(arg) !== 'string') throw new Error(`expected a string argument, found ${typeof(arg)}`);
if (realloc === undefined) {
const buf = cachedTextEncoder.encode(arg);
const ptr = malloc(buf.length, 1) >>> 0;
@ -82,7 +100,7 @@ function passStringToWasm0(arg, malloc, realloc) {
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
const ret = encodeString(arg, view);
if (ret.read !== arg.length) throw new Error('failed to pass whole string');
offset += ret.written;
ptr = realloc(ptr, len, offset, 1) >>> 0;
}
@ -104,6 +122,12 @@ function isLikeNone(x) {
return x === undefined || x === null;
}
function _assertBoolean(n) {
if (typeof(n) !== 'boolean') {
throw new Error(`expected a boolean argument, found ${typeof(n)}`);
}
}
const CLOSURE_DTORS = (typeof FinalizationRegistry === 'undefined')
? { register: () => {}, unregister: () => {} }
: new FinalizationRegistry(state => {
@ -210,12 +234,19 @@ export function ludus(src) {
return ret;
}
function _assertNum(n) {
if (typeof(n) !== 'number') throw new Error(`expected a number argument, found ${typeof(n)}`);
}
function __wbg_adapter_20(arg0, arg1, arg2) {
wasm.closure338_externref_shim(arg0, arg1, arg2);
_assertNum(arg0);
_assertNum(arg1);
wasm.closure353_externref_shim(arg0, arg1, arg2);
}
function __wbg_adapter_46(arg0, arg1, arg2, arg3) {
wasm.closure351_externref_shim(arg0, arg1, arg2, arg3);
_assertNum(arg0);
_assertNum(arg1);
wasm.closure376_externref_shim(arg0, arg1, arg2, arg3);
}
async function __wbg_load(module, imports) {
@ -260,7 +291,7 @@ function __wbg_get_imports() {
const ret = arg0.call(arg1, arg2);
return ret;
}, arguments) };
imports.wbg.__wbg_error_7534b8e9a36f1ab4 = function(arg0, arg1) {
imports.wbg.__wbg_error_7534b8e9a36f1ab4 = function() { return logError(function (arg0, arg1) {
let deferred0_0;
let deferred0_1;
try {
@ -270,7 +301,7 @@ function __wbg_get_imports() {
} finally {
wasm.__wbindgen_free(deferred0_0, deferred0_1, 1);
}
};
}, arguments) };
imports.wbg.__wbg_io_5a3c8ea72d8c6ea3 = function() { return handleError(function (arg0, arg1) {
let deferred0_0;
let deferred0_1;
@ -283,10 +314,10 @@ function __wbg_get_imports() {
wasm.__wbindgen_free(deferred0_0, deferred0_1, 1);
}
}, arguments) };
imports.wbg.__wbg_log_11652c6a56eeddfb = function(arg0, arg1) {
imports.wbg.__wbg_log_11652c6a56eeddfb = function() { return logError(function (arg0, arg1) {
console.log(getStringFromWasm0(arg0, arg1));
};
imports.wbg.__wbg_new_23a2665fac83c611 = function(arg0, arg1) {
}, arguments) };
imports.wbg.__wbg_new_23a2665fac83c611 = function() { return logError(function (arg0, arg1) {
try {
var state0 = {a: arg0, b: arg1};
var cb0 = (arg0, arg1) => {
@ -303,65 +334,65 @@ function __wbg_get_imports() {
} finally {
state0.a = state0.b = 0;
}
};
imports.wbg.__wbg_new_8a6f238a6ece86ea = function() {
}, arguments) };
imports.wbg.__wbg_new_8a6f238a6ece86ea = function() { return logError(function () {
const ret = new Error();
return ret;
};
imports.wbg.__wbg_newnoargs_105ed471475aaf50 = function(arg0, arg1) {
}, arguments) };
imports.wbg.__wbg_newnoargs_105ed471475aaf50 = function() { return logError(function (arg0, arg1) {
const ret = new Function(getStringFromWasm0(arg0, arg1));
return ret;
};
imports.wbg.__wbg_now_8dddb61fa4928554 = function() {
}, arguments) };
imports.wbg.__wbg_now_8dddb61fa4928554 = function() { return logError(function () {
const ret = Date.now();
return ret;
};
imports.wbg.__wbg_queueMicrotask_97d92b4fcc8a61c5 = function(arg0) {
}, arguments) };
imports.wbg.__wbg_queueMicrotask_97d92b4fcc8a61c5 = function() { return logError(function (arg0) {
queueMicrotask(arg0);
};
imports.wbg.__wbg_queueMicrotask_d3219def82552485 = function(arg0) {
}, arguments) };
imports.wbg.__wbg_queueMicrotask_d3219def82552485 = function() { return logError(function (arg0) {
const ret = arg0.queueMicrotask;
return ret;
};
imports.wbg.__wbg_random_57c118f142535bb6 = function() {
}, arguments) };
imports.wbg.__wbg_random_57c118f142535bb6 = function() { return logError(function () {
const ret = Math.random();
return ret;
};
imports.wbg.__wbg_resolve_4851785c9c5f573d = function(arg0) {
}, arguments) };
imports.wbg.__wbg_resolve_4851785c9c5f573d = function() { return logError(function (arg0) {
const ret = Promise.resolve(arg0);
return ret;
};
imports.wbg.__wbg_stack_0ed75d68575b0f3c = function(arg0, arg1) {
}, arguments) };
imports.wbg.__wbg_stack_0ed75d68575b0f3c = function() { return logError(function (arg0, arg1) {
const ret = arg1.stack;
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.__wbg_static_accessor_GLOBAL_88a902d13a557d07 = function() {
}, arguments) };
imports.wbg.__wbg_static_accessor_GLOBAL_88a902d13a557d07 = function() { return logError(function () {
const ret = typeof global === 'undefined' ? null : global;
return isLikeNone(ret) ? 0 : addToExternrefTable0(ret);
};
imports.wbg.__wbg_static_accessor_GLOBAL_THIS_56578be7e9f832b0 = function() {
}, arguments) };
imports.wbg.__wbg_static_accessor_GLOBAL_THIS_56578be7e9f832b0 = function() { return logError(function () {
const ret = typeof globalThis === 'undefined' ? null : globalThis;
return isLikeNone(ret) ? 0 : addToExternrefTable0(ret);
};
imports.wbg.__wbg_static_accessor_SELF_37c5d418e4bf5819 = function() {
}, arguments) };
imports.wbg.__wbg_static_accessor_SELF_37c5d418e4bf5819 = function() { return logError(function () {
const ret = typeof self === 'undefined' ? null : self;
return isLikeNone(ret) ? 0 : addToExternrefTable0(ret);
};
imports.wbg.__wbg_static_accessor_WINDOW_5de37043a91a9c40 = function() {
}, arguments) };
imports.wbg.__wbg_static_accessor_WINDOW_5de37043a91a9c40 = function() { return logError(function () {
const ret = typeof window === 'undefined' ? null : window;
return isLikeNone(ret) ? 0 : addToExternrefTable0(ret);
};
imports.wbg.__wbg_then_44b73946d2fb3e7d = function(arg0, arg1) {
}, arguments) };
imports.wbg.__wbg_then_44b73946d2fb3e7d = function() { return logError(function (arg0, arg1) {
const ret = arg0.then(arg1);
return ret;
};
imports.wbg.__wbg_then_48b406749878a531 = function(arg0, arg1, arg2) {
}, arguments) };
imports.wbg.__wbg_then_48b406749878a531 = function() { return logError(function (arg0, arg1, arg2) {
const ret = arg0.then(arg1, arg2);
return ret;
};
}, arguments) };
imports.wbg.__wbindgen_cb_drop = function(arg0) {
const obj = arg0.original;
if (obj.cnt-- == 1) {
@ -369,12 +400,13 @@ function __wbg_get_imports() {
return true;
}
const ret = false;
_assertBoolean(ret);
return ret;
};
imports.wbg.__wbindgen_closure_wrapper1041 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 339, __wbg_adapter_20);
imports.wbg.__wbindgen_closure_wrapper8094 = function() { return logError(function (arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 354, __wbg_adapter_20);
return ret;
};
}, arguments) };
imports.wbg.__wbindgen_debug_string = function(arg0, arg1) {
const ret = debugString(arg1);
const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
@ -394,10 +426,12 @@ function __wbg_get_imports() {
};
imports.wbg.__wbindgen_is_function = function(arg0) {
const ret = typeof(arg0) === 'function';
_assertBoolean(ret);
return ret;
};
imports.wbg.__wbindgen_is_undefined = function(arg0) {
const ret = arg0 === undefined;
_assertBoolean(ret);
return ret;
};
imports.wbg.__wbindgen_string_get = function(arg0, arg1) {

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 closure338_externref_shim: (a: number, b: number, c: any) => void;
export const closure351_externref_shim: (a: number, b: number, c: any, d: any) => void;
export const closure353_externref_shim: (a: number, b: number, c: any) => void;
export const closure376_externref_shim: (a: number, b: number, c: any, d: any) => void;
export const __wbindgen_start: () => void;

View File

@ -277,7 +277,6 @@ pub fn print(x: &Value) -> Value {
.map(|val| format!("{val}"))
.collect::<Vec<_>>()
.join(" ");
// println!("{out}");
console_log!("{out}");
Value::Keyword("ok")
}

View File

@ -359,8 +359,8 @@ impl Creature {
}
pub fn interpret(&mut self) {
println!("starting process {}", self.pid);
println!(
console_log!("starting process {}", self.pid);
console_log!(
"mbx: {}",
self.mbx
.iter()
@ -376,7 +376,7 @@ impl Creature {
return;
}
if self.r#yield {
// println!("process {} has explicitly yielded", self.pid);
console_log!("yielding from {}", self.pid);
return;
}
if self.reductions >= MAX_REDUCTIONS {
@ -1119,6 +1119,11 @@ impl Creature {
}
Value::Keyword(key) => {
let dict = self.pop();
if arity != 1 {
return self.panic_with(
"called keywords may only take a single argument".to_string(),
);
}
match dict {
Value::Dict(d) => self
.push(d.get(&Key::Keyword(key)).unwrap_or(&Value::Nil).clone()),
@ -1153,15 +1158,6 @@ impl Creature {
));
}
let splat_arity = called.as_fn().splat_arity();
if splat_arity > 0 && arity >= splat_arity {
let splatted_args = self.stack.split_off(
self.stack.len() - (arity - splat_arity) as usize - 1,
);
let gathered_args = Vector::from(splatted_args);
self.push(Value::List(Box::new(gathered_args)));
}
let mut scrutinee = vec![];
for i in 0..arity {
scrutinee.push(
@ -1171,6 +1167,15 @@ impl Creature {
}
self.scrutinee = Some(Value::tuple(scrutinee));
let splat_arity = called.as_fn().splat_arity();
if splat_arity > 0 && arity >= splat_arity {
let splatted_args = self.stack.split_off(
self.stack.len() - (arity - splat_arity) as usize - 1,
);
let gathered_args = Vector::from(splatted_args);
self.push(Value::List(Box::new(gathered_args)));
}
let arity = if splat_arity > 0 {
splat_arity.min(arity)
} else {
@ -1239,6 +1244,11 @@ impl Creature {
}
Value::Keyword(key) => {
let dict = self.pop();
if arity != 1 {
return self.panic_with(
"called keywords may only take a single argument".to_string(),
);
}
match dict {
Value::Dict(d) => self
.push(d.get(&Key::Keyword(key)).unwrap_or(&Value::Nil).clone()),

View File

@ -456,6 +456,22 @@ impl World {
self.last_io = now();
}
fn send_ludus_msg(&mut self, msg: String) {
let console = self.buffers.console();
let mut console = console.as_ref().borrow_mut();
let Value::List(ref mut console) = *console else {unreachable!("expect console to be a list")};
console.push_back(Value::string(msg));
}
fn report_process_end(&mut self) {
let result = self.active_result().clone().unwrap();
let msg = match result {
Ok(value) => format!("process {} returned with {}", self.active_id().unwrap(), value.show()),
Err(panic) => format!("process {} panicked with {}", self.active_id().unwrap(), crate::errors::panic(panic))
};
self.send_ludus_msg(msg);
}
pub async fn run(&mut self) {
self.activate_main();
self.ready_io().await;
@ -475,6 +491,8 @@ impl World {
let outbox = self.complete_main();
do_io(outbox).await;
return;
} else {
self.report_process_end()
}
self.kill_active();
}