Skip to content

Commit

Permalink
feat: iterate on APIs (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
doomspork committed Jan 9, 2024
1 parent 597e56b commit 470db44
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 69 deletions.
2 changes: 1 addition & 1 deletion lib/library.ex
Expand Up @@ -77,7 +77,7 @@ defmodule Luau.Library do

def unquote(wrapped_func)(_arity, args, state) do
res = apply(__MODULE__, unquote(func), args)
{:ok, state}
{[res], state}
end
end
end
Expand Down
13 changes: 5 additions & 8 deletions lib/luau.ex
Expand Up @@ -5,15 +5,12 @@ defmodule Luau do

alias Luau.Runtime

@spec initialize(Keyword.t()) :: Runtime.t()
def initialize(args) do
Runtime.initialize(args)
@spec init() :: Runtime.t()
def init do
Runtime.init()
end

@spec execute(Runtime.t(), String.t()) :: {any(), Runtime.t()}
def execute(runtime, lua) do
{res, new_state} = :luerl.do(lua, runtime.state)

{res, %{runtime | state: new_state}}
def run(runtime, lua) do
Runtime.run(runtime, lua)
end
end
68 changes: 40 additions & 28 deletions lib/runtime.ex
@@ -1,50 +1,62 @@
defmodule Luau.Runtime do
@type t :: %__MODULE__{
id: nil | String.t(),
libraries: [Luau.Library.t()],
lua: [String.t()],
modules: [Luau.Library.t()],
state: tuple(),
variables: %{String.t() => any()}
variables: %{[String.t()] => any()}
}

defstruct id: nil, libraries: [], state: nil, variables: %{}
alias Luerl.New, as: Luerl

@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()}
defstruct id: nil, modules: [], lua: [], state: nil, variables: %{}

runtime
|> add_libraries(libraries)
|> set_variables(variables)
@type result :: {:ok, t()}
@type error :: {:error, atom() | String.t()}

@spec init() :: Luau.Runtime.t()
def init do
%Luau.Runtime{id: Nanoid.generate(), state: Luerl.init()}
end

@spec set_variable(Luau.Runtime.t(), String.t(), any()) :: Luau.Runtime.t()
@spec set_variable(Luau.Runtime.t(), [String.t()] | String.t(), any()) :: result | error
def set_variable(runtime, key, value) do
new_state = :luerl.set_table([key], value, runtime.state)
key = List.wrap(key)

%{runtime | state: new_state, variables: Map.put(runtime.variables, key, value)}
end
case Luerl.set_table_keys_dec(runtime.state, key, value) do
{:ok, _result, new_state} ->
{:ok, %{runtime | state: new_state, variables: Map.put(runtime.variables, key, value)}}

@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)
{:lua_error, reason, _state} ->
{:error, reason}
end
end

%{runtime | state: new_state, libraries: [library | runtime.libraries]}
@spec load_module!(Luau.Runtime.t(), Luau.Library.t()) :: Luau.Runtime.t()
def load_module!(runtime, module) do
new_state = Luerl.load_module_dec(runtime.state, [module.scope()], module)
%{runtime | state: new_state, modules: [module | runtime.modules]}
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)
@spec load_lua!(Luau.Runtime.t(), String.t()) :: Luau.Runtime.t()
def load_lua!(runtime, path) do
case Luerl.dofile(runtime.state, String.to_charlist(path)) do
{:ok, _result, new_state} ->
%{runtime | state: new_state, lua: [path | runtime.lua]}

{res, %{runtime | state: new_state}}
:error ->
raise "Could not load Lua file #{path}, file not found"
end
end

defp add_libraries(runtime, libraries) do
Enum.reduce(libraries, runtime, &add_library(&2, &1))
end
@spec run(Luau.Runtime.t(), String.t()) :: {:ok, any(), Luau.Runtime.t()} | error
def run(runtime, lua) do
case Luerl.do(runtime.state, lua) do
{:ok, result, new_state} ->
{:ok, result, %{runtime | state: new_state}}

defp set_variables(runtime, variables) do
Enum.reduce(variables, runtime, fn {key, value}, runtime -> set_variable(runtime, key, value) end)
{:error, reason, _state} ->
{:error, reason}
end
end
end
3 changes: 2 additions & 1 deletion mix.exs
Expand Up @@ -11,6 +11,7 @@ defmodule Luau.MixProject do
]
end

