Skip to content

Commit e374506

Browse files
committed
Implement NSRunLoop
1 parent 12ae4fe commit e374506

File tree

4 files changed

+116
-10
lines changed

4 files changed

+116
-10
lines changed

Foundation.xcodeproj/project.pbxproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,13 @@
198198
5BF7AEBF1BCD51F9008F214A /* NSURL.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BDC3F4A1BCC5DCB00ED97BB /* NSURL.swift */; };
199199
5BF7AEC01BCD51F9008F214A /* NSUUID.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BDC3F4B1BCC5DCB00ED97BB /* NSUUID.swift */; };
200200
5BF7AEC11BCD51F9008F214A /* NSValue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BDC3F4C1BCC5DCB00ED97BB /* NSValue.swift */; };
201+
61E0117C1C1B554D000037DD /* TestNSRunLoop.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61E0117B1C1B554D000037DD /* TestNSRunLoop.swift */; };
202+
61E0117D1C1B5590000037DD /* NSRunLoop.swift in Sources */ = {isa = PBXBuildFile; fileRef = EADE0B761BD15DFF00C49C64 /* NSRunLoop.swift */; };
203+
61E0117E1C1B55B9000037DD /* NSTimer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BDC3F481BCC5DCB00ED97BB /* NSTimer.swift */; };
204+
61E0117F1C1B5990000037DD /* CFRunLoop.c in Sources */ = {isa = PBXBuildFile; fileRef = 5B5D88D81BBC9AD800234F36 /* CFRunLoop.c */; };
205+
61E011801C1B5994000037DD /* CFSocket.c in Sources */ = {isa = PBXBuildFile; fileRef = 5B5D88E01BBC9B0300234F36 /* CFSocket.c */; };
206+
61E011811C1B5998000037DD /* CFMessagePort.c in Sources */ = {isa = PBXBuildFile; fileRef = 5B5D88DC1BBC9AEC00234F36 /* CFMessagePort.c */; };
207+
61E011821C1B599A000037DD /* CFMachPort.c in Sources */ = {isa = PBXBuildFile; fileRef = 5B5D88D01BBC9AAC00234F36 /* CFMachPort.c */; };
201208
61F8AE7D1C180FC600FB62F0 /* TestNSNotificationCenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61F8AE7C1C180FC600FB62F0 /* TestNSNotificationCenter.swift */; };
202209
6E203B8D1C1303BB003B2576 /* TestNSBundle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E203B8C1C1303BB003B2576 /* TestNSBundle.swift */; };
203210
7A7D6FBB1C16439400957E2E /* TestNSURLResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A7D6FBA1C16439400957E2E /* TestNSURLResponse.swift */; };
@@ -536,6 +543,7 @@
536543
5BDC405C1BD6D83B00ED97BB /* TestFoundation.app */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = TestFoundation.app; sourceTree = BUILT_PRODUCTS_DIR; };
537544
5BF7AEC21BCD568D008F214A /* ForSwiftFoundationOnly.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ForSwiftFoundationOnly.h; sourceTree = "<group>"; };
538545
5EB6A15C1C188FC40037DCB8 /* TestNSJSONSerialization.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSJSONSerialization.swift; sourceTree = "<group>"; };
546+
61E0117B1C1B554D000037DD /* TestNSRunLoop.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSRunLoop.swift; sourceTree = "<group>"; };
539547
61F8AE7C1C180FC600FB62F0 /* TestNSNotificationCenter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSNotificationCenter.swift; sourceTree = "<group>"; };
540548
6E203B8C1C1303BB003B2576 /* TestNSBundle.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSBundle.swift; sourceTree = "<group>"; };
541549
7A7D6FBA1C16439400957E2E /* TestNSURLResponse.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSURLResponse.swift; sourceTree = "<group>"; };
@@ -1056,6 +1064,7 @@
10561064
4DC1D07F1C12EEEF00B5948A /* TestNSPipe.swift */,
10571065
EA66F6401BF1619600136161 /* TestNSPropertyList.swift */,
10581066
E876A73D1C1180E000F279EC /* TestNSRange.swift */,
1067+
61E0117B1C1B554D000037DD /* TestNSRunLoop.swift */,
10591068
EA66F6411BF1619600136161 /* TestNSSet.swift */,
10601069
EA66F6421BF1619600136161 /* TestNSString.swift */,
10611070
C2A9D75B1C15C08B00993803 /* TestNSUUID.swift */,
@@ -1592,6 +1601,7 @@
15921601
EADE0BA91BD15E0000C49C64 /* NSNull.swift in Sources */,
15931602
EADE0BC01BD15E0000C49C64 /* NSURLProtectionSpace.swift in Sources */,
15941603
5BF7AEAC1BCD51F9008F214A /* NSEnumerator.swift in Sources */,
1604+
61E0117E1C1B55B9000037DD /* NSTimer.swift in Sources */,
15951605
EADE0BCD1BD15E0000C49C64 /* NSXMLParser.swift in Sources */,
15961606
5BDC3FD01BCF17E600ED97BB /* NSCFSet.swift in Sources */,
15971607
EADE0B931BD15DFF00C49C64 /* NSComparisonPredicate.swift in Sources */,
@@ -1659,6 +1669,7 @@
16591669
5BF7AEB41BCD51F9008F214A /* NSObject.swift in Sources */,
16601670
EADE0B521BD09F2F00C49C64 /* NSByteCountFormatter.swift in Sources */,
16611671
EADE0BC11BD15E0000C49C64 /* NSURLProtocol.swift in Sources */,
1672+
61E0117D1C1B5590000037DD /* NSRunLoop.swift in Sources */,
16621673
EADE0BC81BD15E0000C49C64 /* NSXMLDTD.swift in Sources */,
16631674
EADE0BA61BD15E0000C49C64 /* NSMassFormatter.swift in Sources */,
16641675
EADE0BCC1BD15E0000C49C64 /* NSXMLNodeOptions.swift in Sources */,
@@ -1684,6 +1695,7 @@
16841695
buildActionMask = 2147483647;
16851696
files = (
16861697
5B7C8A871BEA7FDB00C5B690 /* CFLocale.c in Sources */,
1698+
61E011801C1B5994000037DD /* CFSocket.c in Sources */,
16871699
5B7C8AB71BEA801700C5B690 /* CFUnicodePrecomposition.c in Sources */,
16881700
5B7C8A721BEA7FCE00C5B690 /* CFBase.c in Sources */,
16891701
5B7C8A951BEA7FEC00C5B690 /* CFXMLTree.c in Sources */,
@@ -1693,6 +1705,7 @@
16931705
5B7C8A7F1BEA7FCE00C5B690 /* CFData.c in Sources */,
16941706
5B7C8A9C1BEA7FF900C5B690 /* CFBundle.c in Sources */,
16951707
5B7C8AB51BEA801700C5B690 /* CFUniChar.c in Sources */,
1708+
61E011811C1B5998000037DD /* CFMessagePort.c in Sources */,
16961709
5B7C8AB61BEA801700C5B690 /* CFUnicodeDecomposition.c in Sources */,
16971710
5B7C8A881BEA7FDB00C5B690 /* CFLocaleIdentifier.c in Sources */,
16981711
5B7C8AB01BEA801700C5B690 /* CFBuiltinConverters.c in Sources */,
@@ -1716,6 +1729,7 @@
17161729
5B7C8A8C1BEA7FE200C5B690 /* CFDate.c in Sources */,
17171730
5B7C8A751BEA7FCE00C5B690 /* CFRuntime.c in Sources */,
17181731
5B7C8A7C1BEA7FCE00C5B690 /* CFBasicHash.c in Sources */,
1732+
61E011821C1B599A000037DD /* CFMachPort.c in Sources */,
17191733
5B7C8A921BEA7FEC00C5B690 /* CFXMLInputStream.c in Sources */,
17201734
5B7C8AAA1BEA800D00C5B690 /* CFBurstTrie.c in Sources */,
17211735
5B7C8A9E1BEA7FF900C5B690 /* CFPlugIn_Instance.c in Sources */,
@@ -1735,6 +1749,7 @@
17351749
5B7C8A971BEA7FF900C5B690 /* CFBundle_Grok.c in Sources */,
17361750
5B7C8ABB1BEA802100C5B690 /* CFURLAccess.c in Sources */,
17371751
5B7C8A901BEA7FEC00C5B690 /* CFOldStylePList.c in Sources */,
1752+
61E0117F1C1B5990000037DD /* CFRunLoop.c in Sources */,
17381753
5B7C8A7B1BEA7FCE00C5B690 /* CFBag.c in Sources */,
17391754
5B7C8AA21BEA800400C5B690 /* CFPreferences.c in Sources */,
17401755
5B7C8A811BEA7FCE00C5B690 /* CFSet.c in Sources */,
@@ -1789,6 +1804,7 @@
17891804
EA66F64C1BF1619600136161 /* TestNSDictionary.swift in Sources */,
17901805
ED58F76F1C134B3A00E6A5BE /* (null) in Sources */,
17911806
EA66F6581BF1619600136161 /* TestNSURL.swift in Sources */,
1807+
61E0117C1C1B554D000037DD /* TestNSRunLoop.swift in Sources */,
17921808
EA66F6441BF1619600136161 /* main.swift in Sources */,
17931809
);
17941810
runOnlyForDeploymentPostprocessing = 0;

