1
1
import _CJavaScriptKit
2
2
3
3
public protocol JSValueConvertible {
4
- func jsValue( ) -> JSValue
4
+ static func jsValue( from: Self ) -> JSValue
5
+ }
6
+
7
+ extension JSValue {
8
+ public init < T: JSValueConvertible > ( from jsValue: T ) {
9
+ self = T . jsValue ( from: jsValue)
10
+ }
5
11
}
6
12
7
13
extension JSValue : JSValueConvertible {
8
- public func jsValue( ) -> JSValue { self }
14
+ public static func jsValue( from jsValue : Self ) -> JSValue { jsValue }
9
15
}
10
16
11
17
extension Bool : JSValueConvertible {
12
- public func jsValue( ) -> JSValue { . boolean( self ) }
18
+ public static func jsValue( from bool : Self ) -> JSValue { . boolean( bool ) }
13
19
}
14
20
15
21
extension Int : JSValueConvertible {
16
- public func jsValue( ) -> JSValue { . number( Double ( self ) ) }
22
+ public static func jsValue( from int : Self ) -> JSValue { . number( Double ( int ) ) }
17
23
}
18
24
19
25
extension Int8 : JSValueConvertible {
20
- public func jsValue( ) -> JSValue { . number( Double ( self ) ) }
26
+ public static func jsValue( from int8 : Self ) -> JSValue { . number( Double ( int8 ) ) }
21
27
}
22
28
23
29
extension Int16 : JSValueConvertible {
24
- public func jsValue( ) -> JSValue { . number( Double ( self ) ) }
30
+ public static func jsValue( from int16 : Self ) -> JSValue { . number( Double ( int16 ) ) }
25
31
}
26
32
27
33
extension Int32 : JSValueConvertible {
28
- public func jsValue( ) -> JSValue { . number( Double ( self ) ) }
34
+ public static func jsValue( from int32 : Self ) -> JSValue { . number( Double ( int32 ) ) }
29
35
}
30
36
31
37
extension UInt : JSValueConvertible {
32
- public func jsValue( ) -> JSValue { . number( Double ( self ) ) }
38
+ public static func jsValue( from uint : Self ) -> JSValue { . number( Double ( uint ) ) }
33
39
}
34
40
35
41
extension UInt8 : JSValueConvertible {
36
- public func jsValue( ) -> JSValue { . number( Double ( self ) ) }
42
+ public static func jsValue( from uint8 : Self ) -> JSValue { . number( Double ( uint8 ) ) }
37
43
}
38
44
39
45
extension UInt16 : JSValueConvertible {
40
- public func jsValue( ) -> JSValue { . number( Double ( self ) ) }
46
+ public static func jsValue( from uint16 : Self ) -> JSValue { . number( Double ( uint16 ) ) }
41
47
}
42
48
43
49
extension Float : JSValueConvertible {
44
- public func jsValue( ) -> JSValue { . number( Double ( self ) ) }
50
+ public static func jsValue( from float : Self ) -> JSValue { . number( Double ( float ) ) }
45
51
}
46
52
47
53
extension Double : JSValueConvertible {
48
- public func jsValue( ) -> JSValue { . number( self ) }
54
+ public static func jsValue( from double : Self ) -> JSValue { . number( double ) }
49
55
}
50
56
51
57
extension String : JSValueConvertible {
52
- public func jsValue( ) -> JSValue { . string( self ) }
58
+ public static func jsValue( from string : Self ) -> JSValue { . string( string ) }
53
59
}
54
60
55
61
extension JSObjectRef : JSValueConvertible {
@@ -59,65 +65,53 @@ extension JSObjectRef: JSValueConvertible {
59
65
60
66
private let Object = JSObjectRef . global. Object. function!
61
67
62
- extension Dictionary where Value: JSValueConvertible , Key == String {
63
- public func jsValue( ) -> JSValue {
64
- Swift . Dictionary < Key , JSValueConvertible > . jsValue ( self ) ( )
65
- }
66
- }
67
-
68
- extension Dictionary : JSValueConvertible where Value == JSValueConvertible , Key == String {
69
- public func jsValue( ) -> JSValue {
68
+ extension Dictionary : JSValueConvertible where Value: JSValueConvertible , Key == String {
69
+ public static func jsValue( from dict: Self ) -> JSValue {
70
70
let object = Object . new ( )
71
- for (key, value) in self {
72
- object [ key] = value . jsValue ( )
71
+ for (key, value) in dict {
72
+ object [ key] = JSValue ( from : value )
73
73
}
74
74
return . object( object)
75
75
}
76
76
}
77
77
78
78
private let Array = JSObjectRef . global. Array. function!
79
79
80
- extension Array where Element: JSValueConvertible {
81
- public func jsValue( ) -> JSValue {
82
- Swift . Array < JSValueConvertible > . jsValue ( self ) ( )
83
- }
84
- }
85
-
86
- extension Array : JSValueConvertible where Element == JSValueConvertible {
87
- public func jsValue( ) -> JSValue {
88
- let array = Array . new ( count)
89
- for (index, element) in enumerated ( ) {
90
- array [ index] = element. jsValue ( )
80
+ extension Array : JSValueConvertible where Element: JSValueConvertible {
81
+ public static func jsValue( from array: Self ) -> JSValue {
82
+ let jsArray = Array . new ( array. count)
83
+ for (index, element) in array. enumerated ( ) {
84
+ jsArray [ index] = JSValue ( from: element)
91
85
}
92
- return . object( array )
86
+ return . object( jsArray )
93
87
}
94
88
}
95
89
96
90
extension RawJSValue : JSValueConvertible {
97
- public func jsValue( ) -> JSValue {
98
- switch kind {
91
+ public static func jsValue( from rawJSValue : Self ) -> JSValue {
92
+ switch rawJSValue . kind {
99
93
case . invalid:
100
94
fatalError ( )
101
95
case . boolean:
102
- return . boolean( payload1 != 0 )
96
+ return . boolean( rawJSValue . payload1 != 0 )
103
97
case . number:
104
- return . number( payload3)
98
+ return . number( rawJSValue . payload3)
105
99
case . string:
106
100
// +1 for null terminator
107
- let buffer = malloc ( Int ( payload2 + 1 ) ) !. assumingMemoryBound ( to: UInt8 . self)
101
+ let buffer = malloc ( Int ( rawJSValue . payload2 + 1 ) ) !. assumingMemoryBound ( to: UInt8 . self)
108
102
defer { free ( buffer) }
109
- _load_string ( JavaScriptObjectRef ( payload1) , buffer)
110
- buffer [ Int ( payload2) ] = 0
103
+ _load_string ( JavaScriptObjectRef ( rawJSValue . payload1) , buffer)
104
+ buffer [ Int ( rawJSValue . payload2) ] = 0
111
105
let string = String ( decodingCString: UnsafePointer ( buffer) , as: UTF8 . self)
112
106
return . string( string)
113
107
case . object:
114
- return . object( JSObjectRef ( id: UInt32 ( payload1) ) )
108
+ return . object( JSObjectRef ( id: UInt32 ( rawJSValue . payload1) ) )
115
109
case . null:
116
110
return . null
117
111
case . undefined:
118
112
return . undefined
119
113
case . function:
120
- return . function( JSFunctionRef ( id: UInt32 ( payload1) ) )
114
+ return . function( JSFunctionRef ( id: UInt32 ( rawJSValue . payload1) ) )
121
115
default :
122
116
fatalError ( " unreachable " )
123
117
}
0 commit comments