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