Skip to content

Commit 21eca31

Browse files
committed
WIP: Add linux support
1 parent dfa65ef commit 21eca31

File tree

6 files changed

+262
-14
lines changed

6 files changed

+262
-14
lines changed

.github/workflows/swift.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,36 @@ jobs:
3232
- name: Upload Pages artifact
3333
uses: actions/upload-pages-artifact@v2
3434

35+
build-linux:
36+
37+
runs-on: ubuntu-latest
38+
39+
steps:
40+
- name: Checkout repository
41+
uses: actions/checkout@v3
42+
with:
43+
submodules: recursive
44+
- uses: swift-actions/setup-swift@v2
45+
- name: Build
46+
run: swift build -v
47+
- name: Run tests
48+
run: swift test -v
49+
50+
build-oss:
51+
52+
runs-on: macos-13
53+
54+
steps:
55+
- name: Checkout repository
56+
uses: actions/checkout@v3
57+
with:
58+
submodules: recursive
59+
- uses: swift-actions/setup-swift@v2
60+
- name: Build
61+
run: swift build -v
62+
- name: Run tests
63+
run: swift test -v
64+
3565
deploy:
3666
needs: build
3767
if: ${{ github.ref == 'refs/heads/main' }}

Package.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ let package = Package(
3434
resources: [],
3535
swiftSettings: [
3636
// .define("LUASWIFT_NO_FOUNDATION")
37+
.define("LUASWIFT_ANYHASHABLE_BROKEN", .when(platforms: [.linux]))
3738
]
3839
),
3940
.target(
@@ -82,6 +83,7 @@ let package = Package(
8283
],
8384
swiftSettings: [
8485
// .define("LUASWIFT_NO_FOUNDATION")
86+
.define("LUASWIFT_ANYHASHABLE_BROKEN", .when(platforms: [.linux]))
8587
],
8688
plugins: [
8789
.plugin(name: "EmbedLuaPlugin")

Sources/Lua/LuaFoundation.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#if !LUASWIFT_NO_FOUNDATION // canImport(Foundation)
55

66
import Foundation
7+
import CoreFoundation
78
import CLua
89

910
/// Represents all the String encodings that this framework can convert strings to and from.

Sources/Lua/LuaState.swift

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,9 +935,13 @@ extension UnsafeMutablePointer where Pointee == lua_State {
935935
return ptr
936936
case .number:
937937
if let intVal = tointeger(index) {
938+
#if LUASWIFT_ANYHASHABLE_BROKEN
939+
return intVal
940+
#else
938941
// Integers are returned type-erased (thanks to AnyHashable) meaning fewer cast restrictions in
939942
// eg tovalue()
940943
return AnyHashable(intVal)
944+
#endif
941945
} else {
942946
return tonumber(index)
943947
}
@@ -1117,6 +1121,48 @@ extension UnsafeMutablePointer where Pointee == lua_State {
11171121
}
11181122
}
11191123

1124+
#if LUASWIFT_ANYHASHABLE_BROKEN
1125+
// Then the directCast clause above won't have worked, and we need to try every integer type
1126+
if t == .number, let intVal = value as? lua_Integer {
1127+
if let intSubType = Int(exactly: intVal), let ret = intSubType as? T {
1128+
return ret
1129+
}
1130+
if let intSubType = Int8(exactly: intVal), let ret = intSubType as? T {
1131+
return ret
1132+
}
1133+
if let intSubType = Int16(exactly: intVal), let ret = intSubType as? T {
1134+
return ret
1135+
}
1136+
if let intSubType = Int32(exactly: intVal), let ret = intSubType as? T {
1137+
return ret
1138+
}
1139+
if let intSubType = Int64(exactly: intVal), let ret = intSubType as? T {
1140+
return ret
1141+
}
1142+
if let intSubType = UInt(exactly: intVal), let ret = intSubType as? T {
1143+
return ret
1144+
}
1145+
if let intSubType = UInt8(exactly: intVal), let ret = intSubType as? T {
1146+
return ret
1147+
}
1148+
if let intSubType = UInt16(exactly: intVal), let ret = intSubType as? T {
1149+
return ret
1150+
}
1151+
if let intSubType = UInt32(exactly: intVal), let ret = intSubType as? T {
1152+
return ret
1153+
}
1154+
if let intSubType = UInt64(exactly: intVal), let ret = intSubType as? T {
1155+
return ret
1156+
}
1157+
if let dbl = Double(exactly: intVal), let ret = dbl as? T {
1158+
return ret
1159+
}
1160+
if let flt = Float(exactly: intVal), let ret = flt as? T {
1161+
return ret
1162+
}
1163+
}
1164+
#endif
1165+
11201166
return nil
11211167
}
11221168

Sources/Lua/LuaTableRef.swift

Lines changed: 132 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public struct LuaTableRef {
8888
}
8989
}
9090

