Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
118 additions
and
69 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
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,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 |
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
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
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,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 |
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,3 @@ | ||
function hello(name) | ||
return "Hello " .. name .. "!" | ||
end |