# Path.join([:code.priv_dir(:sidecar), "lua", name])
# Run "mix help compile.app" to learn about applications.
def application do
[
Expand All @@ -21,7 +22,7 @@ defmodule Luau.MixProject do
# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:luerl, github: "rvirding/luerl", branch: "develop"},
{:luerl, "~> 1.1"},
{:nanoid, "~> 2.1.0"},

# Dev & Test dependencies
Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Expand Up @@ -7,7 +7,7 @@
"ex_doc": {:hex, :ex_doc, "0.31.0", "06eb1dfd787445d9cab9a45088405593dd3bb7fe99e097eaa71f37ba80c7a676", [:mix], [{:earmark_parser, "~> 1.4.39", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.1", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "5350cafa6b7f77bdd107aa2199fe277acf29d739aba5aee7e865fc680c62a110"},
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
"jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"},
"luerl": {:git, "https://github.com/rvirding/luerl.git", "b9b66871a2752c76292a8afa7a8dad53e4f67bc8", [branch: "develop"]},
"luerl": {:hex, :luerl, "1.1.1", "083518e437586f6631150d39c4bff242ed2ec80cb14a3299a0c2628f07a2ff7f", [:rebar3], [], "hexpm", "e17ef246a7ff876ec90e68792a39708979416004d4eacfe8a7643206b9470773"},
"makeup": {:hex, :makeup, "1.1.1", "fa0bc768698053b2b3869fa8a62616501ff9d11a562f3ce39580d60860c3a55e", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "5dc62fbdd0de44de194898b6710692490be74baa02d9d108bc29f007783b0b48"},
"makeup_elixir": {:hex, :makeup_elixir, "0.16.1", "cc9e3ca312f1cfeccc572b37a09980287e243648108384b97ff2b76e505c3555", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "e127a341ad1b209bd80f7bd1620a15693a9908ed780c3b763bccf7d200c767c6"},
"makeup_erlang": {:hex, :makeup_erlang, "0.1.3", "d684f4bac8690e70b06eb52dad65d26de2eefa44cd19d64a8095e1417df7c8fd", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "b78dc853d2e670ff6390b605d807263bf606da3c82be37f9d7f68635bd886fc9"},
Expand Down
4 changes: 2 additions & 2 deletions test/luau_test.exs
Expand Up @@ -12,9 +12,9 @@ defmodule LuauTest do
end
end

describe "initialize/1" do
describe "init/0" do
test "returns a runtime" do
assert %Luau.Runtime{} = Luau.initialize([])
assert %Luau.Runtime{} = Luau.init()
end
end

Expand Down
79 changes: 51 additions & 28 deletions test/runtime_test.exs
Expand Up @@ -3,59 +3,82 @@ defmodule Luau.RuntimeTest do

alias Luau.Runtime

defmodule BarLibrary do
use Luau.Library, scope: "bar"
defmodule AdderLibrary do
use Luau.Library, scope: "Adder"

deflua add(a, b) do
a + b
end
end

defmodule FooLibrary do
use Luau.Library, scope: "foo"
defmodule EvenLibrary do
use Luau.Library, scope: "Even"

deflua is_even(value) do
rem(value, 2) == 0
end
end

describe "initialize/0" do
describe "init/0" do
test "returns a runtime containing the Lua state and unique id" do
assert %Runtime{id: id, state: state} = Runtime.initialize()
assert %Runtime{id: id, state: state} = Runtime.init()
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}
{:ok, runtime: Runtime.init()}
end

test "sets a variable", %{runtime: runtime} do
name = ["name"]
value = "Robert"
assert {:ok, %Runtime{variables: %{^name => ^value}}} = Runtime.set_variable(runtime, name, value)
end

test "wraps variable path as necessary", %{runtime: runtime} do
name = "name"
value = "Robert"

assert %Runtime{variables: %{^name => ^value}} = Runtime.set_variable(runtime, name, value)
assert {:ok, %Runtime{variables: %{[^name] => ^value}}} = Runtime.set_variable(runtime, name, value)
end
end

describe "add_library/2" do
describe "load_module!/2" do
setup do
runtime = Runtime.initialize()
{:ok, runtime: Runtime.init()}
end

{:ok, runtime: runtime}
test "load a Luau.Library to the lua runtime", %{runtime: runtime} do
assert %Runtime{modules: [EvenLibrary, AdderLibrary]} =
loaded_runtime = runtime |> Runtime.load_module!(AdderLibrary) |> Runtime.load_module!(EvenLibrary)

script = """
local a = Adder.add(3,3)
local b = Even.is_even(a)
return b
"""

assert {:ok, [true], _runtime} = Runtime.run(loaded_runtime, script)
end
end

describe "load_lua!/2" do
setup do
{:ok, runtime: Runtime.init()}
end

test "loads simple lua script into runtime", %{runtime: runtime} do
path = Path.join([__DIR__, "support", "hello.lua"])
assert %Runtime{lua: [^path]} = loaded_runtime = Runtime.load_lua!(runtime, path)

script = """
return hello("Robert")
"""

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)
assert {:ok, ["Hello Robert!"], _runtime} = Runtime.run(loaded_runtime, script)
end
end
end
13 changes: 13 additions & 0 deletions test/support/enum.lua
@@ -0,0 +1,13 @@
local E = {}

function E.any(values, callback)
for i, v in ipairs(values) do
if callback(v) then
return true
end
end

return false
end

Enum = E
3 changes: 3 additions & 0 deletions test/support/hello.lua
@@ -0,0 +1,3 @@
function hello(name)
return "Hello " .. name .. "!"
end

0 comments on commit 470db44

Please sign in to comment.