Foundation/NSRunLoop.swift

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,51 @@
77
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
88
//
99

10+
import CoreFoundation
1011

11-
public let NSDefaultRunLoopMode: String = "NSDefaultRunLoopMode"
12-
public let NSRunLoopCommonModes: String = "NSRunLoopCommonModes"
12+
public let NSDefaultRunLoopMode: String = kCFRunLoopDefaultMode._swiftObject
13+
public let NSRunLoopCommonModes: String = kCFRunLoopCommonModes._swiftObject
1314

1415
public class NSRunLoop : NSObject {
16+
typealias CFType = CFRunLoopRef
17+
internal var _cfObject : CFType!
18+
internal static var _mainRunLoop : NSRunLoop = {
19+
return NSRunLoop(cfObject: CFRunLoopGetMain())
20+
}()
21+
22+
internal init(cfObject : CFRunLoopRef) {
23+
_cfObject = cfObject
24+
}
1525

1626
public class func currentRunLoop() -> NSRunLoop {
17-
NSUnimplemented()
27+
return NSRunLoop(cfObject: CFRunLoopGetCurrent())
1828
}
1929

2030
public class func mainRunLoop() -> NSRunLoop {
21-
NSUnimplemented()
31+
return _mainRunLoop
2232
}
2333

2434
public var currentMode: String? {
25-
NSUnimplemented()
35+
return CFRunLoopCopyCurrentMode(_cfObject)?._swiftObject
2636
}
2737

2838
public func addTimer(timer: NSTimer, forMode mode: String) {
29-
NSUnimplemented()
39+
CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer._cfObject, mode._cfObject)
3040
}
3141

