From 2543ac1dacc7c4e407c19fcb3417ba32e3593c1a Mon Sep 17 00:00:00 2001 From: Scott Richmond Date: Sun, 6 Jul 2025 16:39:29 -0400 Subject: [PATCH] have rust make words Former-commit-id: a75a5b888165ec7cf85ffb844fa695e5a1710074 --- assets/test_prelude.ld | 2 +- pkg/rudus.d.ts | 4 +- pkg/rudus.js | 114 ++++++++++++++++++++++++++--------------- pkg/rudus_bg.wasm | 4 +- pkg/rudus_bg.wasm.d.ts | 4 +- src/base.rs | 37 ++++++++++--- src/io.rs | 6 +-- src/value.rs | 2 +- src/world.rs | 10 ++-- 9 files changed, 119 insertions(+), 64 deletions(-) diff --git a/assets/test_prelude.ld b/assets/test_prelude.ld index 6129593..0d7ceae 100644 --- a/assets/test_prelude.ld +++ b/assets/test_prelude.ld @@ -431,7 +431,7 @@ fn condense { fn words { "Takes a string and returns a list of the words in the string. Strips all whitespace." - (str as :string) -> do str > condense > strip > split (_, " ") + (str as :string) -> base :words (str) } fn sentence { diff --git a/pkg/rudus.d.ts b/pkg/rudus.d.ts index 3f4c0ce..bb38565 100644 --- a/pkg/rudus.d.ts +++ b/pkg/rudus.d.ts @@ -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 closure352_externref_shim: (a: number, b: number, c: any) => void; - readonly closure365_externref_shim: (a: number, b: number, c: any, d: any) => void; + readonly closure359_externref_shim: (a: number, b: number, c: any) => void; + readonly closure382_externref_shim: (a: number, b: number, c: any, d: any) => void; readonly __wbindgen_start: () => void; } diff --git a/pkg/rudus.js b/pkg/rudus.js index b94afaa..d045a63 100644 --- a/pkg/rudus.js +++ b/pkg/rudus.js @@ -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 ""; + } + }()); + 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.closure352_externref_shim(arg0, arg1, arg2); + _assertNum(arg0); + _assertNum(arg1); + wasm.closure359_externref_shim(arg0, arg1, arg2); } function __wbg_adapter_46(arg0, arg1, arg2, arg3) { - wasm.closure365_externref_shim(arg0, arg1, arg2, arg3); + _assertNum(arg0); + _assertNum(arg1); + wasm.closure382_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_wrapper1073 = function(arg0, arg1, arg2) { - const ret = makeMutClosure(arg0, arg1, 353, __wbg_adapter_20); + imports.wbg.__wbindgen_closure_wrapper8162 = function() { return logError(function (arg0, arg1, arg2) { + const ret = makeMutClosure(arg0, arg1, 360, __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) { diff --git a/pkg/rudus_bg.wasm b/pkg/rudus_bg.wasm index 6c2aa01..3dfe8ae 100644 --- a/pkg/rudus_bg.wasm +++ b/pkg/rudus_bg.wasm @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:ec30dbe53a87764b02c763e240ae033c5d4d5392fbeb9c8fc7d98503a7c20590 -size 2696880 +oid sha256:0e6cd933e45820661869767cca874e943b75601ee276187d7c5e09d8a3f3541c +size 16777075 diff --git a/pkg/rudus_bg.wasm.d.ts b/pkg/rudus_bg.wasm.d.ts index 7a72a31..af356e0 100644 --- a/pkg/rudus_bg.wasm.d.ts +++ b/pkg/rudus_bg.wasm.d.ts @@ -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 closure352_externref_shim: (a: number, b: number, c: any) => void; -export const closure365_externref_shim: (a: number, b: number, c: any, d: any) => void; +export const closure359_externref_shim: (a: number, b: number, c: any) => void; +export const closure382_externref_shim: (a: number, b: number, c: any, d: any) => void; export const __wbindgen_start: () => void; diff --git a/src/base.rs b/src/base.rs index c2607ce..c541058 100644 --- a/src/base.rs +++ b/src/base.rs @@ -85,7 +85,7 @@ pub fn chars(x: &Value) -> Value { Value::Interned(s) => { let mut charlist = vector![]; for char in s.chars() { - charlist.push_back(Value::string(char.to_string())) + charlist.push_back(Value::from_string(char.to_string())) } Value::Tuple(Rc::new(vec![ Value::Keyword("ok"), @@ -95,7 +95,7 @@ pub fn chars(x: &Value) -> Value { Value::String(s) => { let mut charlist = vector![]; for char in s.chars() { - charlist.push_back(Value::string(char.to_string())) + charlist.push_back(Value::from_string(char.to_string())) } Value::Tuple(Rc::new(vec![ Value::Keyword("ok"), @@ -106,6 +106,23 @@ pub fn chars(x: &Value) -> Value { } } +pub fn words(str: &Value) -> Value { + console_log!("wordsing words"); + let str = Value::as_string(str); + let mut words = Vector::new(); + let mut word = String::new(); + for char in str.chars() { + if char.is_alphanumeric() { + word.push(char) + } else if !word.is_empty() { + words.push_back(Value::from_string(word)); + word = String::new() + } + } + console_log!("words gathered into vector; returning to ludus"); + Value::list(words) +} + // TODO: figure out how to get to opportunistic mutation here pub fn concat(x: &Value, y: &Value) -> Value { match (x, y) { @@ -193,11 +210,11 @@ pub fn first(ordered: &Value) -> Value { None => Value::Nil, }, Value::String(s) => match s.chars().next() { - Some(char) => Value::string(char.to_string()), + Some(char) => Value::from_string(char.to_string()), None => Value::Nil, }, Value::Interned(s) => match s.chars().next() { - Some(char) => Value::string(char.to_string()), + Some(char) => Value::from_string(char.to_string()), None => Value::Nil, }, _ => unreachable!("internal Ludus error"), @@ -225,14 +242,14 @@ pub fn at(ordered: &Value, i: &Value) -> Value { (Value::String(s), Value::Number(n)) => { let i = f64::from(*n) as usize; match s.chars().nth(i) { - Some(n) => Value::string(n.to_string()), + Some(n) => Value::from_string(n.to_string()), None => Value::Nil, } } (Value::Interned(s), Value::Number(n)) => { let i = f64::from(*n) as usize; match s.chars().nth(i) { - Some(n) => Value::string(n.to_string()), + Some(n) => Value::from_string(n.to_string()), None => Value::Nil, } } @@ -269,11 +286,11 @@ pub fn last(ordered: &Value) -> Value { None => Value::Nil, }, Value::String(s) => match s.chars().last() { - Some(char) => Value::string(char.to_string()), + Some(char) => Value::from_string(char.to_string()), None => Value::Nil, }, Value::Interned(s) => match s.chars().last() { - Some(char) => Value::string(char.to_string()), + Some(char) => Value::from_string(char.to_string()), None => Value::Nil, }, _ => unreachable!("internal Ludus error"), @@ -717,6 +734,10 @@ pub fn make_base() -> Value { "upcase", Value::BaseFn(Box::new(BaseFn::Unary("upcase", upcase))), ), + ( + "words", + Value::BaseFn(Box::new(BaseFn::Unary("words", words))), + ), ]; let members = members .iter() diff --git a/src/io.rs b/src/io.rs index 98abf4f..cf8fbe7 100644 --- a/src/io.rs +++ b/src/io.rs @@ -51,11 +51,11 @@ impl std::fmt::Display for MsgIn { impl MsgIn { pub fn into_value(self) -> Value { match self { - MsgIn::Input(str) => Value::string(str), + MsgIn::Input(str) => Value::from_string(str), MsgIn::Fetch(url, status_f64, string) => { - let url = Value::string(url); + let url = Value::from_string(url); let status = Value::from_f64(status_f64); - let text = Value::string(string); + let text = Value::from_string(string); let result_tuple = if status_f64 == 200.0 { Value::tuple(vec![OK, text]) } else { diff --git a/src/value.rs b/src/value.rs index a2a8ae9..cadb2b3 100644 --- a/src/value.rs +++ b/src/value.rs @@ -455,7 +455,7 @@ impl Value { } } - pub fn string(str: String) -> Value { + pub fn from_string(str: String) -> Value { Value::String(Rc::new(str)) } diff --git a/src/world.rs b/src/world.rs index 929597a..fb91c6f 100644 --- a/src/world.rs +++ b/src/world.rs @@ -390,7 +390,7 @@ impl World { if zoo_msgs.is_empty() { None } else { - let inner = zoo_msgs.into_iter().map(Value::string).collect::>(); + let inner = zoo_msgs.into_iter().map(Value::from_string).collect::>(); Some(MsgOut::Console(Value::list(inner))) } } @@ -437,9 +437,9 @@ impl World { let result = self.active_result().clone().unwrap(); self.result = Some(result.clone()); match result { - Ok(value) => outbox.push(MsgOut::Complete(Value::string(value.show()))), + Ok(value) => outbox.push(MsgOut::Complete(Value::from_string(value.show()))), Err(p) => { - outbox.push(MsgOut::Console(Value::list(imbl::vector!(Value::string("Ludus panicked!".to_string()))))); + outbox.push(MsgOut::Console(Value::list(imbl::vector!(Value::from_string("Ludus panicked!".to_string()))))); outbox.push(MsgOut::Error(panic(p))) } } @@ -460,7 +460,7 @@ impl World { } fn fill_input(&mut self, str: String) { - let value = Value::string(str); + let value = Value::from_string(str); let working = RefCell::new(value); let input = self.buffers.input(); input.swap(&working); @@ -497,7 +497,7 @@ impl World { 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)); + console.push_back(Value::from_string(msg)); } fn report_process_end(&mut self) {