Skip to content
This repository was archived by the owner on Nov 12, 2025. It is now read-only.

Commit c78d9db

Browse files
committed
0.1.1
Added most of the necessary functions and changed the globals structure a lot with modules and enums. Missing a lot of the aux library and need to work on making structs like the lua_Buffer
1 parent 7fbb7ab commit c78d9db

4 files changed

Lines changed: 137 additions & 28 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "rglua"
33
description = "Rust bindings to the lua api for gmod binary module creation"
4-
version = "0.1.0"
4+
version = "0.1.1"
55
authors = ["Vurv <vurvdevelops@gmail.com>"]
66
keywords = ["glua","garrysmod","lua"]
77
readme = "README.md"

src/globals.rs

Lines changed: 91 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,93 @@
11
use crate::types::*;
2-
pub static LUA_REGISTRYINDEX: CInt = -10000;
3-
pub static LUA_ENVIRONINDEX: CInt = -10001;
4-
pub static LUA_GLOBALSINDEX: CInt = -10002;
52

6-
pub static LUA_MULTRET: CInt = -1;
3+
pub mod Lua {
4+
use super::CInt;
5+
6+
pub static REGISTRYINDEX: CInt = -10000;
7+
pub static ENVIRONINDEX: CInt = -10000;
8+
pub static GLOBALSINDEX: CInt = -10000;
9+
10+
pub static MULTRET: CInt = -1;
11+
pub static SIGNATURE: &'static str = "\x1bLua";
12+
pub static MINSTACK: CInt = 20;
13+
14+
pub static NUMTYPES: CInt = 9;
15+
pub static NUMTAGS: CInt = NUMTYPES;
16+
17+
18+
// Proper enums to use. Cast these to integers when using them
19+
pub enum Type {
20+
None = -1,
21+
Nil,
22+
Bool,
23+
LUserData,
24+
Number,
25+
String,
26+
Table,
27+
Function,
28+
UserData,
29+
Thread
30+
}
31+
32+
pub enum Status {
33+
Ok = 0,
34+
Yield,
35+
ErrRun,
36+
ErrSyntax,
37+
ErrMem,
38+
ErrErr
39+
}
40+
41+
// Garbage collection
42+
pub enum Gc {
43+
Stop = 0,
44+
Restart,
45+
Collect,
46+
Count,
47+
CountB,
48+
Step,
49+
SetPause,
50+
SetStepMul,
51+
IsRunning,
52+
Gen,
53+
Inc // 11
54+
}
55+
56+
// To be used with debug.sethook
57+
pub enum Hook {
58+
Call = 0,
59+
Ret,
60+
Line,
61+
Count,
62+
TailCall
63+
}
64+
65+
pub enum Mask {
66+
Call = (1 << Hook::Call as i32),
67+
Ret = (1 << Hook::Ret as i32),
68+
Line = (1 << Hook::Line as i32),
69+
Count = (1 << Hook::Count as i32)
70+
}
71+
}
72+
73+
pub mod Jit {
74+
pub enum Mode {
75+
ENGINE,
76+
DEBUG,
77+
FUNC,
78+
ALLFUNC,
79+
ALLSUBFUNC,
80+
TRACE,
81+
WRAPCFUNC = 0x10,
82+
MAX,
83+
MASK = 0x0ff // LUAJIT_MODE_MASK
84+
}
85+
86+
use super::CInt;
87+
// Associated Constants, woah
88+
impl Mode {
89+
pub const OFF: CInt = 0x0000;
90+
pub const ON: CInt = 0x0100;
91+
pub const FLUSH: CInt = 0x0200;
92+
}
93+
}

src/lib.rs

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ pub mod globals;
66
pub mod helpers;
77

88
use types::*;
9-
use globals::{
10-
LUA_GLOBALSINDEX
11-
};
9+
use globals::Lua;
1210

