Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .formatter.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"],
plugins: [Styler]
]
47 changes: 25 additions & 22 deletions lib/lua.ex
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
external_resource = "README.md"

defmodule Lua do
@external_resource "README.md"
@moduledoc @external_resource
@moduledoc external_resource
|> File.read!()
|> String.split("<!-- MDOC !-->")
|> Enum.fetch!(1)

alias Lua.Util
alias Lua.VM.AssertionError
alias Lua.VM.RuntimeError
alias Lua.VM.State
alias Lua.VM.TypeError
alias Lua.VM.Value

@external_resource external_resource
@type t :: %__MODULE__{}

# Compiler.compile/1 currently always succeeds but the spec allows {:error, _}
Expand All @@ -13,9 +22,6 @@ defmodule Lua do

defstruct [:state]

alias Lua.Util
alias Lua.VM.{State, Value}

@default_sandbox [
[:io],
[:file],
Expand Down Expand Up @@ -66,7 +72,7 @@ defmodule Lua do
opts = Keyword.validate!(opts, sandboxed: @default_sandbox, exclude: [])
exclude = Keyword.fetch!(opts, :exclude)

state = State.new() |> Lua.VM.Stdlib.install()
state = Lua.VM.Stdlib.install(State.new())

opts
|> Keyword.fetch!(:sandboxed)
Expand Down Expand Up @@ -123,7 +129,7 @@ defmodule Lua do
def sandbox(lua, path) do
set!(lua, path, fn args ->
raise Lua.RuntimeException,
"#{Lua.Util.format_function(path, Enum.count(args))} is sandboxed"
"#{Util.format_function(path, Enum.count(args))} is sandboxed"
end)
end

Expand Down Expand Up @@ -222,7 +228,7 @@ defmodule Lua do

case func.(args, wrap(state)) do
{:error, reason, %__MODULE__{}} ->
raise Lua.VM.RuntimeError, value: reason
raise RuntimeError, value: reason

{value, %__MODULE__{} = lua} ->
value = List.wrap(value)
Expand Down Expand Up @@ -457,13 +463,13 @@ defmodule Lua do
e in [Lua.RuntimeException, Lua.CompilerException] ->
reraise e, __STACKTRACE__

e in [Lua.VM.RuntimeError] ->
e in [RuntimeError] ->
reraise Lua.RuntimeException, Exception.message(e), __STACKTRACE__

e in [Lua.VM.TypeError] ->
e in [TypeError] ->
reraise Lua.RuntimeException, Exception.message(e), __STACKTRACE__

e in [Lua.VM.AssertionError] ->
e in [AssertionError] ->
reraise Lua.RuntimeException, Exception.message(e), __STACKTRACE__

e ->
Expand All @@ -487,13 +493,13 @@ defmodule Lua do
e in [Lua.RuntimeException, Lua.CompilerException] ->
reraise e, __STACKTRACE__

e in [Lua.VM.RuntimeError] ->
e in [RuntimeError] ->
reraise Lua.RuntimeException, Exception.message(e), __STACKTRACE__

e in [Lua.VM.TypeError] ->
e in [TypeError] ->
reraise Lua.RuntimeException, Exception.message(e), __STACKTRACE__

e in [Lua.VM.AssertionError] ->
e in [AssertionError] ->
reraise Lua.RuntimeException, Exception.message(e), __STACKTRACE__

e ->
Expand Down Expand Up @@ -586,8 +592,7 @@ defmodule Lua do
42

"""
def call_function(%__MODULE__{state: state} = lua, func, args)
when is_tuple(func) do
def call_function(%__MODULE__{state: state} = lua, func, args) when is_tuple(func) do
case do_call_function(func, args, state) do
{:ok, results, new_state} -> {:ok, results, %{lua | state: new_state}}
{:error, reason, new_state} -> {:error, reason, %{lua | state: new_state}}
Expand Down Expand Up @@ -785,8 +790,7 @@ defmodule Lua do
lua = ensure_scope!(lua, scope)

lua =
module.__lua_functions__()
|> Enum.reduce(lua, fn {name, with_state?, variadic?}, lua ->
Enum.reduce(module.__lua_functions__(), lua, fn {name, with_state?, variadic?}, lua ->
arities = Map.get(funcs, name)

func =
Expand Down Expand Up @@ -911,10 +915,10 @@ defmodule Lua do
message: "maps must be explicitly encoded to tables using Lua.encode!/2"

{:error, reason} ->
raise Lua.VM.RuntimeError, value: reason
raise RuntimeError, value: reason

{:error, reason, %Lua{}} ->
raise Lua.VM.RuntimeError, value: reason
raise RuntimeError, value: reason

{data, %Lua{} = returned_lua} ->
data = List.wrap(data)
Expand Down Expand Up @@ -942,8 +946,7 @@ defmodule Lua do
end
catch
thrown_value ->
{:error,
"Value thrown during function '#{function_name}' execution: #{inspect(thrown_value)}"}
{:error, "Value thrown during function '#{function_name}' execution: #{inspect(thrown_value)}"}
end

defp ensure_scope!(lua, []) do
Expand Down
13 changes: 6 additions & 7 deletions lib/lua/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@ defmodule Lua.API do

quote do
@behaviour Lua.API
Module.register_attribute(__MODULE__, :lua_function, accumulate: true)
@before_compile Lua.API

import Lua.API,
only: [
Expand All @@ -108,6 +106,9 @@ defmodule Lua.API do
is_mfa: 1
]

Module.register_attribute(__MODULE__, :lua_function, accumulate: true)
@before_compile Lua.API

@impl Lua.API
def scope do
unquote(scope)
Expand Down Expand Up @@ -161,7 +162,7 @@ defmodule Lua.API do
"""
defmacro runtime_exception!(message) do
quote do
unless function_exported?(__MODULE__, :scope, 0) do
if !function_exported?(__MODULE__, :scope, 0) do
raise "runtime_exception!/1 can only be called on modules implementing Lua.API"
end

Expand Down Expand Up @@ -248,8 +249,7 @@ defmodule Lua.API do

quote do
@lua_function validate_func!(
{unquote(name), true,
Module.delete_attribute(__MODULE__, :variadic) || false},
{unquote(name), true, Module.delete_attribute(__MODULE__, :variadic) || false},
__MODULE__,
@lua_function
)
Expand All @@ -265,8 +265,7 @@ defmodule Lua.API do

quote do
@lua_function validate_func!(
{unquote(name), false,
Module.delete_attribute(__MODULE__, :variadic) || false},
{unquote(name), false, Module.delete_attribute(__MODULE__, :variadic) || false},
__MODULE__,
@lua_function
)
Expand Down
6 changes: 4 additions & 2 deletions lib/lua/ast/block.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ defmodule Lua.AST.Block do
Blocks create a new scope for local variables.
"""

alias Lua.AST.{Meta, Statement}
alias Lua.AST.Meta
alias Lua.AST.Statement

@type t :: %__MODULE__{
stmts: [Statement.t()],
Expand Down Expand Up @@ -39,7 +40,8 @@ defmodule Lua.AST.Chunk do
A chunk is essentially a block that represents a complete unit of Lua code.
"""

alias Lua.AST.{Meta, Block}
alias Lua.AST.Block
alias Lua.AST.Meta

@type t :: %__MODULE__{
block: Block.t(),
Expand Down
8 changes: 6 additions & 2 deletions lib/lua/ast/builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ defmodule Lua.AST.Builder do
])
"""

alias Lua.AST.{Chunk, Block, Meta, Expr, Statement}
alias Lua.AST.Block
alias Lua.AST.Chunk
alias Lua.AST.Expr
alias Lua.AST.Meta
alias Lua.AST.Statement

# Chunk and Block

Expand Down Expand Up @@ -425,7 +429,7 @@ defmodule Lua.AST.Builder do
%Statement.If{
condition: condition,
then_block: block(then_stmts),
elseifs: Keyword.get(opts, :elseif, []) |> Enum.map(fn {c, s} -> {c, block(s)} end),
elseifs: opts |> Keyword.get(:elseif, []) |> Enum.map(fn {c, s} -> {c, block(s)} end),
else_block: if(else_stmts = Keyword.get(opts, :else), do: block(else_stmts)),
meta: Keyword.get(opts, :meta)
}
Expand Down
25 changes: 13 additions & 12 deletions lib/lua/ast/expr.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule Lua.AST.Expr do
All expression nodes include a `meta` field for position tracking.
"""

