Compare commits
No commits in common. "1ec60b9362f53e9bf5e0eeda8a7ae0b7322c5506" and "989e217917d2744b395a712807b0051d42a2baef" have entirely different histories.
1ec60b9362
...
989e217917
|
@ -3,6 +3,8 @@ name = "rudus"
|
||||||
version = "0.0.1"
|
version = "0.0.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
crate-type = ["cdylib", "rlib"]
|
crate-type = ["cdylib", "rlib"]
|
||||||
|
|
||||||
|
@ -17,3 +19,4 @@ wasm-bindgen-futures = "0.4.50"
|
||||||
serde = {version = "1.0", features = ["derive"]}
|
serde = {version = "1.0", features = ["derive"]}
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
console_error_panic_hook = "0.1.7"
|
console_error_panic_hook = "0.1.7"
|
||||||
|
# talc = "4.4.3"
|
||||||
|
|
|
@ -1034,7 +1034,7 @@ box turtle_state = turtle_init
|
||||||
fn apply_command
|
fn apply_command
|
||||||
|
|
||||||
fn add_command! (command) -> {
|
fn add_command! (command) -> {
|
||||||
update! (turtle_commands, append (_, (:turtle_0, command)))
|
update! (turtle_commands, append (_, command))
|
||||||
let prev = unbox (turtle_state)
|
let prev = unbox (turtle_state)
|
||||||
let curr = apply_command (prev, command)
|
let curr = apply_command (prev, command)
|
||||||
store! (turtle_state, curr)
|
store! (turtle_state, curr)
|
||||||
|
|
|
@ -1181,30 +1181,6 @@ That leaves the following list:
|
||||||
Happy Canada day!
|
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.
|
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?
|
|
||||||
|
|
||||||
|
|
101
pkg/ludus.js
101
pkg/ludus.js
|
@ -1,126 +1,82 @@
|
||||||
if (window) window.ludus = {run, kill, flush_stdout, stdout, p5, svg, flush_commands, commands, result, input, is_running}
|
if (window) window.ludus = {run, kill, flush_console, p5, svg, turtle_commands, result, input}
|
||||||
|
|
||||||
const worker = new Worker("worker.js", {type: "module"})
|
const worker = new Worker("worker.js", {type: "module"})
|
||||||
|
|
||||||
let outbox = []
|
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 = handle_messages
|
worker.onmessage = async (e) => {
|
||||||
|
|
||||||
function handle_messages (e) {
|
|
||||||
let msgs
|
let msgs
|
||||||
try {
|
try {
|
||||||
msgs = JSON.parse(e.data)
|
msgs = JSON.parse(e.data)
|
||||||
} catch {
|
} catch {
|
||||||
console.log(e.data)
|
console.log(e.data)
|
||||||
throw Error("Main: bad json from Ludus")
|
throw Error("bad json from Ludus")
|
||||||
}
|
}
|
||||||
for (const msg of msgs) {
|
for (const msg of msgs) {
|
||||||
switch (msg.verb) {
|
switch (msg.verb) {
|
||||||
case "complete": {
|
case "complete": {
|
||||||
console.log("Main: ludus completed with => ", msg.data)
|
console.log("ludus completed with => ", msg.data)
|
||||||
ludus_result = msg.data
|
res = msg.data
|
||||||
running = false
|
running = false
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
// TODO: do more than report these
|
|
||||||
case "console": {
|
case "console": {
|
||||||
console.log("Main: ludus says => ", msg.data)
|
console.log("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
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function io_poller () {
|
let res = null
|
||||||
|
let code = null
|
||||||
|
let running = false
|
||||||
|
let io_interval_id = null
|
||||||
|
|
||||||
|
const io_poller = () => {
|
||||||
if (io_interval_id && !running) {
|
if (io_interval_id && !running) {
|
||||||
// flush the outbox one last time
|
|
||||||
worker.postMessage(outbox)
|
|
||||||
// cancel the poller
|
|
||||||
clearInterval(io_interval_id)
|
clearInterval(io_interval_id)
|
||||||
} else {
|
return
|
||||||
worker.postMessage(outbox)
|
|
||||||
outbox = []
|
|
||||||
}
|
}
|
||||||
|
worker.postMessage(outbox)
|
||||||
|
outbox = []
|
||||||
}
|
}
|
||||||
|
|
||||||
function start_io_polling () {
|
function poll_io () {
|
||||||
io_interval_id = setInterval(io_poller, 10)
|
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) {
|
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
|
||||||
code = source
|
code = source
|
||||||
outbox = [{verb: "run", data: source}]
|
outbox.push({verb: "run", data: source})
|
||||||
start_io_polling()
|
poll_io()
|
||||||
}
|
}
|
||||||
|
|
||||||
// tells if the ludus script is still running
|
|
||||||
export function is_running() {
|
|
||||||
return running
|
|
||||||
}
|
|
||||||
|
|
||||||
// kills a ludus script
|
|
||||||
export function kill () {
|
export function kill () {
|
||||||
running = false
|
running = false
|
||||||
outbox.push({verb: "kill"})
|
outbox.push({verb: "kill"})
|
||||||
}
|
}
|
||||||
|
|
||||||
// sends text into ludus (status: not working)
|
|
||||||
export function input (text) {
|
export function input (text) {
|
||||||
outbox.push({verb: "input", data: text})
|
outbox.push({verb: "input", data: text})
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns the contents of the ludus console and resets the console
|
export function flush_console () {
|
||||||
export function flush_stdout () {
|
if (!res) return ""
|
||||||
let out = ludus_console
|
return res.io.stdout.data
|
||||||
ludus_console = ""
|
|
||||||
out
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns the contents of the ludus console, retaining them
|
export function turtle_commands () {
|
||||||
export function stdout () {
|
if (!res) return []
|
||||||
return ludus_console
|
return res.io.turtle.data
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 () {
|
export function result () {
|
||||||
return ludus_result
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////// turtle plumbing below
|
|
||||||
// TODO: refactor this out into modules
|
|
||||||
const turtle_init = {
|
const turtle_init = {
|
||||||
position: [0, 0],
|
position: [0, 0],
|
||||||
heading: 0,
|
heading: 0,
|
||||||
|
@ -175,9 +131,8 @@ function unit_of (heading) {
|
||||||
return [Math.cos(radians), Math.sin(radians)]
|
return [Math.cos(radians), Math.sin(radians)]
|
||||||
}
|
}
|
||||||
|
|
||||||
function command_to_state (prev_state, command) {
|
function command_to_state (prev_state, curr_command) {
|
||||||
const [_target, curr_command] = command
|
const verb = curr_command[0]
|
||||||
const [verb] = curr_command
|
|
||||||
switch (verb) {
|
switch (verb) {
|
||||||
case "goto": {
|
case "goto": {
|
||||||
const [_, x, y] = curr_command
|
const [_, x, y] = curr_command
|
||||||
|
@ -394,7 +349,7 @@ export function svg (commands) {
|
||||||
return accum
|
return accum
|
||||||
|
|
||||||
}, {maxX: 0, maxY: 0, minX: 0, minY: 0})
|
}, {maxX: 0, maxY: 0, minX: 0, minY: 0})
|
||||||
const [r, g, b] = resolve_color(background_color)
|
const [r, g, b, a] = resolve_color(background_color)
|
||||||
if ((r+g+b)/3 > 128) turtle_color = [0, 0, 0, 150]
|
if ((r+g+b)/3 > 128) turtle_color = [0, 0, 0, 150]
|
||||||
const view_width = (maxX - minX) * 1.2
|
const view_width = (maxX - minX) * 1.2
|
||||||
const view_height = (maxY - minY) * 1.2
|
const view_height = (maxY - minY) * 1.2
|
||||||
|
|
4
pkg/rudus.d.ts
vendored
4
pkg/rudus.d.ts
vendored
|
@ -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 closure323_externref_shim: (a: number, b: number, c: any) => void;
|
readonly closure327_externref_shim: (a: number, b: number, c: any) => void;
|
||||||
readonly closure336_externref_shim: (a: number, b: number, c: any, d: any) => void;
|
readonly closure340_externref_shim: (a: number, b: number, c: any, d: any) => void;
|
||||||
readonly __wbindgen_start: () => void;
|
readonly __wbindgen_start: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
93
pkg/rudus.js
93
pkg/rudus.js
|
@ -134,6 +134,71 @@ function makeMutClosure(arg0, arg1, dtor, f) {
|
||||||
CLOSURE_DTORS.register(real, state, state);
|
CLOSURE_DTORS.register(real, state, state);
|
||||||
return real;
|
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
|
* @param {string} src
|
||||||
* @returns {Promise<string>}
|
* @returns {Promise<string>}
|
||||||
|
@ -145,12 +210,12 @@ export function ludus(src) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
function __wbg_adapter_20(arg0, arg1, arg2) {
|
function __wbg_adapter_22(arg0, arg1, arg2) {
|
||||||
wasm.closure323_externref_shim(arg0, arg1, arg2);
|
wasm.closure327_externref_shim(arg0, arg1, arg2);
|
||||||
}
|
}
|
||||||
|
|
||||||
function __wbg_adapter_48(arg0, arg1, arg2, arg3) {
|
function __wbg_adapter_52(arg0, arg1, arg2, arg3) {
|
||||||
wasm.closure336_externref_shim(arg0, arg1, arg2, arg3);
|
wasm.closure340_externref_shim(arg0, arg1, arg2, arg3);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function __wbg_load(module, imports) {
|
async function __wbg_load(module, imports) {
|
||||||
|
@ -206,7 +271,7 @@ function __wbg_get_imports() {
|
||||||
wasm.__wbindgen_free(deferred0_0, deferred0_1, 1);
|
wasm.__wbindgen_free(deferred0_0, deferred0_1, 1);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
imports.wbg.__wbg_io_5a3c8ea72d8c6ea3 = function() { return handleError(function (arg0, arg1) {
|
imports.wbg.__wbg_io_4b41f8089de924df = function(arg0, arg1) {
|
||||||
let deferred0_0;
|
let deferred0_0;
|
||||||
let deferred0_1;
|
let deferred0_1;
|
||||||
try {
|
try {
|
||||||
|
@ -217,7 +282,7 @@ function __wbg_get_imports() {
|
||||||
} finally {
|
} finally {
|
||||||
wasm.__wbindgen_free(deferred0_0, deferred0_1, 1);
|
wasm.__wbindgen_free(deferred0_0, deferred0_1, 1);
|
||||||
}
|
}
|
||||||
}, arguments) };
|
};
|
||||||
imports.wbg.__wbg_log_86d603e98cc11395 = function(arg0, arg1) {
|
imports.wbg.__wbg_log_86d603e98cc11395 = function(arg0, arg1) {
|
||||||
let deferred0_0;
|
let deferred0_0;
|
||||||
let deferred0_1;
|
let deferred0_1;
|
||||||
|
@ -229,6 +294,9 @@ function __wbg_get_imports() {
|
||||||
wasm.__wbindgen_free(deferred0_0, deferred0_1, 1);
|
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) {
|
imports.wbg.__wbg_log_edeb598b620f1ba2 = function(arg0, arg1) {
|
||||||
let deferred0_0;
|
let deferred0_0;
|
||||||
let deferred0_1;
|
let deferred0_1;
|
||||||
|
@ -247,7 +315,7 @@ function __wbg_get_imports() {
|
||||||
const a = state0.a;
|
const a = state0.a;
|
||||||
state0.a = 0;
|
state0.a = 0;
|
||||||
try {
|
try {
|
||||||
return __wbg_adapter_48(a, state0.b, arg0, arg1);
|
return __wbg_adapter_52(a, state0.b, arg0, arg1);
|
||||||
} finally {
|
} finally {
|
||||||
state0.a = a;
|
state0.a = a;
|
||||||
}
|
}
|
||||||
|
@ -325,10 +393,17 @@ function __wbg_get_imports() {
|
||||||
const ret = false;
|
const ret = false;
|
||||||
return ret;
|
return ret;
|
||||||
};
|
};
|
||||||
imports.wbg.__wbindgen_closure_wrapper969 = function(arg0, arg1, arg2) {
|
imports.wbg.__wbindgen_closure_wrapper974 = function(arg0, arg1, arg2) {
|
||||||
const ret = makeMutClosure(arg0, arg1, 324, __wbg_adapter_20);
|
const ret = makeMutClosure(arg0, arg1, 328, __wbg_adapter_22);
|
||||||
return ret;
|
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() {
|
imports.wbg.__wbindgen_init_externref_table = function() {
|
||||||
const table = wasm.__wbindgen_export_2;
|
const table = wasm.__wbindgen_export_2;
|
||||||
const offset = table.grow(4);
|
const offset = table.grow(4);
|
||||||
|
|
Binary file not shown.
4
pkg/rudus_bg.wasm.d.ts
vendored
4
pkg/rudus_bg.wasm.d.ts
vendored
|
@ -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 closure323_externref_shim: (a: number, b: number, c: any) => void;
|
export const closure327_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 closure340_externref_shim: (a: number, b: number, c: any, d: any) => void;
|
||||||
export const __wbindgen_start: () => void;
|
export const __wbindgen_start: () => void;
|
||||||
|
|
|
@ -1,55 +1,39 @@
|
||||||
import init, {ludus} from "./rudus.js";
|
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) {
|
export function io (out) {
|
||||||
// only send messages if we have some
|
|
||||||
if (out.length > 0) postMessage(out)
|
if (out.length > 0) postMessage(out)
|
||||||
// make an event handler that captures and delivers messages from the main thread
|
return new Promise((resolve, _) => {
|
||||||
// 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) => {
|
onmessage = (e) => {
|
||||||
|
// console.log("Worker: from Ludus:", e.data)
|
||||||
resolve(JSON.stringify(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) {
|
async function run(e) {
|
||||||
// we must NEVER run `await init()` twice
|
if (!loaded_wasm) {
|
||||||
if (!initialized_wasm) {
|
loaded_wasm = true
|
||||||
// this must come before the init call
|
|
||||||
initialized_wasm = true
|
|
||||||
await init()
|
await init()
|
||||||
console.log("Worker: Ludus has been initialized.")
|
console.log("Worker: Ludus has been initialized.")
|
||||||
}
|
}
|
||||||
// the data is always an array; we only really expect one member tho
|
|
||||||
let msgs = e.data
|
let msgs = e.data
|
||||||
for (const msg of msgs) {
|
for (const msg of msgs) {
|
||||||
// evaluate source if we get some
|
|
||||||
if (msg.verb === "run" && typeof msg.data === 'string') {
|
if (msg.verb === "run" && typeof msg.data === 'string') {
|
||||||
// temporarily stash an empty function so we don't keep calling this one if we receive additional messages
|
// console.log("running ludus!")
|
||||||
onmessage = () => {}
|
onmessage = () => {}
|
||||||
// actually run the ludus--which will call `io`--and replace `run` as the event handler for ipc
|
console.log("Worker: Beginning new Ludus run.")
|
||||||
await ludus(msg.data)
|
await ludus(msg.data)
|
||||||
// once we've returned from `ludus`, make this the event handler again
|
|
||||||
onmessage = run
|
onmessage = run
|
||||||
} else {
|
} else {
|
||||||
// report and swallow any malformed startup messages
|
|
||||||
console.log("Worker: Did not get valid startup message. Instead got:")
|
console.log("Worker: Did not get valid startup message. Instead got:")
|
||||||
console.log(e.data)
|
console.log(e.data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onmessage = run
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -8,8 +8,7 @@ use std::rc::Rc;
|
||||||
|
|
||||||
#[wasm_bindgen(module = "/pkg/worker.js")]
|
#[wasm_bindgen(module = "/pkg/worker.js")]
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#[wasm_bindgen(catch)]
|
async fn io (output: String) -> JsValue;
|
||||||
async fn io (output: String) -> Result<JsValue, JsValue>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
|
@ -118,11 +117,6 @@ impl MsgIn {
|
||||||
pub async fn do_io (msgs: Vec<MsgOut>) -> Vec<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 outbox = format!("[{}]", msgs.iter().map(|msg| msg.to_json()).collect::<Vec<_>>().join(","));
|
||||||
let inbox = io (outbox).await;
|
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 = 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");
|
let inbox: Vec<MsgIn> = serde_json::from_str(inbox.as_str()).expect("response from js should be valid");
|
||||||
if !inbox.is_empty() {
|
if !inbox.is_empty() {
|
||||||
|
|
59
src/lib.rs
59
src/lib.rs
|
@ -114,6 +114,7 @@ extern "C" {
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub async fn ludus(src: String) -> String {
|
pub async fn ludus(src: String) -> String {
|
||||||
|
log("Ludus: starting ludus run.");
|
||||||
console_error_panic_hook::set_once();
|
console_error_panic_hook::set_once();
|
||||||
let src = src.to_string().leak();
|
let src = src.to_string().leak();
|
||||||
let (tokens, lex_errs) = lexer().parse(src).into_output_errors();
|
let (tokens, lex_errs) = lexer().parse(src).into_output_errors();
|
||||||
|
@ -133,6 +134,7 @@ pub async fn ludus(src: String) -> String {
|
||||||
let parsed: &'static Spanned<Ast> = Box::leak(Box::new(parse_result.unwrap()));
|
let parsed: &'static Spanned<Ast> = Box::leak(Box::new(parse_result.unwrap()));
|
||||||
|
|
||||||
let prelude = prelude();
|
let prelude = prelude();
|
||||||
|
let postlude = prelude.clone();
|
||||||
// let prelude = imbl::HashMap::new();
|
// let prelude = imbl::HashMap::new();
|
||||||
|
|
||||||
let mut validator = Validator::new(&parsed.0, &parsed.1, "user input", src, prelude.clone());
|
let mut validator = Validator::new(&parsed.0, &parsed.1, "user input", src, prelude.clone());
|
||||||
|
@ -146,7 +148,7 @@ pub async fn ludus(src: String) -> String {
|
||||||
|
|
||||||
let mut compiler = Compiler::new(
|
let mut compiler = Compiler::new(
|
||||||
parsed,
|
parsed,
|
||||||
"ludus script",
|
"sandbox",
|
||||||
src,
|
src,
|
||||||
0,
|
0,
|
||||||
prelude.clone(),
|
prelude.clone(),
|
||||||
|
@ -154,7 +156,6 @@ pub async fn ludus(src: String) -> String {
|
||||||
);
|
);
|
||||||
|
|
||||||
compiler.compile();
|
compiler.compile();
|
||||||
|
|
||||||
if DEBUG_SCRIPT_COMPILE {
|
if DEBUG_SCRIPT_COMPILE {
|
||||||
println!("=== source code ===");
|
println!("=== source code ===");
|
||||||
println!("{src}");
|
println!("{src}");
|
||||||
|
@ -172,13 +173,63 @@ pub async fn ludus(src: String) -> String {
|
||||||
world.run().await;
|
world.run().await;
|
||||||
let result = world.result.clone().unwrap();
|
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 {
|
let output = match result {
|
||||||
Ok(val) => val.show(),
|
Ok(val) => val.show(),
|
||||||
Err(panic) => format!("Ludus panicked! {panic}")
|
Err(panic) => {
|
||||||
|
console = format!("{console}\nLudus panicked! {panic}");
|
||||||
|
"".to_string()
|
||||||
|
}
|
||||||
};
|
};
|
||||||
if DEBUG_SCRIPT_RUN {
|
if DEBUG_SCRIPT_RUN {
|
||||||
// vm.print_stack();
|
// vm.print_stack();
|
||||||
}
|
}
|
||||||
|
|
||||||
output
|
// 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())
|
||||||
}
|
}
|
||||||
|
|
36
src/world.rs
36
src/world.rs
|
@ -212,6 +212,12 @@ impl Zoo {
|
||||||
let mut proc = Status::Nested(proc);
|
let mut proc = Status::Nested(proc);
|
||||||
swap(&mut proc, &mut self.procs[*idx]);
|
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 {
|
pub fn is_available(&self) -> bool {
|
||||||
|
@ -227,10 +233,6 @@ impl Zoo {
|
||||||
let starting_idx = self.active_idx;
|
let starting_idx = self.active_idx;
|
||||||
self.active_idx = (self.active_idx + 1) % self.procs.len();
|
self.active_idx = (self.active_idx + 1) % self.procs.len();
|
||||||
while !self.is_available() {
|
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 {
|
if self.active_idx == starting_idx {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
@ -253,7 +255,7 @@ impl Zoo {
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub struct Buffers {
|
pub struct Buffers {
|
||||||
console: Value,
|
console: Value,
|
||||||
commands: Value,
|
// commands: Value,
|
||||||
// fetch_outbox: Value,
|
// fetch_outbox: Value,
|
||||||
// fetch_inbox: Value,
|
// fetch_inbox: Value,
|
||||||
input: Value,
|
input: Value,
|
||||||
|
@ -263,7 +265,7 @@ impl Buffers {
|
||||||
pub fn new (prelude: imbl::HashMap<&'static str, Value>) -> Buffers {
|
pub fn new (prelude: imbl::HashMap<&'static str, Value>) -> 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("commands").unwrap().clone(),
|
||||||
// fetch_outbox: prelude.get("fetch_outbox").unwrap().clone(),
|
// fetch_outbox: prelude.get("fetch_outbox").unwrap().clone(),
|
||||||
// fetch_inbox: prelude.get("fetch_inbox").unwrap().clone(),
|
// fetch_inbox: prelude.get("fetch_inbox").unwrap().clone(),
|
||||||
input: prelude.get("input").unwrap().clone(),
|
input: prelude.get("input").unwrap().clone(),
|
||||||
|
@ -278,10 +280,9 @@ impl Buffers {
|
||||||
self.input.as_box()
|
self.input.as_box()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn commands (&self) -> Rc<RefCell<Value>> {
|
// pub fn commands (&self) -> Rc<RefCell<Value>> {
|
||||||
self.commands.as_box()
|
// self.commands.as_box()
|
||||||
}
|
// }
|
||||||
|
|
||||||
// pub fn fetch_outbox (&self) -> Rc<RefCell<Value>> {
|
// pub fn fetch_outbox (&self) -> Rc<RefCell<Value>> {
|
||||||
// self.fetch_outbox.as_box()
|
// self.fetch_outbox.as_box()
|
||||||
// }
|
// }
|
||||||
|
@ -366,9 +367,6 @@ impl World {
|
||||||
if let Some(console) = self.flush_console() {
|
if let Some(console) = self.flush_console() {
|
||||||
outbox.push(console);
|
outbox.push(console);
|
||||||
}
|
}
|
||||||
if let Some(commands) = self.flush_commands() {
|
|
||||||
outbox.push(commands);
|
|
||||||
}
|
|
||||||
outbox
|
outbox
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -381,18 +379,6 @@ impl World {
|
||||||
Some(MsgOut::Console(working_value.clone()))
|
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> {
|
fn complete_main(&mut self) -> Vec<MsgOut> {
|
||||||
let mut outbox = self.flush_buffers();
|
let mut outbox = self.flush_buffers();
|
||||||
// TODO: if we have a panic, actually add the panic message to the console
|
// TODO: if we have a panic, actually add the panic message to the console
|
||||||
|
|
Loading…
Reference in New Issue
Block a user