Skip to content

Commit 3601d48

Browse files
committed
1、修复对象创建时在init方法中调用self相关的属性和方法导致栈溢出问题。
2、修复iOS/OSX下Lua中直接调用subclass派生类型无法进行扩展类型属性定义问题。 Former-commit-id: e877f01e6aa34b5126506e10fb183f68209d01f7
1 parent 4d579a8 commit 3601d48

22 files changed

Lines changed: 178 additions & 283 deletions

Sample/iOS_OSX/LuaScript/test.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ function LSCTPerson.prototype:destroy ()
55
end
66

77
local person = LSCTPerson.create();
8-
person:setName('vimfung');
8+
person.name = "vimfung";
99
person:walk();
1010
person:speak();

Sample/iOS_OSX/Sample-OSX-Swift/LSCTNativeData.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,25 @@
77
//
88

99
import Cocoa
10+
import LuaScriptCore_OSX_Swift
1011

11-
class LSCTNativeData: NSObject
12+
class LSCTNativeData: NSObject, LuaExportType
1213
{
13-
var dataId : String? = nil;
14+
@objc var dataId : String? = nil;
1415

1516
private var _data : Dictionary<String, String> = Dictionary();
1617

17-
public class func createData () -> LSCTNativeData
18+
@objc public class func createData () -> LSCTNativeData
1819
{
1920
return LSCTNativeData();
2021
}
2122

22-
public func setData(value : String, key : String) -> Void
23+
@objc public func setData(value : String, key : String) -> Void
2324
{
2425
_data[key] = value;
2526
}
2627

27-
public func getData(key : String) -> String
28+
@objc public func getData(key : String) -> String
2829
{
2930
return _data[key]!;
3031
}

Sample/iOS_OSX/Sample-OSX-Swift/LSCTPerson.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88

99
import LuaScriptCore_OSX_Swift
1010

11-
class LSCTPerson: LSCObjectClass
11+
class LSCTPerson: NSObject, LuaExportType
1212
{
13-
var name : String? = nil;
13+
@objc var name : String? = nil;
1414

15-
func speak() -> Void
15+
@objc func speak() -> Void
1616
{
1717
NSLog("%@ speak", name ?? "noname");
1818
}
1919

20-
func walk() -> Void
20+
@objc func walk() -> Void
2121
{
2222
NSLog("%@ walk", name ?? "noname");
2323
}

Sample/iOS_OSX/Sample-OSX-Swift/LogModule.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
import LuaScriptCore_OSX_Swift
1010

11-
class LogModule: LSCModule
11+
class LogModule: NSObject, LuaExportType
1212
{
13-
static func writeLog(message : String) -> Void
13+
@objc static func writeLog(message : String) -> Void
1414
{
1515
NSLog("** message = %@", message);
1616
}

Sample/iOS_OSX/Sample-OSX-Swift/ViewController1.swift

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ class ViewController1: NSViewController {
1313

1414
var _context : LuaContext = LuaContext();
1515
var _hasRegMethod : Bool = false;
16-
var _hasRegModule : Bool = false;
17-
var _hasRegClass : Bool = false;
18-
var _hasImportClass : Bool = false;
1916

2017
override func viewDidLoad() {
2118
super.viewDidLoad()
@@ -79,12 +76,6 @@ class ViewController1: NSViewController {
7976
/// - Parameter sender: 事件对象
8077
@IBAction func regModuleButtonClickedHandler(_ sender: Any)
8178
{
82-
if (!_hasRegModule)
83-
{
84-
_hasRegModule = true;
85-
_context.registerModule(moduleClass: LogModule.self);
86-
}
87-
8879
_ = _context.evalScript(script: "LogModule.writeLog('Hello Lua Module!');");
8980
}
9081

@@ -93,12 +84,6 @@ class ViewController1: NSViewController {
9384
/// - Parameter sender: 事件对象
9485
@IBAction func regClassButtonClickedHandler(_ sender: Any)
9586
{
96-
if (!_hasRegClass)
97-
{
98-
_hasRegClass = true;
99-
_context.registerModule(moduleClass: LSCTPerson.self);
100-
}
101-
10287
_ = _context.evalScript(filePath: "test.lua");
10388
}
10489

@@ -107,14 +92,7 @@ class ViewController1: NSViewController {
10792
/// - Parameter sender: 事件对象
10893
@IBAction func importNativeClassButtonClickedHandler(_ sender: Any)
10994
{
110-
if !_hasRegModule
111-
{
112-
_hasImportClass = true;
113-
_context.registerModule(moduleClass: LuaClassImport.self);
114-
LuaClassImport.setInculdesClasses(classes: [LSCTNativeData.self], context: _context);
115-
}
116-
117-
_ = _context.evalScript(script: "local Data = ClassImport('Sample_OSX_Swift.LSCTNativeData'); print(Data); local d = Data.create(); print(d); d:setDataId('xxxx'); print(d:dataId()); d:setData('xxx','testKey'); print(d:getData('testKey'));");
95+
_ = _context.evalScript(script: "local Data = LSCTNativeData; print(Data); local d = Data.create(); print(d); d.dataId = 'xxxx'; print(d.dataId); d:setData('xxx','testKey'); print(d:getData('testKey'));");
11896
}
11997
}
12098

Sample/iOS_OSX/Sample-OSX/ViewController.m

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,12 @@ @interface ViewController ()
2424
*/
2525
@property(nonatomic) BOOL hasRegMethod;
2626

27-
/**
28-
* 是否注册模块
29-
*/
30-
@property (nonatomic) BOOL hasRegModule;
31-
32-
/**
33-
* 是否注册类
34-
*/
35-
@property (nonatomic) BOOL hasRegClass;
36-
37-
/**
38-
是否导入类
39-
*/
40-
@property (nonatomic) BOOL hasImportClass;
41-
4227
@end
4328

4429
@implementation ViewController
4530

46-
- (void)viewDidLoad {
31+
- (void)viewDidLoad
32+
{
4733
[super viewDidLoad];
4834

4935
// Do any additional setup after loading the view.
@@ -132,12 +118,6 @@ - (IBAction)callLuaMethodClickedHandler:(id)sender {
132118
*/
133119
- (IBAction)registerModuleClickedHandler:(id)sender
134120
{
135-
if (!self.hasRegModule)
136-
{
137-
self.hasRegModule = YES;
138-
[self.context registerModuleWithClass:[LogModule class]];
139-
}
140-
141121
[self.context evalScriptFromString:@"LogModule.writeLog('Hello Lua Module!');"];
142122
}
143123

@@ -149,12 +129,6 @@ - (IBAction)registerModuleClickedHandler:(id)sender
149129
*/
150130
- (IBAction)registerClassClickedHandler:(id)sender
151131
{
152-
if (!self.hasRegClass)
153-
{
154-
self.hasRegClass = YES;
155-
[self.context registerModuleWithClass:[LSCTPerson class]];
156-
}
157-
158132
[self.context evalScriptFromFile:[[NSBundle mainBundle] pathForResource:@"test"
159133
ofType:@"lua"]];
160134
}
@@ -166,14 +140,7 @@ - (IBAction)registerClassClickedHandler:(id)sender
166140
*/
167141
- (IBAction)importNativeClassClickedHandler:(id)sender
168142
{
169-
if (!self.hasImportClass)
170-
{
171-
self.hasImportClass = YES;
172-
[self.context registerModuleWithClass:[LSCClassImport class]];
173-
[LSCClassImport setInculdesClasses:@[LSCTPerson.class, LSCTNativeData.class] withContext:self.context];
174-
}
175-
176-
[self.context evalScriptFromString:@"local Data = ClassImport('LSCTNativeData'); print(Data); local d = Data.create(); print(d); d:setDataId('xxxx'); print(d:dataId()); d:setData('xxx','testKey'); print(d:getData('testKey'));"];
143+
[self.context evalScriptFromString:@"local Data = LSCTNativeData; print(Data); local d = Data.create(); print(d); d.dataId = 'xxxx'; print(d.dataId); d:setData('xxx','testKey'); print(d:getData('testKey'));"];
177144
}
178145

179146
@end

Sample/iOS_OSX/Sample-iOS-Swift/LSCTNativeData.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,25 @@
77
//
88

99
import UIKit
10+
import LuaScriptCore_iOS_Swift
1011

11-
class LSCTNativeData: NSObject
12+
class LSCTNativeData: NSObject, LuaExportType
1213
{
13-
var dataId : String? = nil;
14+
@objc var dataId : String? = nil;
1415

1516
private var _data : Dictionary<String, String> = Dictionary();
1617

17-
public class func createData () -> LSCTNativeData
18+
@objc public class func createData () -> LSCTNativeData
1819
{
1920
return LSCTNativeData();
2021
}
2122

22-
public func setData(value : String, key : String) -> Void
23+
@objc public func setData(value : String, key : String) -> Void
2324
{
2425
_data[key] = value;
2526
}
2627

27-
public func getData(key : String) -> String
28+
@objc public func getData(key : String) -> String
2829
{
2930
return _data[key]!;
3031
}

Sample/iOS_OSX/Sample-iOS-Swift/LSCTPerson.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88

99
import LuaScriptCore_iOS_Swift
1010

11-
class LSCTPerson: LSCObjectClass
11+
class LSCTPerson : NSObject, LuaExportType
1212
{
1313
var name : String? = nil;
1414

15-
func speak() -> Void
15+
@objc func speak() -> Void
1616
{
1717
NSLog("%@ speak", name ?? "noname");
1818
}
1919

20-
func walk() -> Void
20+
@objc func walk() -> Void
2121
{
2222
NSLog("%@ walk", name ?? "noname");
2323
}

Sample/iOS_OSX/Sample-iOS-Swift/LogModule.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
import LuaScriptCore_iOS_Swift
1010

11-
class LogModule: LSCModule
11+
class LogModule : NSObject, LuaExportType
1212
{
13-
static func writeLog(message : String) -> Void
13+
@objc static func writeLog(message : String) -> Void
1414
{
1515
NSLog("** message = %@", message);
1616
}

Sample/iOS_OSX/Sample-iOS-Swift/ViewController.swift

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ class ViewController: UIViewController {
1313

1414
var _context : LuaContext = LuaContext();
1515
var _hasRegMethod : Bool = false;
16-
var _hasRegModule : Bool = false;
17-
var _hasRegClass : Bool = false;
18-
var _hasImportClass : Bool = false;
1916

2017
/// 解析脚本按钮点击
2118
///
@@ -76,12 +73,6 @@ class ViewController: UIViewController {
7673
/// - Parameter sender: 事件对象
7774
@IBAction func regModuleButtonClickedHandler(_ sender: Any)
7875
{
79-
if (!_hasRegModule)
80-
{
81-
_hasRegModule = true;
82-
_context.registerModule(moduleClass: LogModule.self);
83-
}
84-
8576
_ = _context.evalScript(script: "LogModule.writeLog('Hello Lua Module!');");
8677
}
8778

@@ -91,12 +82,6 @@ class ViewController: UIViewController {
9182
/// - Parameter sender: 事件对象
9283
@IBAction func regClassButtonClickedHandler(_ sender: Any)
9384
{
94-
if (!_hasRegClass)
95-
{
96-
_hasRegClass = true;
97-
_context.registerModule(moduleClass: LSCTPerson.self);
98-
}
99-
10085
_ = _context.evalScript(filePath: "test.lua");
10186
}
10287

@@ -106,14 +91,7 @@ class ViewController: UIViewController {
10691
/// - Parameter sender: 事件对象
10792
@IBAction func importNativeClassButtonClickedHandler(_ sender: Any)
10893
{
109-
if !_hasRegModule
110-
{
111-
_hasImportClass = true;
112-
_context.registerModule(moduleClass: LuaClassImport.self);
113-
LuaClassImport.setInculdesClasses(classes: [LSCTNativeData.self], context: _context);
114-
}
115-
116-
_ = _context.evalScript(script: "local Data = ClassImport('Sample_iOS_Swift.LSCTNativeData'); print(Data); local d = Data.create(); print(d); d:setDataId('xxxx'); print(d:dataId()); d:setData('xxx','testKey'); print(d:getData('testKey'));");
94+
_ = _context.evalScript(script: "local Data = LSCTNativeData; print(Data); local d = Data.create(); print(d); d.dataId = 'xxxx'; print(d.dataId); d:setData('xxx','testKey'); print(d:getData('testKey'));");
11795
}
11896
}
11997

0 commit comments

Comments
 (0)