Execute Lua code

This commit is contained in:
Condorra 2024-08-17 16:42:01 +10:00
parent c0131aca62
commit 1738cb1f80
3 changed files with 14 additions and 16 deletions

View File

@ -10,16 +10,18 @@ fn reentrant_command_handler(
term_frame: &TermFrame,
command_in: &str,
) {
web_sys::console::log_1(&"Inside command handler".into());
echo_to_term_frame(globals, term_frame, "Hello World!\n").unwrap_or(());
echo_to_term_frame(globals, term_frame, "\r").unwrap_or(());
for command in parse_commands(command_in).commands {
match command.split_out_command() {
None => (),
Some((cmd, rest)) => {
if cmd == "##" {
match lua_state.execute(&join(rest.arguments.iter(), " ")) {
Ok(msg) => echo_to_term_frame(globals, term_frame, &msg).unwrap_or(()),
Err(msg) => echo_to_term_frame(globals, term_frame, &msg).unwrap_or(()),
Ok(()) => (),
Err(msg) => {
echo_to_term_frame(globals, term_frame, &format!("{}\r\n", msg))
.unwrap_or(())
}
}
}
}
@ -32,7 +34,7 @@ pub fn command_handler(globals: &GlobalCell, term_frame: &TermFrame, command_in:
Err(_) => echo_to_term_frame(
globals,
term_frame,
"Attempt to re-enter command handler during processing.\n",
"Attempt to re-enter command handler during processing.\r\n",
)
.unwrap_or(()), // Ignore error handling error.
Ok(mut lua_state_m) => {

View File

@ -18,7 +18,7 @@ impl LuaState {
Ok(LuaState { interp, exec })
}
pub fn execute(&mut self, command: &str) -> Result<String, String> {
pub fn execute(&mut self, command: &str) -> Result<(), String> {
self.interp
.try_enter(|ctx| {
let closure = Closure::load(ctx, None, format!("return ({})", command).as_bytes())
@ -27,7 +27,9 @@ impl LuaState {
Ok(())
})
.map_err(|err| format!("{}", err))?;
Ok("Blah".to_owned())
self.interp
.execute::<()>(&self.exec)
.map_err(|err| format!("{}", err))
}
}
@ -57,8 +59,8 @@ pub fn install_lua_globals(global: &GlobalCell) -> Result<(), String> {
fn echo_frame(ctx: Context, global: GlobalCell) -> Callback {
Callback::from_fn(&ctx, move |ctx, _ex, mut stack| {
let frame_no: u64 = stack.consume(ctx)?;
let message: piccolo::String = stack.consume(ctx)?;
let frame_no: u64 = stack.from_front(ctx)?;
let message: piccolo::String = stack.from_front(ctx)?;
let message_str = str::from_utf8(message.as_bytes())
.map_err(|_| "Expected message to echo to be UTF-8.".into_value(ctx))?;
echo_to_term_frame(&global, &TermFrame(frame_no), message_str)

View File

@ -207,13 +207,7 @@ pub fn echo_to_term_frame(
.frame_registry
.borrow()
.get(frame_id)
.ok_or_else(|| {
web_sys::console::log_2(
&"Attempt to echo to frame that doesn't exist.".into(),
&frame_id.0.into(),
);
"Attempt to echo to frame that doesn't exist."
})?
.ok_or_else(|| "Attempt to echo to frame that doesn't exist.")?
.term
.write(message);
Ok(())