Skip to content

Commit

Permalink
This mega-commit introduces Visual Studio 2015 CTP 6 support.
Browse files Browse the repository at this point in the history
sol::object had a few reference leaks in the way it retrieved values: it now does it properly without leaving the stack at +1 item
sol::stack was drastically cleaned up, with the following key change:
    * sol::stack::push now returns an integer of the number of things its pushed (usually 1, but can be more) (Thanks, @PrincessNyanara!)
    * sol::stack::call now calls functions flexibly, and getting is done more reliably
    * due to the innovation of stack::call and using absolute indices, we no longer have to use reverse_call style programming to deal with lua
    * sol::reference::get_type is now const-correct
    * sol::state and sol::table now have a cleaned up `get` implementation since it is no longer held back by the ugliness of VC++'s incapability to handle templates
    * the name `sol::userdata` now belongs to a type that actually encapsualtes a void* with a pusher/getter than gets a userdata void* value (TODO: give it a template to make it static_cast to that type on get?)
    * lightuserdata_t -> light_userdata, upvalue_t -> upvalue as type names (mostly details)
    * pushers for various types were updated to return integers
  • Loading branch information
ThePhD committed Mar 2, 2015
1 parent fa84161 commit bd4492b
Show file tree
Hide file tree
Showing 13 changed files with 456 additions and 324 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -33,3 +33,4 @@ main.ninja
luajit-2.0.3/
.dropbox*
desktop.ini
lua-5.3.0/
17 changes: 17 additions & 0 deletions sol/default_construct.hpp
Expand Up @@ -33,6 +33,23 @@ struct default_construct {
alloc.construct(obj, std::forward<Args>(args)...);
}
};

template <typename T>
struct placement_construct {
T obj;

template <typename... Args>
placement_construct( Args&&... args ) : obj(std::forward<Args>(args)...) {

}

template<typename... Args>
void operator()(Args&&... args) const {
std::allocator<Unqualified<T>> alloc{};
alloc.construct(obj, std::forward<Args>(args)...);
}
};

} // sol

#endif // SOL_DEFAULT_CONSTRUCTOR_HPP
40 changes: 25 additions & 15 deletions sol/function.hpp
Expand Up @@ -39,24 +39,31 @@ class function : public reference {
lua_call(state(), static_cast<uint32_t>(argcount), static_cast<uint32_t>(resultcount));
}

template<typename... Ret>
std::tuple<Ret...> invoke(types<Ret...>, std::size_t n) const {
template<std::size_t... I, typename... Ret>
std::tuple<Ret...> invoke(indices<I...>, types<Ret...>, std::size_t n) const {
luacall(n, sizeof...(Ret));
return stack::pop_reverse_call(state(), std::make_tuple<Ret...>, types<Ret...>());
const int nreturns = static_cast<int>(sizeof...(Ret));
const int stacksize = lua_gettop(state());
const int firstreturn = std::max(0, stacksize - nreturns) + 1;
auto r = std::make_tuple(stack::get<Ret>(state(), firstreturn + I)...);
lua_pop(state(), nreturns);
return r;
}

template<typename Ret>
Ret invoke(types<Ret>, std::size_t n) const {
template<std::size_t I, typename Ret>
Ret invoke(indices<I>, types<Ret>, std::size_t n) const {
luacall(n, 1);
return stack::pop<Ret>(state());
}

void invoke(types<void>, std::size_t n) const {
template <std::size_t I>
void invoke(indices<I>, types<void>, std::size_t n) const {
luacall(n, 0);
}

void invoke(types<>, std::size_t n) const {
luacall(n, 0);
void invoke(indices<>, types<>, std::size_t n) const {
auto tr = types<void>();
invoke(tr, tr, n);
}

public:
Expand All @@ -80,8 +87,9 @@ class function : public reference {
template<typename... Ret, typename... Args>
typename return_type<Ret...>::type call(Args&&... args) const {
push();
stack::push_args(state(), std::forward<Args>(args)...);
return invoke(types<Ret...>(), sizeof...(Args));
int pushcount = stack::push_args(state(), std::forward<Args>(args)...);
auto tr = types<Ret...>();
return invoke(tr, tr, pushcount);
}
};

Expand Down Expand Up @@ -175,8 +183,8 @@ struct pusher<function_sig_t<Sigs...>> {
lua_CFunction freefunc = &static_member_function<Decay<decltype(*userptr)>, Fx>::call;

int upvalues = stack::detail::push_as_upvalues(L, memfxptr);
stack::push(L, userobjdata);
++upvalues;
upvalues += stack::push(L, userobjdata);

stack::push(L, freefunc, upvalues);
}

Expand Down Expand Up @@ -209,15 +217,17 @@ struct pusher<function_sig_t<Sigs...>> {
}

template<typename... Args>
static void push(lua_State* L, Args&&... args) {
static int push(lua_State* L, Args&&... args) {
// Set will always place one thing (function) on the stack
set(L, std::forward<Args>(args)...);
return 1;
}
};

template<typename Signature>
struct pusher<std::function<Signature>> {
static void push(lua_State* L, std::function<Signature> fx) {
pusher<function_t>{}.push(L, std::move(fx));
static int push(lua_State* L, std::function<Signature> fx) {
return pusher<function_t>{}.push(L, std::move(fx));
}
};

Expand Down

0 comments on commit bd4492b

Please sign in to comment.