Skip to content

Commit 3d19b26

Browse files
authored
Merge pull request #134 from Peefy/feat-lua-api
feat: initial lua api
2 parents 74e7055 + 9940c4e commit 3d19b26

File tree

9 files changed

+192
-2
lines changed

9 files changed

+192
-2
lines changed

.github/workflows/lua-test.yaml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: lua-test
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- master
8+
tags:
9+
- '*'
10+
pull_request:
11+
branches:
12+
- main
13+
paths:
14+
- "lua/**"
15+
- ".github/workflows/lua-test.yaml"
16+
workflow_dispatch:
17+
18+
concurrency:
19+
group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event_name }}
20+
cancel-in-progress: true
21+
22+
permissions:
23+
contents: read
24+
25+
jobs:
26+
build-and-test:
27+
defaults:
28+
run:
29+
working-directory: "lua"
30+
runs-on: ubuntu-latest
31+
steps:
32+
- uses: actions/checkout@v4
33+
- name: Setup lua toolchain
34+
run: |
35+
sudo apt-get update
36+
sudo apt-get install -y lua-busted luarocks liblua5.2-dev
37+
- name: Install Rust
38+
uses: actions-rs/toolchain@v1
39+
with:
40+
toolchain: 1.79
41+
override: true
42+
components: clippy, rustfmt
43+
- name: Build
44+
run: make

lua/Cargo.toml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[package]
2+
name = "kcl-lib-lua"
3+
version = "0.10.0-beta.1"
4+
edition = "2021"
5+
publish = false
6+
7+
[features]
8+
default = ["mlua/lua52"]
9+
lua52 = ["mlua", "mlua/lua52"]
10+
11+
[lib]
12+
crate-type = ["cdylib"]
13+
14+
[dependencies]
15+
mlua = { version = "0.9", features = [
16+
"module",
17+
"macros",
18+
], default-features = false, optional = true }
19+
kclvm-api = { git = "https://github.com/kcl-lang/kcl", version = "0.10.0-beta.1" }

lua/Makefile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
.PHONY: all
2+
all: build
3+
4+
.PHONY: build
5+
build:
6+
cargo build -r
7+
8+
.PHONY: fmt
9+
fmt:
10+
cargo fmt
11+
12+
.PHONY: check
13+
check:
14+
cargo check
15+
16+
.PHONY: clean
17+
clean:
18+
cargo clean

lua/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# KCL Artifact Library for Lua
2+
3+
This repo is under development, PRs welcome!
4+
5+
## Build from Source
6+
7+
**Prerequisites**
8+
9+
+ Lua
10+
+ Cargo
11+
12+
### Lua version
13+
14+
You have to enable one of the features: lua54, lua53, lua52, lua51, luajit(52) or luau in `Cargo.toml`, according to the chosen Lua version. **Default Lua version is 5.2**.
15+
16+
If you build on macos, you can set the environment to prevent link errors.
17+
18+
```shell
19+
# Set cargo build target on macos
20+
export MACOSX_DEPLOYMENT_TARGET='10.13'
21+
```
22+
23+
### Linux
24+
25+
```shell
26+
make
27+
# copy to lua share library directory
28+
cp ./target/release/libkcl_lib_lua.so /usr/lib/lua/5.2/kcl_lib.so
29+
```

lua/kcl_lib-0.10.0-1.rockspec

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package = "kcl_lib"
2+
version = "0.10.0-1"
3+
4+
source = {
5+
url = "git+https://github.com/kcl-lang/kcl",
6+
}
7+
8+
description = {
9+
summary = "KCL Lua Bindings",
10+
detailed = [[
11+
KCL Lua Bindings
12+
]],
13+
homepage = "https://kcl-lang.io/",
14+
license = " Apache-2.0"
15+
}
16+
17+
dependencies = {
18+
"lua >= 5.1",
19+
"luarocks-build-rust-mlua = 0.2.0",
20+
}
21+
22+
build = {
23+
type = "rust-mlua",
24+
modules = {
25+
["kcl_lib"] = "kcl_lib_lua",
26+
},
27+
target_path = "target",
28+
}

lua/src/lib.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
extern crate kclvm_api;
2+
3+
use mlua::prelude::*;
4+
5+
/// Execute KCL file with arguments and return the JSON/YAML result.
6+
fn exec_program<'a>(lua: &'a Lua, args: LuaTable<'a>) -> LuaResult<LuaTable<'a>> {
7+
let api = kclvm_api::API::default();
8+
let work_dir: String = args.get("work_dir")?;
9+
let k_filename_list: Vec<String> = args.get("k_filename_list")?;
10+
let k_code_list: Vec<String> = args.get("k_code_list")?;
11+
12+
let result = match api.exec_program(&kclvm_api::ExecProgramArgs {
13+
work_dir,
14+
k_filename_list,
15+
k_code_list,
16+
..Default::default()
17+
}) {
18+
Ok(r) => r,
19+
Err(e) => return Err(LuaError::external(e)),
20+
};
21+
22+
let t = lua.create_table()?;
23+
t.set("json_result", lua.create_string(result.json_result)?)?;
24+
t.set("yaml_result", lua.create_string(result.yaml_result)?)?;
25+
t.set("log_message", lua.create_string(result.log_message)?)?;
26+
t.set("err_message", lua.create_string(result.err_message)?)?;
27+
Ok(t)
28+
}
29+
30+
#[mlua::lua_module]
31+
fn kcl_lib(lua: &Lua) -> LuaResult<LuaTable> {
32+
let module = lua.create_table()?;
33+
module.set("exec_program", lua.create_function(exec_program)?)?;
34+
Ok(module)
35+
}

lua/test/exec_test.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
local kcl_lib = require("kcl_lib")
2+
3+
describe("kcl lua lib unit test", function()
4+
describe("exec_program", function()
5+
it("operator function in fs schema", function()
6+
local result, err = kcl.exec_program({k_filename_list=["./test_data/schema.k"]})
7+
assert.is_nil(err)
8+
assert.are.equal(result.yaml_result, "app:\n replicas: 2")
9+
end)
10+
end)
11+
end)

lua/test/test_data/schema.k

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
schema AppConfig:
2+
replicas: int
3+
4+
app: AppConfig {
5+
replicas: 2
6+
}

swift/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ This repo is under development, PRs welcome!
44

55
## Developing
66

7-
If you build on macos, you can set the environment to prevent link errors.
8-
97
**Prerequisites**
108

119
+ Swift 5.8+
1210
+ Cargo
1311

12+
If you build on macos, you can set the environment to prevent link errors.
13+
1414
```shell
1515
# Set cargo build target on macos
1616
export MACOSX_DEPLOYMENT_TARGET='10.13'

0 commit comments

Comments
 (0)