Skip to content
This repository has been archived by the owner on Mar 10, 2023. It is now read-only.

Make vm public #9

Merged
merged 2 commits into from Aug 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 40 additions & 1 deletion src/actor.rs
Expand Up @@ -11,7 +11,7 @@ use uuid::Uuid;

use message::LuaMessage;

use builder::LuaActorBuilder;
use builder::{LuaActorBuilder, WithVmCallback};

/// Top level struct which holds a lua state for itself.
///
Expand Down Expand Up @@ -52,8 +52,14 @@ impl LuaActor {
started: Option<String>,
handle: Option<String>,
stopped: Option<String>,
vm_callback: Option<Box<WithVmCallback>>
) -> Result<LuaActor, LuaError> {
let vm = Lua::new();

if let Some(vm_callback) = vm_callback {
vm_callback(&vm)?;
}

let prelude = include_str!("lua/prelude.lua");
vm.eval::<()>(prelude, Some("Prelude"))?;
{
Expand Down Expand Up @@ -605,4 +611,37 @@ mod tests {

system.run();
}

#[test]
fn lua_actor_with_vm() {
let system = System::new("test");

let addr = LuaActorBuilder::new()
.on_handle_with_lua(
r#"
return greet(ctx.msg)
"#,
)
.with_vm(move |vm| {
let greet = vm.create_function(|_, name: String| {
Ok(format!("Hello, {}!", name))
})?;

vm.globals().set("greet", greet)?;

Ok(())
})
.build()
.unwrap()
.start();


let l = addr.send(LuaMessage::from("World"));
Arbiter::spawn(l.map(|res| {
assert_eq!(res, LuaMessage::from("Hello, World!"));
System::current().stop();
}).map_err(|e| println!("actor dead {}", e)));

system.run();
}
}
26 changes: 18 additions & 8 deletions src/builder.rs
Expand Up @@ -2,12 +2,15 @@ use std::fs::File;
use std::io::prelude::*;

use actor::LuaActor;
use rlua::Error as LuaError;
use rlua::{Lua, Error as LuaError};

pub type WithVmCallback = Fn(&Lua) -> Result<(), LuaError>;

pub struct LuaActorBuilder {
started: Option<String>,
handle: Option<String>,
stopped: Option<String>,
with_vm_callback: Option<Box<WithVmCallback>>,
}

impl Default for LuaActorBuilder {
Expand All @@ -17,6 +20,7 @@ impl Default for LuaActorBuilder {
started: noop.clone(),
handle: noop.clone(),
stopped: noop.clone(),
with_vm_callback: None,
}
}
}
Expand All @@ -26,40 +30,46 @@ impl LuaActorBuilder {
LuaActorBuilder::default()
}

pub fn on_started(&mut self, filename: &str) -> &mut Self {
pub fn on_started(mut self, filename: &str) -> Self {
self.started = Some(read_to_string(filename));
self
}

pub fn on_started_with_lua(&mut self, script: &str) -> &mut Self {
pub fn on_started_with_lua(mut self, script: &str) -> Self {
self.started = Some(script.to_string());
self
}

pub fn on_handle(&mut self, filename: &str) -> &mut Self {
pub fn on_handle(mut self, filename: &str) -> Self {
self.handle = Some(read_to_string(filename));
self
}
pub fn on_handle_with_lua(&mut self, script: &str) -> &mut Self {
pub fn on_handle_with_lua(mut self, script: &str) -> Self {
self.handle = Some(script.to_string());
self
}

pub fn on_stopped(&mut self, filename: &str) -> &mut Self {
pub fn on_stopped(mut self, filename: &str) -> Self {
self.stopped = Some(read_to_string(filename));
self
}

pub fn on_stopped_with_lua(&mut self, script: &str) -> &mut Self {
pub fn on_stopped_with_lua(mut self, script: &str) -> Self {
self.stopped = Some(script.to_string());
self
}

pub fn build(&self) -> Result<LuaActor, LuaError> {
pub fn with_vm<F: Fn(&Lua) -> Result<(), LuaError> + 'static>(mut self, callback: F) -> Self {
self.with_vm_callback = Some(Box::new(callback));
self
}

pub fn build(self) -> Result<LuaActor, LuaError> {
LuaActor::new(
self.started.clone(),
self.handle.clone(),
self.stopped.clone(),
self.with_vm_callback,
)
}
}
Expand Down