Giter Club home page Giter Club logo

actix-lua's Introduction

actix-lua

Build Status Latest Version API Documentation

A safe scripting environment for actix with the Lua Programming Language:

  • Each LuaActor is an isolated Lua VM.
  • Communicate between actors with predefined message types: String, Integer, Number, Boolean, Nil, and Table.
  • Asynchronous send between actors with Lua coroutine.

For more info about the "safety", check rlua's README.

Synopsis

A basic Lua actor

extern crate actix_lua;
use actix_lua::{LuaActorBuilder, LuaMessage};

fn main () {
    let addr = LuaActorBuilder::new()
        .on_handle_with_lua(r#"return ctx.msg + 42"#)
        .build()
        .unwrap()
        .start();

    let res = addr.send(LuaMessage:from(100));
    // return: 142
}

You can send messages to other actor asynchronously with ctx.send

struct Callback;
impl Actor for Callback {
    type Context = Context<Self>;
}

impl Handler<LuaMessage> for Callback {
    type Result = LuaMessage;

    fn handle(&mut self, msg: LuaMessage, _ctx: &mut Context<Self>) -> Self::Result {
        LuaMessage::String("hello".to_string())
    }
}

let mut actor = LuaActorBuilder::new()
    // create a new LuaActor from a lua script when the actor is started.
    // send message to the newly created actor with `ctx.send`, block and wait for its response.
    .on_started_with_lua(
        r#"
    local result = ctx.send("callback, "Hello")
    print(result) -- print "hello"
    "#).build()
    .unwrap();

actor.add_recipients("callback", Callback.start().recipient());

actor.start();

Install

Add actix-lua to your Cargo.toml:

[dependencies]
actix-lua = "0.7"

Example

Check examples directory.

There's also a write-up about analyzing streaming data with actix-lua. link

Lua Actor

Use LuaActor to integrate Lua scripts to your system with actor model.

Message

In actor model, actors communicate with messages. LuaMessage is the only message type accepted by LuaActor:

  • LuaMessage can be converted to/from primitive types with LuaMessage::from().
  • Lua types(e.g. number, table) will be convert to LuaMessage automatically.

Lua API

Note: Avoid declaring global variables in your Lua script. It might conflict with future actix-lua update and break your program.

ctx.msg

The message sent to Lua actor.

ctx.notify(msg)

Send message msg to self.

ctx.notify_later(msg, seconds)

Send message msg to self after specified period of time.

local result = ctx.send(recipient, msg)

Send message msg to `recipient asynchronously and wait for response.

Equivalent to actix::Recipient.send.

ctx.do_send(recipient, msg)

Send message msg to recipient.

Equivalent to actix::Recipient.do_send.

ctx.terminate()

Terminate actor execution.

License

The MIT License

actix-lua's People

Contributors

arnaz87 avatar choznerol avatar naturallymitchell avatar poga avatar voidxnull avatar yanns avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

actix-lua's Issues

Typed functions panic on wrong argument types

When lua calls rust defined functions with wrong argument types it panics the thread, it doesn't propagate the error for lua or rust to handle it, it straight up panics.

It may happen with any error returned by rust code called from lua handler code, but I don't know, I just saw it happen with wrong typed arguments.

Option to not panic when `error()` is called in Lua script

Before 0.4.0 and 0.3.4, if error() is called inside the Lua script, it will be consumed silently. Now it will panic with proper stack trace.

It's easiler to debug now. But, sometimes panicking is too destructive. One use-case is a live-reloading environment. We might want to just show the error trace and continue the execution, allows user to have a chance to fix their script without restarting the whole program.

This is mostly about the last part of the invoke function in actor.rs. Maybe we should return a Err instead of just panic!.

rust libraries in child actors

Is your feature request related to a problem? Please describe.
We're making a framework on top of actix-lua, which has lua bindings for various rust libraries, that's why I need access to the vm from rust before running lua, to set the bindings as globals. We're now starting to actually use the actors functionality and create actors from the lua code, but the child actors don't share globals with the parent actors, and there's where the bindings are.

Describe the solution you'd like
The optimal solution would be just that all actors shared globals, but I guess that's not possible for safety reasons and whatnot. Then the next best solution is to somehow have access to the child actor's vm in rust, as I have already with the main actor, somehow intercept the actor creation process to setup the globals we need.

Describe alternatives you've considered
Pass the libraries as messages. That's ugly IMO and not even possible because of the message types limitation (I don't know why that restriction but that's out of the scope)

Maybe there's another way to pass data to child actors different than messages, or a shared space somewhere (either in lua's or rust's side), or maybe creating a shared space is a better solution than giving access to what I suggested before.

re-export `rlua`

Since we're exposing underlying Lua with the build_with_vm builder method. In order to prevent user provides a Lua vm built by different version of rlua, I believe we should also export the rlua actix-lua is using?

Add support for more scripting languages

Is your feature request related to a problem? Please describe.
Lua is only one good scripting language. Others, especially native Rust ones would be good to support.

Describe the solution you'd like
Make each language a plugin or feature, perhaps.

Describe alternatives you've considered
Forking actix-lua to make actix-otherlang, but that would be very limited and stale.

Additional context
Some good looking languages: Ullage, Molten, Rulox, Tox, and SimpleJIT Demo.

lua-web: match any request method

Is it possible with actix-web to handle / filter any method, rather than only on GET requests?

So far, I've tried this to web.lua and can match any GET request:

diff --git a/examples/lua-web/web.lua b/examples/lua-web/web.lua
index 6d04f1b..474d037 100644
--- a/examples/lua-web/web.lua
+++ b/examples/lua-web/web.lua
@@ -10,15 +10,12 @@ local ret
 
 print(ctx.msg)
 
-r:match('GET', '/hello', function (params)
-  print("get!!!")
 
-  local html = liluat.render(tmpl, {title="hello world", verb="Hello "})
+local html = liluat.render(tmpl, {title="hello world", verb="Hello "})
 
-  print(html)
+print(html)
 
-  ret = html
-end)
+ret = html
 
 r:execute(ctx.msg.method, '/' .. ctx.msg.path)

but I think this line in main.rs is too narrow:

.resource("/{path:.*}", |r| r.method(http::Method::GET).with(get))
    }).bind("127.0.0.1:8080")

and it looks like App::filter() would allow more flexibility

Access to the vm outside the handler

I want to make a repl, but I'd need access to the internal vm to execute code. I saw pull #9 and it gives access to the vm on initialization, but it doesn't work for a repl.

One possibility would be to somehow make the LuaActor use another's vm, in with_vm instead of passing a reference of the vm to the function, make the function return a reference of a vm, so that the owner of the actor is also the owner of the vm, I don't know if that's even possible though.

Another would be to create a LuaMessage variant with a function which receives a reference to the vm.

I like the second most but I don't really like either that much.

better error handling

Currently, there areunwrap everywhere. Some of them are needed. Some of them should be refactored into proper Result. We should have a cleaner big pictures about errors propagation.

Also need to document about how to deal with lua errors such as syntax error, error()...etc.

Allow registering LuaActor to actix register

Is your feature request related to a problem? Please describe.
Currently, you can't register LuaActor to System or Arbiter since it does not implement SystemService. In order to implement SystemService, LuaActor must impl Default.

The problem is, how do we implement Default for LuaActor? I think the reason actix requires Default for services is that it need to restart them when crashed?

Describe the solution you'd like
Figure out how to implement Default for LuaActor. Then implement SystemService, ArbiterService, and Supervised for it.

Describe alternatives you've considered
Not sure, need to understand more detail on actix's registry.

Use `LuaActor` with `SyncArbiter`

SyncArbiter allows us to put blocking statements to separated threads.

LuaActor is implemented with AsyncContext. In order to start an actor with SyncArbiter, it has to with SyncContext.

However, SyncContext does not provides rich APIs from AsyncContext, therefore it's not really compatible between these two implementation.

Maybe we can provide an alternative implementation such as SyncLuaActor?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.