Skip to content

Commit fe25a2d

Browse files
committed
多虚拟机,并且在虚拟机中不断的新建及释放协程,可能会出现协程指针重复 fix #257
1 parent a904935 commit fe25a2d

File tree

16 files changed

+84
-43
lines changed

16 files changed

+84
-43
lines changed
Binary file not shown.
24 Bytes
Binary file not shown.

Assets/Plugins/iOS/libxlua.a

2.05 KB
Binary file not shown.

Assets/Plugins/x86/libxlua.so

56 Bytes
Binary file not shown.

Assets/Plugins/x86/xlua.dll

-3 KB
Binary file not shown.

Assets/Plugins/x86_64/libxlua.so

128 Bytes
Binary file not shown.

Assets/Plugins/x86_64/xlua.dll

-3.5 KB
Binary file not shown.
36 Bytes
Binary file not shown.

Assets/XLua/Src/LuaDLL.cs

+3
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,9 @@ public static bool xlua_is_eq_str(IntPtr L, int index, string str)
553553
[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
554554
public static extern bool xlua_is_eq_str(IntPtr L, int index, string str, int str_len);
555555

556+
[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
557+
public static extern IntPtr xlua_gl(IntPtr L);
558+
556559
#if GEN_CODE_MINIMIZE
557560
[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
558561
public static extern void xlua_set_csharp_wrapper_caller(IntPtr wrapper);

Assets/XLua/Src/LuaEnv.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ internal object luaEnvLock
5757
}
5858
#endif
5959

60-
const int LIB_VERSION_EXPECT = 103;
60+
const int LIB_VERSION_EXPECT = 104;
6161

6262
public LuaEnv()
6363
{
64-
if (LuaAPI.xlua_get_lib_version() != LIB_VERSION_EXPECT && LuaAPI.xlua_get_lib_version() != (LIB_VERSION_EXPECT -1))
64+
if (LuaAPI.xlua_get_lib_version() != LIB_VERSION_EXPECT)
6565
{
6666
throw new InvalidProgramException("wrong lib version expect:"
6767
+ LIB_VERSION_EXPECT + " but got:" + LuaAPI.xlua_get_lib_version());

Assets/XLua/Src/ObjectTranslatorPool.cs

+28-35
Original file line numberDiff line numberDiff line change
@@ -46,60 +46,53 @@ public ObjectTranslatorPool ()
4646

4747
public void Add (RealStatePtr L, ObjectTranslator translator)
4848
{
49-
translators.Add(L , new WeakReference(translator));
50-
}
49+
var ptr = LuaAPI.xlua_gl(L);
50+
lastPtr = ptr;
51+
lastTranslator = translator;
52+
#if !SINGLE_ENV
53+
translators.Add(ptr , new WeakReference(translator));
54+
#endif
55+
}
5156

52-
RealStatePtr lastState = default(RealStatePtr);
57+
RealStatePtr lastPtr = default(RealStatePtr);
5358
ObjectTranslator lastTranslator = default(ObjectTranslator);
5459

5560
public ObjectTranslator Find (RealStatePtr L)
5661
{
57-
if (lastState == L) return lastTranslator;
58-
if (translators.ContainsKey(L))
59-
{
60-
lastState = L;
61-
lastTranslator = translators[L].Target as ObjectTranslator;
62-
return lastTranslator;
63-
}
64-
65-
RealStatePtr main = Utils.GetMainState (L);
66-
67-
if (translators.ContainsKey(main))
62+
#if SINGLE_ENV
63+
return lastTranslator;
64+
#else
65+
var ptr = LuaAPI.xlua_gl(L);
66+
if (lastPtr == ptr) return lastTranslator;
67+
if (translators.ContainsKey(ptr))
6868
{
69-
lastState = L;
70-
lastTranslator = translators[main].Target as ObjectTranslator;
71-
translators[L] = new WeakReference(lastTranslator);
69+
lastPtr = ptr;
70+
lastTranslator = translators[ptr].Target as ObjectTranslator;
7271
return lastTranslator;
7372
}
7473

7574
return null;
76-
}
75+
#endif
76+
}
7777

7878
public void Remove (RealStatePtr L)
7979
{
80-
if (!translators.ContainsKey (L))
80+
#if SINGLE_ENV
81+
lastPtr = default(RealStatePtr);
82+
lastTranslator = default(ObjectTranslator);
83+
#else
84+
var ptr = LuaAPI.xlua_gl(L);
85+
if (!translators.ContainsKey (ptr))
8186
return;
8287

83-
if (lastState == L)
88+
if (lastPtr == ptr)
8489
{
85-
lastState = default(RealStatePtr);
90+
lastPtr = default(RealStatePtr);
8691
lastTranslator = default(ObjectTranslator);
8792
}
88-
ObjectTranslator translator = translators[L].Target as ObjectTranslator;
89-
List<RealStatePtr> toberemove = new List<RealStatePtr>();
9093

91-
foreach(var kv in translators)
92-
{
93-
if ((kv.Value.Target as ObjectTranslator) == translator)
94-
{
95-
toberemove.Add(kv.Key);
96-
}
97-
}
98-
99-
foreach (var ls in toberemove)
100-
{
101-
translators.Remove(ls);
102-
}
94+
translators.Remove(ptr);
95+
#endif
10396
}
10497
}
10598
}

WebGLPlugins/xlua.c

+35-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
#include <stdint.h>
1717
#include "i64lib.h"
1818