3242
public func addPort(aPort: NSPort, forMode mode: String) {
43+
// CFRunLoopAddSource(CFRunLoopGetCurrent(), aPort._cfObject, mode._cfObject)
3344
NSUnimplemented()
3445
}
3546

3647
public func removePort(aPort: NSPort, forMode mode: String) {
48+
// CFRunLoopRemoveSource(CFRunLoopGetCurrent(), aPort._cfObject, mode._cfObject)
3749
NSUnimplemented()
3850
}
3951

4052
public func limitDateForMode(mode: String) -> NSDate? {
41-
NSUnimplemented()
53+
let nextTimerFireAbsoluteTime = CFRunLoopGetNextTimerFireDate(CFRunLoopGetCurrent(), mode._cfObject)
54+
return NSDate(timeIntervalSinceReferenceDate: nextTimerFireAbsoluteTime)
4255
}
4356

4457
public func acceptInputForMode(mode: String, beforeDate limitDate: NSDate) {
@@ -50,15 +63,17 @@ public class NSRunLoop : NSObject {
5063
extension NSRunLoop {
5164

5265
public func run() {
53-
NSUnimplemented()
66+
runUntilDate(NSDate.distantFuture());
5467
}
5568

5669
public func runUntilDate(limitDate: NSDate) {
57-
NSUnimplemented()
70+
runMode(NSDefaultRunLoopMode, beforeDate: limitDate)
5871
}
5972

6073
public func runMode(mode: String, beforeDate limitDate: NSDate) -> Bool {
61-
NSUnimplemented()
74+
let result: Int32 = CFRunLoopRunSpecific(_cfObject, mode._cfObject, limitDate.timeIntervalSinceNow, false)
75+
let runloopResult = CFRunLoopRunResult(rawValue: result)
76+
return runloopResult == .HandledSource || runloopResult == .TimedOut
6277
}
6378

6479
}

TestFoundation/TestNSRunLoop.swift

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// This source file is part of the Swift.org open source project
2+
//
3+
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
4+
// Licensed under Apache License v2.0 with Runtime Library Exception
5+
//
6+
// See http://swift.org/LICENSE.txt for license information
7+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
8+
//
9+
10+
11+
#if DEPLOYMENT_RUNTIME_OBJC || os(Linux)
12+
import Foundation
13+
import XCTest
14+
#else
15+
import SwiftFoundation
16+
import SwiftXCTest
17+
#endif
18+
19+
20+
class TestNSRunLoop : XCTestCase {
21+
var allTests : [(String, () -> ())] {
22+
return [
23+
("test_runLoopInit", test_runLoopInit),
24+
("test_runLoopRunMode", test_runLoopRunMode),
25+
("test_runLoopLimitDate", test_runLoopLimitDate),
26+
]
27+
}
28+
29+
func test_runLoopInit() {
30+
let mainRunLoop = NSRunLoop.mainRunLoop()
31+
XCTAssertNotNil(mainRunLoop)
32+
let currentRunLoop = NSRunLoop.currentRunLoop()
33+
XCTAssertNotNil(currentRunLoop)
34+
}
35+
36+
func test_runLoopRunMode() {
37+
let runLoop = NSRunLoop.currentRunLoop()
38+
let timeInterval = NSTimeInterval(0.05)
39+
let endDate = NSDate(timeInterval: timeInterval, sinceDate: NSDate())
40+
var flag = false
41+
42+
let dummyTimer = NSTimer.scheduledTimer(0.01, repeats: false) { _ in
43+
flag = true
44+
guard let runLoopMode = runLoop.currentMode else {
45+
XCTFail("Run loop mode is not defined")
46+
return
47+
}
48+
49+
XCTAssertEqual(runLoopMode, NSDefaultRunLoopMode)
50+
}
51+
runLoop.addTimer(dummyTimer, forMode: NSDefaultRunLoopMode)
52+
let result = runLoop.runMode(NSDefaultRunLoopMode, beforeDate: endDate)
53+
54+
XCTAssertFalse(result) // should be .Finished
55+
XCTAssertTrue(flag)
56+
}
57+
58+
func test_runLoopLimitDate() {
59+
let runLoop = NSRunLoop.currentRunLoop()
60+
let timeInterval = NSTimeInterval(1)
61+
let expectedTimeInterval = NSDate(timeInterval: timeInterval, sinceDate: NSDate()).timeIntervalSince1970
62+
63+
let dummyTimer = NSTimer.scheduledTimer(timeInterval, repeats: false) { _ in }
64+
runLoop.addTimer(dummyTimer, forMode: NSDefaultRunLoopMode)
65+
66+
guard let timerTickInterval = runLoop.limitDateForMode(NSDefaultRunLoopMode)?.timeIntervalSince1970 else {
67+
XCTFail()
68+
return
69+
}
70+
71+
XCTAssertLessThan(abs(timerTickInterval - expectedTimeInterval), 0.01)
72+
}
73+
74+
}

TestFoundation/main.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ XCTMain([
4141
TestNSPipe(),
4242
TestNSPropertyList(),
4343
TestNSRange(),
44+
TestNSRunLoop(),
4445
TestNSScanner(),
4546
TestNSSet(),
4647
TestNSString(),

0 commit comments

Comments
 (0)