change usertype adding to independent functions of each type
This commit is contained in:
parent
8743ab21f7
commit
f8da71f2f1
@ -8,35 +8,16 @@ extern "C"
|
||||
#include "threading.h"
|
||||
#include "shared-table.h"
|
||||
|
||||
sol::table core::init_state(sol::state_view& lua)
|
||||
extern "C" int luaopen_libwoofer(lua_State *L)
|
||||
{
|
||||
sol::usertype<LuaThread> thread_api(
|
||||
sol::call_construction(), sol::constructors<sol::types<sol::function, sol::variadic_args>>(),
|
||||
"join", &LuaThread::join,
|
||||
"thread_id", &LuaThread::thread_id
|
||||
);
|
||||
sol::stack::push(lua, thread_api);
|
||||
auto thread_obj = sol::stack::pop<sol::object>(lua);
|
||||
|
||||
sol::usertype<core::SharedTable> share_api(
|
||||
sol::call_construction(), sol::default_constructor,
|
||||
sol::meta_function::new_index, &core::SharedTable::luaSet,
|
||||
sol::meta_function::index, &core::SharedTable::luaGet
|
||||
);
|
||||
sol::stack::push(lua, share_api);
|
||||
auto share_obj = sol::stack::pop<sol::object>(lua);
|
||||
|
||||
sol::state_view lua(L);
|
||||
auto thread_obj = threading::LuaThread::get_user_type(lua);
|
||||
auto share_obj = share_data::SharedTable::get_user_type(lua);
|
||||
sol::table public_api = lua.create_table_with(
|
||||
"thread", thread_obj,
|
||||
"share", share_obj
|
||||
);
|
||||
return public_api;
|
||||
}
|
||||
|
||||
extern "C" int luaopen_libwoofer(lua_State *L)
|
||||
{
|
||||
sol::state_view lua(L);
|
||||
sol::stack::push(lua, core::init_state(lua));
|
||||
sol::stack::push(lua, public_api);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
@ -3,13 +3,17 @@
|
||||
#include <cassert>
|
||||
#include <mutex>
|
||||
|
||||
namespace core {
|
||||
namespace share_data {
|
||||
|
||||
void SharedTable::bind(sol::state_view& lua) noexcept {
|
||||
lua.new_usertype<SharedTable>("shared_table",
|
||||
sol::meta_function::new_index, &SharedTable::luaSet,
|
||||
sol::meta_function::index, &SharedTable::luaGet,
|
||||
sol::meta_function::length, &SharedTable::size);
|
||||
sol::object SharedTable::get_user_type(sol::state_view& lua) noexcept {
|
||||
static sol::usertype<share_data::SharedTable> type(
|
||||
sol::call_construction(), sol::default_constructor,
|
||||
sol::meta_function::new_index, &share_data::SharedTable::luaSet,
|
||||
sol::meta_function::index, &share_data::SharedTable::luaGet,
|
||||
sol::meta_function::length, &SharedTable::size
|
||||
);
|
||||
sol::stack::push(lua, type);
|
||||
return sol::stack::pop<sol::object>(lua);
|
||||
}
|
||||
|
||||
void SharedTable::luaSet(sol::stack_object luaKey, sol::stack_object luaValue) noexcept {
|
||||
|
||||
@ -9,9 +9,7 @@
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
namespace core {
|
||||
|
||||
sol::table init_state(sol::state_view& lua);
|
||||
namespace share_data {
|
||||
|
||||
class SharedTable {
|
||||
public:
|
||||
@ -20,8 +18,7 @@ public:
|
||||
void luaSet(sol::stack_object luaKey, sol::stack_object luaValue) noexcept;
|
||||
sol::object luaGet(sol::stack_object key, sol::this_state state) noexcept;
|
||||
|
||||
// Add usertype to state
|
||||
static void bind(sol::state_view& lua) noexcept;
|
||||
static sol::object get_user_type(sol::state_view& lua) noexcept;
|
||||
|
||||
private: // lau bindings
|
||||
size_t size() noexcept;
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
|
||||
namespace core {
|
||||
namespace share_data {
|
||||
|
||||
class SpinMutex {
|
||||
public:
|
||||
|
||||
@ -7,7 +7,7 @@
|
||||
|
||||
#include <cassert>
|
||||
|
||||
namespace core {
|
||||
namespace share_data {
|
||||
|
||||
bool FunctionHolder::rawCompare(const BaseHolder* other) const noexcept {
|
||||
return function_ == static_cast<const FunctionHolder*>(other)->function_;
|
||||
@ -79,4 +79,4 @@ bool StoredObject::operator<(const StoredObject& o) const noexcept {
|
||||
return data_.get() < o.data_.get();
|
||||
}
|
||||
|
||||
} // core
|
||||
}
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
#include <utility>
|
||||
#include <iostream>
|
||||
|
||||
namespace core {
|
||||
namespace share_data {
|
||||
|
||||
#define ERROR std::cerr
|
||||
|
||||
@ -148,8 +148,8 @@ private:
|
||||
namespace std {
|
||||
|
||||
template<>
|
||||
struct hash<core::StoredObject> {
|
||||
std::size_t operator()(const core::StoredObject &object) const noexcept {
|
||||
struct hash<share_data::StoredObject> {
|
||||
std::size_t operator()(const share_data::StoredObject &object) const noexcept {
|
||||
return object.hash();
|
||||
}
|
||||
};
|
||||
|
||||
@ -1,7 +1,8 @@
|
||||
#include "threading.h"
|
||||
|
||||
LuaThread::LuaThread(const sol::function& function, const sol::variadic_args& args) noexcept
|
||||
{
|
||||
namespace threading {
|
||||
|
||||
LuaThread::LuaThread(const sol::function& function, const sol::variadic_args& args) noexcept{
|
||||
// 1. Dump function to string
|
||||
sol::state_view lua(function.lua_state());
|
||||
str_function_ = lua["string"]["dump"](function);
|
||||
@ -13,8 +14,8 @@ LuaThread::LuaThread(const sol::function& function, const sol::variadic_args& ar
|
||||
sol::lib::base, sol::lib::string,
|
||||
sol::lib::package, sol::lib::io, sol::lib::os
|
||||
);
|
||||
auto thread_table = core::init_state(*p_state_);
|
||||
(void)thread_table;
|
||||
get_user_type(*p_state_);
|
||||
share_data::SharedTable::get_user_type(*p_state_);
|
||||
|
||||
// 3. Save parameters
|
||||
validate_args(args);
|
||||
@ -34,7 +35,7 @@ void LuaThread::validate_args(const sol::variadic_args& args) noexcept
|
||||
const auto end = --args.end();
|
||||
for(auto iter = args.begin(); iter != end; iter++)
|
||||
{
|
||||
core::StoredObject store(iter->get<sol::object>());
|
||||
share_data::StoredObject store(iter->get<sol::object>());
|
||||
arguments_.push_back(store.unpack(sol::this_state{p_state_->lua_state()}));
|
||||
}
|
||||
}
|
||||
@ -64,3 +65,16 @@ std::string LuaThread::thread_id() noexcept
|
||||
ss << std::this_thread::get_id();
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
sol::object LuaThread::get_user_type(sol::state_view& lua) noexcept
|
||||
{
|
||||
static sol::usertype<LuaThread> type(
|
||||
sol::call_construction(), sol::constructors<sol::types<sol::function, sol::variadic_args>>(),
|
||||
"join", &LuaThread::join,
|
||||
"thread_id", &LuaThread::thread_id
|
||||
);
|
||||
sol::stack::push(lua, type);
|
||||
return sol::stack::pop<sol::object>(lua);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -7,13 +7,17 @@
|
||||
|
||||
#include "shared-table.h"
|
||||
|
||||
namespace threading {
|
||||
|
||||
class LuaThread
|
||||
{
|
||||
public:
|
||||
LuaThread(const sol::function& function, const sol::variadic_args& args) noexcept;
|
||||
virtual ~LuaThread() noexcept;
|
||||
void join() noexcept;
|
||||
|
||||
static std::string thread_id() noexcept;
|
||||
static sol::object get_user_type(sol::state_view& lua) noexcept;
|
||||
|
||||
private:
|
||||
void work() noexcept;
|
||||
@ -24,3 +28,5 @@ private:
|
||||
std::shared_ptr<std::thread> p_thread_;
|
||||
std::vector<sol::object> arguments_;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user