Skip to content

henry232323/async-lua-embed

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Async Lua Embed

This small package is intended to provide the tools to allow the embedding of user generated scripts into an async environment. This package allows the definition and execution of asynchronous Python functions and methods in the user defined scripts, which may allow users to interface with things like Discord bot

Installation

python -m pip install git+https://github.com/henry232323/async-lua-embed

Example

For an example of most of the package's functionality:

import asyncio
import aiohttp as aiohttp
from async_lua import DAO, Environment, CommandExecutor


class Pokemon(DAO):
  def __init__(self, dispatch, name):
    super().__init__(dispatch)
    self.name: str = name

  async def get_type(self):
    async with aiohttp.ClientSession() as session:
      data = await session.get(f"https://pokeapi.co/api/v2/pokemon/{self.name}")
      js = await data.json()
      return js["types"][0]["type"]["name"]


class ApiEnvironment(Environment):
  def __init__(self, exec, dispatch):
    super().__init__(exec, dispatch)

    self.register("ditto", Pokemon(dispatch, "ditto"))

  async def say(self, *args):
    """Prints a message"""
    print(*args)


code = """
say(ditto.get_type())
"""


async def context():
  exec = CommandExecutor()
  exec.register_env(ApiEnvironment)
  await exec.execute(code)


asyncio.get_event_loop().run_until_complete(context())

Sandboxing

This library implements a simple sandbox based on some of the links below, including the final module wholesale.

Model

  • Open an executor in which the Lua will be executed
  • Run all Lua in an elaborate sandbox limiting access, memory, and number of instructions
  • No need to kill threads when the sandbox (hopefully) manages itself
  • A DAO is intended to be a surrogate for seamlessly embedding async functions into an object, allowing control over the object with Python implemented async functions
  • Asynchronous nature is maintained by suspending execution with coroutines until async completes
    • Commands are yielded by the coroutine, processed, and sent back into the coro

About

Embed Lua in Python with access to Python's async functions

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published