Skip to content

Commit

Permalink
feat(libcorn): lua support via library export
Browse files Browse the repository at this point in the history
Introduces the structure for a Lua process to load libcorn.so
  • Loading branch information
A-Cloud-Ninja authored and JakeStanger committed Aug 3, 2023
1 parent ff731dc commit 848f706
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 1 deletion.
106 changes: 106 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,36 @@ which can parse Corn into valid JavaScript objects.
import * as corn from 'libcorn';

const parsed = corn.parse('{foo = "bar"}');
console.log(parsed) // { foo: "bar" }
console.log(parsed) // Map(foo -> "bar")
```

### Lua

Lua support can be built into the library using __one of__ the feature flags,
allowing you to bind directly to `libcorn.so`:

- `lua51`
- `lua52`
- `lua53`
- `lua54`
- `luajit`
- `luajit52`

So long as `libcorn.so` is in Lua's module path, it can be then be used as below:

```lua
local libcorn = require("libcorn")
local success, res = pcall(libcorn.parse, '{foo = "bar"}')

if success then
print(res.foo) -- lua table
else
print(res) -- pretty printed error
end
```

Thanks to [A-Cloud-Ninja](https://github.com/A-Cloud-Ninja) for adding Lua support!

## Writing Corn

> This section gives all the outputs in JSON format. Remember you can output in any supported format!
Expand Down
7 changes: 7 additions & 0 deletions libcorn/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ keywords = ["configuration", "language", "wasm", "pest", "peg"]
[features]
wasm = ["wasm-bindgen", "serde-wasm-bindgen", "console_error_panic_hook", "wee_alloc"]
bench = ["criterion"]
lua51 = ["mlua/lua51"]
lua52 = ["mlua/lua52"]
lua53 = ["mlua/lua53"]
lua54 = ["mlua/lua54"]
luajit = ["mlua/luajit"]
luajit52 = ["mlua/luajit52"]

[lib]
name = "corn"
Expand All @@ -26,6 +32,7 @@ wasm-bindgen = { version = "0.2.83", optional = true }
serde-wasm-bindgen = { version = "0.5.0", optional = true }
console_error_panic_hook = { version = "0.1.7", optional = true }
wee_alloc = { version = "0.4.5", optional = true }
mlua = { version = "0.8.9", features = ["vendored", "module", "macros", "serialize"], optional = true }

# bench
criterion = { version = "0.5.1", features = ["html_reports"], optional = true }
Expand Down
10 changes: 10 additions & 0 deletions libcorn/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ pub mod error;
mod parser;

mod de;
#[cfg(any(
feature = "lua",
feature = "lua51",
feature = "lua52",
feature = "lua53",
feature = "lua54",
feature = "luajit",
feature = "luajit52"
))]
mod lua;
#[cfg(feature = "wasm")]
mod wasm;

Expand Down
24 changes: 24 additions & 0 deletions libcorn/src/lua.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use crate::Value;
use mlua::prelude::*;

impl<'lua> ToLua<'lua> for Value<'lua> {
fn to_lua(self, lua: &'lua Lua) -> LuaResult<LuaValue<'lua>> {
lua.to_value(&self)
}
}

fn lua_parse(lua: &Lua, config: String) -> LuaResult<LuaValue> {
let res = crate::parse(&config);
match res {
Ok(v) => Ok(lua.to_value(&v)?),
Err(e) => Err(LuaError::RuntimeError(e.to_string())),
}
}

#[mlua::lua_module]
fn libcorn(lua: &Lua) -> LuaResult<LuaTable> {
let exports = lua.create_table()?;
let parse = lua.create_function(lua_parse)?;
exports.set("parse", parse)?;
Ok(exports)
}

0 comments on commit 848f706

Please sign in to comment.