alias Lua.AST.Expr
alias Lua.AST.Meta

defmodule Nil do
Expand Down Expand Up @@ -74,8 +75,8 @@ defmodule Lua.AST.Expr do

@type t :: %__MODULE__{
op: op(),
left: Lua.AST.Expr.t(),
right: Lua.AST.Expr.t(),
left: Expr.t(),
right: Expr.t(),
meta: Meta.t() | nil
}
end
Expand All @@ -95,7 +96,7 @@ defmodule Lua.AST.Expr do

@type t :: %__MODULE__{
op: op(),
operand: Lua.AST.Expr.t(),
operand: Expr.t(),
meta: Meta.t() | nil
}
end
Expand All @@ -112,8 +113,8 @@ defmodule Lua.AST.Expr do
defstruct [:fields, :meta]

@type field ::
{:list, Lua.AST.Expr.t()}
| {:record, Lua.AST.Expr.t(), Lua.AST.Expr.t()}
{:list, Expr.t()}
| {:record, Expr.t(), Expr.t()}

@type t :: %__MODULE__{
fields: [field()],
Expand All @@ -128,8 +129,8 @@ defmodule Lua.AST.Expr do
defstruct [:func, :args, :meta]

@type t :: %__MODULE__{
func: Lua.AST.Expr.t(),
args: [Lua.AST.Expr.t()],
func: Expr.t(),
args: [Expr.t()],
meta: Meta.t() | nil
}
end
Expand All @@ -143,9 +144,9 @@ defmodule Lua.AST.Expr do
defstruct [:object, :method, :args, :meta]

@type t :: %__MODULE__{
object: Lua.AST.Expr.t(),
object: Expr.t(),
method: String.t(),
args: [Lua.AST.Expr.t()],
args: [Expr.t()],
meta: Meta.t() | nil
}
end
Expand All @@ -157,8 +158,8 @@ defmodule Lua.AST.Expr do
defstruct [:table, :key, :meta]

@type t :: %__MODULE__{
table: Lua.AST.Expr.t(),
key: Lua.AST.Expr.t(),
table: Expr.t(),
key: Expr.t(),
meta: Meta.t() | nil
}
end
Expand All @@ -172,7 +173,7 @@ defmodule Lua.AST.Expr do
defstruct [:table, :field, :meta]

@type t :: %__MODULE__{
table: Lua.AST.Expr.t(),
table: Expr.t(),
field: String.t(),
meta: Meta.t() | nil
}
Expand Down
Loading