1311
use std::path::{Path, PathBuf};
1412
extern crate dlopen;
@@ -24,9 +22,11 @@ pub struct LuaSharedInterface {
2422
pub luaL_loadbufferx: extern fn(state: LuaState, code: CharBuf, size: SizeT, id: CharBuf, mode: CharBuf) -> CInt,
2523
pub luaL_loadbuffer: extern fn(state: LuaState, code: CharBuf, size: SizeT, id: CharBuf) -> CInt,
2624
pub luaL_loadstring: extern fn(state: LuaState, code: CharBuf) -> CInt,
25+
2726
pub lua_pcall: extern fn(state: LuaState, nargs: CInt, nresults: CInt, msgh: CInt) -> CInt,
2827
pub lua_call: extern fn(state: LuaState, nargs: CInt, nresults: CInt) -> CInt,
2928
pub lua_cpcall: extern fn(state: LuaState, func: LuaCFunction, userdata: *mut CVoid ) -> CInt,
29+
pub luaL_callmeta: extern fn(state: LuaState, obj: CInt, name: CharBuf) -> CInt,
3030

3131
// Setters
3232
pub lua_setfield: extern fn(state: LuaState, idx: CInt, name: CharBuf),
@@ -41,11 +41,17 @@ pub struct LuaSharedInterface {
4141
pub lua_getfield: extern fn(state: LuaState, idx: CInt, key: CharBuf),
4242
pub lua_getupvalue: extern fn(state: LuaState, fidx: CInt, idx: CInt) -> CharBuf,
4343
pub lua_type: extern fn(state: LuaState, idx: CInt) -> CInt,
44+
pub lua_typename: extern fn(state: LuaState, typeid: CInt) -> CharBuf, // To be used with the return value of lua_type
4445

4546
// Getters (with "to")
4647
pub lua_tolstring: extern fn(state: LuaState, ind: CInt, size: SizeT) -> CharBuf,
4748
pub lua_toboolean: extern fn(state: LuaState, idx: CInt) -> CInt,
4849
pub lua_tocfunction: extern fn(state: LuaState, idx: CInt) -> LuaCFunction,
50+
pub lua_tointeger: extern fn(state: LuaState, idx: CInt) -> LuaInteger,
51+
pub lua_tonumber: extern fn(state: LuaState, idx: CInt) -> LuaNumber,
52+
pub lua_topointer: extern fn(state: LuaState, idx: CInt) -> *mut CVoid,
53+
pub lua_tothread: extern fn(state: LuaState, idx: CInt) -> LuaState,
54+
pub lua_touserdata: extern fn(state: LuaState, idx: CInt) -> *mut CVoid,
4955

5056
// Push functions
5157
pub lua_pushstring: extern fn(state: LuaState, s: CharBuf),
@@ -55,12 +61,35 @@ pub struct LuaSharedInterface {
5561
pub lua_pushvalue: extern fn(state: LuaState, idx: CInt),
5662
pub lua_pushcclosure: extern fn(state: LuaState, fnc: LuaCFunction, idx: CInt),
5763

64+
pub lua_checkstack: extern fn(state: LuaState, size: CInt, msg: CharBuf),
65+
66+
// Type Checks
67+
pub luaL_checkinteger: extern fn(state: LuaState, narg: CInt) -> LuaInteger,
68+
pub luaL_checknumber: extern fn(state: LuaState, narg: CInt) -> LuaNumber,
69+
pub luaL_checkstring: extern fn(state: LuaState, narg: CInt) -> CharBuf,
70+
pub luaL_checklstring: extern fn(state: LuaState, narg: CInt) -> CharBuf,
71+
pub luaL_checklong: extern fn(state: LuaState, narg: CInt) -> CLong,
72+
73+
// Type Checks that return nothing
74+
pub luaL_checkany: extern fn(state: LuaState, narg: CInt),
75+
pub luaL_checktype: extern fn(state: LuaState, narg: CInt, typeid: CInt),
76+
pub luaL_checkudata: extern fn(state: LuaState, narg: CInt, len: SizeT),
77+
pub luaL_argcheck: extern fn(state: LuaState, cond: CInt, narg: CInt, msg: CharBuf),
78+
5879
// Creation
5980
pub luaL_newstate: extern fn() -> LuaState,
6081
pub lua_createtable: extern fn(state: LuaState, narr: CInt, nrec: CInt),
6182

62-
// Raise Errors
63-
pub luaL_typerror: extern fn(state: LuaState, narg: CInt, typename: CharBuf) -> CInt
83+
// JIT
84+
// Returns 1 for success, 0 for failure
85+
pub luaJIT_setmode: extern fn(state: LuaState, idx: CInt, jit_mode: CInt) -> CInt,
86+
87+
// Coroutines / Yielding
88+
pub lua_yield: extern fn(state: LuaState, nresults: CInt) -> CInt,
89+
pub lua_status: extern fn(state: LuaState) -> CInt,
90+
91+
// Raising Errors
92+
pub luaL_typerror: extern fn(state: LuaState, narg: CInt, typename: CharBuf) -> CInt,
6493
}
6594

6695
// C++ Macros & Custom Functions
@@ -70,20 +99,24 @@ impl LuaSharedInterface {
7099
}
71100

72101
pub fn lua_isboolean(&self, state: LuaState, ind: CInt) -> bool {
73-
self.lua_type(state, ind) == luatypes::BOOL
102+
self.lua_type(state, ind) == Lua::Type::Bool as i32
74103
}
75104

76105
pub fn lua_setglobal(&self, state: LuaState, name: CharBuf) {
77-
self.lua_setfield(state, LUA_GLOBALSINDEX, name);
106+
self.lua_setfield(state, Lua::GLOBALSINDEX, name);
78107
}
79108

80109
pub fn lua_getglobal(&self, state: LuaState, name: CharBuf) {
81-
self.lua_getfield(state, LUA_GLOBALSINDEX, name);
110+
self.lua_getfield(state, Lua::GLOBALSINDEX, name);
82111
}
83112

84113
pub fn lua_pushcfunction(&self, state: LuaState, fnc: LuaCFunction) {
85114
self.lua_pushcclosure(state, fnc, 0);
86115
}
116+
117+
pub fn lua_tostring(&self, state: LuaState, idx: CInt) -> CharBuf {
118+
self.lua_tolstring(state, idx, 0)
119+
}
87120
}
88121

89122
// Global Static Stuff

src/types.rs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,11 @@
1-
21
pub type CVoid = std::ffi::c_void;
32
pub type SizeT = usize;
3+
44
pub type LuaNumber = f64; // All lua numbers are doubles in Lua 5.1 (Glua)
5+
pub type LuaInteger = isize;
6+
57
pub type LuaState = *mut CVoid; // Raw Lua state.
68
pub type CharBuf = *const i8; // const char*
79
pub type CInt = i32;
810
pub type LuaCFunction = extern "C" fn(LuaState) -> CInt;
9-
10-
pub mod luatypes {
11-
use super::CInt;
12-
pub static NONE: CInt = -1;
13-
pub static NIL: CInt = 0;
14-
pub static BOOL: CInt = 1;
15-
pub static LUSERDATA: CInt = 2;
16-
pub static NUMBER: CInt = 3;
17-
pub static STRING: CInt = 4;
18-
pub static TABLE: CInt = 5;
19-
pub static FUNCTION: CInt = 6;
20-
pub static USERDATA: CInt = 7;
21-
pub static THREAD: CInt = 8;
22-
}
11+
pub type CLong = i64;

0 commit comments

Comments
 (0)