91+
// ElementType will only ever be Any or AnyHashable (needed when resolving Dictionary keys)
9192
private func doResolveArray<ElementType>(test: (Array<ElementType>) -> Bool) -> Array<ElementType>? {
9293
var testArray = Array<ElementType>()
9394
func good(_ val: Any) -> Bool {
@@ -119,7 +120,11 @@ public struct LuaTableRef {
119120
elementType = .rawpointer
120121
acceptsAny = false
121122
} else {
123+
#if LUASWIFT_ANYHASHABLE_BROKEN
124+
elementType = TypeConstraint(intTest: good)
125+
#else
122126
elementType = nil
127+
#endif
123128
acceptsAny = false
124129
}
125130

@@ -162,6 +167,10 @@ public struct LuaTableRef {
162167
result.append(ref.guessType()) // as per tovalue() docs
163168
case .dict, .array, .hashableDict, .hashableArray, .direct, .rawpointer: // None of these are applicable for TypeConstraint(stringTest:)
164169
fatalError()
170+
#if LUASWIFT_ANYHASHABLE_BROKEN
171+
case .int, .int8, .int16, .int32, .int64, .uint, .uint8, .uint16, .uint32, .uint64: // Ditto
172+
fatalError()
173+
#endif
165174
case .none: // TypeConstraint(stringTest:) failed to find any compatible type
166175
return nil
167176
}
@@ -193,10 +202,14 @@ public struct LuaTableRef {
193202
resolvedVal = nil
194203
}
195204
case .string, .bytes, .direct, .rawpointer: // None of these are applicable for TypeConstraint(tableTest:)
196-
return nil
205+
fatalError()
206+
#if LUASWIFT_ANYHASHABLE_BROKEN
207+
case .int, .int8, .int16, .int32, .int64, .uint, .uint8, .uint16, .uint32, .uint64: // Ditto
208+
fatalError()
209+
#endif
197210
#if !LUASWIFT_NO_FOUNDATION
198211
case .data: // ditto
199-
return nil
212+
fatalError()
200213
#endif
201214
case .none: // TypeConstraint(tableTest:) failed to find any compatible type
202215
return nil
@@ -211,6 +224,19 @@ public struct LuaTableRef {
211224
} else if elementType == .rawpointer, let mut = value as? UnsafeMutableRawPointer {
212225
result.append(UnsafeRawPointer(mut))
213226
} else {
227+
#if LUASWIFT_ANYHASHABLE_BROKEN
228+
if let elementType {
229+
switch elementType {
230+
case .int, .int8, .int16, .int32, .int64, .uint, .uint8, .uint16, .uint32, .uint64:
231+
// Reuse PossibleValue's actualValue cos I'm lazy (L isn't actually used but must be specified...)
232+
result.append(PossibleValue(type: elementType, testValue: value).actualValue(L, 0, value)!)
233+
continue
234+
default:
235+
break
236+
}
237+
238+
}
239+
#endif
214240
// Nothing from toany has made T happy, give up
215241
return nil
216242
}
@@ -330,6 +356,10 @@ public struct LuaTableRef {
330356
case .data: self.testValue = emptyData
331357
#endif
332358
case .direct: self.testValue = testValue!
359+
#if LUASWIFT_ANYHASHABLE_BROKEN
360+
case .int, .int8, .int16, .int32, .int64, .uint, .uint8, .uint16, .uint32, .uint64:
361+
self.testValue = testValue!
362+
#endif
333363
case .luavalue: self.testValue = LuaValue.nilValue
334364
case .anyhashable: self.testValue = opaqueHashable
335365
case .rawpointer: self.testValue = dummyRawPtr
@@ -346,6 +376,18 @@ public struct LuaTableRef {
346376
case .array, .hashableArray: fatalError("Can't call actualValue on an array")
347377
case .dict, .hashableDict: fatalError("Can't call actualValue on a dict")
348378
case .direct: return anyVal
379+
#if LUASWIFT_ANYHASHABLE_BROKEN
380+
case .int: return Int(exactly: anyVal as! lua_Integer)
381+
case .int8: return Int8(exactly: anyVal as! lua_Integer)
382+
case .int16: return Int16(exactly: anyVal as! lua_Integer)
383+
case .int32: return Int32(exactly: anyVal as! lua_Integer)
384+
case .int64: return Int64(exactly: anyVal as! lua_Integer)
385+
case .uint: return UInt(exactly: anyVal as! lua_Integer)
386+
case .uint8: return UInt8(exactly: anyVal as! lua_Integer)
387+
case .uint16: return UInt16(exactly: anyVal as! lua_Integer)
388+
case .uint32: return UInt32(exactly: anyVal as! lua_Integer)
389+
case .uint64: return UInt64(exactly: anyVal as! lua_Integer)
390+
#endif
349391
case .luavalue: return L.ref(index: index)
350392
case .anyhashable:
351393
if let stringRef {
@@ -416,6 +458,46 @@ public struct LuaTableRef {
416458
if type == nil || type == .direct {
417459
result.append(PossibleValue(type: .direct, testValue: value))
418460
}
461+
#if LUASWIFT_ANYHASHABLE_BROKEN
462+
if value is lua_Integer {
463+
if type == nil || type == .int {
464+
result.append(PossibleValue(type: .int, testValue: 0 as Int))
465+
}
466+
if type == nil || type == .int8 {
467+
result.append(PossibleValue(type: .int8, testValue: 0 as Int8))
468+
}
469+
if type == nil || type == .int16 {
470+
result.append(PossibleValue(type: .int16, testValue: 0 as Int16))
471+
}
472+
if type == nil || type == .int16 {
473+
result.append(PossibleValue(type: .int16, testValue: 0 as Int16))
474+
}
475+
if type == nil || type == .int32 {
476+
result.append(PossibleValue(type: .int32, testValue: 0 as Int32))
477+
}
478+
if type == nil || type == .int64 {
479+
result.append(PossibleValue(type: .int64, testValue: 0 as Int64))
480+
}
481+
if type == nil || type == .uint {
482+
result.append(PossibleValue(type: .uint, testValue: 0 as UInt))
483+
}
484+
if type == nil || type == .uint8 {
485+
result.append(PossibleValue(type: .uint8, testValue: 0 as UInt8))
486+
}
487+
if type == nil || type == .uint16 {
488+
result.append(PossibleValue(type: .uint16, testValue: 0 as UInt16))
489+
}
490+
if type == nil || type == .uint16 {
491+
result.append(PossibleValue(type: .uint16, testValue: 0 as UInt16))
492+
}
493+
if type == nil || type == .uint32 {
494+
result.append(PossibleValue(type: .uint32, testValue: 0 as UInt32))
495+
}
496+
if type == nil || type == .uint64 {
497+
result.append(PossibleValue(type: .uint64, testValue: 0 as UInt64))
498+
}
499+
}
500+
#endif
419501
if type == nil || type == .rawpointer {
420502
result.append(PossibleValue(type: .rawpointer))
421503
}
@@ -462,6 +544,18 @@ enum TypeConstraint {
462544
case anyhashable // AnyHashable (or Any, in some contexts)
463545
case luavalue
464546
case rawpointer // UnsafeRawPointer (relevant when we have UnsafeMutableRawPointer from a [light]userdata)
547+
#if LUASWIFT_ANYHASHABLE_BROKEN
548+
case int
549+
case int8
550+
case int16
551+
case int32
552+
case int64
553+
case uint
554+
case uint8
555+
case uint16
556+
case uint32
557+
case uint64
558+
#endif
465559
}
466560

467561
extension TypeConstraint {
@@ -495,6 +589,34 @@ extension TypeConstraint {
495589
return nil
496590
}
497591
}
592+
593+
#if LUASWIFT_ANYHASHABLE_BROKEN
594+
init?(intTest test: (Any) -> Bool) {
595+
if test(0 as Int) {
596+
self = .int
597+
} else if test(0 as Int8) {
598+
self = .int8
599+
} else if test(0 as Int16) {
600+
self = .int16
601+
} else if test(0 as Int32) {
602+
self = .int32
603+
} else if test(0 as Int64) {
604+
self = .int64
605+
} else if test(0 as UInt) {
606+
self = .uint
607+
} else if test(0 as UInt8) {
608+
self = .uint8
609+
} else if test(0 as UInt16) {
610+
self = .uint16
611+
} else if test(0 as UInt32) {
612+
self = .uint32
613+
} else if test(0 as UInt64) {
614+
self = .uint64
615+
} else {
616+
return nil
617+
}
618+
}
619+
#endif
498620
}
499621

500622
fileprivate func isArrayType<T>(_: T?) -> Bool {
@@ -517,9 +639,9 @@ extension Dictionary where Key == AnyHashable, Value == Any {
517639
///
518640
/// This function is also defined on `Dictionary<AnyHashable, AnyHashable>`, see ``luaTableToArray()-3ngmn``.
519641
public func luaTableToArray() -> [Any]? {
520-
var intKeys: [Int] = []
642+
var intKeys: [lua_Integer] = []
521643
for (k, _) in self {
522-
if let intKey = k as? Int, intKey > 0 {
644+
if let intKey = k as? lua_Integer, intKey > 0 {
523645
intKeys.append(intKey)
524646
} else {
525647
// Non integer key found, doom
@@ -530,9 +652,9 @@ extension Dictionary where Key == AnyHashable, Value == Any {
530652
// Now check all those integer keys are a sequence and build the result
531653
intKeys.sort()
532654
var result: [Any] = []
533-
var i = 1
655+
var i: lua_Integer = 1
534656
while i <= intKeys.count {
535-
if intKeys[i-1] == i {
657+
if intKeys[Int(i-1)] == i {
536658
result.append(self[i]!)
537659
i = i + 1
538660
} else {
@@ -557,9 +679,9 @@ extension Dictionary where Key == AnyHashable, Value == AnyHashable {
557679
///
558680
/// This function is also defined on `Dictionary<AnyHashable, Any>`, see ``luaTableToArray()-7jqqs``.
559681
public func luaTableToArray() -> [AnyHashable]? {
560-
var intKeys: [Int] = []
682+
var intKeys: [lua_Integer] = []
561683
for (k, _) in self {
562-
if let intKey = k as? Int, intKey > 0 {
684+
if let intKey = k as? lua_Integer, intKey > 0 {
563685
intKeys.append(intKey)
564686
} else {
565687
// Non integer key found, doom
@@ -570,9 +692,9 @@ extension Dictionary where Key == AnyHashable, Value == AnyHashable {
570692
// Now check all those integer keys are a sequence and build the result
571693
intKeys.sort()
572694
var result: [AnyHashable] = []
573-
var i = 1
695+
var i: lua_Integer = 1
574696
while i <= intKeys.count {
575-
if intKeys[i-1] == i {
697+
if intKeys[Int(i-1)] == i {
576698
result.append(self[i]!)
577699
i = i + 1
578700
} else {

0 commit comments

Comments
 (0)