Skip to content

Commit 6ff2941

Browse files
Add doc comments for public APIs (Part 2) (#57)
* Add doc comments for public APIs (Part 2) * Update Sources/JavaScriptKit/BasicObjects/JSArray.swift Co-authored-by: Max Desiatov <[email protected]> * Update Sources/JavaScriptKit/BasicObjects/JSArray.swift Co-authored-by: Max Desiatov <[email protected]> * Update Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift Co-authored-by: Max Desiatov <[email protected]> * Update Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift Co-authored-by: Max Desiatov <[email protected]> * Update Sources/JavaScriptKit/JSValueConvertible.swift Co-authored-by: Max Desiatov <[email protected]> * Update Sources/JavaScriptKit/JSValueDecoder.swift Co-authored-by: Max Desiatov <[email protected]> Co-authored-by: Max Desiatov <[email protected]>
1 parent 2eade6f commit 6ff2941

File tree

6 files changed

+42
-5
lines changed

6 files changed

+42
-5
lines changed

Sources/JavaScriptKit/BasicObjects/JSArray.swift

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/// A wrapper around [the JavaScript Array class](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array)
2+
/// that exposes its properties in a type-safe and Swifty way.
13
public class JSArray {
24
static let classObject = JSObject.global.Array.function!
35

@@ -6,7 +8,11 @@ public class JSArray {
68
}
79

810
let ref: JSObject
9-
11+
12+
/// Construct a `JSArray` from Array `JSObject`.
13+
/// Return `nil` if the object is not an Array.
14+
///
15+
/// - Parameter object: A `JSObject` expected to be a JavaScript Array
1016
public init?(_ ref: JSObject) {
1117
guard Self.isArray(ref) else { return nil }
1218
self.ref = ref

Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift

+17-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ public protocol TypedArrayElement: JSValueConvertible, JSValueConstructible {
99
static var typedArrayClass: JSFunction { get }
1010
}
1111

12+
/// A wrapper around [the JavaScript TypedArray class](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/TypedArray)
13+
/// that exposes its properties in a type-safe and Swifty way.
1214
public class JSTypedArray<Element>: JSValueConvertible, ExpressibleByArrayLiteral where Element: TypedArrayElement {
1315
let ref: JSObject
1416
public func jsValue() -> JSValue {
@@ -29,11 +31,18 @@ public class JSTypedArray<Element>: JSValueConvertible, ExpressibleByArrayLitera
2931
self.ref = object
3032
}
3133

34+
/// Construct a `JSTypedArray` from TypedArray `JSObject`.
35+
/// Return `nil` if the object is not TypedArray.
36+
///
37+
/// - Parameter object: A `JSObject` expected to be TypedArray
3238
public init?(_ object: JSObject) {
3339
guard object.isInstanceOf(Element.typedArrayClass) else { return nil }
3440
self.ref = object
3541
}
3642

43+
/// Initialize a new instance of TypedArray in JavaScript environment with given length zero value.
44+
///
45+
/// - Parameter length: The length of elements that will be allocated.
3746
public convenience init(length: Int) {
3847
let jsObject = Element.typedArrayClass.new(length)
3948
self.init(unsafe: jsObject)
@@ -42,17 +51,21 @@ public class JSTypedArray<Element>: JSValueConvertible, ExpressibleByArrayLitera
4251
required public convenience init(arrayLiteral elements: Element...) {
4352
self.init(elements)
4453
}
45-
54+
55+
/// Initialize a new instance of TypedArray in JavaScript environment with given elements.
56+
///
57+
/// - Parameter array: The array that will be copied to create a new instance of TypedArray
4658
public convenience init(_ array: [Element]) {
4759
var resultObj = JavaScriptObjectRef()
4860
array.withUnsafeBufferPointer { ptr in
4961
_create_typed_array(Element.typedArrayKind, ptr.baseAddress!, Int32(array.count), &resultObj)
5062
}
5163
self.init(unsafe: JSObject(id: resultObj))
5264
}
53-
54-
public convenience init(_ stride: StrideTo<Element>) where Element: Strideable {
55-
self.init(stride.map({ $0 }))
65+
66+
/// Convenience initializer for `Sequence`.
67+
public convenience init<S: Sequence>(_ sequence: S) {
68+
self.init(sequence.map({ $0 }))
5669
}
5770
}
5871

Sources/JavaScriptKit/Compatibility.swift

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/// The compatibility runtime library version.
2+
/// Notes: If you change any interface of runtime library, please increment
3+
/// this and `SwiftRuntime.version` in `./Runtime/src/index.ts`.
14
@_cdecl("swjs_library_version")
25
func _library_version() -> Double {
36
return 600

Sources/JavaScriptKit/JSValueConstructible.swift

+6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1+
/// Types conforming to this protocol can be constructed from `JSValue`.
12
public protocol JSValueConstructible {
3+
/// Construct an instance of `Self`, if possible, from the given `JSValue`.
4+
/// Return `nil` if fail to construct.
5+
///
6+
/// - Parameter value: The `JSValue` to decode
7+
/// - Returns: An instance of `Self`, if one was successfully constructed from the value.
28
static func construct(from value: JSValue) -> Self?
39
}
410

Sources/JavaScriptKit/JSValueConvertible.swift

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import _CJavaScriptKit
22

3+
/// Confirming types are convertible to `JSValue`.
34
public protocol JSValueConvertible {
5+
/// Convert this object into a `JSValue`.
46
func jsValue() -> JSValue
57
}
68

Sources/JavaScriptKit/JSValueDecoder.swift

+7
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,16 @@ extension _Decoder: SingleValueDecodingContainer {
229229
}
230230
}
231231

232+
/// `JSValueDecoder` facilitates the decoding of JavaScript value into semantic `Decodable` types.
232233
public class JSValueDecoder {
234+
235+
/// Initializes a new `JSValueDecoder`.
233236
public init() {}
234237

238+
/// Decodes a top-level value of the given type from the given JavaScript value representation.
239+
///
240+
/// - Parameter T: The type of the value to decode.
241+
/// - Parameter value: The `JSValue` to decode from.
235242
public func decode<T>(
236243
_: T.Type = T.self,
237244
from value: JSValue,

0 commit comments

Comments
 (0)