get fetch up & running

This commit is contained in:
Scott Richmond 2025-07-01 18:52:03 -04:00
parent b12d0e00aa
commit 5b2fd5e2d7
11 changed files with 142 additions and 40 deletions

View File

@ -1,6 +1,9 @@
&&& buffers: shared memory with Rust &&& buffers: shared memory with Rust
& use types that are all either empty or any
box console = [] box console = []
box input = "" box input = ""
box fetch_outbox = ""
box fetch_inbox = ()
& the very base: know something's type & the very base: know something's type
fn type { fn type {
@ -1244,7 +1247,7 @@ fn alive? {
} }
fn link! { fn link! {
"Creates a 'hard link' between two processes: if either one dies, they both do." "Creates a link between two processes. There are two types of links: `:report`, which sends a message to pid1 when pid2 dies; and `:enforce`, which causes a panic in one when the other dies. The default is `:report`."
(pid1 as :keyword, pid2 as :keyword) -> link! (pid1, pid2, :report) (pid1 as :keyword, pid2 as :keyword) -> link! (pid1, pid2, :report)
(pid1 as :keyword, pid2 as :keyword, :report) -> base :process (:link_report, pid1, pid2) (pid1 as :keyword, pid2 as :keyword, :report) -> base :process (:link_report, pid1, pid2)
(pid1 as :keyword, pid2 as :keyword, :enforce) -> base :process (:link_enforce, pid1, pid2) (pid1 as :keyword, pid2 as :keyword, :enforce) -> base :process (:link_enforce, pid1, pid2)
@ -1260,7 +1263,43 @@ fn sleep! {
(ms as :number) -> base :process (:sleep, ms) (ms as :number) -> base :process (:sleep, ms)
} }
& TODO: make this more robust, to handle multiple pending requests w/o data races
fn request_fetch! {
(pid as :keyword, url as :string) -> {
store! (fetch_outbox, url)
request_fetch! (pid)
}
(pid as :keyword) -> {
if empty? (unbox (fetch_inbox))
then {
yield! ()
request_fetch! (pid)
}
else {
send (pid, (:reply, unbox (fetch_inbox)))
store! (fetch_inbox, ())
}
}
}
fn fetch! {
"Requests the contents of the URL passed in. Returns a result tuple of `(:ok, {contents})` or `(:err, {status code})`."
(url) -> {
let pid = self ()
spawn! (fn () -> {
print! ("spawning fetch request in", self ())
request_fetch! (pid, url)
})
let out = receive {
(:reply, response) -> response
}
print! ("received response: {out}")
out
}
}
#{ #{
& completed actor functions
self self
send send
spawn! spawn!
@ -1269,10 +1308,17 @@ fn sleep! {
alive? alive?
flush! flush!
& wip actor functions
link! link!
& shared memory w/ rust
console console
input input
fetch_outbox
fetch_inbox
& a fetch fn
fetch!
abs abs
abs abs

View File

@ -1193,8 +1193,8 @@ We've got one bug to address in Firefox before I continue:
After that: After that:
* [ ] implement other verbs beside `console`: * [ ] implement other verbs beside `console`:
- [x] `command` - [x] `command`
- [ ] `input` - [x] `input`
* [ ] js->rust->ludus buffer (in Rust code) * [x] js->rust->ludus buffer (in Rust code)
* [ ] ludus abstractions around this buffer (in Ludus code) * [ ] ludus abstractions around this buffer (in Ludus code)
- [ ] `fetch`--request & response - [ ] `fetch`--request & response
* [ ] request: ludus->rust->js->net * [ ] request: ludus->rust->js->net

View File

@ -13,7 +13,7 @@ let io_interval_id = null
worker.onmessage = handle_messages worker.onmessage = handle_messages
function handle_messages (e) { async function handle_messages (e) {
let msgs let msgs
try { try {
msgs = JSON.parse(e.data) msgs = JSON.parse(e.data)
@ -42,6 +42,13 @@ function handle_messages (e) {
} }
break break
} }
case "Fetch": {
console.log("Main: ludus requests => ", msg.data)
const res = await fetch(msg.data, {mode: "cors"})
const text = await res.text()
console.log("Main: js responds => ", text)
outbox.push({verb: "Fetch", data: [msg.data, res.status, text]})
}
} }
} }
} }
@ -67,6 +74,7 @@ function start_io_polling () {
export function run (source) { export function run (source) {
if (running) "TODO: handle this? should not be running" if (running) "TODO: handle this? should not be running"
running = true running = true
result = null
code = source code = source
outbox = [{verb: "Run", data: source}] outbox = [{verb: "Run", data: source}]
start_io_polling() start_io_polling()

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_malloc: (a: number, b: number) => number;
readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number; readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
readonly __wbindgen_export_6: WebAssembly.Table; readonly __wbindgen_export_6: WebAssembly.Table;
readonly closure342_externref_shim: (a: number, b: number, c: any) => void; readonly closure346_externref_shim: (a: number, b: number, c: any) => void;
readonly closure366_externref_shim: (a: number, b: number, c: any, d: any) => void; readonly closure370_externref_shim: (a: number, b: number, c: any, d: any) => void;
readonly __wbindgen_start: () => void; readonly __wbindgen_start: () => void;
} }

View File

@ -240,13 +240,13 @@ function _assertNum(n) {
function __wbg_adapter_22(arg0, arg1, arg2) { function __wbg_adapter_22(arg0, arg1, arg2) {
_assertNum(arg0); _assertNum(arg0);
_assertNum(arg1); _assertNum(arg1);
wasm.closure342_externref_shim(arg0, arg1, arg2); wasm.closure346_externref_shim(arg0, arg1, arg2);
} }
function __wbg_adapter_50(arg0, arg1, arg2, arg3) { function __wbg_adapter_52(arg0, arg1, arg2, arg3) {
_assertNum(arg0); _assertNum(arg0);
_assertNum(arg1); _assertNum(arg1);
wasm.closure366_externref_shim(arg0, arg1, arg2, arg3); wasm.closure370_externref_shim(arg0, arg1, arg2, arg3);
} }
async function __wbg_load(module, imports) { async function __wbg_load(module, imports) {
@ -325,6 +325,9 @@ function __wbg_get_imports() {
wasm.__wbindgen_free(deferred0_0, deferred0_1, 1); wasm.__wbindgen_free(deferred0_0, deferred0_1, 1);
} }
}, arguments) }; }, arguments) };
imports.wbg.__wbg_log_9e426f8e841e42d3 = function() { return logError(function (arg0, arg1) {
console.log(getStringFromWasm0(arg0, arg1));
}, arguments) };
imports.wbg.__wbg_log_edeb598b620f1ba2 = function() { return logError(function (arg0, arg1) { imports.wbg.__wbg_log_edeb598b620f1ba2 = function() { return logError(function (arg0, arg1) {
let deferred0_0; let deferred0_0;
let deferred0_1; let deferred0_1;
@ -343,7 +346,7 @@ function __wbg_get_imports() {
const a = state0.a; const a = state0.a;
state0.a = 0; state0.a = 0;
try { try {
return __wbg_adapter_50(a, state0.b, arg0, arg1); return __wbg_adapter_52(a, state0.b, arg0, arg1);
} finally { } finally {
state0.a = a; state0.a = a;
} }
@ -422,8 +425,8 @@ function __wbg_get_imports() {
_assertBoolean(ret); _assertBoolean(ret);
return ret; return ret;
}; };
imports.wbg.__wbindgen_closure_wrapper7647 = function() { return logError(function (arg0, arg1, arg2) { imports.wbg.__wbindgen_closure_wrapper7695 = function() { return logError(function (arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 343, __wbg_adapter_22); const ret = makeMutClosure(arg0, arg1, 347, __wbg_adapter_22);
return ret; return ret;
}, arguments) }; }, arguments) };
imports.wbg.__wbindgen_debug_string = function(arg0, arg1) { imports.wbg.__wbindgen_debug_string = 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_malloc: (a: number, b: number) => number;
export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number; export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
export const __wbindgen_export_6: WebAssembly.Table; export const __wbindgen_export_6: WebAssembly.Table;
export const closure342_externref_shim: (a: number, b: number, c: any) => void; export const closure346_externref_shim: (a: number, b: number, c: any) => void;
export const closure366_externref_shim: (a: number, b: number, c: any, d: any) => void; export const closure370_externref_shim: (a: number, b: number, c: any, d: any) => void;
export const __wbindgen_start: () => void; export const __wbindgen_start: () => void;

View File

@ -31,7 +31,7 @@ fn make_json_payload(verb: &'static str, data: String) -> String {
pub enum MsgOut { pub enum MsgOut {
Console(Lines), Console(Lines),
Commands(Commands), Commands(Commands),
SlurpRequest(Url), Fetch(Url),
Complete(FinalValue), Complete(FinalValue),
} }
@ -46,7 +46,6 @@ impl MsgOut {
match self { match self {
MsgOut::Complete(value) => match value { MsgOut::Complete(value) => match value {
Ok(value) => { Ok(value) => {
log(format!("value is: {}", value.show()));
make_json_payload("Complete", serde_json::to_string(&value.show()).unwrap()) make_json_payload("Complete", serde_json::to_string(&value.show()).unwrap())
}, },
Err(_) => make_json_payload("Complete", "\"null\"".to_string()) Err(_) => make_json_payload("Complete", "\"null\"".to_string())
@ -57,7 +56,7 @@ impl MsgOut {
let vals_json = format!("[{vals_json}]"); let vals_json = format!("[{vals_json}]");
make_json_payload("Commands", vals_json) make_json_payload("Commands", vals_json)
} }
MsgOut::SlurpRequest(value) => { MsgOut::Fetch(value) => {
// TODO: do parsing here? // TODO: do parsing here?
// Right now, defer to fetch // Right now, defer to fetch
let url = value.to_json().unwrap(); let url = value.to_json().unwrap();
@ -77,7 +76,7 @@ impl MsgOut {
#[serde(tag = "verb", content = "data")] #[serde(tag = "verb", content = "data")]
pub enum MsgIn { pub enum MsgIn {
Input(String), Input(String),
Fetch(String, String, String), Fetch(String, f64, String),
Kill, Kill,
Keyboard(Vec<String>), Keyboard(Vec<String>),
} }
@ -87,6 +86,7 @@ impl std::fmt::Display for MsgIn {
match self { match self {
MsgIn::Input(str) => write!(f, "Input: {str}"), MsgIn::Input(str) => write!(f, "Input: {str}"),
MsgIn::Kill => write!(f, "Kill"), MsgIn::Kill => write!(f, "Kill"),
MsgIn::Fetch(url, code, text) => write!(f, "Fetch: {url} :: {code} ::\n{text}"),
_ => todo!() _ => todo!()
} }
} }
@ -96,11 +96,15 @@ impl MsgIn {
pub fn to_value(self) -> Value { pub fn to_value(self) -> Value {
match self { match self {
MsgIn::Input(str) => Value::string(str), MsgIn::Input(str) => Value::string(str),
MsgIn::Fetch(url, status, string) => { MsgIn::Fetch(url, status_f64, string) => {
let url = Value::string(url); let url = Value::string(url);
let status = Value::keyword(status); let status = Value::Number(status_f64);
let string = Value::string(string); let text = Value::string(string);
let result_tuple = Value::tuple(vec![status, string]); let result_tuple = if status_f64 == 200.0 {
Value::tuple(vec![Value::keyword("ok".to_string()), text])
} else {
Value::tuple(vec![Value::keyword("err".to_string()), status])
};
Value::tuple(vec![url, result_tuple]) Value::tuple(vec![url, result_tuple])
} }
MsgIn::Kill => Value::Nothing, MsgIn::Kill => Value::Nothing,

View File

@ -60,9 +60,9 @@ fn prelude() -> HashMap<&'static str, Value> {
.into_output_errors(); .into_output_errors();
if !parse_errors.is_empty() { if !parse_errors.is_empty() {
println!("ERROR PARSING PRELUDE:"); log("ERROR PARSING PRELUDE:");
println!("{:?}", parse_errors); log(format!("{:?}", parse_errors).as_str());
panic!(); panic!("parsing errors in prelude");
} }
let parsed = parsed.unwrap(); let parsed = parsed.unwrap();
@ -77,8 +77,9 @@ fn prelude() -> HashMap<&'static str, Value> {
validator.validate(); validator.validate();
if !validator.errors.is_empty() { if !validator.errors.is_empty() {
println!("VALIDATION ERRORS IN PRLUDE:"); log("VALIDATION ERRORS IN PRLUDE:");
report_invalidation(validator.errors); // report_invalidation(validator.errors);
log(format!("{:?}", validator.errors).as_str());
panic!("validator errors in prelude"); panic!("validator errors in prelude");
} }

View File

@ -398,6 +398,21 @@ impl Value {
} }
} }
pub fn as_string(&self) -> Rc<String> {
match self {
Value::String(str) => str.clone(),
Value::Interned(str) => Rc::new(str.to_string()),
_ => unreachable!("expected value to be a string"),
}
}
pub fn as_tuple(&self) -> Rc<Vec<Value>> {
match self {
Value::Tuple(members) => members.clone(),
_ => unreachable!("expected value to be a tuple"),
}
}
pub fn string(str: String) -> Value { pub fn string(str: String) -> Value {
Value::String(Rc::new(str)) Value::String(Rc::new(str))
} }

View File

@ -254,8 +254,8 @@ impl Zoo {
pub struct Buffers { pub struct Buffers {
console: Value, console: Value,
commands: Value, commands: Value,
// fetch_outbox: Value, fetch_out: Value,
// fetch_inbox: Value, fetch_in: Value,
input: Value, input: Value,
} }
@ -264,8 +264,8 @@ impl Buffers {
Buffers { Buffers {
console: prelude.get("console").unwrap().clone(), console: prelude.get("console").unwrap().clone(),
commands: prelude.get("turtle_commands").unwrap().clone(), commands: prelude.get("turtle_commands").unwrap().clone(),
// fetch_outbox: prelude.get("fetch_outbox").unwrap().clone(), fetch_out: prelude.get("fetch_outbox").unwrap().clone(),
// fetch_inbox: prelude.get("fetch_inbox").unwrap().clone(), fetch_in: prelude.get("fetch_inbox").unwrap().clone(),
input: prelude.get("input").unwrap().clone(), input: prelude.get("input").unwrap().clone(),
} }
} }
@ -282,12 +282,13 @@ impl Buffers {
self.commands.as_box() self.commands.as_box()
} }
// pub fn fetch_outbox (&self) -> Rc<RefCell<Value>> { pub fn fetch_out (&self) -> Rc<RefCell<Value>> {
// self.fetch_outbox.as_box() self.fetch_out.as_box()
// } }
// pub fn fetch_inbox (&self) -> Rc<RefCell<Value>> {
// self.fetch_inbox.as_box() pub fn fetch_in (&self) -> Rc<RefCell<Value>> {
// } self.fetch_in.as_box()
}
} }
@ -369,17 +370,35 @@ impl World {
if let Some(commands) = self.flush_commands() { if let Some(commands) = self.flush_commands() {
outbox.push(commands); outbox.push(commands);
} }
if let Some(fetch) = self.make_fetch_happen() {
outbox.push(fetch);
}
outbox outbox
} }
fn make_fetch_happen(&self) -> Option<MsgOut> {
let out = self.buffers.fetch_out();
let working = RefCell::new(Value::Interned(""));
out.swap(&working);
let working = working.borrow();
if working.as_string().is_empty() {
None
} else {
Some(MsgOut::Fetch(working.clone()))
}
}
fn flush_console(&self) -> Option<MsgOut> { fn flush_console(&self) -> Option<MsgOut> {
let console = self.buffers.console(); let console = self.buffers.console();
let working_copy = RefCell::new(Value::new_list()); let working_copy = RefCell::new(Value::new_list());
console.swap(&working_copy); console.swap(&working_copy);
let working_value = working_copy.borrow(); let working_value = working_copy.borrow();
if working_value.as_list().is_empty() { return None; } if working_value.as_list().is_empty() {
None
} else {
Some(MsgOut::Console(working_value.clone())) Some(MsgOut::Console(working_value.clone()))
} }
}
fn flush_commands(&self) -> Option<MsgOut> { fn flush_commands(&self) -> Option<MsgOut> {
let commands = self.buffers.commands(); let commands = self.buffers.commands();
@ -422,11 +441,17 @@ impl World {
input.swap(&working); input.swap(&working);
} }
fn fetch_reply(&mut self, reply: Value) {
let inbox_rc = self.buffers.fetch_in();
inbox_rc.replace(reply);
}
fn fill_buffers(&mut self, inbox: Vec<MsgIn>) { fn fill_buffers(&mut self, inbox: Vec<MsgIn>) {
for msg in inbox { for msg in inbox {
match msg { match msg {
MsgIn::Input(str) => self.fill_input(str), MsgIn::Input(str) => self.fill_input(str),
MsgIn::Kill => self.kill_signal = true, MsgIn::Kill => self.kill_signal = true,
MsgIn::Fetch(..) => self.fetch_reply(msg.to_value()),
_ => todo!() _ => todo!()
} }
} }