From f7c26afd4e815504be87438849f5b045a5c75adc Mon Sep 17 00:00:00 2001 From: vimfung Date: Wed, 31 May 2017 09:52:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DiOS=EF=BC=8FOSX=E4=B8=8BFunct?= =?UTF-8?q?ion=E6=8F=90=E6=97=A9=E9=87=8A=E6=94=BE=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Former-commit-id: b84f6c870e78dd302b9ca0dc343e34ebdff5cb5e --- Source/iOS_OSX/Code/LSCFunction.m | 20 ++++---------------- Source/iOS_OSX/Code/LSCFunction_Private.h | 8 +++++++- Source/iOS_OSX/LSCDataExchanger.h | 10 ++++++++++ Source/iOS_OSX/LSCDataExchanger.m | 21 ++++++++++++++------- 4 files changed, 35 insertions(+), 24 deletions(-) diff --git a/Source/iOS_OSX/Code/LSCFunction.m b/Source/iOS_OSX/Code/LSCFunction.m index 7ce72c86..8c1fa4ad 100644 --- a/Source/iOS_OSX/Code/LSCFunction.m +++ b/Source/iOS_OSX/Code/LSCFunction.m @@ -11,16 +11,6 @@ #import "LSCContext_Private.h" #import "LSCValue_Private.h" #import "LSCTuple_Private.h" -#import "LSCManagedObjectProtocol.h" - -@interface LSCFunction () - -/** - 连接标识 - */ -@property (nonatomic, copy) NSString *_linkId; - -@end @implementation LSCFunction @@ -29,8 +19,11 @@ - (instancetype)initWithContext:(LSCContext *)context index:(NSInteger)index if (self = [super init]) { self.context = context; - self._linkId = [NSString stringWithFormat:@"%p", self]; + self.linkId = [NSString stringWithFormat:@"%p", self]; + //设置Lua对象到_vars_表中 + [self.context.dataExchanger setLubObjectByStackIndex:index objectId:self.linkId]; + //进行引用 [self.context retainValue:[LSCValue functionValue:self]]; } @@ -112,11 +105,6 @@ - (LSCValue *)invokeWithArguments:(NSArray *)arguments #pragma mark - LSCManagedObjectProtocol -- (NSString *)linkId -{ - return self._linkId; -} - - (BOOL)pushWithContext:(LSCContext *)context { return YES; diff --git a/Source/iOS_OSX/Code/LSCFunction_Private.h b/Source/iOS_OSX/Code/LSCFunction_Private.h index b21eac23..b94c3c7a 100644 --- a/Source/iOS_OSX/Code/LSCFunction_Private.h +++ b/Source/iOS_OSX/Code/LSCFunction_Private.h @@ -8,10 +8,11 @@ #import "LSCFunction.h" #import "lua.h" +#import "LSCManagedObjectProtocol.h" @class LSCContext; -@interface LSCFunction () +@interface LSCFunction () /** 上下文对象 @@ -24,6 +25,11 @@ */ @property (nonatomic, copy) NSString *index; +/** + 连接标识 + */ +@property (nonatomic, copy) NSString *linkId; + /** 初始化Lua方法 diff --git a/Source/iOS_OSX/LSCDataExchanger.h b/Source/iOS_OSX/LSCDataExchanger.h index c76c0db4..8fdd2908 100644 --- a/Source/iOS_OSX/LSCDataExchanger.h +++ b/Source/iOS_OSX/LSCDataExchanger.h @@ -46,6 +46,14 @@ */ - (void)getLuaObject:(id)nativeObject; +/** + 设置Lua对象 + + @param index Lua对象在栈中索引 + @param objectId 对象标识 + */ +- (void)setLubObjectByStackIndex:(NSInteger)index objectId:(NSString *)objectId; + /** 保留对象对应在Lua中的引用 @@ -60,4 +68,6 @@ */ - (void)releaseLuaObject:(id)nativeObject; + + @end diff --git a/Source/iOS_OSX/LSCDataExchanger.m b/Source/iOS_OSX/LSCDataExchanger.m index 8b098f70..a2d9bfb3 100644 --- a/Source/iOS_OSX/LSCDataExchanger.m +++ b/Source/iOS_OSX/LSCDataExchanger.m @@ -205,13 +205,7 @@ - (LSCValue *)valueByStackIndex:(int)index if (objectId && (type == LUA_TTABLE || type == LUA_TUSERDATA || type == LUA_TLIGHTUSERDATA || type == LUA_TFUNCTION)) { //将引用对象放入表中 - [self doActionInVarsTable:^{ - - //放入对象到_vars_表中 - lua_pushvalue(state, (int)index); - lua_setfield(state, -2, objectId.UTF8String); - - }]; + [self setLubObjectByStackIndex:index objectId:objectId]; } return value; @@ -328,6 +322,19 @@ - (void)getLuaObject:(id)nativeObject } } +- (void)setLubObjectByStackIndex:(NSInteger)index objectId:(NSString *)objectId +{ + lua_State *state = self.context.state; + + [self doActionInVarsTable:^{ + + //放入对象到_vars_表中 + lua_pushvalue(state, (int)index); + lua_setfield(state, -2, objectId.UTF8String); + + }]; +} + - (void)retainLuaObject:(id)nativeObject { if (nativeObject)