Skip to content

Commit

Permalink
Cleanup the docs so that examples are copy-pastable (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
davydog187 authored Sep 27, 2024
1 parent 31826ed commit 0339a11
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,33 @@

`Lua` can be run using the `eval!/2` function

``` elixir
iex> {[4], _} =
...> Lua.eval!(~LUA"""
...> return 2 + 2
...> """)
iex> {[4], _} = Lua.eval!("return 2 + 2")

```
## Compile-time validation

Use the `~LUA` sigil to parse and validate your Lua code at compile time

#iex> {[4], _} = Lua.eval!(~LUA[return 2 +])
** (Lua.CompilerException) Failed to compile Lua!

Using the `c` modifier transforms your Lua code into a `t:Lua.Chunk.t/0` at compile-time,
which will speed up execution at runtime since the Lua no longer needs to be parsed

iex> {[4], _} = Lua.eval!(~LUA[return 2 + 2]c)

## Exposing Elixir functions to Lua

The simplest way to expose an Elixir function to Lua is using the `Lua.set!/3` function

``` elixir
import Lua, only: [sigil_LUA: 2]

lua =
Lua.set!(Lua.new(), [:sum], fn args ->
[Enum.sum(args)]
end)

{[10], _} = Lua.eval!(lua, ~LUA"return sum(1, 2, 3, 4)"c)
{[10], _} = Lua.eval!(lua, ~LUA[return sum(1, 2, 3, 4)]c)
```

For easily expressing APIs, `Lua` provides the `deflua` macro for exposing Elixir functions to Lua
Expand All @@ -50,13 +58,12 @@ defmodule MyAPI do

deflua double(v), do: 2 * v
end

import Lua, only: [sigil_LUA: 2]

lua = Lua.new() |> Lua.load_api(MyAPI)

{[10], _} =
Lua.eval!(lua, ~LUA"""
return double(5)
""")
{[10], _} = Lua.eval!(lua, ~LUA[return double(5)])
```

## Calling Lua functions from Elixir
Expand All @@ -73,9 +80,11 @@ defmodule MyAPI do
end
end

import Lua, only: [sigil_LUA: 2]

lua = Lua.new() |> Lua.load_api(MyAPI)

{["wow"], _} = Lua.eval!(lua, ~LUA"return example.foo(\"WOW\")")
{["wow"], _} = Lua.eval!(lua, ~LUA[return example.foo("WOW")])
```

## Modify Lua state from Elixir
Expand All @@ -99,6 +108,8 @@ defmodule Queue do
end
end

import Lua, only: [sigil_LUA: 2]

lua = Lua.new() |> Lua.load_api(Queue)

{[queue], _} =
Expand Down

0 comments on commit 0339a11

Please sign in to comment.