Skip to content

Commit

Permalink
feat: Rename to Lunar (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
doomspork committed Jan 12, 2024
1 parent 1f65d08 commit 16c2e81
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 208 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ erl_crash.dump
*.ez

# Ignore package tarball (built via "mix hex.build").
luau-*.tar
lunar-*.tar

# Temporary files, for example, from tests.
/tmp/
Expand Down
14 changes: 3 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
# Lūʻau
# Lunar

Welcome to the Lua Lūʻau!

## What's in a name?

In ancient Hawaii, there were strict rules about who could eat together and what they could eat.
However, in 1819, King Kamehameha II abolished the religious laws and broke the taboos by eating with women marking the end of the kapu (taboo) system.
From this symbolic act were born the lūʻau parties.

Taking inspiration from King Kamehameha II's symbolic act of ending the kapu system we'll explore the power of Lua to break down taboos and unleash new possibilities.
Experimental Elixir wrapper for luerl.

## Installation

<!-- {x-release-please-start-version} -->
```elixir
def deps do
[
{:luau, "~> 0.1.0"}
{:lunar, "~> 0.1.0"}
]
end
```
Expand Down
16 changes: 8 additions & 8 deletions lib/library.ex
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
defmodule Luau.Library do
defmodule Lunar.Library do
@moduledoc """
Defines a library that will extend the Lua runtime.
## Example usage
defmodule MyLibrary do
use Luau.Library, scope: "my_library"
use Lunar.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__
Expand All @@ -29,25 +29,25 @@ defmodule Luau.Library do

Module.register_attribute(__MODULE__, :lua_functions, accumulate: true, persist: true)

import Luau.Library, only: [deflua: 2]
import Lunar.Library, only: [deflua: 2]

@before_compile unquote(__MODULE__)

@behaviour Luau.Library
@behaviour Lunar.Library

@impl Luau.Library
@impl Lunar.Library
def install(luerl_state) do
:luerl_heap.alloc_table(table(), luerl_state)
end

@impl Luau.Library
@impl Lunar.Library
def scope, do: unquote(scope)
end
end

defmacro __before_compile__(_) do
quote do
@impl Luau.Library
@impl Lunar.Library
def table do
functions =
:functions
Expand Down
97 changes: 0 additions & 97 deletions lib/luau.ex

This file was deleted.

97 changes: 97 additions & 0 deletions lib/lunar.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
defmodule Lunar do
@moduledoc """
Let's get this party started!
"""
@type t :: %__MODULE__{
id: nil | String.t(),
lua_files: [String.t()],
modules: [Lunar.Library.t()],
state: tuple(),
variables: %{[String.t()] => any()}
}

alias Luerl.New, as: Luerl

defstruct id: nil, modules: [], lua_files: [], state: nil, variables: %{}

@type result :: {:ok, t()}
@type error :: {:error, atom() | String.t()}

@doc """
Initialize a new Lunar
"""
@spec init() :: Lunar.t()
def init do
%Lunar{id: Nanoid.generate(), state: Luerl.init()}
end

@doc """
Encodes an Elixir value and makes it available at the key/path.
# Examples
iex> lunar = Lunar.init()
iex> lunar = Lunar.set_variable(lunar, "a", 1)
"""
@spec set_variable(Lunar.t(), [String.t()] | String.t(), any()) :: result | error
def set_variable(lunar, key, value) do
key = List.wrap(key)

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

{:lua_error, reason, _state} ->
{:error, reason}
end
end

@doc """
Load a Lunar.Library into state.
# Examples
iex> lunar = Lunar.init()
iex> lunar = Lunar.load_module!(lunar, Math)
"""
@spec load_module!(Lunar.t(), Lunar.Library.t()) :: Lunar.t()
def load_module!(lunar, module) do
new_state = Luerl.load_module_dec(lunar.state, [module.scope()], module)
%{lunar | state: new_state, modules: [module | lunar.modules]}
end

@doc """
Load Lua code into state from file.
"""
@spec load_lua!(Lunar.t(), String.t()) :: Lunar.t()
def load_lua!(lunar, path) do
case Luerl.dofile(lunar.state, String.to_charlist(path)) do
{:ok, _result, new_state} ->
%{lunar | state: new_state, lua_files: [path | lunar.lua_files]}

:error ->
raise "Could not load Lua file #{path}, file not found"
end
end

@doc """
Evaluate Lua code within a given Lunar
# Examples
iex> lunar = Lunar.init()
iex> lunar = Lunar.load_module!(lunar, Math)
iex> {:ok, lunar} = Lunar.set_variable(lunar, a, 1)
iex> {:ok, [10], _lunar} = Lunar.run(lunar, "return Math.add(a, 9)")
"""
@spec run(Lunar.t(), String.t()) :: {:ok, any(), Lunar.t()} | error
def run(lunar, lua) do
case Luerl.do(lunar.state, lua) do
{:ok, result, new_state} ->
{:ok, result, %{lunar | state: new_state}}

{:error, reason, _state} ->
{:error, reason}
end
end
end
4 changes: 2 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
defmodule Luau.MixProject do
defmodule Lunar.MixProject do
use Mix.Project

def project do
[
app: :luau,
app: :lunar,
deps: deps(),
elixir: "~> 1.15",
elixirc_paths: elixirc_paths(Mix.env()),
Expand Down
88 changes: 0 additions & 88 deletions test/luau_test.exs

This file was deleted.

Loading

0 comments on commit 16c2e81

Please sign in to comment.