Execute Lua code
This commit is contained in:
parent
c0131aca62
commit
1738cb1f80
@ -10,16 +10,18 @@ fn reentrant_command_handler(
|
|||||||
term_frame: &TermFrame,
|
term_frame: &TermFrame,
|
||||||
command_in: &str,
|
command_in: &str,
|
||||||
) {
|
) {
|
||||||
web_sys::console::log_1(&"Inside command handler".into());
|
echo_to_term_frame(globals, term_frame, "\r").unwrap_or(());
|
||||||
echo_to_term_frame(globals, term_frame, "Hello World!\n").unwrap_or(());
|
|
||||||
for command in parse_commands(command_in).commands {
|
for command in parse_commands(command_in).commands {
|
||||||
match command.split_out_command() {
|
match command.split_out_command() {
|
||||||
None => (),
|
None => (),
|
||||||
Some((cmd, rest)) => {
|
Some((cmd, rest)) => {
|
||||||
if cmd == "##" {
|
if cmd == "##" {
|
||||||
match lua_state.execute(&join(rest.arguments.iter(), " ")) {
|
match lua_state.execute(&join(rest.arguments.iter(), " ")) {
|
||||||
Ok(msg) => echo_to_term_frame(globals, term_frame, &msg).unwrap_or(()),
|
Ok(()) => (),
|
||||||
Err(msg) => echo_to_term_frame(globals, term_frame, &msg).unwrap_or(()),
|
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(
|
Err(_) => echo_to_term_frame(
|
||||||
globals,
|
globals,
|
||||||
term_frame,
|
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.
|
.unwrap_or(()), // Ignore error handling error.
|
||||||
Ok(mut lua_state_m) => {
|
Ok(mut lua_state_m) => {
|
||||||
|
@ -18,7 +18,7 @@ impl LuaState {
|
|||||||
Ok(LuaState { interp, exec })
|
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
|
self.interp
|
||||||
.try_enter(|ctx| {
|
.try_enter(|ctx| {
|
||||||
let closure = Closure::load(ctx, None, format!("return ({})", command).as_bytes())
|
let closure = Closure::load(ctx, None, format!("return ({})", command).as_bytes())
|
||||||
@ -27,7 +27,9 @@ impl LuaState {
|
|||||||
Ok(())
|
Ok(())
|
||||||
})
|
})
|
||||||
.map_err(|err| format!("{}", err))?;
|
.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 {
|
fn echo_frame(ctx: Context, global: GlobalCell) -> Callback {
|
||||||
Callback::from_fn(&ctx, move |ctx, _ex, mut stack| {
|
Callback::from_fn(&ctx, move |ctx, _ex, mut stack| {
|
||||||
let frame_no: u64 = stack.consume(ctx)?;
|
let frame_no: u64 = stack.from_front(ctx)?;
|
||||||
let message: piccolo::String = stack.consume(ctx)?;
|
let message: piccolo::String = stack.from_front(ctx)?;
|
||||||
let message_str = str::from_utf8(message.as_bytes())
|
let message_str = str::from_utf8(message.as_bytes())
|
||||||
.map_err(|_| "Expected message to echo to be UTF-8.".into_value(ctx))?;
|
.map_err(|_| "Expected message to echo to be UTF-8.".into_value(ctx))?;
|
||||||
echo_to_term_frame(&global, &TermFrame(frame_no), message_str)
|
echo_to_term_frame(&global, &TermFrame(frame_no), message_str)
|
||||||
|
@ -207,13 +207,7 @@ pub fn echo_to_term_frame(
|
|||||||
.frame_registry
|
.frame_registry
|
||||||
.borrow()
|
.borrow()
|
||||||
.get(frame_id)
|
.get(frame_id)
|
||||||
.ok_or_else(|| {
|
.ok_or_else(|| "Attempt to echo to frame that doesn't exist.")?
|
||||||
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."
|
|
||||||
})?
|
|
||||||
.term
|
.term
|
||||||
.write(message);
|
.write(message);
|
||||||
Ok(())
|
Ok(())
|
||||||
|
Loading…
Reference in New Issue
Block a user