Skip to content

Commit 71cb477

Browse files
committed
Merge branch 'no-yield-deferred-fibers' into 'master'
feat(fiber): implement deferred fibers without yields Closes #14 See merge request picodata/brod/tarantool-module!47
2 parents 3851530 + 5cbe60e commit 71cb477

File tree

8 files changed

+818
-84
lines changed

8 files changed

+818
-84
lines changed

hlua/ffi/src/lib.rs

Lines changed: 303 additions & 45 deletions
Large diffs are not rendered by default.

hlua/hlua/src/lib.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,12 @@ pub use lua_tables::LuaTable;
125125
pub use lua_tables::LuaTableIterator;
126126
pub use tuples::TuplePushError;
127127
pub use userdata::UserdataOnStack;
128-
pub use userdata::{push_userdata, read_userdata};
128+
pub use userdata::{push_userdata, read_userdata, push_some_userdata};
129129
pub use values::StringInLua;
130130

131+
// Needed for `lua_error` macro
132+
pub use ffi::luaL_error;
133+
131134
mod any;
132135
mod functions_write;
133136
mod lua_functions;
@@ -369,7 +372,7 @@ pub trait PushOne<L>: Push<L> {}
369372

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

@@ -520,7 +523,7 @@ impl<'lua> Lua<'lua> {
520523
/// Opens all standard Lua libraries.
521524
///
522525
/// See the reference for the standard library here:
523-
/// https://www.lua.org/manual/5.2/manual.html#6
526+
/// <https://www.lua.org/manual/5.2/manual.html#6>
524527
///
525528
/// This is done by calling `luaL_openlibs`.
526529
///
@@ -538,71 +541,71 @@ impl<'lua> Lua<'lua> {
538541

539542
/// Opens base library.
540543
///
541-
/// https://www.lua.org/manual/5.2/manual.html#pdf-luaopen_base
544+
/// <https://www.lua.org/manual/5.2/manual.html#pdf-luaopen_base>
542545
#[inline]
543546
pub fn open_base(&mut self) {
544547
unsafe { ffi::luaopen_base(self.lua.0) }
545548
}
546549

547550
/// Opens bit32 library.
548551
///
549-
/// https://www.lua.org/manual/5.2/manual.html#pdf-luaopen_bit32
552+
/// <https://www.lua.org/manual/5.2/manual.html#pdf-luaopen_bit32>
550553
#[inline]
551554
pub fn open_bit(&mut self) {
552555
unsafe { ffi::luaopen_bit(self.lua.0) }
553556
}
554557

555558
/// Opens debug library.
556559
///
557-
/// https://www.lua.org/manual/5.2/manual.html#pdf-luaopen_debug
560+
/// <https://www.lua.org/manual/5.2/manual.html#pdf-luaopen_debug>
558561
#[inline]
559562
pub fn open_debug(&mut self) {
560563
unsafe { ffi::luaopen_debug(self.lua.0) }
561564
}
562565

563566
/// Opens io library.
564567
///
565-
/// https://www.lua.org/manual/5.2/manual.html#pdf-luaopen_io
568+
/// <https://www.lua.org/manual/5.2/manual.html#pdf-luaopen_io>
566569
#[inline]
567570
pub fn open_io(&mut self) {
568571
unsafe { ffi::luaopen_io(self.lua.0) }
569572
}
570573

571574
/// Opens math library.
572575
///
573-
/// https://www.lua.org/manual/5.2/manual.html#pdf-luaopen_math
576+
/// <https://www.lua.org/manual/5.2/manual.html#pdf-luaopen_math>
574577
#[inline]
575578
pub fn open_math(&mut self) {
576579
unsafe { ffi::luaopen_math(self.lua.0) }
577580
}
578581

579582
/// Opens os library.
580583
///
581-
/// https://www.lua.org/manual/5.2/manual.html#pdf-luaopen_os
584+
/// <https://www.lua.org/manual/5.2/manual.html#pdf-luaopen_os>
582585
#[inline]
583586
pub fn open_os(&mut self) {
584587
unsafe { ffi::luaopen_os(self.lua.0) }
585588
}
586589

587590
/// Opens package library.
588591
///
589-
/// https://www.lua.org/manual/5.2/manual.html#pdf-luaopen_package
592+
/// <https://www.lua.org/manual/5.2/manual.html#pdf-luaopen_package>
590593
#[inline]
591594
pub fn open_package(&mut self) {
592595
unsafe { ffi::luaopen_package(self.lua.0) }
593596
}
594597

595598
/// Opens string library.
596599
///
597-
/// https://www.lua.org/manual/5.2/manual.html#pdf-luaopen_string
600+
/// <https://www.lua.org/manual/5.2/manual.html#pdf-luaopen_string>
598601
#[inline]
599602
pub fn open_string(&mut self) {
600603
unsafe { ffi::luaopen_string(self.lua.0) }
601604
}
602605

603606
/// Opens table library.
604607
///
605-
/// https://www.lua.org/manual/5.2/manual.html#pdf-luaopen_table
608+
/// <https://www.lua.org/manual/5.2/manual.html#pdf-luaopen_table>
606609
#[inline]
607610
pub fn open_table(&mut self) {
608611
unsafe { ffi::luaopen_table(self.lua.0) }

hlua/hlua/src/macros.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,21 @@ macro_rules! implement_lua_read {
6060
}
6161
};
6262
}
63+
64+
#[macro_export]
65+
macro_rules! c_ptr {
66+
($s:literal) => {
67+
::std::concat!($s, "\0").as_bytes().as_ptr() as *mut i8
68+
};
69+
}
70+
71+
#[macro_export]
72+
macro_rules! lua_error {
73+
($l:expr, $msg:literal) => {
74+
{
75+
$crate::luaL_error($l, c_ptr!($msg));
76+
unreachable!()
77+
}
78+
}
79+
}
80+

hlua/hlua/src/userdata.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,45 @@ use crate::{
1313
LuaRead,
1414
InsideCallback,
1515
LuaTable,
16+
c_ptr,
1617
};
1718

19+
/// Pushes `value` of type `T` onto the stack as a userdata. The value is
20+
/// put inside a `Option` so that it can be safely moved out of there. Useful
21+
/// for example when passing `FnOnce` as a c closure, because it must be dropped
22+
/// after the call.
23+
/// *[0, +1, -]*
24+
pub unsafe fn push_some_userdata<T>(lua: *mut ffi::lua_State, value: T) {
25+
type UDBox<T> = Option<T>;
26+
let ud_ptr = ffi::lua_newuserdata(lua, std::mem::size_of::<UDBox<T>>());
27+
std::ptr::write(ud_ptr as *mut UDBox<T>, Some(value));
28+
29+
if std::mem::needs_drop::<T>() {
30+
// Creating a metatable.
31+
ffi::lua_newtable(lua);
32+
33+
// Index "__gc" in the metatable calls the object's destructor.
34+
ffi::lua_pushstring(lua, c_ptr!("__gc"));
35+
ffi::lua_pushcfunction(lua, wrap_gc::<T>);
36+
ffi::lua_settable(lua, -3);
37+
38+
ffi::lua_setmetatable(lua, -2);
39+
}
40+
41+
/// A callback for the "__gc" event. It checks if the value was moved out
42+
/// and if not it drops the value.
43+
unsafe extern "C" fn wrap_gc<T>(lua: *mut ffi::lua_State) -> i32 {
44+
let ud_ptr = ffi::lua_touserdata(lua, 1);
45+
let ud = (ud_ptr as *mut UDBox<T>)
46+
.as_mut()
47+
.expect("__gc called with userdata pointing to NULL");
48+
drop(ud.take());
49+
50+
0
51+
}
52+
}
53+
54+
1855
// Called when an object inside Lua is being dropped.
1956
#[inline]
2057
extern "C" fn destructor_wrapper<T>(lua: *mut ffi::lua_State) -> libc::c_int {

tarantool/src/error.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use rmp::decode::{MarkerReadError, NumValueReadError, ValueReadError};
2626
use rmp::encode::ValueWriteError;
2727

2828
use crate::ffi::tarantool as ffi;
29+
use crate::hlua::LuaError;
2930

3031
/// A specialized [`Result`] type for the crate
3132
pub type Result<T> = std::result::Result<T, Error>;
@@ -71,6 +72,9 @@ pub enum Error {
7172
#[cfg(feature = "net_box")]
7273
#[fail(display = "Sever respond with error: {}", _0)]
7374
Remote(crate::net_box::ResponseError),
75+
76+
#[fail(display = "Lua error: {}", _0)]
77+
LuaError(LuaError),
7478
}
7579

7680
impl From<io::Error> for Error {
@@ -142,6 +146,12 @@ impl From<crate::net_box::ResponseError> for Error {
142146
}
143147
}
144148

149+
impl From<LuaError> for Error {
150+
fn from(error: LuaError) -> Self {
151+
Error::LuaError(error)
152+
}
153+
}
154+
145155
/// Transaction-related error cases
146156
#[derive(Debug, Fail)]
147157
pub enum TransactionError {

0 commit comments

Comments
 (0)