Skip to content

Commit b62b78e

Browse files
committed
Make JS{Array,Date,Error} conform to JSBridgedClass
1 parent 5fe4b96 commit b62b78e

File tree

6 files changed

+49
-33
lines changed

6 files changed

+49
-33
lines changed

Sources/JavaScriptKit/BasicObjects/JSArray.swift

+25-17
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,56 @@
1-
public class JSArray {
2-
static let classObject = JSObject.global.Array.function!
1+
public class JSArray: JSBridgedClass {
2+
public static let constructor = JSObject.global.Array.function!
33

44
static func isArray(_ object: JSObject) -> Bool {
5-
classObject.isArray!(object).boolean!
5+
constructor.isArray!(object).boolean!
66
}
77

8-
let ref: JSObject
8+
public let jsObject: JSObject
99

10-
public init?(_ ref: JSObject) {
11-
guard Self.isArray(ref) else { return nil }
12-
self.ref = ref
10+
public required convenience init?(from value: JSValue) {
11+
guard let object = value.object else { return nil }
12+
self.init(object)
13+
}
14+
15+
public convenience init?(_ jsObject: JSObject) {
16+
guard Self.isArray(jsObject) else { return nil }
17+
self.init(withCompatibleObject: jsObject)
18+
}
19+
public required init(withCompatibleObject jsObject: JSObject) {
20+
self.jsObject = jsObject
1321
}
1422
}
1523

1624
extension JSArray: RandomAccessCollection {
1725
public typealias Element = JSValue
1826

1927
public func makeIterator() -> Iterator {
20-
Iterator(ref: ref)
28+
Iterator(jsObject: jsObject)
2129
}
2230

2331
public class Iterator: IteratorProtocol {
24-
let ref: JSObject
32+
let jsObject: JSObject
2533
var index = 0
26-
init(ref: JSObject) {
27-
self.ref = ref
34+
init(jsObject: JSObject) {
35+
self.jsObject = jsObject
2836
}
2937

3038
public func next() -> Element? {
3139
let currentIndex = index
32-
guard currentIndex < Int(ref.length.number!) else {
40+
guard currentIndex < Int(jsObject.length.number!) else {
3341
return nil
3442
}
3543
index += 1
36-
guard ref.hasOwnProperty!(currentIndex).boolean! else {
44+
guard jsObject.hasOwnProperty!(currentIndex).boolean! else {
3745
return next()
3846
}
39-
let value = ref[currentIndex]
47+
let value = jsObject[currentIndex]
4048
return value
4149
}
4250
}
4351

4452
public subscript(position: Int) -> JSValue {
45-
ref[position]
53+
jsObject[position]
4654
}
4755

4856
public var startIndex: Int { 0 }
@@ -62,14 +70,14 @@ extension JSArray: RandomAccessCollection {
6270
/// array.count // 2
6371
/// ```
6472
public var length: Int {
65-
return Int(ref.length.number!)
73+
Int(jsObject.length.number!)
6674
}
6775

6876
/// The number of elements in that array **not** including empty hole.
6977
/// Note that `count` syncs with the number that `Iterator` can iterate.
7078
/// See also: `JSArray.length`
7179
public var count: Int {
72-
return getObjectValuesLength(ref)
80+
getObjectValuesLength(jsObject)
7381
}
7482
}
7583

Sources/JavaScriptKit/BasicObjects/JSDate.swift

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ in the naming. Parts of the JavaScript `Date` API that are not consistent across
66
implementations are not exposed in a type-safe manner, you should access the underlying `jsObject`
77
property if you need those.
88
*/
9-
public final class JSDate {
9+
public final class JSDate: JSBridgedClass {
1010
/// The constructor function used to create new `Date` objects.
11-
private static let constructor = JSObject.global.Date.function!
11+
public static let constructor = JSObject.global.Date.function!
1212

1313
/// The underlying JavaScript `Date` object.
1414
public let jsObject: JSObject
@@ -39,6 +39,10 @@ public final class JSDate {
3939
jsObject = Self.constructor.new(year, monthIndex, day, hours, minutes, seconds, milliseconds)
4040
}
4141

42+
public init(withCompatibleObject jsObject: JSObject) {
43+
self.jsObject = jsObject
44+
}
45+
4246
/// Year of this date in local time zone.
4347
public var fullYear: Int {
4448
get {

Sources/JavaScriptKit/BasicObjects/JSError.swift

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
class](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) that
33
exposes its properties in a type-safe way.
44
*/
5-
public final class JSError: Error {
5+
public final class JSError: Error, JSBridgedClass {
66
/// The constructor function used to create new `Error` objects.
7-
private static let constructor = JSObject.global.Error.function!
7+
public static let constructor = JSObject.global.Error.function!
88

99
/// The underlying JavaScript `Error` object.
1010
public let jsObject: JSObject
@@ -14,6 +14,10 @@ public final class JSError: Error {
1414
jsObject = Self.constructor.new([message])
1515
}
1616

17+
public init(withCompatibleObject jsObject: JSObject) {
18+
self.jsObject = jsObject
19+
}
20+
1721
/// The error message of the underlying `Error` object.
1822
public var message: String {
1923
jsObject.message.string!

Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,23 @@ public protocol TypedArrayElement: JSValueConvertible, JSValueConstructible {
1010
}
1111

1212
public class JSTypedArray<Element>: JSBridgedClass, ExpressibleByArrayLiteral where Element: TypedArrayElement {
13-
public static var classRef: JSFunction { Element.typedArrayClass }
14-
public var objectRef: JSObject
13+
public static var constructor: JSFunction { Element.typedArrayClass }
14+
public var jsObject: JSObject
1515
public subscript(_ index: Int) -> Element {
1616
get {
17-
return Element.construct(from: objectRef[index])!
17+
return Element.construct(from: jsObject[index])!
1818
}
1919
set {
20-
self.objectRef[index] = newValue.jsValue()
20+
self.jsObject[index] = newValue.jsValue()
2121
}
2222
}
2323

2424
public init(length: Int) {
25-
objectRef = Element.typedArrayClass.new(length)
25+
jsObject = Element.typedArrayClass.new(length)
2626
}
2727

2828
required public init(withCompatibleObject jsObject: JSObject) {
29-
objectRef = jsObject
29+
self.jsObject = jsObject
3030
}
3131

3232
required public convenience init(arrayLiteral elements: Element...) {

Sources/JavaScriptKit/JSBridgedType.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ extension JSBridgedType {
1717

1818

1919
public protocol JSBridgedClass: JSBridgedType {
20-
static var classRef: JSFunction { get }
21-
var objectRef: JSObject { get }
22-
init(withCompatibleObject objectRef: JSObject)
20+
static var constructor: JSFunction { get }
21+
var jsObject: JSObject { get }
22+
init(withCompatibleObject jsObject: JSObject)
2323
}
2424

2525
extension JSBridgedClass {
26-
public var value: JSValue { objectRef.jsValue() }
26+
public var value: JSValue { jsObject.jsValue() }
2727
public init?(from value: JSValue) {
28-
guard let object = value.object, object.isInstanceOf(Self.classRef) else { return nil }
28+
guard let object = value.object, object.isInstanceOf(Self.constructor) else { return nil }
2929
self.init(withCompatibleObject: object)
3030
}
3131
}

Sources/JavaScriptKit/Support.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public func staticCast<Type: JSBridgedType>(_ ref: JSBridgedType) -> Type? {
5050
}
5151

5252
public func dynamicCast<Type: JSBridgedClass>(_ ref: JSBridgedClass) -> Type? {
53-
guard ref.objectRef.isInstanceOf(Type.classRef) else {
53+
guard ref.jsObject.isInstanceOf(Type.constructor) else {
5454
return nil
5555
}
5656
return staticCast(ref)

0 commit comments

Comments
 (0)