Skip to content

Commit

Permalink
Implemented Lua.encode() and Lua.decode().
Browse files Browse the repository at this point in the history
  • Loading branch information
artob committed Sep 9, 2016
1 parent 6de0627 commit 64ba582
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Added `Lua.set_global/3`, `Lua.get_global/2`.
- Added `Lua.set_package_path/2`.
- Added `Lua.require!/2`.
- Added `Lua.encode/1` and `Lua.decode/1`.

## [0.3.0] - 2016-08-08
### Added
Expand Down
20 changes: 20 additions & 0 deletions lib/lua.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,26 @@
defmodule Lua do
alias Lua.{Chunk, Error, State}

@doc "Encodes an Elixir term as a Lua value."
@spec encode(nil | boolean | number | binary | atom) :: nil | boolean | float | binary
def encode(term)
def encode(nil), do: nil
def encode(false), do: false
def encode(true), do: true
def encode(value) when is_integer(value), do: :erlang.float(value)
def encode(value) when is_float(value), do: value
def encode(value) when is_binary(value), do: value
def encode(value) when is_atom(value), do: Atom.to_string(value)

@doc "Decodes a Lua value as an Elixir term."
@spec decode(nil | boolean | number | binary) :: term
def decode(value)
def decode(nil), do: nil
def decode(false), do: false
def decode(true), do: true
def decode(value) when is_number(value), do: value
def decode(value) when is_binary(value), do: value

@doc "Performs garbage collection."
@spec gc(Lua.State.t) :: Lua.State.t
def gc(%State{luerl: state}) do
Expand Down

0 comments on commit 64ba582

Please sign in to comment.