mirror of
https://github.com/mlua-rs/mlua
synced 2026-06-08 16:05:43 +00:00
Add Lua::push() helper
This commit is contained in:
+17
-12
@@ -1413,8 +1413,8 @@ impl Lua {
|
||||
let protect = !self.unlikely_memory_error();
|
||||
push_table(state, 0, lower_bound, protect)?;
|
||||
for (k, v) in iter {
|
||||
self.push_value(k.into_lua(self)?)?;
|
||||
self.push_value(v.into_lua(self)?)?;
|
||||
self.push(k)?;
|
||||
self.push(v)?;
|
||||
if protect {
|
||||
protect_lua!(state, 3, 1, fn(state) ffi::lua_rawset(state, -3))?;
|
||||
} else {
|
||||
@@ -1442,7 +1442,7 @@ impl Lua {
|
||||
let protect = !self.unlikely_memory_error();
|
||||
push_table(state, lower_bound, 0, protect)?;
|
||||
for (i, v) in iter.enumerate() {
|
||||
self.push_value(v.into_lua(self)?)?;
|
||||
self.push(v)?;
|
||||
if protect {
|
||||
protect_lua!(state, 2, 1, |state| {
|
||||
ffi::lua_rawseti(state, -2, (i + 1) as Integer);
|
||||
@@ -1977,12 +1977,11 @@ impl Lua {
|
||||
T: IntoLua<'lua>,
|
||||
{
|
||||
let state = self.state();
|
||||
let t = t.into_lua(self)?;
|
||||
unsafe {
|
||||
let _sg = StackGuard::new(state);
|
||||
check_stack(state, 5)?;
|
||||
|
||||
self.push_value(t)?;
|
||||
self.push(t)?;
|
||||
rawset_field(state, ffi::LUA_REGISTRYINDEX, name)
|
||||
}
|
||||
}
|
||||
@@ -2269,6 +2268,12 @@ impl Lua {
|
||||
extra.app_data.remove()
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[inline(always)]
|
||||
pub unsafe fn push<'lua>(&'lua self, value: impl IntoLua<'lua>) -> Result<()> {
|
||||
value.push_into_stack(self)
|
||||
}
|
||||
|
||||
/// Pushes a value onto the Lua stack.
|
||||
///
|
||||
/// Uses 2 stack spaces, does not call checkstack.
|
||||
@@ -2643,12 +2648,12 @@ impl Lua {
|
||||
let metatable_nrec = metatable_nrec + registry.async_meta_methods.len();
|
||||
push_table(state, 0, metatable_nrec, true)?;
|
||||
for (k, m) in registry.meta_methods {
|
||||
self.push_value(Value::Function(self.create_callback(m)?))?;
|
||||
self.push(self.create_callback(m)?)?;
|
||||
rawset_field(state, -2, MetaMethod::validate(&k)?)?;
|
||||
}
|
||||
#[cfg(feature = "async")]
|
||||
for (k, m) in registry.async_meta_methods {
|
||||
self.push_value(Value::Function(self.create_async_callback(m)?))?;
|
||||
self.push(self.create_async_callback(m)?)?;
|
||||
rawset_field(state, -2, MetaMethod::validate(&k)?)?;
|
||||
}
|
||||
let mut has_name = false;
|
||||
@@ -2699,7 +2704,7 @@ impl Lua {
|
||||
if field_getters_nrec > 0 {
|
||||
push_table(state, 0, field_getters_nrec, true)?;
|
||||
for (k, m) in registry.field_getters {
|
||||
self.push_value(Value::Function(self.create_callback(m)?))?;
|
||||
self.push(self.create_callback(m)?)?;
|
||||
rawset_field(state, -2, &k)?;
|
||||
}
|
||||
field_getters_index = Some(ffi::lua_absindex(state, -1));
|
||||
@@ -2711,7 +2716,7 @@ impl Lua {
|
||||
if field_setters_nrec > 0 {
|
||||
push_table(state, 0, field_setters_nrec, true)?;
|
||||
for (k, m) in registry.field_setters {
|
||||
self.push_value(Value::Function(self.create_callback(m)?))?;
|
||||
self.push(self.create_callback(m)?)?;
|
||||
rawset_field(state, -2, &k)?;
|
||||
}
|
||||
field_setters_index = Some(ffi::lua_absindex(state, -1));
|
||||
@@ -2734,12 +2739,12 @@ impl Lua {
|
||||
}
|
||||
}
|
||||
for (k, m) in registry.methods {
|
||||
self.push_value(Value::Function(self.create_callback(m)?))?;
|
||||
self.push(self.create_callback(m)?)?;
|
||||
rawset_field(state, -2, &k)?;
|
||||
}
|
||||
#[cfg(feature = "async")]
|
||||
for (k, m) in registry.async_methods {
|
||||
self.push_value(Value::Function(self.create_async_callback(m)?))?;
|
||||
self.push(self.create_async_callback(m)?)?;
|
||||
rawset_field(state, -2, &k)?;
|
||||
}
|
||||
match index_type {
|
||||
@@ -2990,7 +2995,7 @@ impl Lua {
|
||||
nresults => {
|
||||
let results = MultiValue::from_stack_multi(nresults, lua)?;
|
||||
ffi::lua_pushinteger(state, nresults as _);
|
||||
lua.push_value(Value::Table(lua.create_sequence_from(results)?))?;
|
||||
lua.push(lua.create_sequence_from(results)?)?;
|
||||
Ok(2)
|
||||
}
|
||||
}
|
||||
|
||||
+5
-5
@@ -19,7 +19,7 @@ use crate::util::{
|
||||
self, assert_stack, check_stack, init_userdata_metatable, push_string, push_table,
|
||||
rawset_field, short_type_name, take_userdata, StackGuard,
|
||||
};
|
||||
use crate::value::{FromLua, FromLuaMulti, IntoLua, IntoLuaMulti, Value};
|
||||
use crate::value::{FromLua, FromLuaMulti, IntoLua, IntoLuaMulti};
|
||||
|
||||
#[cfg(feature = "lua54")]
|
||||
use crate::userdata::USER_VALUE_MAXSLOT;
|
||||
@@ -405,7 +405,7 @@ impl<'lua, 'scope> Scope<'lua, 'scope> {
|
||||
let meta_methods_nrec = registry.meta_methods.len() + registry.meta_fields.len() + 1;
|
||||
push_table(state, 0, meta_methods_nrec, true)?;
|
||||
for (k, m) in registry.meta_methods {
|
||||
lua.push_value(Value::Function(wrap_method(self, ud_ptr, &k, m)?))?;
|
||||
lua.push(wrap_method(self, ud_ptr, &k, m)?)?;
|
||||
rawset_field(state, -2, MetaMethod::validate(&k)?)?;
|
||||
}
|
||||
let mut has_name = false;
|
||||
@@ -455,7 +455,7 @@ impl<'lua, 'scope> Scope<'lua, 'scope> {
|
||||
if field_getters_nrec > 0 {
|
||||
push_table(state, 0, field_getters_nrec, true)?;
|
||||
for (k, m) in registry.field_getters {
|
||||
lua.push_value(Value::Function(wrap_method(self, ud_ptr, &k, m)?))?;
|
||||
lua.push(wrap_method(self, ud_ptr, &k, m)?)?;
|
||||
rawset_field(state, -2, &k)?;
|
||||
}
|
||||
field_getters_index = Some(ffi::lua_absindex(state, -1));
|
||||
@@ -466,7 +466,7 @@ impl<'lua, 'scope> Scope<'lua, 'scope> {
|
||||
if field_setters_nrec > 0 {
|
||||
push_table(state, 0, field_setters_nrec, true)?;
|
||||
for (k, m) in registry.field_setters {
|
||||
lua.push_value(Value::Function(wrap_method(self, ud_ptr, &k, m)?))?;
|
||||
lua.push(wrap_method(self, ud_ptr, &k, m)?)?;
|
||||
rawset_field(state, -2, &k)?;
|
||||
}
|
||||
field_setters_index = Some(ffi::lua_absindex(state, -1));
|
||||
@@ -478,7 +478,7 @@ impl<'lua, 'scope> Scope<'lua, 'scope> {
|
||||
// Create table used for methods lookup
|
||||
push_table(state, 0, methods_nrec, true)?;
|
||||
for (k, m) in registry.methods {
|
||||
lua.push_value(Value::Function(wrap_method(self, ud_ptr, &k, m)?))?;
|
||||
lua.push(wrap_method(self, ud_ptr, &k, m)?)?;
|
||||
rawset_field(state, -2, &k)?;
|
||||
}
|
||||
methods_index = Some(ffi::lua_absindex(state, -1));
|
||||
|
||||
+2
-2
@@ -919,7 +919,7 @@ impl<'lua> AnyUserData<'lua> {
|
||||
check_stack(state, 5)?;
|
||||
|
||||
lua.push_userdata_ref(&self.0)?;
|
||||
lua.push_value(v.into_lua(lua)?)?;
|
||||
lua.push(v)?;
|
||||
|
||||
#[cfg(feature = "lua54")]
|
||||
if n < USER_VALUE_MAXSLOT {
|
||||
@@ -1014,7 +1014,7 @@ impl<'lua> AnyUserData<'lua> {
|
||||
check_stack(state, 5)?;
|
||||
|
||||
lua.push_userdata_ref(&self.0)?;
|
||||
lua.push_value(v.into_lua(lua)?)?;
|
||||
lua.push(v)?;
|
||||
|
||||
// Multiple (extra) user values are emulated by storing them in a table
|
||||
protect_lua!(state, 2, 0, |state| {
|
||||
|
||||
Reference in New Issue
Block a user