Skip to content

Commit ad0f26e

Browse files
committed
Swift 2.0 ready
1 parent 28929a2 commit ad0f26e

File tree

13 files changed

+51
-83
lines changed

13 files changed

+51
-83
lines changed

GoSwift.xcodeproj/project.pbxproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@
104104
0311F56F1B20A06F003616AD /* Project object */ = {
105105
isa = PBXProject;
106106
attributes = {
107-
LastUpgradeCheck = 0630;
107+
LastSwiftUpdateCheck = 0700;
108+
LastUpgradeCheck = 0700;
108109
ORGANIZATIONNAME = "ONcast, LLC";
109110
TargetAttributes = {
110111
0311F5771B20A06F003616AD = {
@@ -174,6 +175,7 @@
174175
CURRENT_PROJECT_VERSION = 1;
175176
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
176177
ENABLE_STRICT_OBJC_MSGSEND = YES;
178+
ENABLE_TESTABILITY = YES;
177179
GCC_C_LANGUAGE_STANDARD = gnu99;
178180
GCC_DYNAMIC_NO_PIC = NO;
179181
GCC_NO_COMMON_BLOCKS = YES;
@@ -251,6 +253,7 @@
251253
INFOPLIST_FILE = GoSwift/Info.plist;
252254
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
253255
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
256+
PRODUCT_BUNDLE_IDENTIFIER = "com.oncast.$(PRODUCT_NAME:rfc1034identifier)";
254257
PRODUCT_NAME = "$(TARGET_NAME)";
255258
SKIP_INSTALL = YES;
256259
};
@@ -266,6 +269,7 @@
266269
INFOPLIST_FILE = GoSwift/Info.plist;
267270
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
268271
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
272+
PRODUCT_BUNDLE_IDENTIFIER = "com.oncast.$(PRODUCT_NAME:rfc1034identifier)";
269273
PRODUCT_NAME = "$(TARGET_NAME)";
270274
SKIP_INSTALL = YES;
271275
};

GoSwift.xcodeproj/xcshareddata/xcschemes/GoSwift.xcscheme

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0630"
3+
LastUpgradeVersion = "0700"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"
@@ -62,6 +62,8 @@
6262
ReferencedContainer = "container:GoSwift.xcodeproj">
6363
</BuildableReference>
6464
</MacroExpansion>
65+
<AdditionalOptions>
66+
</AdditionalOptions>
6567
</TestAction>
6668
<LaunchAction
6769
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
@@ -71,6 +73,7 @@
7173
buildConfiguration = "Debug"
7274
ignoresPersistentStateOnLaunch = "NO"
7375
debugDocumentVersioning = "YES"
76+
debugServiceExtension = "internal"
7477
allowLocationSimulation = "YES">
7578
<MacroExpansion>
7679
<BuildableReference

GoSwift/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<key>CFBundleExecutable</key>
88
<string>$(EXECUTABLE_NAME)</string>
99
<key>CFBundleIdentifier</key>
10-
<string>com.oncast.$(PRODUCT_NAME:rfc1034identifier)</string>
10+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
1111
<key>CFBundleInfoDictionaryVersion</key>
1212
<string>6.0</string>
1313
<key>CFBundleName</key>

GoSwift/go.swift

Lines changed: 24 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,18 @@
1111

1212
import Foundation
1313

14-
public typealias Error = NSError
15-
extension Error : Printable, Equatable {
16-
public func error() -> String {
17-
return description
18-
}
19-
convenience public init(error : String){
20-
self.init(domain: "goswift", code: -1, userInfo: [NSLocalizedDescriptionKey:error])
21-
}
22-
override public var description : String {
23-
return localizedDescription
24-
}
25-
}
26-
public func ==(lhs: Error, rhs: Error) -> Bool {
27-
return lhs.description == rhs.description
28-
}
29-
private let pt_entry: @objc_block (UnsafeMutablePointer<Void>) -> UnsafeMutablePointer<Void> = { (ctx) in
14+
private let pt_entry: @convention(c) (UnsafeMutablePointer<Void>) -> UnsafeMutablePointer<Void> = { (ctx) in
3015
let np = UnsafeMutablePointer<()->()>(ctx)
3116
np.memory()
3217
np.destroy()
3318
np.dealloc(1)
3419
return nil
3520
}
36-
private var pt_entry_imp = imp_implementationWithBlock(unsafeBitCast(pt_entry, AnyObject.self))
37-
private let pt_entry_fp = CFunctionPointer<(UnsafeMutablePointer<Void>) -> UnsafeMutablePointer<Void>>(pt_entry_imp)
3821
public func dispatch_thread(block : ()->()){
3922
let p = UnsafeMutablePointer<()->()>.alloc(1)
4023
p.initialize(block)
4124
var t = pthread_t()
42-
pthread_create(&t, nil, pt_entry_fp, p)
25+
pthread_create(&t, nil, pt_entry, p)
4326
pthread_detach(t)
4427
}
4528
public protocol Locker {
@@ -60,7 +43,7 @@ public class Mutex : Locker {
6043
public func unlock(){
6144
pthread_mutex_unlock(&mutex)
6245
}
63-
func lock(closure:()->()){
46+
func lock(closure : ()->()){
6447
lock()
6548
closure()
6649
unlock()
@@ -196,7 +179,7 @@ public class Chan<T> : ChanAny {
196179
cond.locker.unlock()
197180
return (nil, true, false)
198181
}
199-
var msg = msgs.removeAtIndex(0)
182+
let msg = msgs.removeAtIndex(0)
200183
cond.broadcast()
201184
cond.locker.unlock()
202185
return (msg, true, true)
@@ -220,7 +203,7 @@ public class Chan<T> : ChanAny {
220203
if msgs.count > 0 {
221204
flag = true
222205
mutex!.unlock()
223-
var msg = msgs.removeAtIndex(0)
206+
let msg = msgs.removeAtIndex(0)
224207
cond.broadcast()
225208
cond.locker.unlock()
226209
return (msg, true, true)
@@ -237,7 +220,7 @@ public class Chan<T> : ChanAny {
237220
return (nil, false, true)
238221
}
239222
if msgs.count > 0 {
240-
var msg = msgs.removeAtIndex(0)
223+
let msg = msgs.removeAtIndex(0)
241224
cond.broadcast()
242225
cond.locker.unlock()
243226
return (msg, true, true)
@@ -257,12 +240,12 @@ public func <-<T>(l: Chan<T>, r: T?){
257240
}
258241
public prefix func <?<T>(r: Chan<T>) -> (T?, Bool){
259242
var flag = false
260-
let (v, ok, ready) = r.receive(true, mutex: nil, flag: &flag)
243+
let (v, ok, _) = r.receive(true, mutex: nil, flag: &flag)
261244
return (v as? T, ok)
262245
}
263246
public prefix func <-<T>(r: Chan<T>) -> T?{
264247
var flag = false
265-
let (v, ok, ready) = r.receive(true, mutex: nil, flag: &flag)
248+
let (v, _, _) = r.receive(true, mutex: nil, flag: &flag)
266249
return v as? T
267250
}
268251
public func close<T>(chan : Chan<T>){
@@ -281,36 +264,31 @@ private struct GoPanicError {
281264
}
282265
private class GoRoutineStack {
283266
var error = GoPanicError()
284-
var defers : [()->()] = []
285267
var (jump, jumped) = (UnsafeMutablePointer<Int32>(), false)
286-
var (select, cases:[(msg : Any?, ok : Bool)->()], chans:[ChanAny], defalt:(()->())?) = (false, [], [], nil)
268+
var select = false
269+
var cases : [(msg : Any?, ok : Bool)->()] = []
270+
var chans : [ChanAny] = []
271+
var defalt : (()->())?
287272
init(){
288273
jump = UnsafeMutablePointer<Int32>(malloc(4*50))
289274
}
290275
deinit{
291276
free(jump)
292277
}
293-
func unwind(){
294-
for var i = defers.count - 1; i >= 0; i-- {
295-
defers[i]()
296-
}
297-
defers = []
298-
}
299278
}
300279
private class GoRoutine {
301280
var stack = [GoRoutineStack]()
302281
func $(closure:()->()){
303282
let s = GoRoutineStack()
304283
if stack.count > 0 {
305-
var ls = stack.last!
284+
let ls = stack.last!
306285
s.error = ls.error
307286
ls.error.what = nil
308287
}
309288
stack.append(s)
310289
if setjmp(s.jump) == 0{
311290
closure()
312291
}
313-
s.unwind()
314292
stack.removeLast()
315293
if s.error.what != nil{
316294
if stack.count > 0 {
@@ -320,12 +298,6 @@ private class GoRoutine {
320298
}
321299
}
322300
}
323-
func defer(file : StaticString = __FILE__, line : UWord = __LINE__, closure: ()->()){
324-
if stack.count == 0{
325-
fatalError("missing ${} context", file: file, line: line)
326-
}
327-
stack.last!.defers.append({self.${closure()}})
328-
}
329301
func panic(what : AnyObject, file : StaticString = __FILE__, line : UWord = __LINE__){
330302
if stack.count == 0{
331303
fatalError("\(what)", file: file, line: line)
@@ -361,14 +333,14 @@ private class GoRoutine {
361333
}
362334
func select(file : StaticString = __FILE__, line : UWord = __LINE__, closure:()->()){
363335
${
364-
var s = self.stack.last!
336+
let s = self.stack.last!
365337
s.select = true
366338
closure()
367-
var idxs = self.randomInts(s.chans.count)
339+
let idxs = self.randomInts(s.chans.count)
368340
if s.defalt != nil{
369341
var (flag, handled) = (false, false)
370342
for i in idxs {
371-
var (msg, ok, ready) = s.chans[i].receive(false, mutex: nil, flag: &flag)
343+
let (msg, ok, ready) = s.chans[i].receive(false, mutex: nil, flag: &flag)
372344
if ready {
373345
s.cases[i](msg: msg, ok: ok)
374346
handled = true
@@ -386,21 +358,21 @@ private class GoRoutine {
386358
NSThread.sleepForTimeInterval(0.05)
387359
}
388360
} else {
389-
var wg = WaitGroup()
361+
let wg = WaitGroup()
390362
wg.add(idxs.count)
391-
var signal : (except : Int)->() = { (except) in
363+
let signal : (except : Int)->() = { (except) in
392364
for i in idxs {
393365
if i != except {
394366
s.chans[i].signal()
395367
}
396368
}
397369
}
398370
var flag = false
399-
var mutex = Mutex()
371+
let mutex = Mutex()
400372
for i in idxs {
401-
var (c, f, ci) = (s.chans[i], s.cases[i], i)
373+
let (c, f, ci) = (s.chans[i], s.cases[i], i)
402374
dispatch_thread {
403-
var (msg, ok, ready) = c.receive(true, mutex: mutex, flag: &flag)
375+
let (msg, ok, ready) = c.receive(true, mutex: mutex, flag: &flag)
404376
if ready {
405377
signal(except: ci)
406378
f(msg: msg, ok: ok)
@@ -498,21 +470,18 @@ public func $(closure: ()->()){
498470
public func go(closure: ()->()){
499471
goapp.go(closure)
500472
}
501-
public func defer(file : StaticString = __FILE__, line : UWord = __LINE__, closure: ()->()){
502-
goapp.routine().defer(file: file, line: line, closure: closure)
503-
}
504473
public func panic(what : AnyObject, file : StaticString = __FILE__, line : UWord = __LINE__){
505474
goapp.routine().panic(what, file: file, line: line)
506475
}
507476
public func recover(file : StaticString = __FILE__, line : UWord = __LINE__) -> AnyObject? {
508-
return goapp.routine().recover(file: file, line: line)
477+
return goapp.routine().recover(file, line: line)
509478
}
510479
public func select(file : StaticString = __FILE__, line : UWord = __LINE__, closure:()->()) {
511-
goapp.routine().select(file: file, line: line, closure: closure)
480+
goapp.routine().select(file, line: line, closure: closure)
512481
}
513482
public func _case<T>(l : Chan<T>, file : StaticString = __FILE__, line : UWord = __LINE__, closure:(msg : T?, ok : Bool)->()) {
514483
goapp.routine().case_(l, file: file, line: line, closure: { (msg, ok) in closure(msg: msg as? T, ok: ok) })
515484
}
516485
public func _default(file : StaticString = __FILE__, line : UWord = __LINE__, closure:()->()) {
517-
goapp.routine().default_(file: file, line: line, closure: closure)
486+
goapp.routine().default_(file, line: line, closure: closure)
518487
}

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Bring some of the more powerful features of Go to your iOS / Swift project such as channels, goroutines, and defers.
44

5-
***Note: Swift 2 now includes the builtin `defer` keyword. At the moment GoSwift is not compatible with Swift 2.*** *A new version of GoSwift is in the works.*
5+
**Built for Swift 2.0** For Swift 1.2 support use *v0.1.4* or earlier.
66

77
##Features
88

@@ -15,7 +15,6 @@ Bring some of the more powerful features of Go to your iOS / Swift project such
1515
- Closing
1616
- Sync Package
1717
- Mutex, Cond, Once, WaitGroup
18-
- Error type
1918

2019
##Example
2120

examples/channel-buffering.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func main() {
1313

1414
// Here we `make` a channel of strings buffering up to
1515
// 2 values.
16-
var messages = Chan<String>(2)
16+
let messages = Chan<String>(2)
1717

1818
// Because this channel is buffered, we can send these
1919
// values into the channel without a corresponding

examples/channel-synchronization.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func main() {
2222

2323
// Start a worker goroutine, giving it the channel to
2424
// notify on.
25-
var done = Chan<Bool>(1)
25+
let done = Chan<Bool>(1)
2626
go { worker(done) }
2727

2828
// Block until we receive a notification from the

examples/channels.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func main() {
1111

1212
// Create a new channel with `make(chan val-type)`.
1313
// Channels are typed by the values they convey.
14-
var messages = Chan<String>()
14+
let messages = Chan<String>()
1515

1616
// _Send_ a value into a channel using the `channel <-`
1717
// syntax. Here we send `"ping"` to the `messages`
@@ -21,6 +21,6 @@ func main() {
2121
// The `<-channel` syntax _receives_ a value from the
2222
// channel. Here we'll receive the `"ping"` message
2323
// we sent above and print it out.
24-
var msg = <-messages
24+
let msg = <-messages
2525
println(msg!)
2626
}

examples/closing-channels.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
// to a worker goroutine. When we have no more jobs for
1212
// the worker we'll `close` the `jobs` channel.
1313
func main() {
14-
var jobs = Chan<Int>(5)
15-
var done = Chan<Bool>()
14+
let jobs = Chan<Int>(5)
15+
let done = Chan<Bool>()
1616

1717
// Here's the worker goroutine. It repeatedly receives
1818
// from `jobs` with `j, more := <-jobs`. In this
@@ -23,7 +23,7 @@ func main() {
2323
// all our jobs.
2424
go {
2525
for ;; {
26-
var (j, more) = <?jobs
26+
let (j, more) = <?jobs
2727
if more {
2828
println("received job \(j!)")
2929
} else {

examples/defer.swift

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,14 @@
1111
// and then close when we're done. Here's how we could
1212
// do that with `defer`.
1313
func main() {
14-
${ // ${} is required for 'defer', 'panic', 'recover'
15-
// Immediately after getting a file object with
16-
// `createFile`, we defer the closing of that file
17-
// with `closeFile`. This will be executed at the end
18-
// of the enclosing function (`main`), after
19-
// `writeFile` has finished.
20-
var f = createFile("/tmp/defer.txt")
21-
defer { closeFile(f) }
22-
writeFile(f)
23-
}
14+
let f = createFile("/tmp/defer.txt")
15+
defer { closeFile(f) }
16+
writeFile(f)
2417
}
2518

2619
func createFile(p : String) -> UnsafeMutablePointer<FILE> {
2720
println("creating")
28-
var f = fopen(p, "wb+")
21+
let f = fopen(p, "wb+")
2922
if f == nil {
3023
panic("file error")
3124
}

0 commit comments

Comments
 (0)