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

Commit

Permalink
Expose vm through LuaActorBuilder::with_vm
Browse files Browse the repository at this point in the history
  • Loading branch information
voidxnull committed Aug 28, 2018
1 parent 7b19a0b commit 84049d8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
8 changes: 7 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
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

0 comments on commit 84049d8

Please sign in to comment.