Skip to content

Commit b7a6a7d

Browse files
committed
fix Signed/UnsignedInteger ConstructibleFromJSValue
1 parent 7af5917 commit b7a6a7d

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

Sources/JavaScriptKit/ConstructibleFromJSValue.swift

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,17 @@ extension Double: ConstructibleFromJSValue {}
3535
extension Float: ConstructibleFromJSValue {}
3636

3737
extension SignedInteger where Self: ConstructibleFromJSValue {
38-
public init(_ bigInt: JSBigInt) {
38+
public init(_ bigInt: JSBigIntExtended) {
3939
self.init(bigInt.int64Value)
4040
}
4141
public static func construct(from value: JSValue) -> Self? {
42-
value.bigInt.map(Self.init) ?? value.number.map(Self.init)
42+
if let number = value.number {
43+
return Self(number)
44+
}
45+
if let bigInt = value.bigInt as? JSBigIntExtended {
46+
return Self(bigInt)
47+
}
48+
return nil
4349
}
4450
}
4551
extension Int: ConstructibleFromJSValue {}
@@ -49,11 +55,17 @@ extension Int32: ConstructibleFromJSValue {}
4955
extension Int64: ConstructibleFromJSValue {}
5056

5157
extension UnsignedInteger where Self: ConstructibleFromJSValue {
52-
public init(_ bigInt: JSBigInt) {
58+
public init(_ bigInt: JSBigIntExtended) {
5359
self.init(bigInt.uInt64Value)
5460
}
5561
public static func construct(from value: JSValue) -> Self? {
56-
value.bigInt.map(Self.init) ?? value.number.map(Self.init)
62+
if let number = value.number {
63+
return Self(number)
64+
}
65+
if let bigInt = value.bigInt as? JSBigIntExtended {
66+
return Self(bigInt)
67+
}
68+
return nil
5769
}
5870
}
5971
extension UInt: ConstructibleFromJSValue {}

Sources/JavaScriptKit/FundamentalObjects/JSBigInt.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,11 @@ public final class JSBigInt: JSObject {
2323
}
2424
}
2525
}
26+
27+
public protocol JSBigIntExtended: JSBigInt {
28+
var int64Value: Int64 { get }
29+
var uInt64Value: UInt64 { get }
30+
31+
init(_ value: Int64)
32+
init(unsigned value: UInt64)
33+
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
@_spi(JSObject_id) import JavaScriptKit
22
import _CJavaScriptKit_I64
33

4-
public extension JSBigInt {
5-
var int64Value: Int64 {
4+
extension JSBigInt: JSBigIntExtended {
5+
public var int64Value: Int64 {
66
_bigint_to_i64(id, true)
77
}
88

9-
var uInt64Value: UInt64 {
9+
public var uInt64Value: UInt64 {
1010
UInt64(bitPattern: _bigint_to_i64(id, false))
1111
}
1212

13-
convenience init(_ value: Int64) {
13+
convenience public init(_ value: Int64) {
1414
self.init(id: _i64_to_bigint(value, true))
1515
}
1616

17-
convenience init(unsigned value: UInt64) {
17+
convenience public init(unsigned value: UInt64) {
1818
self.init(id: _i64_to_bigint(Int64(bitPattern: value), false))
1919
}
2020
}

0 commit comments

Comments
 (0)