Skip to content

Commit dfd82ed

Browse files
committed
Add Lua::push() helper
1 parent 75a15ce commit dfd82ed

File tree

3 files changed

+24
-19
lines changed

3 files changed

+24
-19
lines changed

src/lua.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,8 +1413,8 @@ impl Lua {
14131413
let protect = !self.unlikely_memory_error();
14141414
push_table(state, 0, lower_bound, protect)?;
14151415
for (k, v) in iter {
1416-
self.push_value(k.into_lua(self)?)?;
1417-
self.push_value(v.into_lua(self)?)?;
1416+
self.push(k)?;
1417+
self.push(v)?;
14181418
if protect {
14191419
protect_lua!(state, 3, 1, fn(state) ffi::lua_rawset(state, -3))?;
14201420
} else {
@@ -1442,7 +1442,7 @@ impl Lua {
14421442
let protect = !self.unlikely_memory_error();
14431443
push_table(state, lower_bound, 0, protect)?;
14441444
for (i, v) in iter.enumerate() {
1445-
self.push_value(v.into_lua(self)?)?;
1445+
self.push(v)?;
14461446
if protect {
14471447
protect_lua!(state, 2, 1, |state| {
14481448
ffi::lua_rawseti(state, -2, (i + 1) as Integer);
@@ -1977,12 +1977,11 @@ impl Lua {
19771977
T: IntoLua<'lua>,
19781978
{
19791979
let state = self.state();
1980-
let t = t.into_lua(self)?;
19811980
unsafe {
19821981
let _sg = StackGuard::new(state);
19831982
check_stack(state, 5)?;
19841983

1985-
self.push_value(t)?;
1984+
self.push(t)?;
19861985
rawset_field(state, ffi::LUA_REGISTRYINDEX, name)
19871986
}
19881987
}
@@ -2269,6 +2268,12 @@ impl Lua {
22692268
extra.app_data.remove()
22702269
}
22712270

2271+
#[doc(hidden)]
2272+
#[inline(always)]
2273+
pub unsafe fn push<'lua>(&'lua self, value: impl IntoLua<'lua>) -> Result<()> {
2274+
value.push_into_stack(self)
2275+
}
2276+
22722277
/// Pushes a value onto the Lua stack.
22732278
///
22742279
/// Uses 2 stack spaces, does not call checkstack.
@@ -2643,12 +2648,12 @@ impl Lua {
26432648
let metatable_nrec = metatable_nrec + registry.async_meta_methods.len();
26442649
push_table(state, 0, metatable_nrec, true)?;
26452650
for (k, m) in registry.meta_methods {
2646-
self.push_value(Value::Function(self.create_callback(m)?))?;
2651+
self.push(self.create_callback(m)?)?;
26472652
rawset_field(state, -2, MetaMethod::validate(&k)?)?;
26482653
}
26492654
#[cfg(feature = "async")]
26502655
for (k, m) in registry.async_meta_methods {
2651-
self.push_value(Value::Function(self.create_async_callback(m)?))?;
2656+
self.push(self.create_async_callback(m)?)?;
26522657
rawset_field(state, -2, MetaMethod::validate(&k)?)?;
26532658
}
26542659
let mut has_name = false;
@@ -2699,7 +2704,7 @@ impl Lua {
26992704
if field_getters_nrec > 0 {
27002705
push_table(state, 0, field_getters_nrec, true)?;
27012706
for (k, m) in registry.field_getters {
2702-
self.push_value(Value::Function(self.create_callback(m)?))?;
2707+
self.push(self.create_callback(m)?)?;
27032708
rawset_field(state, -2, &k)?;
27042709
}
27052710
field_getters_index = Some(ffi::lua_absindex(state, -1));
@@ -2711,7 +2716,7 @@ impl Lua {
27112716
if field_setters_nrec > 0 {
27122717
push_table(state, 0, field_setters_nrec, true)?;
27132718
for (k, m) in registry.field_setters {
2714-
self.push_value(Value::Function(self.create_callback(m)?))?;
2719+
self.push(self.create_callback(m)?)?;
27152720
rawset_field(state, -2, &k)?;
27162721
}
27172722
field_setters_index = Some(ffi::lua_absindex(state, -1));
@@ -2734,12 +2739,12 @@ impl Lua {
27342739
}
27352740
}
27362741
for (k, m) in registry.methods {
2737-
self.push_value(Value::Function(self.create_callback(m)?))?;
2742+
self.push(self.create_callback(m)?)?;
27382743
rawset_field(state, -2, &k)?;
27392744
}
27402745
#[cfg(feature = "async")]
27412746
for (k, m) in registry.async_methods {
2742-
self.push_value(Value::Function(self.create_async_callback(m)?))?;
2747+
self.push(self.create_async_callback(m)?)?;
27432748
rawset_field(state, -2, &k)?;
27442749
}
27452750
match index_type {
@@ -2990,7 +2995,7 @@ impl Lua {
29902995
nresults => {
29912996
let results = MultiValue::from_stack_multi(nresults, lua)?;
29922997
ffi::lua_pushinteger(state, nresults as _);
2993-
lua.push_value(Value::Table(lua.create_sequence_from(results)?))?;
2998+
lua.push(lua.create_sequence_from(results)?)?;
29942999
Ok(2)
29953000
}
29963001
}

src/scope.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::util::{
1919
self, assert_stack, check_stack, init_userdata_metatable, push_string, push_table,
2020
rawset_field, short_type_name, take_userdata, StackGuard,
2121
};
22-
use crate::value::{FromLua, FromLuaMulti, IntoLua, IntoLuaMulti, Value};
22+
use crate::value::{FromLua, FromLuaMulti, IntoLua, IntoLuaMulti};
2323

2424
#[cfg(feature = "lua54")]
2525
use crate::userdata::USER_VALUE_MAXSLOT;
@@ -405,7 +405,7 @@ impl<'lua, 'scope> Scope<'lua, 'scope> {
405405
let meta_methods_nrec = registry.meta_methods.len() + registry.meta_fields.len() + 1;
406406
push_table(state, 0, meta_methods_nrec, true)?;
407407
for (k, m) in registry.meta_methods {
408-
lua.push_value(Value::Function(wrap_method(self, ud_ptr, &k, m)?))?;
408+
lua.push(wrap_method(self, ud_ptr, &k, m)?)?;
409409
rawset_field(state, -2, MetaMethod::validate(&k)?)?;
410410
}
411411
let mut has_name = false;
@@ -455,7 +455,7 @@ impl<'lua, 'scope> Scope<'lua, 'scope> {
455455
if field_getters_nrec > 0 {
456456
push_table(state, 0, field_getters_nrec, true)?;
457457
for (k, m) in registry.field_getters {
458-
lua.push_value(Value::Function(wrap_method(self, ud_ptr, &k, m)?))?;
458+
lua.push(wrap_method(self, ud_ptr, &k, m)?)?;
459459
rawset_field(state, -2, &k)?;
460460
}
461461
field_getters_index = Some(ffi::lua_absindex(state, -1));
@@ -466,7 +466,7 @@ impl<'lua, 'scope> Scope<'lua, 'scope> {
466466
if field_setters_nrec > 0 {
467467
push_table(state, 0, field_setters_nrec, true)?;
468468
for (k, m) in registry.field_setters {
469-
lua.push_value(Value::Function(wrap_method(self, ud_ptr, &k, m)?))?;
469+
lua.push(wrap_method(self, ud_ptr, &k, m)?)?;
470470
rawset_field(state, -2, &k)?;
471471
}
472472
field_setters_index = Some(ffi::lua_absindex(state, -1));
@@ -478,7 +478,7 @@ impl<'lua, 'scope> Scope<'lua, 'scope> {
478478
// Create table used for methods lookup
479479
push_table(state, 0, methods_nrec, true)?;
480480
for (k, m) in registry.methods {
481-
lua.push_value(Value::Function(wrap_method(self, ud_ptr, &k, m)?))?;
481+
lua.push(wrap_method(self, ud_ptr, &k, m)?)?;
482482
rawset_field(state, -2, &k)?;
483483
}
484484
methods_index = Some(ffi::lua_absindex(state, -1));

src/userdata.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@ impl<'lua> AnyUserData<'lua> {
919919
check_stack(state, 5)?;
920920

921921
lua.push_userdata_ref(&self.0)?;
922-
lua.push_value(v.into_lua(lua)?)?;
922+
lua.push(v)?;
923923

924924
#[cfg(feature = "lua54")]
925925
if n < USER_VALUE_MAXSLOT {
@@ -1014,7 +1014,7 @@ impl<'lua> AnyUserData<'lua> {
10141014
check_stack(state, 5)?;
10151015

10161016
lua.push_userdata_ref(&self.0)?;
1017-
lua.push_value(v.into_lua(lua)?)?;
1017+
lua.push(v)?;
10181018

10191019
// Multiple (extra) user values are emulated by storing them in a table
10201020
protect_lua!(state, 2, 0, |state| {

0 commit comments

Comments
 (0)