From f4c338c63f3456a8eccd56c35e233843687d55be Mon Sep 17 00:00:00 2001 From: chai Date: Fri, 29 Mar 2019 00:43:25 +0800 Subject: *thread --- source/3rd-party/Luax/luax_class.hpp | 39 ++++++---- source/3rd-party/Luax/luax_class.inl | 10 +-- source/3rd-party/Luax/luax_context.cpp | 29 ------- source/3rd-party/Luax/luax_context.h | 36 --------- source/3rd-party/Luax/luax_enum.cpp | 2 +- source/3rd-party/Luax/luax_enum.h | 3 +- source/3rd-party/Luax/luax_reftable.cpp | 10 +-- source/3rd-party/Luax/luax_runtime.cpp | 36 +++++---- source/3rd-party/Luax/luax_runtime.h | 15 ++-- source/3rd-party/Luax/luax_state.cpp | 28 +++---- source/3rd-party/Luax/luax_state.h | 71 ++++++++--------- source/3rd-party/Luax/luax_vm.cpp | 29 +++++++ source/3rd-party/Luax/luax_vm.h | 38 +++++++++ source/3rd-party/uuid4/uuid4.c | 90 ++++++++++++++++++++++ source/3rd-party/uuid4/uuid4.h | 22 ++++++ source/Asura.Editor/widgets/binding/_button.cpp | 2 +- source/Asura.Editor/widgets/button.h | 8 +- .../asura-lib-core/graphics/binding/_image.cpp | 4 +- source/libs/asura-lib-core/graphics/image.h | 9 ++- source/libs/asura-lib-core/graphics/shader.h | 2 +- source/libs/asura-lib-core/graphics/window.cpp | 50 ------------ source/libs/asura-lib-core/graphics/window.h | 90 ---------------------- .../asura-lib-core/graphics/window_impl_glew.cpp | 0 .../asura-lib-core/graphics/window_impl_glew.h | 0 .../asura-lib-core/graphics/window_impl_glut.cpp | 0 .../asura-lib-core/graphics/window_impl_glut.h | 0 .../asura-lib-core/graphics/window_impl_sdl.cpp | 0 .../libs/asura-lib-core/graphics/window_impl_sdl.h | 0 source/libs/asura-lib-core/input/mouse.h | 4 +- source/libs/asura-lib-core/window/window.cpp | 50 ++++++++++++ source/libs/asura-lib-core/window/window.h | 90 ++++++++++++++++++++++ .../asura-lib-core/window/window_impl_glew.cpp | 0 .../libs/asura-lib-core/window/window_impl_glew.h | 0 .../asura-lib-core/window/window_impl_glut.cpp | 0 .../libs/asura-lib-core/window/window_impl_glut.h | 0 .../libs/asura-lib-core/window/window_impl_sdl.cpp | 0 .../libs/asura-lib-core/window/window_impl_sdl.h | 0 source/libs/asura-lib-openal/sound.h | 8 +- source/libs/asura-lib-openal/sound_decode_task.cpp | 0 source/libs/asura-lib-openal/sound_decode_task.h | 25 ++++++ source/libs/asura-lib-openal/sound_decoder.cpp | 29 +++++++ source/libs/asura-lib-openal/sound_decoder.h | 7 +- .../asura-lib-utils/io/binding/_data_buffer.cpp | 18 ++--- source/libs/asura-lib-utils/io/binding/_file.cpp | 12 +-- .../asura-lib-utils/io/binding/_file_system.cpp | 22 +++--- .../libs/asura-lib-utils/io/binding/_io_task.cpp | 18 ++++- source/libs/asura-lib-utils/io/data_buffer.cpp | 10 +++ source/libs/asura-lib-utils/io/data_buffer.h | 3 + source/libs/asura-lib-utils/io/file.cpp | 10 ++- source/libs/asura-lib-utils/io/io_task.cpp | 25 ++++-- source/libs/asura-lib-utils/io/io_task.h | 26 +++++-- source/libs/asura-lib-utils/scripting/lua_env.h | 28 ++++++- .../asura-lib-utils/threading/binding/_thread.cpp | 21 ++++- source/libs/asura-lib-utils/threading/mutex.h | 16 ++-- source/libs/asura-lib-utils/threading/task.cpp | 12 +++ source/libs/asura-lib-utils/threading/task.h | 11 +-- source/libs/asura-lib-utils/threading/thread.cpp | 14 ++-- source/libs/asura-lib-utils/threading/thread.h | 52 +++++++++++-- .../threading/thread_impl_posix.cpp | 9 +++ .../threading/thread_impl_win32.cpp | 8 +- .../libs/asura-lib-utils/threading/thread_task.cpp | 0 .../libs/asura-lib-utils/threading/thread_task.h | 44 +++++++++++ source/libs/asura-lib-utils/type.h | 8 +- source/libs/asura-lib-utils/utils_module.cpp | 1 + source/libs/asura-lib-utils/utils_module.h | 1 + source/tests/02-luax/main.cpp | 10 +-- source/tests/05-physfs/main.cpp | 37 +++++++-- 67 files changed, 839 insertions(+), 413 deletions(-) delete mode 100644 source/3rd-party/Luax/luax_context.cpp delete mode 100644 source/3rd-party/Luax/luax_context.h create mode 100644 source/3rd-party/Luax/luax_vm.cpp create mode 100644 source/3rd-party/Luax/luax_vm.h create mode 100644 source/3rd-party/uuid4/uuid4.c create mode 100644 source/3rd-party/uuid4/uuid4.h delete mode 100644 source/libs/asura-lib-core/graphics/window.cpp delete mode 100644 source/libs/asura-lib-core/graphics/window.h delete mode 100644 source/libs/asura-lib-core/graphics/window_impl_glew.cpp delete mode 100644 source/libs/asura-lib-core/graphics/window_impl_glew.h delete mode 100644 source/libs/asura-lib-core/graphics/window_impl_glut.cpp delete mode 100644 source/libs/asura-lib-core/graphics/window_impl_glut.h delete mode 100644 source/libs/asura-lib-core/graphics/window_impl_sdl.cpp delete mode 100644 source/libs/asura-lib-core/graphics/window_impl_sdl.h create mode 100644 source/libs/asura-lib-core/window/window.cpp create mode 100644 source/libs/asura-lib-core/window/window.h create mode 100644 source/libs/asura-lib-core/window/window_impl_glew.cpp create mode 100644 source/libs/asura-lib-core/window/window_impl_glew.h create mode 100644 source/libs/asura-lib-core/window/window_impl_glut.cpp create mode 100644 source/libs/asura-lib-core/window/window_impl_glut.h create mode 100644 source/libs/asura-lib-core/window/window_impl_sdl.cpp create mode 100644 source/libs/asura-lib-core/window/window_impl_sdl.h create mode 100644 source/libs/asura-lib-openal/sound_decode_task.cpp create mode 100644 source/libs/asura-lib-openal/sound_decode_task.h create mode 100644 source/libs/asura-lib-openal/sound_decoder.cpp create mode 100644 source/libs/asura-lib-utils/threading/thread_task.cpp create mode 100644 source/libs/asura-lib-utils/threading/thread_task.h (limited to 'source') diff --git a/source/3rd-party/Luax/luax_class.hpp b/source/3rd-party/Luax/luax_class.hpp index cfac2a5..99ac381 100644 --- a/source/3rd-party/Luax/luax_class.hpp +++ b/source/3rd-party/Luax/luax_class.hpp @@ -13,7 +13,8 @@ namespace Luax /// /// RegisterLuaxClass 注册类的方法和成员,比如枚举、常量等到class table - /// LuaxGetFactoryName 获得工厂的类名,同时用来避免注册时错误注册为了singleton,通过编译时报错避免 + /// LuaxGetFactoryName 获得工厂的类名,同时用来避免注册时错误注册为了singleton,通过编译 + /// 时报错避免 /// #define LUAX_DECL_FACTORY(type) \ static void RegisterLuaxClass(Luax::LuaxState&);\ @@ -69,8 +70,9 @@ namespace Luax T* self = state.GetUserdata(1); /// - /// 虚基类,为了实现多态。需要访问下面这些接口的外部基类需要虚继承此类,之后再派生链中就会调用对应实体的方法。注意继承此 - /// 类时不能实现下面的方法,实现在LuaxNativeClass中,实现会导致二义性。 + /// 虚基类,为了实现多态。需要访问下面这些接口的外部基类需要虚继承此类,之后再派生链中就会 + /// 调用对应实体的方法。注意继承此类时不能实现下面的方法,实现在LuaxNativeClass中,实现会 + /// 导致二义性。 /// class ILuaxNativeAccessor { @@ -88,7 +90,8 @@ namespace Luax }; /// - /// 需要暴露给lua的native class需要继承此类。通过lua管理的实例要确保引用计数的正确性,在多个线程中需要确定不会误释放。 + /// 需要暴露给lua的native class需要继承此类。通过lua管理的实例要确保引用计数的正确性,在 + /// 多个线程中需要确定不会误释放。 /// template class LuaxNativeClass : public virtual ILuaxNativeAccessor @@ -98,8 +101,9 @@ namespace Luax static bool IsTypeOf(ILuaxNativeAccessor); /// - /// 将userdata作为key,在ref table里对userdata添加一个引用,以维持userdata的生命周期。相比较member ref,这个用在 - /// 实体会被多次被不同其他实体引用的情况,并频繁销毁这些实体,避免lua频繁的调用gc检测。 + /// 将userdata作为key,在ref table里对userdata添加一个引用,以维持userdata的生命周期。 + /// 相比较member ref,这个用在实体会被多次被不同其他实体引用的情况,并频繁销毁这些实体, + ///避免lua频繁的调用gc检测。 /// template void LuaxRetain(LuaxState& state, U* userdata); @@ -111,8 +115,8 @@ namespace Luax bool PushLuaxMemberRef(LuaxState& state, int refID) override; /// - /// 将userdata push到栈顶,如果没有初始化mUserdata,初始化设置好元表并把初始化好的userdata留在栈顶。并添加一个引用。 - /// 这是一个将native对象所有权移交给lua控制的方法。 + /// 将userdata push到栈顶,如果没有初始化mUserdata,初始化设置好元表并把初始化好的 + /// userdata留在栈顶。并添加一个引用。这是一个将native对象所有权移交给lua控制的方法。 /// bool PushLuaxUserdata(LuaxState& state); bool PushLuaxMemberTable(LuaxState& state); @@ -143,8 +147,8 @@ namespace Luax static void PushLuaxClassTable(LuaxState& state); /// - /// 屏蔽取地址运算符,如果需要地址,只能通过在堆上创建实例得到。在栈上和静态区的变量不能取地址。保证引用计数的准确。如 - /// 果需要穿引用,使用引用传递而不是传递地址。 + /// 屏蔽取地址运算符,如果需要地址,只能通过在堆上创建实例得到。在栈上和静态区的变量不能 + /// 取地址。保证引用计数的准确。如果需要穿引用,使用引用传递而不是传递地址。 /// void* operator &(); @@ -158,17 +162,18 @@ namespace Luax /// static LuaxStrongRef mClassTable; /// - /// 如果类是单例,这个用来保存singleton的引用关系,以保证不会被回收类似普通类的ref table。单例的成员是全生命周期的, - /// 所以直接在_LUAX_STRONGREF_TABLE。单例对userdata进行LuaxRetain\LuaxRelease和member ref操作时和工厂实例不同, - /// 是存在下面这个ref table里的,这个table在_LUAX_STRONGREF_TABLE里。 + /// 如果类是单例,这个用来保存singleton的引用关系,以保证不会被回收类似普通类的ref table。 + /// 单例的成员是全生命周期的,所以直接在_LUAX_STRONGREF_TABLE。单例对userdata进行 + /// LuaxRetain\LuaxRelease和member ref操作时和工厂实例不同,是存在下面这个ref table里 + /// 的,这个table在_LUAX_STRONGREF_TABLE里。 /// static LuaxStrongRef mSingletonRefTable; /// /// 通过userdata可以拿到: - /// 1: ref table - /// 2: member table - /// 3: class table + /// 1: ref table + /// 2: member table + /// 3: class table /// LuaxWeakRef mUserdata; @@ -193,7 +198,7 @@ namespace Luax #endif }; - //-------------------------------------------------------------------------------------------------------------- + //--------------------------------------------------------------------------------// #if LUAX_ENABLE_PLAIN_CLASS /// diff --git a/source/3rd-party/Luax/luax_class.inl b/source/3rd-party/Luax/luax_class.inl index 7ee3de9..d1c8c4b 100644 --- a/source/3rd-party/Luax/luax_class.inl +++ b/source/3rd-party/Luax/luax_class.inl @@ -1,7 +1,7 @@ namespace Luax { - //---------------------------------------------------------------------------------------------------------------- + //--------------------------------------------------------------------------------// // 接口 /// @@ -17,7 +17,7 @@ namespace Luax return 1; } - //---------------------------------------------------------------------------------------------------------------- + //--------------------------------------------------------------------------------// /// /// 注册工厂和单例共有的类成员 @@ -205,8 +205,8 @@ namespace Luax assert(!T::IsLuaxClassSingleton()); assert(!mUserdata); - // 创建userdata并留在栈顶 - state.PushPtrUserdata(this); + // 创建userdata并留在栈顶,注意地址要转换为T*,直接用this可能会导致多重继承的类丧失多态。 + state.PushPtrUserdata(static_cast(this)); lua_newtable(state); // ref table,无法在lua处访问,用来管理C对象的生命周期 lua_newtable(state); // member table,lua中创建的对象成员都保存在这里 @@ -315,7 +315,7 @@ namespace Luax } } - //-------------------------------------------------------------------------------------------------------------- + //--------------------------------------------------------------------------------// /// /// 释放工厂创建的实例 diff --git a/source/3rd-party/Luax/luax_context.cpp b/source/3rd-party/Luax/luax_context.cpp deleted file mode 100644 index d41c24f..0000000 --- a/source/3rd-party/Luax/luax_context.cpp +++ /dev/null @@ -1,29 +0,0 @@ -#include "luax_context.h" - -namespace Luax -{ - - LuaxContext::LuaxContext(lua_State* L) - : state(L) - { - assert(state); - } - - LuaxContext::~LuaxContext() - { - } - - // 初始化context - void LuaxContext::Setup() - { - SetupRefTables(); - } - - void LuaxContext::SetupRefTables() - { - // strong ref和 weak ref - strongRefTable.Init(state, "_LUAX_STRONGREF_TABLE"); - weakRefTable.Init(state, "_LUAX_WEAKREF_TABLE", "v"); - } - -} \ No newline at end of file diff --git a/source/3rd-party/Luax/luax_context.h b/source/3rd-party/Luax/luax_context.h deleted file mode 100644 index 01c5937..0000000 --- a/source/3rd-party/Luax/luax_context.h +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef __LUAX_CONTEXT_H__ -#define __LUAX_CONTEXT_H__ - -#include "luax_ref.h" -#include "luax_config.h" -#include "luax_state.h" - -namespace Luax -{ - - /// - /// 单个lua_state相关的context。是一系列代理的集合,拷贝也没关系,主要是为了节约内存。 - /// - class LuaxContext - { - public: - LuaxContext(lua_State* L); - ~LuaxContext(); - - void Setup(); - - LuaxState state; // lua state - LuaxRefTable strongRefTable; // strong ref table - LuaxRefTable weakRefTable; // weak ref table - - size_t objectCount; // 统计所有在此state中创建的实例 - - private: - - void SetupRefTables(); - - }; - -} - -#endif \ No newline at end of file diff --git a/source/3rd-party/Luax/luax_enum.cpp b/source/3rd-party/Luax/luax_enum.cpp index b054d98..60dd552 100644 --- a/source/3rd-party/Luax/luax_enum.cpp +++ b/source/3rd-party/Luax/luax_enum.cpp @@ -34,7 +34,7 @@ namespace Luax return luaL_error(L, "Enum called \"%s\" is readonly.", name); } - //-------------------------------------------------------------------------------------------------------------- + //--------------------------------------------------------------------------------// #if LUAX_ENABLE_PLAIN_ENUM int LuaxPlainEnum::registry(lua_State* L) { diff --git a/source/3rd-party/Luax/luax_enum.h b/source/3rd-party/Luax/luax_enum.h index c385dc5..395eaf0 100644 --- a/source/3rd-party/Luax/luax_enum.h +++ b/source/3rd-party/Luax/luax_enum.h @@ -19,7 +19,8 @@ namespace Luax extern int l_rmt__newindex(lua_State* L); - //-------------------------------------------------------------------------------------------------------------- + //--------------------------------------------------------------------------------// + #if LUAX_ENABLE_PLAIN_ENUM /// /// 纯lua的枚举,创建不可修改的table diff --git a/source/3rd-party/Luax/luax_reftable.cpp b/source/3rd-party/Luax/luax_reftable.cpp index 602c9eb..461e2d9 100644 --- a/source/3rd-party/Luax/luax_reftable.cpp +++ b/source/3rd-party/Luax/luax_reftable.cpp @@ -72,7 +72,7 @@ namespace Luax int LuaxRefTable::Ref(LuaxState& state, int idx) { - assert(mState && mState == state.GetHandle()); + assert(mState); idx = state.AbsIndex(idx); state.GetField(LUA_REGISTRYINDEX, mName); // ref table @@ -85,7 +85,7 @@ namespace Luax void LuaxRefTable::Unref(LuaxState& state, int refID) { - assert(mState && mState == state.GetHandle()); + assert(mState); state.GetField(LUA_REGISTRYINDEX, mName); // ref table luaL_unref(state, -1, refID); @@ -95,14 +95,14 @@ namespace Luax void LuaxRefTable::PushRefTable(LuaxState& state) { - assert(mState && mState == state.GetHandle()); + assert(mState); lua_getfield(state, LUA_REGISTRYINDEX, mName); } void LuaxRefTable::PushRef(LuaxState& state, int refID) { - assert(mState && mState == state.GetHandle()); + assert(mState); lua_getfield(state, LUA_REGISTRYINDEX, mName); lua_rawgeti(state, -1, refID); @@ -111,7 +111,7 @@ namespace Luax void LuaxRefTable::Clear(LuaxState& state) { - assert(mState && mState == state.GetHandle()); + assert(mState); lua_newtable(state); state.SetField(LUA_REGISTRYINDEX, mName); diff --git a/source/3rd-party/Luax/luax_runtime.cpp b/source/3rd-party/Luax/luax_runtime.cpp index b45d36a..dcf7b92 100644 --- a/source/3rd-party/Luax/luax_runtime.cpp +++ b/source/3rd-party/Luax/luax_runtime.cpp @@ -27,61 +27,69 @@ namespace Luax assert(L); // 1) 加入 - mContexts.insert(pair(L, LuaxContext(L))); + mContexts.insert(pair(L, new LuaxVM(L))); // 2) 初始化context (*this)[L].Setup(); return L; } - +/* + lua_State* LuaxRuntime::CreateThread(lua_State* main) + { + lua_State* thread = lua_newthread(main); + mContexts.insert(pair(thread, mContexts[main])); + return thread; + } +*/ void LuaxRuntime::Close(lua_State* L) { - map::iterator it = mContexts.find(L); + map::iterator it = mContexts.find(L); if (it != mContexts.end()) { - lua_close(it->second.state); + lua_close(it->second->state); mContexts.erase(it); } } bool LuaxRuntime::HasLuaxState(lua_State* L) { - map::iterator it = mContexts.find(L); + map::iterator it = mContexts.find(L); return it != mContexts.end(); } LuaxState& LuaxRuntime::GetLuaxState(lua_State* L) { - map::iterator it = mContexts.find(L); + map::iterator it = mContexts.find(L); if (it != mContexts.end()) { - return it->second.state; + return it->second->state; } } LuaxRefTable& LuaxRuntime::GetStrongRefTable(lua_State* L) { - map::iterator it = mContexts.find(L); + map::iterator it = mContexts.find(L); if (it != mContexts.end()) { - return it->second.strongRefTable; + return it->second->strongRefTable; } } LuaxRefTable& LuaxRuntime::GetWeaksRefTable(lua_State* L) { - map::iterator it = mContexts.find(L); + map::iterator it = mContexts.find(L); if (it != mContexts.end()) { - return it->second.weakRefTable; + return it->second->weakRefTable; } } - LuaxContext& LuaxRuntime::operator[](lua_State* L) + LuaxVM& LuaxRuntime::operator[](lua_State* L) { - map::iterator it = mContexts.find(L); + map::iterator it = mContexts.find(L); assert(it != mContexts.end()); - return it->second; + + return *it->second; } } diff --git a/source/3rd-party/Luax/luax_runtime.h b/source/3rd-party/Luax/luax_runtime.h index 1234627..27c9af4 100644 --- a/source/3rd-party/Luax/luax_runtime.h +++ b/source/3rd-party/Luax/luax_runtime.h @@ -6,7 +6,7 @@ #include "luax_ref.h" #include "luax_config.h" #include "luax_state.h" -#include "luax_context.h" +#include "luax_vm.h" namespace Luax { @@ -21,10 +21,15 @@ namespace Luax static LuaxRuntime& Get(); /// - /// 创建一个新的lua_State并保存下来。返回的lua_State*是一个8\4字节的key。 + /// 创建一个新的lua虚拟机和此虚拟机的主线程并保存下来。返回的lua_State*是一个8\4字节的key。 /// lua_State* Open(); - +/* + /// + /// 从主线程创建一个子线程。并添加到context的映射。主要是为了方便访问context,类似主线程那样访问。 + /// + lua_State* CreateThread(lua_State* mainThread); +*/ /// /// 关闭lua_State并将其从runtime中删除。 /// @@ -35,7 +40,7 @@ namespace Luax LuaxRefTable& GetStrongRefTable(lua_State* L); LuaxRefTable& GetWeaksRefTable(lua_State* L); - LuaxContext& operator[](lua_State* L); + LuaxVM& operator[](lua_State* L); private: @@ -47,7 +52,7 @@ namespace Luax /// /// 从lua_State handle到context的映射 /// - std::map mContexts; + std::map mContexts; }; diff --git a/source/3rd-party/Luax/luax_state.cpp b/source/3rd-party/Luax/luax_state.cpp index b7b89a3..f453be7 100644 --- a/source/3rd-party/Luax/luax_state.cpp +++ b/source/3rd-party/Luax/luax_state.cpp @@ -741,7 +741,7 @@ namespace Luax } template <> - bool LuaxState::CheckParam < bool >(int idx) + bool LuaxState::CheckValue < bool >(int idx) { bool b = false; if (lua_type(mState, idx) == LUA_TBOOLEAN) @@ -756,73 +756,73 @@ namespace Luax } template <> - cc8* LuaxState::CheckParam < cc8* >(int idx) + cc8* LuaxState::CheckValue < cc8* >(int idx) { return luaL_checkstring(mState, idx); } template <> - double LuaxState::CheckParam < double >(int idx) + double LuaxState::CheckValue < double >(int idx) { return luaL_checknumber(mState, idx); } template <> - float LuaxState::CheckParam < float >(int idx) + float LuaxState::CheckValue < float >(int idx) { return luaL_checknumber(mState, idx); } template <> - s8 LuaxState::CheckParam < s8 >(int idx) + s8 LuaxState::CheckValue < s8 >(int idx) { return luaL_checkinteger(mState, idx); } template <> - s16 LuaxState::CheckParam < s16 >(int idx) + s16 LuaxState::CheckValue < s16 >(int idx) { return luaL_checkinteger(mState, idx); } template <> - s32 LuaxState::CheckParam < s32 >(int idx) + s32 LuaxState::CheckValue < s32 >(int idx) { return luaL_checkinteger(mState, idx); } template <> - s64 LuaxState::CheckParam < s64 >(int idx) + s64 LuaxState::CheckValue < s64 >(int idx) { return luaL_checkinteger(mState, idx); } template <> - u8 LuaxState::CheckParam < u8 >(int idx) + u8 LuaxState::CheckValue < u8 >(int idx) { return luaL_checkinteger(mState, idx); } template <> - u16 LuaxState::CheckParam < u16 >(int idx) + u16 LuaxState::CheckValue < u16 >(int idx) { return luaL_checkinteger(mState, idx); } template <> - u32 LuaxState::CheckParam < u32 >(int idx) + u32 LuaxState::CheckValue < u32 >(int idx) { return luaL_checkinteger(mState, idx); } template <> - u64 LuaxState::CheckParam < u64 >(int idx) + u64 LuaxState::CheckValue < u64 >(int idx) { return luaL_checkinteger(mState, idx); } template <> - std::string LuaxState::CheckParam < std::string >(int idx) + std::string LuaxState::CheckValue < std::string >(int idx) { return luaL_checkstring(mState, idx); } @@ -831,7 +831,7 @@ namespace Luax /// check light userdata /// template <> - const void* LuaxState::CheckParam < const void* >(int idx) + const void* LuaxState::CheckValue < const void* >(int idx) { if (IsType(idx, LUA_TLIGHTUSERDATA)) { diff --git a/source/3rd-party/Luax/luax_state.h b/source/3rd-party/Luax/luax_state.h index e9c394a..b6d1ff0 100644 --- a/source/3rd-party/Luax/luax_state.h +++ b/source/3rd-party/Luax/luax_state.h @@ -9,7 +9,7 @@ namespace Luax { - class LuaxContext; + class LuaxVM; class LuaxEnum; class LuaxStrongRef; class LuaxWeakRef; @@ -20,7 +20,8 @@ namespace Luax /// member table /// ref table /// userdata - /// 从userdata通过getmetatable获取上级metatable。除此之外还有一个class table注册在对应的名称空间里。 + /// 从userdata通过getmetatable获取上级metatable。除此之外还有一个class table注册在对应 + /// 的名称空间里。 /// LUAX_API class LuaxState { @@ -36,11 +37,11 @@ namespace Luax /// inline lua_State* GetHandle() { return mState; }; - //------------------------------------------------------------------------------------------------------------ + //------------------------------------------------------------------------------// void OpenLibs(); - //------------------------------------------------------------------------------------------------------------ + //------------------------------------------------------------------------------// // 名称空间管理,名称空间就是一个表,_G是最上面的表 void PushGlobalNamespace(); @@ -48,7 +49,7 @@ namespace Luax void PopNamespace(); bool IsNamespace(int idx); - //------------------------------------------------------------------------------------------------------------ + //------------------------------------------------------------------------------// void SetTop(int top); int GetTop(); @@ -56,7 +57,7 @@ namespace Luax int AbsIndex(int idx); void Call(int nArgs, int nResults); - //------------------------------------------------------------------------------------------------------------ + //------------------------------------------------------------------------------// void GetField(int idx, cc8* name); void GetField(int idx, int key); @@ -117,11 +118,11 @@ namespace Luax template T* GetUserdata(int idx = 1); - //------------------------------------------------------------------------------------------------------------ + //------------------------------------------------------------------------------// int ErrorType(int idx, cc8* hint); - //------------------------------------------------------------------------------------------------------------ + //------------------------------------------------------------------------------// template T GetValue(int idx, T default_value); template T GetField(int idx, int key, T value); @@ -130,14 +131,14 @@ namespace Luax template void SetFieldByIndex(int idx, int key, T value); template T* CheckUserdata(int idx); - template T CheckParam(int idx); + template T CheckValue(int idx); - //------------------------------------------------------------------------------------------------------------ + //------------------------------------------------------------------------------// void DoString(const std::string& code); void DoFile(const std::string& file); - //------------------------------------------------------------------------------------------------------------ + //------------------------------------------------------------------------------// // 注册方法 /// @@ -166,9 +167,9 @@ namespace Luax void RegisterMethod(cc8* fname, lua_CFunction func); /// - /// 把preloader加到package.preload里,当require"libname"时lua的loader_preload根据libname找到preloader直接加载。 - /// 用来实现需要require的时候才加载,并且加载过一次后package.loaded记录下来,下次不会再加载。通过require会调用这个 - /// preloader。 + /// 把preloader加到package.preload里,当require"libname"时lua的loader_preload根据 + /// libname找到preloader直接加载。用来实现需要require的时候才加载,并且加载过一次后 + /// package.loaded记录下来,下次不会再加载。通过require会调用这个preloader。 /// void RegisterPreloader(cc8* libname, lua_CFunction preloader); @@ -191,11 +192,11 @@ namespace Luax void RegisterPlainEnumRegistry(cc8* name); #endif - //------------------------------------------------------------------------------------------------------------ + //------------------------------------------------------------------------------// protected: - friend class LuaxContext; + friend class LuaxVM; LuaxState(lua_State* state); LuaxState(const LuaxState& state); @@ -211,7 +212,7 @@ namespace Luax }; - //-------------------------------------------------------------------------------------------------------------- + //--------------------------------------------------------------------------------// // GetValue()模板特化 template <> bool LuaxState::GetValue < bool >(int idx, const bool value); @@ -229,23 +230,23 @@ namespace Luax template <> std::string LuaxState::GetValue < std::string >(int idx, const std::string value); template <> const void* LuaxState::GetValue < const void* >(int idx, const void* value); - //-------------------------------------------------------------------------------------------------------------- - // CheckParam模板特化 - - template <> bool LuaxState::CheckParam < bool >(int idx); - template <> cc8* LuaxState::CheckParam < cc8* >(int idx); - template <> double LuaxState::CheckParam < double >(int idx); - template <> float LuaxState::CheckParam < float >(int idx); - template <> s8 LuaxState::CheckParam < s8 >(int idx); - template <> s16 LuaxState::CheckParam < s16 >(int idx); - template <> s32 LuaxState::CheckParam < s32 >(int idx); - template <> s64 LuaxState::CheckParam < s64 >(int idx); - template <> u8 LuaxState::CheckParam < u8 >(int idx); - template <> u16 LuaxState::CheckParam < u16 >(int idx); - template <> u32 LuaxState::CheckParam < u32 >(int idx); - template <> u64 LuaxState::CheckParam < u64 >(int idx); - template <> std::string LuaxState::CheckParam < std::string >(int idx); - template <> const void* LuaxState::CheckParam < const void* >(int idx); + //--------------------------------------------------------------------------------// + // CheckValue模板特化 + + template <> bool LuaxState::CheckValue < bool >(int idx); + template <> cc8* LuaxState::CheckValue < cc8* >(int idx); + template <> double LuaxState::CheckValue < double >(int idx); + template <> float LuaxState::CheckValue < float >(int idx); + template <> s8 LuaxState::CheckValue < s8 >(int idx); + template <> s16 LuaxState::CheckValue < s16 >(int idx); + template <> s32 LuaxState::CheckValue < s32 >(int idx); + template <> s64 LuaxState::CheckValue < s64 >(int idx); + template <> u8 LuaxState::CheckValue < u8 >(int idx); + template <> u16 LuaxState::CheckValue < u16 >(int idx); + template <> u32 LuaxState::CheckValue < u32 >(int idx); + template <> u64 LuaxState::CheckValue < u64 >(int idx); + template <> std::string LuaxState::CheckValue < std::string >(int idx); + template <> const void* LuaxState::CheckValue < const void* >(int idx); /// /// 在成员方法里创建LuaxState并对参数进行检查。 @@ -258,7 +259,7 @@ namespace Luax #define LUAX_STATE(L) \ Luax::LuaxState& state = Luax::LuaxRuntime::Get().GetLuaxState(L) - //-------------------------------------------------------------------------------------------------------------- + //--------------------------------------------------------------------------------// /// /// 确保不安全的lua调用能够在调用之后返回到最初stack状态。 diff --git a/source/3rd-party/Luax/luax_vm.cpp b/source/3rd-party/Luax/luax_vm.cpp new file mode 100644 index 0000000..4257b4d --- /dev/null +++ b/source/3rd-party/Luax/luax_vm.cpp @@ -0,0 +1,29 @@ +#include "luax_vm.h" + +namespace Luax +{ + + LuaxVM::LuaxVM(lua_State* L) + : state(L) + { + assert(state); + } + + LuaxVM::~LuaxVM() + { + } + + // 初始化context + void LuaxVM::Setup() + { + SetupRefTables(); + } + + void LuaxVM::SetupRefTables() + { + // strong ref和 weak ref + strongRefTable.Init(state, "_LUAX_STRONGREF_TABLE"); + weakRefTable.Init(state, "_LUAX_WEAKREF_TABLE", "v"); + } + +} \ No newline at end of file diff --git a/source/3rd-party/Luax/luax_vm.h b/source/3rd-party/Luax/luax_vm.h new file mode 100644 index 0000000..e99d5b5 --- /dev/null +++ b/source/3rd-party/Luax/luax_vm.h @@ -0,0 +1,38 @@ +#ifndef __LUAX_CONTEXT_H__ +#define __LUAX_CONTEXT_H__ + +#include "luax_ref.h" +#include "luax_config.h" +#include "luax_state.h" + +namespace Luax +{ + + /// + /// 单个lua_state相关的context。是一系列代理的集合,拷贝也没关系,主要是为了节约内存。 + /// + class LuaxVM + { + public: + LuaxVM(lua_State* L); + ~LuaxVM(); + + void Setup(); + + LuaxState state; // lua main state + LuaxRefTable strongRefTable; // strong ref table + LuaxRefTable weakRefTable; // weak ref table + + size_t objectCount; // 统计所有在此state中创建的实例 + + private: + + void SetupRefTables(); + + int mThreadsCount; // 绑定这个context的线程数 + + }; + +} + +#endif \ No newline at end of file diff --git a/source/3rd-party/uuid4/uuid4.c b/source/3rd-party/uuid4/uuid4.c new file mode 100644 index 0000000..1b60188 --- /dev/null +++ b/source/3rd-party/uuid4/uuid4.c @@ -0,0 +1,90 @@ +/** + * Copyright (c) 2018 rxi + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MIT license. See LICENSE for details. + */ + +#include +#include + +#if defined(_WIN32) +#include +#include +#endif + +#include "uuid4.h" + + +static uint64_t seed[2]; + + +static uint64_t xorshift128plus(uint64_t *s) { + /* http://xorshift.di.unimi.it/xorshift128plus.c */ + uint64_t s1 = s[0]; + const uint64_t s0 = s[1]; + s[0] = s0; + s1 ^= s1 << 23; + s[1] = s1 ^ s0 ^ (s1 >> 18) ^ (s0 >> 5); + return s[1] + s0; +} + + +int uuid4_init(void) { +#if defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) + int res; + FILE *fp = fopen("/dev/urandom", "rb"); + if (!fp) { + return UUID4_EFAILURE; + } + res = fread(seed, 1, sizeof(seed), fp); + fclose(fp); + if ( res != sizeof(seed) ) { + return UUID4_EFAILURE; + } + +#elif defined(_WIN32) + int res; + HCRYPTPROV hCryptProv; + res = CryptAcquireContext( + &hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT); + if (!res) { + return UUID4_EFAILURE; + } + res = CryptGenRandom(hCryptProv, (DWORD) sizeof(seed), (PBYTE) seed); + CryptReleaseContext(hCryptProv, 0); + if (!res) { + return UUID4_EFAILURE; + } + +#else + #error "unsupported platform" +#endif + return UUID4_ESUCCESS; +} + + +void uuid4_generate(char *dst) { + static const char *template = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"; + static const char *chars = "0123456789abcdef"; + union { unsigned char b[16]; uint64_t word[2]; } s; + const char *p; + int i, n; + /* get random */ + s.word[0] = xorshift128plus(seed); + s.word[1] = xorshift128plus(seed); + /* build string */ + p = template; + i = 0; + while (*p) { + n = s.b[i >> 1]; + n = (i & 1) ? (n >> 4) : (n & 0xf); + switch (*p) { + case 'x' : *dst = chars[n]; i++; break; + case 'y' : *dst = chars[(n & 0x3) + 8]; i++; break; + default : *dst = *p; + } + dst++, p++; + } + *dst = '\0'; +} diff --git a/source/3rd-party/uuid4/uuid4.h b/source/3rd-party/uuid4/uuid4.h new file mode 100644 index 0000000..fb3ed2f --- /dev/null +++ b/source/3rd-party/uuid4/uuid4.h @@ -0,0 +1,22 @@ +/** + * Copyright (c) 2018 rxi + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MIT license. See LICENSE for details. + */ + +#ifndef UUID4_H +#define UUID4_H + +#define UUID4_VERSION "1.0.0" +#define UUID4_LEN 37 + +enum { + UUID4_ESUCCESS = 0, + UUID4_EFAILURE = -1 +}; + +int uuid4_init(void); +void uuid4_generate(char *dst); + +#endif diff --git a/source/Asura.Editor/widgets/binding/_button.cpp b/source/Asura.Editor/widgets/binding/_button.cpp index 463aef6..fbae64f 100644 --- a/source/Asura.Editor/widgets/binding/_button.cpp +++ b/source/Asura.Editor/widgets/binding/_button.cpp @@ -37,7 +37,7 @@ namespace AsuraEditor LUAX_STATE(L); Button* self = state.GetUserdata