Skip to content

Commit 195b5e2

Browse files
committed
Updated logging
1 parent e002eb8 commit 195b5e2

File tree

5 files changed

+61
-4
lines changed

5 files changed

+61
-4
lines changed

Sources/JavaScriptKit/JS Types/JSBluetooth.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public final class JSBluetooth: JSType {
1616

1717
// MARK: - Initialization
1818

19-
public init(_ jsObject: JSObjectRef) {
19+
public init?(_ jsObject: JSObjectRef) {
2020
self.jsObject = jsObject
2121
}
2222

@@ -42,7 +42,7 @@ public final class JSBluetooth: JSType {
4242
// .then(function(bluetoothDevice) { ... })
4343

4444
guard let function = jsObject.requestDevice.function
45-
else { fatalError("Invalid function \(jsObject.requestDevice)") }
45+
else { fatalError("Invalid function \(#function)") }
4646

4747
// FIXME: Improve, support all options
4848
let options = JSObject()
@@ -56,3 +56,8 @@ public final class JSBluetooth: JSType {
5656
return promise
5757
}
5858
}
59+
60+
internal extension JSBluetooth {
61+
62+
static let classObject = JSObjectRef.global.Bluetooth.function!
63+
}

Sources/JavaScriptKit/JS Types/JSBluetoothDevice.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public final class JSBluetoothDevice: JSType {
1414

1515
// MARK: - Initialization
1616

17-
public init(_ jsObject: JSObjectRef) {
17+
public init?(_ jsObject: JSObjectRef) {
1818
self.jsObject = jsObject
1919
}
2020

@@ -24,7 +24,9 @@ public final class JSBluetoothDevice: JSType {
2424
public lazy var id: String = self.jsObject.get("id").string!
2525

2626
/// A string that provices a human-readable name for the device.
27-
public lazy var name: String? = self.jsObject.name.string
27+
public var name: String? {
28+
return self.jsObject.name.string
29+
}
2830

2931
/// Interface of the Web Bluetooth API represents a GATT Server on a remote device.
3032
public lazy var gatt = self.jsObject.gatt.object.flatMap({ JSBluetoothRemoteGATTServer($0) })!

Sources/JavaScriptKit/JS Types/JSBoolean.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,23 @@ public final class JSBoolean: JSType {
2323
}
2424
}
2525

26+
// MARK: - RawRepresentable
27+
28+
extension JSBoolean: RawRepresentable {
29+
30+
public convenience init(rawValue: Bool) {
31+
self.init(rawValue)
32+
}
33+
34+
public var rawValue: Bool {
35+
guard let function = jsObject.valueOf.function
36+
else { fatalError("Invalid function \(#function)") }
37+
return function.apply(this: jsObject).boolean ?? false
38+
}
39+
}
40+
41+
// MARK: - Constants
42+
2643
internal extension JSBoolean {
2744

2845
static let classObject = JSObjectRef.global.Boolean.function!

Sources/JavaScriptKit/JS Types/JSConsole.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ public final class JSConsole: JSType {
5353
public static func assert(_ condition: @autoclosure () -> (Bool), _ arguments: JSValueConvertible...) {
5454
assertFunction.dynamicallyCall(withArguments: [condition()] + arguments.map(print))
5555
}
56+
57+
/**
58+
The `console.assert()` method writes an error message to the console if the assertion is false. If the assertion is true, nothing happens.
59+
*/
60+
public static func assert(_ condition: @autoclosure () -> (JSBoolean), _ arguments: JSValueConvertible...) {
61+
assert(condition().rawValue, arguments)
62+
}
5663
}
5764

5865
internal extension JSConsole {

Sources/JavaScriptKit/Utilities.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// Utilities.swift
3+
//
4+
//
5+
// Created by Alsey Coleman Miller on 6/4/20.
6+
//
7+
8+
/// Unconditionally prints a given message and stops execution.
9+
internal func fatalError(_ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) -> Never {
10+
let message = message()
11+
JSConsole.error("Fatal error: \(message)")
12+
Swift.fatalError(message, file: file, line: line)
13+
}
14+
15+
/// Performs a traditional C-style assert with an optional message.
16+
internal func assert(_ condition: @autoclosure () -> Bool, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) {
17+
let condition = condition()
18+
let message = message()
19+
JSConsole.assert(condition, "Assertion failure: \(message)")
20+
Swift.assert(condition, message, file: file, line: line)
21+
}
22+
23+
/// Performs a traditional C-style assert with an optional message.
24+
internal func assert(_ condition: @autoclosure () -> JSBoolean, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) {
25+
assert(condition().rawValue, message(), file: file, line: line)
26+
}

0 commit comments

Comments
 (0)