Skip to content

Commit c86ce7f

Browse files
authored
Merge pull request #55 from thevurv/lua-api-2
Lua API 2.0
2 parents 2b92d88 + 82ace14 commit c86ce7f

File tree

29 files changed

+780
-668
lines changed

29 files changed

+780
-668
lines changed

packages/autorun-env/src/env.rs

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ macro_rules! wrap {
3434
// todo: potentially add a silenterror type so we can return that and it'll return a nil.
3535
// right now this would kind of leak the fact that it's an autorun function.
3636
lua.push(state, c"");
37-
lua.error(state);
37+
lua.raw.error(state);
3838
} else {
3939
$func(lua, state, env)
4040
}
@@ -44,7 +44,7 @@ macro_rules! wrap {
4444

4545
impl EnvHandle {
4646
pub fn push(&self, lua: &LuaApi, state: *mut LuaState) {
47-
self.env.push(lua, state);
47+
lua.push(state, &self.env);
4848
}
4949

5050
pub fn realm(&self) -> Realm {
@@ -57,25 +57,25 @@ impl EnvHandle {
5757
let lj_state = state as *mut LJState;
5858
let lj_state = unsafe { lj_state.as_ref().context("Failed to dereference LJState")? };
5959

60-
lua.get_fenv(state, func_index);
60+
lua.raw.getfenv(state, func_index);
6161
let function_env_tvalue = index2adr(lj_state, -1).context("Failed to get TValue for function environment")?;
6262
let function_env_gcr = unsafe { (*function_env_tvalue).gcr };
6363

64-
lua.pop(state, 1);
64+
lua.raw.pop(state, 1);
6565
Ok(function_env_gcr == self.env_gcr)
6666
}
6767

6868
pub fn is_active(&self, lua: &LuaApi, state: *mut LuaState) -> bool {
69-
if lua.get_info(state, 1, c"f").is_none() {
69+
if lua.raw.getinfo(state, 1, c"f").is_none() {
7070
// No function info available
7171
return false;
7272
}
7373

74-
lua.get_fenv(state, -1);
74+
lua.raw.getfenv(state, -1);
7575
self.push(lua, state);
7676

77-
let equal = lua.is_raw_equal(state, -1, -2);
78-
lua.pop(state, 3);
77+
let equal = lua.raw.rawequal(state, -1, -2);
78+
lua.raw.pop(state, 3);
7979

8080
equal
8181
}
@@ -117,16 +117,11 @@ impl EnvHandle {
117117

118118
pub fn execute(&self, lua: &LuaApi, state: *mut LuaState, name: &CStr, src: &[u8]) -> anyhow::Result<()> {
119119
let name = self.format_chunk_name(name)?;
120-
if let Err(why) = lua.load_buffer_x(state, src, &name, c"t") {
121-
anyhow::bail!("Failed to compile: {why}");
122-
}
123-
124-
self.push(lua, state);
125-
if lua.set_fenv(state, -2).is_err() {
126-
anyhow::bail!("Failed to set environment");
127-
}
120+
let chunk = lua.load(state, src, &name)?;
121+
lua.setfenv(state, &chunk, &self.env)?;
128122

129-
if let Err(why) = lua.pcall(state, 0, 0, 0) {
123+
lua.push(state, &chunk);
124+
if let Err(why) = lua.raw.pcall(state, 0, 0, 0) {
130125
anyhow::bail!("Failed to execute: {}", why);
131126
}
132127

@@ -141,7 +136,7 @@ impl EnvHandle {
141136
lua.set(state, &env, "_G", Globals);
142137

143138
// todo: refactor luajit code to not depend on the stack
144-
env.push(lua, state);
139+
lua.push(state, &env);
145140

146141
// Can unwrap since we are sure there is something on the stack
147142
let lj_state = state as *mut LJState;
@@ -150,7 +145,7 @@ impl EnvHandle {
150145
let env_gcr = unsafe { (*env_tvalue).gcr };
151146

152147
// todo: refactor luajit code to not depend on the stack
153-
lua.pop(state, 1);
148+
lua.raw.pop(state, 1);
154149

155150
let chunk_nonce = rand::random::<u64>();
156151
Ok(Self {
@@ -181,19 +176,19 @@ impl EnvHandle {
181176

182177
pub fn trigger(&self, lua: &LuaApi, state: *mut LuaState, event_name: &CStr, n_args: c_int) -> anyhow::Result<()> {
183178
lua.push(state, event_name);
184-
lua.insert(state, -(n_args + 1));
179+
lua.raw.insert(state, -(n_args + 1));
185180

186-
self.autorun.push(lua, state);
187-
lua.get_field(state, -1, c"trigger".as_ptr());
188-
lua.remove(state, -2); // remove Autorun table
181+
lua.push(state, &self.autorun);
182+
lua.raw.getfield(state, -1, c"trigger".as_ptr());
183+
lua.raw.remove(state, -2); // remove Autorun table
189184

190-
if lua.type_id(state, -1) != autorun_lua::LuaTypeId::Function {
191-
lua.pop(state, 1);
185+
if lua.raw.typeid(state, -1) != autorun_lua::LuaTypeId::Function {
186+
lua.raw.pop(state, 1);
192187
anyhow::bail!("don't remove Autorun.trigger lil bro.");
193188
}
194189

195-
lua.insert(state, -(n_args + 2));
196-
lua.pcall(state, n_args + 1, 0, 0).map_err(|e| anyhow::anyhow!(e))?;
190+
lua.raw.insert(state, -(n_args + 2));
191+
lua.raw.pcall(state, n_args + 1, 0, 0).map_err(|e| anyhow::anyhow!(e))?;
197192

198193
Ok(())
199194
}
@@ -206,19 +201,19 @@ impl EnvHandle {
206201
n_args: c_int,
207202
) -> anyhow::Result<()> {
208203
lua.push(state, event_name);
209-
lua.insert(state, -(n_args + 1));
204+
lua.raw.insert(state, -(n_args + 1));
210205

211-
self.autorun.push(lua, state);
212-
lua.get_field(state, -1, c"runRemoteCallbacks".as_ptr());
213-
lua.remove(state, -2); // remove Autorun table
206+
lua.push(state, &self.autorun);
207+
lua.raw.getfield(state, -1, c"runRemoteCallbacks".as_ptr());
208+
lua.raw.remove(state, -2); // remove Autorun table
214209

215-
if lua.type_id(state, -1) != autorun_lua::LuaTypeId::Function {
216-
lua.pop(state, 1);
210+
if lua.raw.typeid(state, -1) != autorun_lua::LuaTypeId::Function {
211+
lua.raw.pop(state, 1);
217212
anyhow::bail!("don't remove Autorun.runRemoteCallbacks lil bro.");
218213
}
219214

220-
lua.insert(state, -(n_args + 2));
221-
lua.pcall(state, n_args + 1, 0, 0).map_err(|e| anyhow::anyhow!(e))?;
215+
lua.raw.insert(state, -(n_args + 2));
216+
lua.raw.pcall(state, n_args + 1, 0, 0).map_err(|e| anyhow::anyhow!(e))?;
222217

223218
Ok(())
224219
}

packages/autorun-env/src/functions/append.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use autorun_lua::LuaApi;
44
use autorun_types::LuaState;
55

66
pub fn append(lua: &LuaApi, state: *mut LuaState, env: crate::EnvHandle) -> anyhow::Result<()> {
7-
let target_path = lua.check_string(state, 1);
8-
let content = lua.check_string(state, 2);
7+
let target_path = lua.raw.checkstring(state, 1);
8+
let content = lua.raw.checkstring(state, 2);
99

1010
let plugin = env
1111
.get_active_plugin(lua, state)

packages/autorun-env/src/functions/auth.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ use autorun_types::LuaState;
55

66
pub fn is_function_authorized(lua: &LuaApi, state: *mut LuaState, env: crate::EnvHandle) -> anyhow::Result<bool> {
77
if !matches!(
8-
lua.type_id(state, 1),
8+
lua.raw.typeid(state, 1),
99
autorun_lua::LuaTypeId::Function | autorun_lua::LuaTypeId::Number
1010
) {
1111
anyhow::bail!("First argument must be a function or stack level.");
1212
}
1313

14-
if lua.type_id(state, 1) == autorun_lua::LuaTypeId::Number {
14+
if lua.raw.typeid(state, 1) == autorun_lua::LuaTypeId::Number {
1515
// attempt to resolve the function at the given stack level
1616
let mut debug_info = unsafe { std::mem::zeroed::<DebugInfo>() };
1717
let stack_level = lua.to::<i32>(state, 1);
18-
lua.pop(state, 1); // remove the stack level argument
18+
lua.raw.pop(state, 1); // remove the stack level argument
1919

20-
if lua.get_stack(state, stack_level, &raw mut debug_info as _) == 0 {
20+
if lua.raw.getstack(state, stack_level, &raw mut debug_info as _) == 0 {
2121
anyhow::bail!("Invalid stack level provided.");
2222
}
2323

packages/autorun-env/src/functions/detour.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ use crate::functions::detour::handlers::{detour_handler, retour_handler};
66
use crate::functions::detour::raw::{make_detour_trampoline, make_retour_lua_trampoline};
77
use crate::functions::detour::userdata::Detour;
88
use anyhow::Context;
9-
use autorun_lua::{LuaApi, LuaFunction, LuaTypeId, RawHandle, RawLuaReturn};
9+
use autorun_lua::{LuaApi, LuaCFunction, LuaTypeId, RawHandle, RawLuaReturn};
1010
use autorun_luajit::{GCfunc, LJState, get_gcobj, get_gcobj_mut};
1111
use autorun_types::LuaState;
1212
use retour::GenericDetour;
1313
use std::ffi::c_int;
1414
pub use userdata::{detour_disable, detour_enable, detour_get_original, detour_remove};
1515

1616
pub fn detour(lua: &LuaApi, state: *mut LuaState, _env: crate::EnvHandle) -> anyhow::Result<Detour> {
17-
if lua.is_c_function(state, 1) == 0 {
17+
if !lua.raw.iscfunction(state, 1) {
1818
anyhow::bail!("First argument must be a C function to detour.");
1919
}
2020

@@ -27,12 +27,12 @@ pub fn detour(lua: &LuaApi, state: *mut LuaState, _env: crate::EnvHandle) -> any
2727
anyhow::bail!("Cannot detour a fast-function. Use copyFastFunction instead.");
2828
}
2929

30-
let target_function: LuaFunction = unsafe { std::mem::transmute(gcfunc.c.c) };
30+
let target_function: LuaCFunction = unsafe { std::mem::transmute(gcfunc.c.c) };
3131
if target_function as usize == 0 {
3232
anyhow::bail!("Target function pointer is null.");
3333
}
3434

35-
if lua.type_id(state, 2) != LuaTypeId::Function {
35+
if lua.raw.typeid(state, 2) != LuaTypeId::Function {
3636
anyhow::bail!("Second argument must be a function to use as detour.");
3737
}
3838

@@ -50,7 +50,7 @@ pub fn detour(lua: &LuaApi, state: *mut LuaState, _env: crate::EnvHandle) -> any
5050
let detour = unsafe {
5151
Box::new(GenericDetour::new(
5252
target_function,
53-
std::mem::transmute::<*const u8, LuaFunction>(detour_trampoline.as_ptr()),
53+
std::mem::transmute::<*const u8, LuaCFunction>(detour_trampoline.as_ptr()),
5454
)?)
5555
};
5656

@@ -61,7 +61,7 @@ pub fn detour(lua: &LuaApi, state: *mut LuaState, _env: crate::EnvHandle) -> any
6161
}
6262

6363
// create retour trampoline
64-
let retour_trampoline = make_retour_lua_trampoline(detour.as_ref() as *const GenericDetour<LuaFunction>, retour_handler)?;
64+
let retour_trampoline = make_retour_lua_trampoline(detour.as_ref() as *const GenericDetour<LuaCFunction>, retour_handler)?;
6565

6666
// link the original function pointer
6767
*original_function_ptr = retour_trampoline.as_ptr() as usize;
@@ -76,11 +76,11 @@ pub fn detour(lua: &LuaApi, state: *mut LuaState, _env: crate::EnvHandle) -> any
7676
}
7777

7878
pub fn copy_fast_function(lua: &LuaApi, state: *mut LuaState, _env: crate::EnvHandle) -> anyhow::Result<RawLuaReturn> {
79-
if lua.is_c_function(state, 1) == 0 {
79+
if !lua.raw.iscfunction(state, 1) {
8080
anyhow::bail!("First argument must be a C function to copy.");
8181
}
8282

83-
if lua.type_id(state, 2) != LuaTypeId::Function {
83+
if lua.raw.typeid(state, 2) != LuaTypeId::Function {
8484
anyhow::bail!("Second argument must be a function to use.");
8585
}
8686

@@ -102,9 +102,9 @@ pub fn copy_fast_function(lua: &LuaApi, state: *mut LuaState, _env: crate::EnvHa
102102

103103
// Push it as a closure to call
104104
unsafe {
105-
lua.push_closure(
105+
lua.raw.pushcclosure(
106106
state,
107-
std::mem::transmute::<*const u8, LuaFunction>(trampoline.as_ptr()),
107+
std::mem::transmute::<*const u8, LuaCFunction>(trampoline.as_ptr()),
108108
original_upvalues as c_int,
109109
);
110110
}
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
use crate::functions::detour::raw;
22
use autorun_log::*;
3-
use autorun_lua::{LUA_MULTRET, LuaApi, LuaFunction, RawHandle};
3+
use autorun_lua::{LUA_MULTRET, LuaApi, LuaCFunction, RawHandle};
44
use autorun_types::LuaState;
55

6-
pub extern "C-unwind" fn retour_handler(state: *mut LuaState, detour: *const retour::GenericDetour<LuaFunction>) -> i32 {
6+
pub extern "C-unwind" fn retour_handler(state: *mut LuaState, detour: *const retour::GenericDetour<LuaCFunction>) -> i32 {
77
unsafe { (*detour).call(state) }
88
}
99

1010
pub extern "C-unwind" fn detour_handler(
1111
state: *mut LuaState,
1212
metadata: i32,
1313
lua_api: *const LuaApi,
14-
original_function: *const LuaFunction,
14+
original_function: *const LuaCFunction,
1515
) -> i32 {
1616
let detour_metadata = raw::DetourMetadata::from_packed(metadata);
1717
let callback_id = detour_metadata.callback_ref();
1818
let lua = unsafe { &*lua_api };
1919

2020
let callback_handle = RawHandle::from_id(callback_id);
2121
callback_handle.push(lua, state);
22-
lua.insert(state, 1);
22+
lua.raw.insert(state, 1);
2323

24-
let num_arguments = lua.get_top(state) - 1;
24+
let num_arguments = lua.raw.gettop(state) - 1;
2525

2626
let original_function_included = if original_function as usize != 0 {
2727
// add the original function as the first argument
2828
unsafe {
29-
lua.push_function(state, *original_function);
30-
lua.insert(state, 2);
29+
lua.raw.pushcfunction(state, *original_function);
30+
lua.raw.insert(state, 2);
3131
}
3232

3333
true
@@ -41,12 +41,12 @@ pub extern "C-unwind" fn detour_handler(
4141
num_arguments
4242
};
4343

44-
let base = lua.get_top(state) - num_arguments;
44+
let base = lua.raw.gettop(state) - num_arguments;
4545

46-
if let Err(why) = lua.pcall(state, num_arguments, LUA_MULTRET, 0) {
46+
if let Err(why) = lua.raw.pcall(state, num_arguments, LUA_MULTRET, 0) {
4747
error!("Error calling detour callback: {why}");
4848
return 0;
4949
}
5050

51-
lua.get_top(state) - base + 1
51+
lua.raw.gettop(state) - base + 1
5252
}

packages/autorun-env/src/functions/detour/raw.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use autorun_jit::Function;
22
use autorun_jit::{Arg, CallingConvention, Jump};
3-
use autorun_lua::{LuaApi, LuaFunction, LuaState};
3+
use autorun_lua::{LuaApi, LuaCFunction, LuaState};
44
use std::ffi::c_int;
55

66
const CALLBACK_REF_BITS: u32 = 24;
@@ -42,9 +42,10 @@ type HandlerType = extern "C-unwind" fn(
4242
state: *mut LuaState,
4343
metadata: i32,
4444
lua: *const LuaApi,
45-
original_function: *const LuaFunction,
45+
original_function: *const LuaCFunction,
4646
) -> c_int;
47-
type RetourHandlerType = extern "C-unwind" fn(state: *mut LuaState, detour: *const retour::GenericDetour<LuaFunction>) -> c_int;
47+
type RetourHandlerType =
48+
extern "C-unwind" fn(state: *mut LuaState, detour: *const retour::GenericDetour<LuaCFunction>) -> c_int;
4849

4950
const TRAMPOLINE_SIZE: usize = 64;
5051

@@ -78,7 +79,7 @@ pub fn make_detour_trampoline(
7879
}
7980

8081
pub fn make_retour_lua_trampoline(
81-
detour_ptr: *const retour::GenericDetour<LuaFunction>,
82+
detour_ptr: *const retour::GenericDetour<LuaCFunction>,
8283
handler: RetourHandlerType,
8384
) -> anyhow::Result<Function> {
8485
let mut trampoline = Function::allocate(TRAMPOLINE_SIZE);

packages/autorun-env/src/functions/detour/userdata.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use anyhow::anyhow;
22
use autorun_jit::Function;
3-
use autorun_lua::{LuaApi, LuaFunction, LuaUserdata, RawHandle};
3+
use autorun_lua::{LuaApi, LuaCFunction, LuaUserdata, RawHandle};
44
use autorun_types::LuaState;
55

66
pub struct Detour {
7-
pub detour: Box<retour::GenericDetour<LuaFunction>>,
7+
pub detour: Box<retour::GenericDetour<LuaCFunction>>,
88
pub _detour_callback: RawHandle,
99
pub _detour_trampoline: Function,
1010
pub _retour_trampoline: Function,
@@ -35,11 +35,11 @@ pub fn detour_disable(lua: &LuaApi, state: *mut LuaState, _env: crate::EnvHandle
3535
Ok(())
3636
}
3737

38-
pub fn detour_get_original(lua: &LuaApi, state: *mut LuaState, _env: crate::EnvHandle) -> anyhow::Result<LuaFunction> {
38+
pub fn detour_get_original(lua: &LuaApi, state: *mut LuaState, _env: crate::EnvHandle) -> anyhow::Result<LuaCFunction> {
3939
let detour = lua.to::<*mut Detour>(state, 1);
4040
let detour = unsafe { detour.as_mut() }.ok_or(anyhow!("First argument must be a detour userdata"))?;
4141

42-
Ok(unsafe { std::mem::transmute::<usize, LuaFunction>(*detour.original_function_ptr) })
42+
Ok(unsafe { std::mem::transmute::<usize, LuaCFunction>(*detour.original_function_ptr) })
4343
}
4444

4545
pub fn detour_remove(lua: &LuaApi, state: *mut LuaState, _env: crate::EnvHandle) -> anyhow::Result<()> {

packages/autorun-env/src/functions/exists.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use autorun_lua::LuaApi;
22
use autorun_types::LuaState;
33

44
pub fn exists(lua: &LuaApi, state: *mut LuaState, env: crate::EnvHandle) -> anyhow::Result<bool> {
5-
let target_path = lua.check_string(state, 1);
5+
let target_path = lua.raw.checkstring(state, 1);
66
let plugin = env
77
.get_active_plugin(lua, state)
88
.ok_or(anyhow::anyhow!("dont delete autorun.plugin lil bro."))?;

packages/autorun-env/src/functions/load.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ use autorun_lua::{LuaApi, RawLuaReturn};
22
use autorun_types::LuaState;
33

44
pub fn load(lua: &LuaApi, state: *mut LuaState, env: crate::EnvHandle) -> anyhow::Result<RawLuaReturn> {
5-
let source = lua.check_string(state, 1);
5+
let source = lua.raw.checkstring(state, 1);
66
let chunk_name = lua.to::<Option<&[u8]>>(state, 2).unwrap_or(b"loadstring");
77
let chunk_name = std::ffi::CString::new(chunk_name)?;
88
let chunk_name = env.format_chunk_name(&chunk_name)?;
99

10-
if let Err(why) = lua.load_buffer_x(state, source.as_bytes(), &chunk_name, c"t") {
11-
lua.push_nil(state);
12-
lua.push(state, &why);
10+
if let Err(why) = lua.raw.loadbufferx(state, source.as_bytes(), &chunk_name, c"t") {
11+
lua.raw.pushnil(state);
12+
lua.push(state, why.to_string());
1313
return Ok(RawLuaReturn(2));
1414
}
1515

0 commit comments

Comments
 (0)