Skip to content

Commit

Permalink
iOS / OSX 平台下增加初始化上下文时传入配置接口,允许导出类方法名称使用完整名称。
Browse files Browse the repository at this point in the history
  • Loading branch information
vimfung committed Apr 4, 2019
1 parent 51696b1 commit 9af33ee
Show file tree
Hide file tree
Showing 13 changed files with 188 additions and 37 deletions.
9 changes: 9 additions & 0 deletions Source/iOS_OSX/Code-Swift/LuaConfig.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//
// LuaConfig.swift
// LuaScriptCore
//
// Created by 冯鸿杰 on 2019/4/4.
// Copyright © 2019年 vimfung. All rights reserved.
//

public typealias LuaConfig = LSCConfig;
5 changes: 5 additions & 0 deletions Source/iOS_OSX/Code-Swift/LuaContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public class LuaContext: NSObject
self.rawContext = LSCContext();
}

public init (config : LuaConfig)
{
self.rawContext = LSCContext(config: config);
}

/// 添加搜索路径,对于不在应用主Bundle根目录的lua脚本如果需要require时,则需要指定其搜索路径。
///
/// - Parameter path: 路径字符串
Expand Down
22 changes: 22 additions & 0 deletions Source/iOS_OSX/Code/LSCContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,34 @@

@class LSCFunction;
@class LSCScriptController;
@class LSCConfig;

/**
* Lua上下文对象
*/
@interface LSCContext : NSObject

/**
配置信息
*/
@property (nonatomic, strong, readonly) LSCConfig *config;


/**
初始化
@return 上下文对象
*/
- (instancetype)init;

/**
初始化
@param config 配置信息
@return 上下文对象
*/
- (instancetype)initWithConfig:(LSCConfig *)config;

/**
抛出异常
Expand Down
21 changes: 10 additions & 11 deletions Source/iOS_OSX/Code/LSCContext.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#import "LSCTuple.h"
#import "LSCCoroutine+Private.h"
#import "LSCError.h"
#import "LSCConfig.h"
#import <objc/runtime.h>

/**
Expand All @@ -32,25 +33,17 @@ @interface LSCContext ()

@implementation LSCContext

- (instancetype)init
{
return [self initWithCreateStateHandler:^lua_State *{

return [LSCEngineAdapter newState];

}];
}

- (instancetype)initWithCreateStateHandler:(lua_State* (^)(void))handler
- (instancetype)initWithConfig:(LSCConfig *)config
{
if (self = [super init])
{
_config = config;
self.methodBlocks = [NSMutableDictionary dictionary];

self.optQueue = [[LSCOperationQueue alloc] init];
[self.optQueue performAction:^{

lua_State *state = handler();
lua_State *state = [LSCEngineAdapter newState];

[LSCEngineAdapter gc:state what:LSCGCTypeStop data:0];

Expand Down Expand Up @@ -86,9 +79,15 @@ - (instancetype)initWithCreateStateHandler:(lua_State* (^)(void))handler

}];
}

return self;
}

- (instancetype)init
{
return [self initWithConfig:[LSCConfig defaultConfig]];
}

- (void)dealloc
{
//由于LSCSession在销毁前会进行一次GC,但是在该情况下lua_State已经被close。
Expand Down
8 changes: 0 additions & 8 deletions Source/iOS_OSX/Code/LSCContext_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,6 @@
*/
@property (nonatomic, strong) LSCExportsTypeManager *exportsTypeManager;

/**
初始化
@param handler 创建State处理器
@return 上下文对象
*/
- (instancetype)initWithCreateStateHandler:(lua_State* (^)(void))handler;