19+
#if USING_LUAJIT
20+
#include "lj_obj.h"
21+
#else
22+
#include "lstate.h"
23+
#endif
24+
1925
/*
2026
** stdcall C function support
2127
*/
@@ -34,7 +40,7 @@ LUA_API int xlua_get_registry_index() {
3440
}
3541

3642
LUA_API int xlua_get_lib_version() {
37-
return 102;
43+
return 104;
3844
}
3945

4046
LUA_API int xlua_tocsobj_safe(lua_State *L,int index) {
@@ -830,6 +836,30 @@ LUA_API void xlua_pushcstable(lua_State *L, unsigned int size, int meta_ref) {
830836
lua_setmetatable(L, -2);
831837
}
832838

839+
LUA_API void *xlua_newstruct(lua_State *L, int size, int meta_ref) {
840+
CSharpStruct *css = (CSharpStruct *)lua_newuserdata(L, size + sizeof(int) + sizeof(unsigned int));
841+
css->fake_id = -1;
842+
css->len = size;
843+
lua_rawgeti(L, LUA_REGISTRYINDEX, meta_ref);
844+
lua_setmetatable(L, -2);
845+
return css->data;
846+
}
847+
848+
LUA_API void *xlua_tostruct(lua_State *L, int idx, int meta_ref) {
849+
CSharpStruct *css = (CSharpStruct *)lua_touserdata(L, idx);
850+
if (NULL != css) {
851+
if (lua_getmetatable (L, idx)) {
852+
lua_rawgeti(L, -1, 1);
853+
if (lua_type(L, -1) == LUA_TNUMBER && (int)lua_tointeger(L, -1) == meta_ref) {
854+
lua_pop(L, 2);
855+
return css->data;
856+
}
857+
lua_pop(L, 2);
858+
}
859+
}
860+
return NULL;
861+
}
862+
833863
LUA_API int xlua_gettypeid(lua_State *L, int idx) {
834864
int type_id = -1;
835865
if (lua_type(L, idx) == LUA_TUSERDATA) {
@@ -1184,6 +1214,10 @@ LUA_API int css_clone(lua_State *L) {
11841214
return 1;
11851215
}
11861216

1217+
LUA_API void* xlua_gl(lua_State *L) {
1218+
return G(L);
1219+
}
1220+
11871221
static const luaL_Reg xlualib[] = {
11881222
{"sethook", profiler_set_hook},
11891223
{"genaccessor", gen_css_access},

build/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ if (USING_LUAJIT)
5555
)
5656

5757
set ( LUA_CORE )
58+
set_property( SOURCE xlua.c APPEND PROPERTY COMPILE_DEFINITIONS USING_LUAJIT )
5859
endif ()
5960
set ( LUA_LIB )
6061
else ()

build/plugin_lua53/Plugins/xlua.bundle/Contents/Info.plist

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<key>CFBundleExecutable</key>
1010
<string>xlua</string>
1111
<key>CFBundleIdentifier</key>
12-
<string>com.sineysoft.xlua</string>
12+
<string>com.xlua</string>
1313
<key>CFBundleInfoDictionaryVersion</key>
1414
<string>6.0</string>
1515
<key>CFBundleName</key>
@@ -41,6 +41,6 @@
4141
<key>DTXcodeBuild</key>
4242
<string>7D1014</string>
4343
<key>NSHumanReadableCopyright</key>
44-
<string>Copyright © 2015 Johnche. All rights reserved.</string>
44+
<string>Copyright @2017 THL A29 Limited, a Tencent company. All rights reserved.</string>
4545
</dict>
4646
</plist>

build/plugin_luajit/Plugins/xlua.bundle/Contents/Info.plist

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<key>CFBundleExecutable</key>
1010
<string>xlua</string>
1111
<key>CFBundleIdentifier</key>
12-
<string>com.sineysoft.xlua</string>
12+
<string>com.xlua</string>
1313
<key>CFBundleInfoDictionaryVersion</key>
1414
<string>6.0</string>
1515
<key>CFBundleName</key>
@@ -41,6 +41,6 @@
4141
<key>DTXcodeBuild</key>
4242
<string>7D1014</string>
4343
<key>NSHumanReadableCopyright</key>
44-
<string>Copyright © 2015 Johnche. All rights reserved.</string>
44+
<string>Copyright @2017 THL A29 Limited, a Tencent company. All rights reserved.</string>
4545
</dict>
4646
</plist>

build/xlua.c

+11-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
#include <stdint.h>
1717
#include "i64lib.h"
1818

19+
#if USING_LUAJIT
20+
#include "lj_obj.h"
21+
#else
22+
#include "lstate.h"
23+
#endif
24+
1925
/*
2026
** stdcall C function support
2127
*/
@@ -34,7 +40,7 @@ LUA_API int xlua_get_registry_index() {
3440
}
3541

3642
LUA_API int xlua_get_lib_version() {
37-
return 103;
43+
return 104;
3844
}
3945

4046
LUA_API int xlua_tocsobj_safe(lua_State *L,int index) {
@@ -1208,6 +1214,10 @@ LUA_API int css_clone(lua_State *L) {
12081214
return 1;
12091215
}
12101216

1217+
LUA_API void* xlua_gl(lua_State *L) {
1218+
return G(L);
1219+
}
1220+
12111221
static const luaL_Reg xlualib[] = {
12121222
{"sethook", profiler_set_hook},
12131223
{"genaccessor", gen_css_access},

0 commit comments

Comments
 (0)