{"id":15056350,"url":"https://github.com/tv-labs/lua","last_synced_at":"2025-04-04T10:03:06.478Z","repository":{"id":225935671,"uuid":"767147256","full_name":"tv-labs/lua","owner":"tv-labs","description":"The most ergomonic interface to Luerl in Elixir","archived":false,"fork":false,"pushed_at":"2025-03-27T17:36:38.000Z","size":101,"stargazers_count":81,"open_issues_count":8,"forks_count":1,"subscribers_count":6,"default_branch":"main","last_synced_at":"2025-03-28T09:01:49.575Z","etag":null,"topics":["elixir","erlang","lua","luerl"],"latest_commit_sha":null,"homepage":"https://hexdocs.pm/lua/","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tv-labs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-03-04T19:36:41.000Z","updated_at":"2025-03-27T17:36:40.000Z","dependencies_parsed_at":"2024-03-05T03:48:35.862Z","dependency_job_id":"4e539e95-a7b4-4088-9b2b-3bc83cce7745","html_url":"https://github.com/tv-labs/lua","commit_stats":null,"previous_names":["tv-labs/lua"],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tv-labs%2Flua","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tv-labs%2Flua/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tv-labs%2Flua/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tv-labs%2Flua/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tv-labs","download_url":"https://codeload.github.com/tv-labs/lua/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247149510,"owners_count":20891954,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["elixir","erlang","lua","luerl"],"created_at":"2024-09-24T21:50:03.532Z","updated_at":"2025-04-04T10:03:06.454Z","avatar_url":"https://github.com/tv-labs.png","language":"Elixir","funding_links":[],"categories":["Elixir"],"sub_categories":[],"readme":"# Lua\n\n\u003c!-- MDOC !--\u003e\n\n`Lua` is an ergonomic interface to [Luerl](https://github.com/rvirding/luerl), aiming to be the best way to use Luerl from Elixir.\n\n## Features\n\n* `~LUA` sigil for validating Lua code at compile-time\n* `deflua` macro for exposing Elixir functions to Lua\n* Improved error messages and sandboxing\n* Deep setting/getting variables and state\n* Excellent documentation and guides for working with Luerl\n\n\u003e #### Lua the Elixir library vs Lua the language {: .info}\n\u003e When referring to this library, `Lua` will be stylized as a link.\n\u003e \n\u003e References to Lua the language will be in plaintext and not linked.\n\n## Executing Lua\n\n`Lua` can be run using the `eval!/2` function\n\n    iex\u003e {[4], _} = Lua.eval!(\"return 2 + 2\")\n\n## Compile-time validation\n\nUse the `~LUA` sigil to parse and validate your Lua code at compile time\n\n    iex\u003e import Lua, only: [sigil_LUA: 2]\n\n    #iex\u003e {[4], _} = Lua.eval!(~LUA[return 2 +])\n    ** (Lua.CompilerException) Failed to compile Lua!\n\nUsing the `c` modifier transforms your Lua code into a `t:Lua.Chunk.t/0` at compile-time,\nwhich will speed up execution at runtime since the Lua no longer needs to be parsed\n\n    iex\u003e import Lua, only: [sigil_LUA: 2]\n    iex\u003e {[4], _} = Lua.eval!(~LUA[return 2 + 2]c)\n\n## Exposing Elixir functions to Lua\n\nThe simplest way to expose an Elixir function to Lua is using the `Lua.set!/3` function\n\n``` elixir\nimport Lua, only: [sigil_LUA: 2]\n\nlua = \n  Lua.set!(Lua.new(), [:sum], fn args -\u003e\n    [Enum.sum(args)]\n  end)\n\n{[10], _} = Lua.eval!(lua, ~LUA[return sum(1, 2, 3, 4)]c)\n```\n\nFor easily expressing APIs, `Lua` provides the `deflua` macro for exposing Elixir functions to Lua\n\n``` elixir\ndefmodule MyAPI do\n  use Lua.API\n      \n  deflua double(v), do: 2 * v\nend\n\nimport Lua, only: [sigil_LUA: 2]\n    \nlua = Lua.new() |\u003e Lua.load_api(MyAPI)\n\n{[10], _} = Lua.eval!(lua, ~LUA[return double(5)])\n```\n\n## Calling Lua functions from Elixir\n\n`Lua` can be used to expose complex functions written in Elixir. In some cases, you may want to call Lua functions from Elixir. This can\nbe achieved with the `Lua.call_function!/3` function\n\n``` elixir\ndefmodule MyAPI do\n  use Lua.API, scope: \"example\"\n\n  deflua foo(value), state do\n    Lua.call_function!(state, [:string, :lower], [value])\n  end\nend\n\nimport Lua, only: [sigil_LUA: 2]\n\nlua = Lua.new() |\u003e Lua.load_api(MyAPI)\n\n{[\"wow\"], _} = Lua.eval!(lua, ~LUA[return example.foo(\"WOW\")])\n```\n\n## Modify Lua state from Elixir\n\nYou can also use `Lua` to modify the state of the lua environment inside your Elixir code. Imagine you have a queue module that you\nwant to implement in Elixir, with the queue stored in a global variable\n\n``` elixir\ndefmodule Queue do\n  use Lua.API, scope: \"q\"\n  \n  deflua push(v), state do\n    # Pull out the global variable \"my_queue\" from lua\n    queue = Lua.get!(state, [:my_queue])\n    \n    # Call the Lua function table.insert(table, value)\n    {[], state} = Lua.call_function!(state, [:table, :insert], [queue, v])\n    \n    # Return the modified lua state with no return values\n    {[], state}\n  end\nend\n\nimport Lua, only: [sigil_LUA: 2]\n\nlua = Lua.new() |\u003e Lua.load_api(Queue)\n\n{[queue], _} =\n  Lua.eval!(lua, \"\"\"\n  my_queue = {}\n\n  q.push(\"first\")\n  q.push(\"second\")\n\n  return my_queue\n  \"\"\")\n  \n[\"first\", \"second\"] = Lua.Table.as_list(queue)\n```\n\n## Credits\n\n`Lua` piggy-backs off of Robert Virding's [Luerl](https://github.com/rvirding/luerl) project, which implements a Lua lexer, parser, and full-blown Lua virtual machine that runs inside the BEAM.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftv-labs%2Flua","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftv-labs%2Flua","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftv-labs%2Flua/lists"}