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