/**
通过状态设置当前会话
Expand Down
65 changes: 48 additions & 17 deletions Source/iOS_OSX/Code/LSCExportsTypeManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#import "LSCExportTypeAnnotation.h"
#import "LSCExportPropertyDescriptor.h"
#import "LSCVirtualInstance.h"
#import "LSCConfig.h"
#import <objc/runtime.h>

/**
Expand Down Expand Up @@ -902,30 +903,60 @@ - (void)_exportsInstanceMethods:(LSCExportTypeDescriptor *)typeDescriptor
- (NSString *)_getLuaMethodNameWithSelectorName:(NSString *)selectorName
{
NSString *luaName = selectorName;

NSRange range = [luaName rangeOfString:@":"];
if (range.location != NSNotFound)
{
luaName = [luaName substringToIndex:range.location];
}

range = [luaName rangeOfString:@"With"];
if (range.location != NSNotFound)
if ([luaName hasPrefix:@"init"])
{
luaName = [luaName substringToIndex:range.location];
//检测是否为初始化方法
if (luaName.length > 4)
{
unichar ch = [luaName characterAtIndex:4];
if (ch > 'A' && ch < 'Z')
{
return @"init";
}
}
else
{
return @"init";
}
}

range = [luaName rangeOfString:@"At"];
if (range.location != NSNotFound)

if (self.context.config.fullExportName)
{
luaName = [luaName substringToIndex:range.location];
//使用完整限定名称
luaName = [selectorName stringByReplacingOccurrencesOfString:@":" withString:@"_"];
if ([luaName hasSuffix:@"_"])
{
luaName = [luaName substringWithRange:NSMakeRange(0, luaName.length - 1)];
}
}

range = [luaName rangeOfString:@"By"];
if (range.location != NSNotFound)
else
{
luaName = [luaName substringToIndex:range.location];
NSRange range = [luaName rangeOfString:@":"];
if (range.location != NSNotFound)
{
luaName = [luaName substringToIndex:range.location];
}

range = [luaName rangeOfString:@"With"];
if (range.location != NSNotFound)
{
luaName = [luaName substringToIndex:range.location];
}

range = [luaName rangeOfString:@"At"];
if (range.location != NSNotFound)
{
luaName = [luaName substringToIndex:range.location];
}

range = [luaName rangeOfString:@"By"];
if (range.location != NSNotFound)
{
luaName = [luaName substringToIndex:range.location];
}
}


return luaName;
}
Expand Down
1 change: 1 addition & 0 deletions Source/iOS_OSX/Code/LuaScriptCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#import "LSCTuple.h"
#import "LSCManagedValue.h"
#import "LSCScriptController.h"
#import "LSCConfig.h"

#import "LSCExportType.h"
#import "LSCExportTypeAnnotation.h"
Expand Down
33 changes: 33 additions & 0 deletions Source/iOS_OSX/LSCConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// LSCConfig.h
// LuaScriptCore
//
// Created by 冯鸿杰 on 2019/4/4.
// Copyright © 2019年 vimfung. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN


/**
配置信息
*/
@interface LSCConfig : NSObject

/**
是否使用完整名称导出
*/
@property (nonatomic) BOOL fullExportName;

/**
获取默认配置
@return 配置信息
*/
+ (instancetype)defaultConfig;

@end

NS_ASSUME_NONNULL_END
26 changes: 26 additions & 0 deletions Source/iOS_OSX/LSCConfig.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// LSCConfig.m
// LuaScriptCore
//
// Created by 冯鸿杰 on 2019/4/4.
// Copyright © 2019年 vimfung. All rights reserved.
//

#import "LSCConfig.h"

@implementation LSCConfig

+ (instancetype)defaultConfig
{
static LSCConfig *config = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{

config = [[LSCConfig alloc] init];

});

return config;
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ FOUNDATION_EXPORT const unsigned char LuaScriptCore_OSX_SwiftVersionString[];
#import <LuaScriptCore_OSX_Swift/LSCTuple.h>
#import <LuaScriptCore_OSX_Swift/LSCManagedValue.h>
#import <LuaScriptCore_OSX_Swift/LSCScriptController.h>
#import <LuaScriptCore_OSX_Swift/LSCConfig.h>

#import <LuaScriptCore_OSX_Swift/LSCExportType.h>
#import <LuaScriptCore_OSX_Swift/LSCExportTypeAnnotation.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ FOUNDATION_EXPORT const unsigned char LuaScriptCore_iOS_SwiftVersionString[];
#import <LuaScriptCore_iOS_Swift/LSCTuple.h>
#import <LuaScriptCore_iOS_Swift/LSCManagedValue.h>
#import <LuaScriptCore_iOS_Swift/LSCScriptController.h>
#import <LuaScriptCore_iOS_Swift/LSCConfig.h>

#import <LuaScriptCore_iOS_Swift/LSCExportType.h>
#import <LuaScriptCore_iOS_Swift/LSCExportTypeAnnotation.h>
Expand Down
Loading

0 comments on commit 9af33ee

Please sign in to comment.