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

Commit d1f29c8

Browse files
committed
0.2.0
* Use inline functions instead of macros for lua_pop, lua_get/setglobal etc. * Deprecate cstring! in favor of the user manually converting their strings to pointers. * Types use std::ffi::raw now * Fix printgm from last commit.
1 parent 300064f commit d1f29c8

6 files changed

Lines changed: 143 additions & 147 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.1"
4+
version = "0.2.0"
55
authors = ["Vurv <vurvdevelops@gmail.com>"]
66
keywords = ["glua","garrysmod","lua"]
77
readme = "README.md"

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# rglua [![Release Shield](https://img.shields.io/github/v/release/Vurv78/rglua)](https://github.com/Vurv78/rglua/releases/latest) ![Linux Build Status](https://www.travis-ci.com/Vurv78/rglua.svg?branch=main) [![License](https://img.shields.io/github/license/Vurv78/rglua?color=red)](https://opensource.org/licenses/Apache-2.0) [![github/Vurv78](https://img.shields.io/discord/824727565948157963?color=7289DA&label=chat&logo=discord)](https://discord.gg/epJFC6cNsw)
22

3-
This is a crate that contains bindings for using the lua c api in garrysmod through bindings using rust-dlopen.
3+
This is a crate that contains bindings for using the lua c api in garrysmod through bindings using the rust libloading library.
44
Can be used for either binary modules or just manual injections into gmod, like with [Autorun-rs](https://github.com/Vurv78/Autorun-rs)
55

66
This works by finding a ``lua_shared.dll`` file relative to the currently running program, so you need to make sure your file is either in ``GarrysMod/bin/`` or ``GarrysMod/garrysmod/bin`` for srcds servers. The library will panic if the file is not found.
@@ -39,15 +39,14 @@ Also do this if you have never compiled to 32 bit, to get rustup to install 32 b
3939
## Example Module
4040
```rust
4141
use rglua::{
42-
types::LuaState,
42+
types::{ LuaState, CharBuf },
4343
cstring,
44-
lua_shared::*,
45-
lua_getglobal,
44+
lua_shared::*
4645
};
4746

4847
#[no_mangle]
4948
pub extern fn gmod13_open(state: LuaState) -> i32 {
50-
lua_getglobal!( state, cstring!("print") );
49+
lua_getglobal( state, b"print\0".as_ptr() as CharBuf );
5150
lua_pushstring( state, cstring!("Hello from rust!") );
5251
lua_call( state, 1, 0 );
5352
0

src/globals.rs

Lines changed: 81 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,99 @@
11
use crate::types::*;
22

33
pub mod Lua {
4-
use super::CInt;
4+
use super::CInt;
55

6-
pub static VERSION: &'static str = "Lua 5.1";
7-
pub static RELEASE: &'static str = "Lua 5.1.4";
8-
pub static VERSION_NUM: CInt = 501;
9-
pub static COPYRIGHT: &'static str = "Copyright (C) 1994-2008 Lua.org, PUC-Rio";
10-
pub static AUTHORS: &'static str = "R. Ierusalimschy, L. H. de Figueiredo & W. Celes";
6+
pub static VERSION: &'static str = "Lua 5.1";
7+
pub static RELEASE: &'static str = "Lua 5.1.4";
8+
pub static VERSION_NUM: CInt = 501;
9+
pub static COPYRIGHT: &'static str = "Copyright (C) 1994-2008 Lua.org, PUC-Rio";
10+
pub static AUTHORS: &'static str = "R. Ierusalimschy, L. H. de Figueiredo & W. Celes";
1111

12-
pub static REGISTRYINDEX: CInt = -10000;
13-
pub static ENVIRONINDEX: CInt = -10001;
14-
pub static GLOBALSINDEX: CInt = -10002;
12+
pub static REGISTRYINDEX: CInt = -10000;
13+
pub static ENVIRONINDEX: CInt = -10001;
14+
pub static GLOBALSINDEX: CInt = -10002;
1515

16-
pub static MULTRET: CInt = -1;
17-
pub static SIGNATURE: &'static str = "\x1bLua";
18-
pub static MINSTACK: CInt = 20;
16+
pub static MULTRET: CInt = -1;
17+
pub static SIGNATURE: &'static str = "\x1bLua";
18+
pub static MINSTACK: CInt = 20;
1919

20-
pub static NUMTYPES: CInt = 9;
21-
pub static NUMTAGS: CInt = NUMTYPES;
20+
pub static NUMTYPES: CInt = 9;
21+
pub static NUMTAGS: CInt = NUMTYPES;
2222

2323

24-
// Proper enums to use. Cast these to integers when using them
25-
pub enum Type {
26-
None = -1,
27-
Nil,
28-
Bool,
29-
LUserData,
30-
Number,
31-
String,
32-
Table,
33-
Function,
34-
UserData,
35-
Thread
36-
}
24+
// Proper enums to use. Cast these to integers when using them
25+
pub enum Type {
26+
None = -1,
27+
Nil,
28+
Bool,
29+
LUserData,
30+
Number,
31+
String,
32+
Table,
33+
Function,
34+
UserData,
35+
Thread
36+
}
3737

38-
pub enum Status {
39-
Ok = 0,
40-
Yield,
41-
ErrRun,
42-
ErrSyntax,
43-
ErrMem,
44-
ErrErr
45-
}
38+
pub enum Status {
39+
Ok = 0,
40+
Yield,
41+
ErrRun,
42+
ErrSyntax,
43+
ErrMem,
44+
ErrErr
45+
}
4646

47-
// Garbage collection
48-
pub enum Gc {
49-
Stop = 0,
50-
Restart,
51-
Collect,
52-
Count,
53-
CountB,
54-
Step,
55-
SetPause,
56-
SetStepMul,
57-
IsRunning,
58-
Gen,
59-
Inc // 11
60-
}
47+
// Garbage collection
48+
pub enum Gc {
49+
Stop = 0,
50+
Restart,
51+
Collect,
52+
Count,
53+
CountB,
54+
Step,
55+
SetPause,
56+
SetStepMul,
57+
IsRunning,
58+
Gen,
59+
Inc // 11
60+
}
6161

62-
// To be used with debug.sethook
63-
pub enum Hook {
64-
Call = 0,
65-
Ret,
66-
Line,
67-
Count,
68-
TailCall
69-
}
62+
// To be used with debug.sethook
63+
pub enum Hook {
64+
Call = 0,
65+
Ret,
66+
Line,
67+
Count,
68+
TailCall
69+
}
7070

71-
pub enum Mask {
72-
Call = (1 << Hook::Call as i32),
73-
Ret = (1 << Hook::Ret as i32),
74-
Line = (1 << Hook::Line as i32),
75-
Count = (1 << Hook::Count as i32)
76-
}
71+
pub enum Mask {
72+
Call = (1 << Hook::Call as i32),
73+
Ret = (1 << Hook::Ret as i32),
74+
Line = (1 << Hook::Line as i32),
75+
Count = (1 << Hook::Count as i32)
76+
}
7777
}
7878

7979
pub mod Jit {
80-
pub enum Mode {
81-
ENGINE,
82-
DEBUG,
83-
FUNC,
84-
ALLFUNC,
85-
ALLSUBFUNC,
86-
TRACE,
87-
WRAPCFUNC = 0x10,
88-
MAX,
89-
MASK = 0x0ff // LUAJIT_MODE_MASK
90-
}
80+
pub enum Mode {
81+
ENGINE,
82+
DEBUG,
83+
FUNC,
84+
ALLFUNC,
85+
ALLSUBFUNC,
86+
TRACE,
87+
WRAPCFUNC = 0x10,
88+
MAX,
89+
MASK = 0x0ff // LUAJIT_MODE_MASK
90+
}
9191

92-
use super::CInt;
93-
// Associated Constants, woah
94-
impl Mode {
95-
pub const OFF: CInt = 0x0000;
96-
pub const ON: CInt = 0x0100;
97-
pub const FLUSH: CInt = 0x0200;
98-
}
92+
use super::CInt;
93+
// Associated Constants, woah
94+
impl Mode {
95+
pub const OFF: CInt = 0x0000;
96+
pub const ON: CInt = 0x0100;
97+
pub const FLUSH: CInt = 0x0200;
98+
}
9999
}

src/helpers.rs

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,48 @@
11
#![allow(unused)]
22

33
// Get a const char* from a &str
4+
5+
#[deprecated(
6+
since = "0.2.0",
7+
note = "Use b\"string\".as_ptr() as std::os::raw::c_char format or CStrings directly instead."
8+
)]
49
#[macro_export]
510
macro_rules! cstring {
6-
($rstring:expr) => {
7-
{
8-
let v = std::ffi::CString::new($rstring);
9-
v.expect("Couldn't make CString from rust string").as_ptr()
10-
}
11-
}
11+
($rstring:expr) => {
12+
{
13+
let v = std::ffi::CString::new($rstring);
14+
v.expect("Couldn't make CString from rust string").as_ptr()
15+
}
16+
}
1217
}
1318

1419
// Get a rust &str from a const char*
1520
#[macro_export]
1621
macro_rules! rstring {
17-
($cstring:expr) => {
18-
{
19-
#[allow(unused_unsafe)]
20-
let cstr = unsafe{ std::ffi::CStr::from_ptr($cstring) };
21-
cstr.to_str().expect("Couldn't unwrap CString")
22-
}
23-
}
22+
($cstring:expr) => {
23+
{
24+
#[allow(unused_unsafe)]
25+
let cstr = unsafe{ std::ffi::CStr::from_ptr($cstring) };
26+
cstr.to_str().expect("Couldn't unwrap CString")
27+
}
28+
}
2429
}
2530

2631
#[allow(unused_macros)]
2732
#[macro_export]
2833
/// Like println!, however it prints to the gmod server's console.
34+
// First arg is the lua state.
35+
// Rest are varargs.
36+
// Can be either a variable storing a str literal, or a referenced String / str variable
2937
macro_rules! printgm {
30-
// First arg is the lua state.
31-
// Rest are varargs.
32-
// Can be either a variable storing a str literal, or a referenced String / str variable
33-
($state:expr, $($x:expr),*) => {
34-
{
35-
let stmt = format!( $($x,)* ); // Everything past the state will be as if it were inside a format! call.
36-
rglua::lua_getglobal!($state, rglua::cstring!("print") );
37-
rglua::lua_shared::lua_pushstring($state, rglua::cstring!(stmt) );
38-
// 1 arg, 0 results
39-
rglua::lua_shared::lua_call($state, 1, 0);
40-
}
41-
};
38+
($state:expr, $($x:expr),*) => {
39+
{
40+
let printargs = format!( $($x,)* );
41+
if let Ok(fmt) = std::ffi::CString::new(printargs) {
42+
rglua::lua_shared::lua_getglobal( $state, b"print".as_ptr() as *const std::os::raw::c_char );
43+
rglua::lua_shared::lua_pushstring( $state, fmt.as_ptr() );
44+
rglua::lua_shared::lua_call( $state, 1, 0 );
45+
}
46+
}
47+
};
4248
}

src/lua_shared.rs

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ macro_rules! expose_symbol {
5252
};
5353
}
5454

55-
// dlopen raw lua_shared.dll library. Don't use this unless you know what you're doing.
5655
pub const LUA_SHARED_RAW: Lazy<Library> = Lazy::new(|| {
5756
let path = LUA_SHARED_PATH.as_ref().expect("Couldn't find lua_shared.dll!");
5857
unsafe { Library::new(path).expect("Could not open library") }
@@ -151,44 +150,32 @@ expose_symbol!( lua_rawequal, CInt, (state: LuaState, ind1: CInt, ind2: CInt) );
151150
// Raising Errors
152151
expose_symbol!( luaL_typerror, CInt, (state: LuaState, narg: CInt, typename: CharBuf) );
153152

154-
#[macro_export]
155-
macro_rules! lua_pop {
156-
($state:expr, $ind:literal) => {
157-
rglua::lua_shared::lua_settop( $state, -($ind)-1 );
158-
}
153+
#[inline(always)]
154+
pub fn lua_pop(state: LuaState, ind: CInt) {
155+
lua_settop( state, -(ind)-1 );
159156
}
160157

161-
#[macro_export]
162-
macro_rules! lua_getglobal {
163-
($state:expr, $name:expr) => {
164-
rglua::lua_shared::lua_getfield($state, rglua::globals::Lua::GLOBALSINDEX, $name);
165-
}
158+
#[inline(always)]
159+
pub fn lua_getglobal(state: LuaState, name: CharBuf) {
160+
lua_getfield(state, GLOBALSINDEX, name);
166161
}
167162

168-
#[macro_export]
169-
macro_rules! lua_setglobal {
170-
($state:expr, $name:expr) => {
171-
rglua::lua_shared::lua_setfield($state, rglua::globals::Lua::GLOBALSINDEX, $name);
172-
}
163+
#[inline(always)]
164+
pub fn lua_setglobal(state: LuaState, name: CharBuf) {
165+
lua_setfield(state, GLOBALSINDEX, name);
173166
}
174167

175-
#[macro_export]
176-
macro_rules! lua_pushcfunction {
177-
($state:expr, $fnc:expr) => {
178-
rglua::lua_shared::lua_pushcclosure($state, $fnc, 0);
179-
}
168+
#[inline(always)]
169+
pub fn lua_pushcfunction(state: LuaState, fnc: LuaCFunction) {
170+
lua_pushcclosure(state, fnc, 0);
180171
}
181172

182-
#[macro_export]
183-
macro_rules! lua_tostring {
184-
($state:expr, $idx:expr) => {
185-
rglua::lua_shared::lua_tolstring($state, $idx, 0)
186-
}
173+
#[inline(always)]
174+
pub fn lua_tostring(state: LuaState, idx: CInt) -> CharBuf {
175+
lua_tolstring(state, idx, 0)
187176
}
188177

189-
#[macro_export]
190-
macro_rules! lua_resume {
191-
($state:expr, $narg:expr) => {
192-
rglua::lua_shared::lua_resume_real($state, $narg)
193-
}
178+
#[inline(always)]
179+
pub fn lua_resume(state: LuaState, narg: CInt) -> CInt {
180+
lua_resume_real(state, narg)
194181
}

src/types.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
pub type CVoid = std::ffi::c_void;
1+
use std::os::raw as ffi;
2+
3+
// C FFI Types
4+
pub type CVoid = ffi::c_void;
5+
pub type CInt = ffi::c_int;
6+
pub type CLong = ffi::c_long;
7+
pub type CharBuf = *const ffi::c_char; //*const i8; // const char*
28
pub type SizeT = usize;
39

10+
// Lua Types below
411
pub type LuaNumber = f64; // All lua numbers are doubles in Lua 5.1 (Glua)
512
pub type LuaInteger = isize;
613

714
pub type LuaState = *mut CVoid; // Raw Lua state.
8-
pub type CharBuf = *const i8; // const char*
9-
pub type CInt = i32;
1015
pub type LuaCFunction = extern "C" fn(LuaState) -> CInt;
11-
pub type CLong = i64;

0 commit comments

Comments
 (0)