Skip to content

Commit 11cda35

Browse files
committed
Support cargo test command
1 parent 743b7bf commit 11cda35

File tree

10 files changed

+68
-7
lines changed

10 files changed

+68
-7
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/.vscode
22
/target
3+
/tests/time.txt

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "lua53-rs"
2+
name = "rustmoon"
33
version = "0.1.0"
44
edition = "2021"
55
build = "build/build.rs"

src/bin/lua.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ fn main() {
2020
}
2121
}
2222

23-
extern crate lua53_rs;
23+
extern crate rustmoon;

src/lapi.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use libc::c_int;
66

77
use crate::lstate::lua_State;
8-
use crate::types::{lua_Number, LUA_MULTRET};
8+
use crate::types::{lua_KContext, lua_KFunction, lua_Number, LUA_MULTRET};
99

1010
pub(crate) unsafe fn api_incr_top(L: *mut lua_State) {
1111
(*L).top = (*L).top.add(1);
@@ -29,7 +29,30 @@ pub unsafe fn lua_pop(L: *mut lua_State, n: c_int) {
2929
lua_settop(L, -n - 1)
3030
}
3131

32+
pub unsafe fn lua_call(L: *mut lua_State, n: c_int, r: c_int) {
33+
lua_callk(L, n, r, 0, None)
34+
}
35+
36+
pub unsafe fn lua_pcall(L: *mut lua_State, n: c_int, r: c_int, f: c_int) -> c_int {
37+
lua_pcallk(L, n, r, f, 0, None)
38+
}
39+
3240
extern "C" {
3341
pub fn lua_version(L: *mut lua_State) -> *const lua_Number;
3442
pub fn lua_settop(L: *mut lua_State, idx: c_int);
43+
pub fn lua_pcallk(
44+
L: *mut lua_State,
45+
nargs: c_int,
46+
nresults: c_int,
47+
errfunc: c_int,
48+
ctx: lua_KContext,
49+
k: lua_KFunction,
50+
) -> c_int;
51+
pub fn lua_callk(
52+
L: *mut lua_State,
53+
nargs: c_int,
54+
nresults: c_int,
55+
ctx: lua_KContext,
56+
k: lua_KFunction,
57+
);
3558
}

src/lauxlib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,8 @@ extern "C" {
2020
openf: lua_CFunction,
2121
glb: c_int,
2222
);
23+
pub fn luaL_newstate() -> *mut lua_State;
24+
pub fn luaL_loadstring(L: *mut lua_State, s: *const c_char) -> c_int;
25+
pub fn luaL_loadfilex(L: *mut lua_State, filename: *const c_char, mode: *const c_char)
26+
-> c_int;
2327
}

src/lib.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ pub(crate) mod lstate;
1414
#[macro_use]
1515
pub(crate) mod lobject;
1616

17-
pub(crate) mod lapi;
18-
pub(crate) mod lauxlib;
1917
pub(crate) mod ldebug;
2018
pub(crate) mod ldo;
2119
pub(crate) mod ldump;
@@ -34,4 +32,8 @@ pub(crate) mod ltm;
3432
pub(crate) mod lundump;
3533
pub(crate) mod lvm;
3634
pub(crate) mod lzio;
37-
pub(crate) mod types;
35+
36+
pub mod lapi;
37+
pub mod lauxlib;
38+
pub mod lualib;
39+
pub mod types;

src/lualib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub use crate::linit::luaL_openlibs;

src/macros.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#[macro_export]
12
macro_rules! cstr {
23
($s:expr) => {
34
concat!($s, "\0") as *const str as *const [::std::os::raw::c_char]

tests/tests.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use std::env;
2+
use std::ptr;
3+
4+
use rustmoon::cstr;
5+
use rustmoon::lapi::lua_call;
6+
use rustmoon::lauxlib::{luaL_loadfilex, luaL_loadstring, luaL_newstate};
7+
use rustmoon::lualib::luaL_openlibs;
8+
use rustmoon::types::LUA_OK;
9+
10+
#[test]
11+
fn test_all() {
12+
unsafe {
13+
let state = luaL_newstate();
14+
luaL_openlibs(state);
15+
16+
// Skip non-portable tests
17+
if luaL_loadstring(state, cstr!("_port=true")) != LUA_OK {
18+
panic!("Failed to load Lua code");
19+
}
20+
lua_call(state, 0, 0);
21+
22+
// Run the tests suit
23+
env::set_current_dir("tests").unwrap();
24+
if luaL_loadfilex(state, cstr!("all.lua"), ptr::null()) != LUA_OK {
25+
panic!("Failed to load `all.lua`");
26+
}
27+
lua_call(state, 0, 0);
28+
}
29+
}

0 commit comments

Comments
 (0)