Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Simple runtime and execution (#1)
- Loading branch information
Showing
9 changed files
with
239 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
defmodule Luau.Library do | ||
@moduledoc """ | ||
Defines a library that will extend the Lua runtime. | ||
## Example usage | ||
defmodule MyLibrary do | ||
use Luau.Library, scope: "my_library" | ||
deflua hello(name) do | ||
"Hello, \#{name}!" | ||
end | ||
end | ||
In our Lua code, we can now call `my_library.hello("Robert")` and get back `"Hello, Robert!"`. | ||
""" | ||
@type t :: __MODULE__ | ||
|
||
@callback install(tuple()) :: tuple() | ||
@callback table() :: [tuple()] | ||
@callback scope() :: String.t() | ||
|
||
defmacro __using__(opts) do | ||
scope = Keyword.fetch!(opts, :scope) | ||
|
||
quote do | ||
require Record | ||
Record.defrecord(:erl_mfa, Record.extract(:erl_mfa, from_lib: "luerl/include/luerl.hrl")) | ||
|
||
Module.register_attribute(__MODULE__, :lua_functions, accumulate: true, persist: true) | ||
|
||
import Luau.Library, only: [deflua: 2] | ||
|
||
@before_compile unquote(__MODULE__) | ||
|
||
@behaviour Luau.Library | ||
|
||
@impl Luau.Library | ||
def install(luerl_state) do | ||
:luerl_heap.alloc_table(table(), luerl_state) | ||
end | ||
|
||
@impl Luau.Library | ||
def scope, do: unquote(scope) | ||
end | ||
end | ||
|
||
defmacro __before_compile__(_) do | ||
quote do | ||
@impl Luau.Library | ||
def table do | ||
functions = | ||
:functions | ||
|> __MODULE__.__info__() | ||
|> Enum.group_by(&elem(&1, 0), &elem(&1, 1)) | ||
|
||
Enum.flat_map(@lua_functions, fn {func, wrapped_func} -> | ||
functions | ||
|> Map.get(func, []) | ||
|> Enum.map(&{to_string(func), erl_mfa(m: __MODULE__, f: wrapped_func, a: &1)}) | ||
end) | ||
end | ||
end | ||
end | ||
|
||
defmacro deflua(call, do: body) do | ||
{func, _line, _args} = call | ||
|
||
# credo:disable-for-next-line | ||
wrapped_func = String.to_atom("lua_wrapped_#{func}") | ||
|
||
quote do | ||
@lua_functions {unquote(func), unquote(wrapped_func)} | ||
def unquote(call) do | ||
unquote(body) | ||
end | ||
|
||
def unquote(wrapped_func)(_arity, args, state) do | ||
res = apply(__MODULE__, unquote(func), args) | ||
{:ok, state} | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,19 @@ | ||
defmodule Luau do | ||
@moduledoc """ | ||
Documentation for `Luau`. | ||
Let's get this party started! | ||
""" | ||
|
||
@doc """ | ||
Hello world. | ||
alias Luau.Runtime | ||
|
||
## Examples | ||
@spec initialize(Keyword.t()) :: Runtime.t() | ||
def initialize(args) do | ||
Runtime.initialize(args) | ||
end | ||
|
||
iex> Luau.hello() | ||
:world | ||
@spec execute(Runtime.t(), String.t()) :: {any(), Runtime.t()} | ||
def execute(runtime, lua) do | ||
{res, new_state} = :luerl.do(lua, runtime.state) | ||
|
||
""" | ||
def hello do | ||
:world | ||
{res, %{runtime | state: new_state}} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
defmodule Luau.Runtime do | ||
@type t :: %__MODULE__{ | ||
id: nil | String.t(), | ||
libraries: [Luau.Library.t()], | ||
state: tuple(), | ||
variables: %{String.t() => any()} | ||
} | ||
|
||
defstruct id: nil, libraries: [], state: nil, variables: %{} | ||
|
||
@spec initialize(Keyword.t()) :: Luau.Runtime.t() | ||
def initialize(opts \\ []) do | ||
libraries = Keyword.get(opts, :libraries, []) | ||
variables = Keyword.get(opts, :variables, %{}) | ||
runtime = %Luau.Runtime{id: Nanoid.generate(), state: :luerl.init()} | ||
|
||
runtime | ||
|> add_libraries(libraries) | ||
|> set_variables(variables) | ||
end | ||
|
||
@spec set_variable(Luau.Runtime.t(), String.t(), any()) :: Luau.Runtime.t() | ||
def set_variable(runtime, key, value) do | ||
new_state = :luerl.set_table([key], value, runtime.state) | ||
|
||
%{runtime | state: new_state, variables: Map.put(runtime.variables, key, value)} | ||
end | ||
|
||
@spec add_library(Luau.Runtime.t(), Luau.Library.t()) :: Luau.Runtime.t() | ||
def add_library(runtime, library) do | ||
new_state = :luerl.load_module([library.scope()], library, runtime.state) | ||
|
||
%{runtime | state: new_state, libraries: [library | runtime.libraries]} | ||
end | ||
|
||
@spec run(Luau.Runtime.t(), String.t()) :: {any(), Luau.Runtime.t()} | ||
def run(runtime, lua) do | ||
{res, new_state} = :luerl.do(lua, runtime.state) | ||
|
||
{res, %{runtime | state: new_state}} | ||
end | ||
|
||
defp add_libraries(runtime, libraries) do | ||
Enum.reduce(libraries, runtime, &add_library(&2, &1)) | ||
end | ||
|
||
defp set_variables(runtime, variables) do | ||
Enum.reduce(variables, runtime, fn {key, value}, runtime -> set_variable(runtime, key, value) end) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,27 @@ | ||
defmodule LuauTest do | ||
use ExUnit.Case | ||
doctest Luau | ||
|
||
test "greets the world" do | ||
assert Luau.hello() == :world | ||
defmodule TestLibrary do | ||
@moduledoc """ | ||
A test library for Luau. | ||
""" | ||
use Luau.Library, scope: "test" | ||
|
||
deflua hello(name) do | ||
"Hello, #{name}!" | ||
end | ||
end | ||
|
||
describe "initialize/1" do | ||
test "returns a runtime" do | ||
assert %Luau.Runtime{} = Luau.initialize([]) | ||
end | ||
end | ||
|
||
describe "execute/2" do | ||
test "evaluates Lua" do | ||
# runtime = Luau.initialize(libraries: [TestLibrary], variables: %{name: "Robert"}) | ||
# assert {"Hello, Robert!", %Luau.Runtime{}} = Luau.execute(runtime, ~s/test.hello(name)/) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
defmodule Luau.RuntimeTest do | ||
use ExUnit.Case | ||
|
||
alias Luau.Runtime | ||
|
||
defmodule BarLibrary do | ||
use Luau.Library, scope: "bar" | ||
end | ||
|
||
defmodule FooLibrary do | ||
use Luau.Library, scope: "foo" | ||
end | ||
|
||
describe "initialize/0" do | ||
test "returns a runtime containing the Lua state and unique id" do | ||
assert %Runtime{id: id, state: state} = Runtime.initialize() | ||
assert is_binary(id) | ||
refute is_nil(state) | ||
end | ||
end | ||
|
||
describe "initialize/1" do | ||
test "accepts `:libraries` as an option" do | ||
assert %Runtime{libraries: [BarLibrary]} = Runtime.initialize(libraries: [BarLibrary]) | ||
end | ||
|
||
test "accepts `:variables` as an option" do | ||
assert %Runtime{} = Runtime.initialize(variables: %{name: "Robert"}) | ||
end | ||
end | ||
|
||
describe "set_variable/3" do | ||
setup do | ||
runtime = Runtime.initialize() | ||
|
||
{:ok, runtime: runtime} | ||
end | ||
|
||
test "sets a variable", %{runtime: runtime} do | ||
name = "name" | ||
value = "Robert" | ||
|
||
assert %Runtime{variables: %{^name => ^value}} = Runtime.set_variable(runtime, name, value) | ||
end | ||
end | ||
|
||
describe "add_library/2" do | ||
setup do | ||
runtime = Runtime.initialize() | ||
|
||
{:ok, runtime: runtime} | ||
end | ||
|
||
test "adds a library to the lua runtime", %{runtime: runtime} do | ||
assert %Runtime{libraries: [FooLibrary, BarLibrary]} = | ||
runtime | ||
|> Runtime.add_library(BarLibrary) | ||
|> Runtime.add_library(FooLibrary) | ||
end | ||
end | ||
end |