Skip to content

Commit 479ad20

Browse files
committed
Docs for JSValueCoable-adjacent types
1 parent de5f52e commit 479ad20

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

Sources/JavaScriptKit/JSBridgedType.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
// Use this protocol when your type has no single JavaScript class.
2-
// For example, a union type of multiple classes.
1+
/// Use this protocol when your type has no single JavaScript class.
2+
/// For example, a union type of multiple classes or primitive values.
33
public protocol JSBridgedType: JSValueCodable, CustomStringConvertible {
4+
/// This is the value your class wraps.
45
var value: JSValue { get }
6+
7+
/// If your class is incompatible with the provided value, return `nil`.
58
init?(from value: JSValue)
69
}
710

@@ -15,10 +18,17 @@ extension JSBridgedType {
1518
public var description: String { value.description }
1619
}
1720

18-
21+
/// Conform to this protocol when your Swift class wraps a JavaScript class.
1922
public protocol JSBridgedClass: JSBridgedType {
23+
/// The constructor function for the JavaScript class
2024
static var constructor: JSFunction { get }
25+
26+
/// The JavaScript object wrapped by this instance.
27+
/// You may assume that `jsObject instanceof Self.constructor`
2128
var jsObject: JSObject { get }
29+
30+
/// Create an instannce wrapping the given JavaScript object.
31+
/// You may assume that `jsObject instanceof Self.constructor`
2232
init(withCompatibleObject jsObject: JSObject)
2333
}
2434

Sources/JavaScriptKit/JSValueConstructible.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
/// Objects that can be constructed from a JavaScript value
12
public protocol JSValueConstructible {
3+
/// Return `nil` if the value is not compatible with the conforming Swift type.
24
static func construct(from value: JSValue) -> Self?
35
}
46

Sources/JavaScriptKit/JSValueConvertible.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import _CJavaScriptKit
22

3+
/// Objects that can be converted to a JavaScript value, preferably in a lossless manner.
34
public protocol JSValueConvertible {
5+
/// Create a JSValue that represents this object
46
func jsValue() -> JSValue
57
}
68

Sources/JavaScriptKit/Support.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
protocol _AnyJSValueConvertible: JSValueConvertible {}
22

3+
/// A type-erased wrapper for types conforming to JSValueConvertible
34
public struct AnyJSValueConvertible: JSValueCodable, ExpressibleByNilLiteral {
45
public static let void = AnyJSValueConvertible.construct(from: .undefined)
56

0 commit comments

Comments
 (0)