Skip to content

Commit

Permalink
Merge branch 'no-yield-deferred-fibers' into 'master'
Browse files Browse the repository at this point in the history
feat(fiber): implement deferred fibers without yields

Closes #14

See merge request picodata/brod/tarantool-module!47
  • Loading branch information
gmoshkin committed Nov 1, 2021
2 parents 3851530 + 5cbe60e commit 71cb477
Show file tree
Hide file tree
Showing 8 changed files with 818 additions and 84 deletions.
348 changes: 303 additions & 45 deletions hlua/ffi/src/lib.rs

Large diffs are not rendered by default.

27 changes: 15 additions & 12 deletions hlua/hlua/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,12 @@ pub use lua_tables::LuaTable;
pub use lua_tables::LuaTableIterator;
pub use tuples::TuplePushError;
pub use userdata::UserdataOnStack;
pub use userdata::{push_userdata, read_userdata};
pub use userdata::{push_userdata, read_userdata, push_some_userdata};
pub use values::StringInLua;

// Needed for `lua_error` macro
pub use ffi::luaL_error;

mod any;
mod functions_write;
mod lua_functions;
Expand Down Expand Up @@ -369,7 +372,7 @@ pub trait PushOne<L>: Push<L> {}

/// Type that cannot be instantiated.
///
/// Will be replaced with `!` eventually (https://github.com/rust-lang/rust/issues/35121).
/// Will be replaced with `!` eventually (<https://github.com/rust-lang/rust/issues/35121>).
#[derive(Debug, Copy, Clone)]
pub enum Void {}

Expand Down Expand Up @@ -520,7 +523,7 @@ impl<'lua> Lua<'lua> {
/// Opens all standard Lua libraries.
///
/// See the reference for the standard library here:
/// https://www.lua.org/manual/5.2/manual.html#6
/// <https://www.lua.org/manual/5.2/manual.html#6>
///
/// This is done by calling `luaL_openlibs`.
///
Expand All @@ -538,71 +541,71 @@ impl<'lua> Lua<'lua> {

/// Opens base library.
///
/// https://www.lua.org/manual/5.2/manual.html#pdf-luaopen_base
/// <https://www.lua.org/manual/5.2/manual.html#pdf-luaopen_base>
#[inline]
pub fn open_base(&mut self) {
unsafe { ffi::luaopen_base(self.lua.0) }
}

/// Opens bit32 library.
///
/// https://www.lua.org/manual/5.2/manual.html#pdf-luaopen_bit32
/// <https://www.lua.org/manual/5.2/manual.html#pdf-luaopen_bit32>
#[inline]
pub fn open_bit(&mut self) {
unsafe { ffi::luaopen_bit(self.lua.0) }
}

/// Opens debug library.
///
/// https://www.lua.org/manual/5.2/manual.html#pdf-luaopen_debug
/// <https://www.lua.org/manual/5.2/manual.html#pdf-luaopen_debug>
#[inline]
pub fn open_debug(&mut self) {
unsafe { ffi::luaopen_debug(self.lua.0) }
}

/// Opens io library.
///
/// https://www.lua.org/manual/5.2/manual.html#pdf-luaopen_io
/// <https://www.lua.org/manual/5.2/manual.html#pdf-luaopen_io>
#[inline]
pub fn open_io(&mut self) {
unsafe { ffi::luaopen_io(self.lua.0) }
}

/// Opens math library.
///
/// https://www.lua.org/manual/5.2/manual.html#pdf-luaopen_math
/// <https://www.lua.org/manual/5.2/manual.html#pdf-luaopen_math>
#[inline]
pub fn open_math(&mut self) {
unsafe { ffi::luaopen_math(self.lua.0) }
}

/// Opens os library.
///
/// https://www.lua.org/manual/5.2/manual.html#pdf-luaopen_os
/// <https://www.lua.org/manual/5.2/manual.html#pdf-luaopen_os>
#[inline]
pub fn open_os(&mut self) {
unsafe { ffi::luaopen_os(self.lua.0) }
}

/// Opens package library.
///
/// https://www.lua.org/manual/5.2/manual.html#pdf-luaopen_package
/// <https://www.lua.org/manual/5.2/manual.html#pdf-luaopen_package>
#[inline]
pub fn open_package(&mut self) {
unsafe { ffi::luaopen_package(self.lua.0) }
}

/// Opens string library.
///
/// https://www.lua.org/manual/5.2/manual.html#pdf-luaopen_string
/// <https://www.lua.org/manual/5.2/manual.html#pdf-luaopen_string>
#[inline]
pub fn open_string(&mut self) {
unsafe { ffi::luaopen_string(self.lua.0) }
}

/// Opens table library.
///
/// https://www.lua.org/manual/5.2/manual.html#pdf-luaopen_table
/// <https://www.lua.org/manual/5.2/manual.html#pdf-luaopen_table>
#[inline]
pub fn open_table(&mut self) {
unsafe { ffi::luaopen_table(self.lua.0) }
Expand Down
18 changes: 18 additions & 0 deletions hlua/hlua/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,21 @@ macro_rules! implement_lua_read {
}
};
}

#[macro_export]
macro_rules! c_ptr {
($s:literal) => {
::std::concat!($s, "\0").as_bytes().as_ptr() as *mut i8
};
}

#[macro_export]
macro_rules! lua_error {
($l:expr, $msg:literal) => {
{
$crate::luaL_error($l, c_ptr!($msg));
unreachable!()
}
}
}

37 changes: 37 additions & 0 deletions hlua/hlua/src/userdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,45 @@ use crate::{
LuaRead,
InsideCallback,
LuaTable,
c_ptr,
};

/// Pushes `value` of type `T` onto the stack as a userdata. The value is
/// put inside a `Option` so that it can be safely moved out of there. Useful
/// for example when passing `FnOnce` as a c closure, because it must be dropped
/// after the call.
/// *[0, +1, -]*
pub unsafe fn push_some_userdata<T>(lua: *mut ffi::lua_State, value: T) {
type UDBox<T> = Option<T>;
let ud_ptr = ffi::lua_newuserdata(lua, std::mem::size_of::<UDBox<T>>());
std::ptr::write(ud_ptr as *mut UDBox<T>, Some(value));

if std::mem::needs_drop::<T>() {
// Creating a metatable.
ffi::lua_newtable(lua);

// Index "__gc" in the metatable calls the object's destructor.
ffi::lua_pushstring(lua, c_ptr!("__gc"));
ffi::lua_pushcfunction(lua, wrap_gc::<T>);
ffi::lua_settable(lua, -3);

ffi::lua_setmetatable(lua, -2);
}

/// A callback for the "__gc" event. It checks if the value was moved out
/// and if not it drops the value.
unsafe extern "C" fn wrap_gc<T>(lua: *mut ffi::lua_State) -> i32 {
let ud_ptr = ffi::lua_touserdata(lua, 1);
let ud = (ud_ptr as *mut UDBox<T>)
.as_mut()
.expect("__gc called with userdata pointing to NULL");
drop(ud.take());

0
}
}


// Called when an object inside Lua is being dropped.
#[inline]
extern "C" fn destructor_wrapper<T>(lua: *mut ffi::lua_State) -> libc::c_int {
Expand Down
10 changes: 10 additions & 0 deletions tarantool/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use rmp::decode::{MarkerReadError, NumValueReadError, ValueReadError};
use rmp::encode::ValueWriteError;

use crate::ffi::tarantool as ffi;
use crate::hlua::LuaError;

/// A specialized [`Result`] type for the crate
pub type Result<T> = std::result::Result<T, Error>;
Expand Down Expand Up @@ -71,6 +72,9 @@ pub enum Error {
#[cfg(feature = "net_box")]
#[fail(display = "Sever respond with error: {}", _0)]
Remote(crate::net_box::ResponseError),

#[fail(display = "Lua error: {}", _0)]
LuaError(LuaError),
}

impl From<io::Error> for Error {
Expand Down Expand Up @@ -142,6 +146,12 @@ impl From<crate::net_box::ResponseError> for Error {
}
}

impl From<LuaError> for Error {
fn from(error: LuaError) -> Self {
Error::LuaError(error)
}
}

/// Transaction-related error cases
#[derive(Debug, Fail)]
pub enum TransactionError {
Expand Down
Loading

0 comments on commit 71cb477

Please sign in to comment.