@@ -10,6 +10,7 @@ public enum JSValue: Equatable {
10
10
case null
11
11
case undefined
12
12
case function( JSFunction )
13
+ case symbol( JSSymbol )
13
14
14
15
/// Returns the `Bool` value of this JS value if its type is boolean.
15
16
/// If not, returns `nil`.
@@ -67,6 +68,13 @@ public enum JSValue: Equatable {
67
68
}
68
69
}
69
70
71
+ public var symbol : JSSymbol ? {
72
+ switch self {
73
+ case let . symbol( symbol) : return symbol
74
+ default : return nil
75
+ }
76
+ }
77
+
70
78
/// Returns the `true` if this JS value is null.
71
79
/// If not, returns `false`.
72
80
public var isNull : Bool {
@@ -80,39 +88,38 @@ public enum JSValue: Equatable {
80
88
}
81
89
}
82
90
83
- extension JSValue {
91
+ public extension JSValue {
84
92
/// An unsafe convenience method of `JSObject.subscript(_ name: String) -> ((ConvertibleToJSValue...) -> JSValue)?`
85
93
/// - Precondition: `self` must be a JavaScript Object and specified member should be a callable object.
86
- public subscript( dynamicMember name: String ) -> ( ( ConvertibleToJSValue . . . ) -> JSValue ) {
94
+ subscript( dynamicMember name: String ) -> ( ( ConvertibleToJSValue . . . ) -> JSValue ) {
87
95
object![ dynamicMember: name] !
88
96
}
89
97
90
98
/// An unsafe convenience method of `JSObject.subscript(_ index: Int) -> JSValue`
91
99
/// - Precondition: `self` must be a JavaScript Object.
92
- public subscript( dynamicMember name: String ) -> JSValue {
100
+ subscript( dynamicMember name: String ) -> JSValue {
93
101
get { self . object![ name] }
94
102
set { self . object![ name] = newValue }
95
103
}
96
104
97
105
/// An unsafe convenience method of `JSObject.subscript(_ index: Int) -> JSValue`
98
106
/// - Precondition: `self` must be a JavaScript Object.
99
- public subscript( _ index: Int ) -> JSValue {
107
+ subscript( _ index: Int ) -> JSValue {
100
108
get { object![ index] }
101
109
set { object![ index] = newValue }
102
110
}
103
111
}
104
112
105
113
extension JSValue : Swift . Error { }
106
114
107
- extension JSValue {
108
- public func fromJSValue< Type> ( ) -> Type ? where Type: ConstructibleFromJSValue {
115
+ public extension JSValue {
116
+ func fromJSValue< Type> ( ) -> Type ? where Type: ConstructibleFromJSValue {
109
117
return Type . construct ( from: self )
110
118
}
111
119
}
112
120
113
- extension JSValue {
114
-
115
- public static func string( _ value: String ) -> JSValue {
121
+ public extension JSValue {
122
+ static func string( _ value: String ) -> JSValue {
116
123
. string( JSString ( value) )
117
124
}
118
125
@@ -141,12 +148,12 @@ extension JSValue {
141
148
/// eventListenter.release()
142
149
/// ```
143
150
@available ( * , deprecated, message: " Please create JSClosure directly and manage its lifetime manually. " )
144
- public static func function( _ body: @escaping ( [ JSValue ] ) -> JSValue ) -> JSValue {
151
+ static func function( _ body: @escaping ( [ JSValue ] ) -> JSValue ) -> JSValue {
145
152
. object( JSClosure ( body) )
146
153
}
147
154
148
155
@available ( * , deprecated, renamed: " object " , message: " JSClosure is no longer a subclass of JSFunction. Use .object(closure) instead. " )
149
- public static func function( _ closure: JSClosure ) -> JSValue {
156
+ static func function( _ closure: JSClosure ) -> JSValue {
150
157
. object( closure)
151
158
}
152
159
}
@@ -170,7 +177,7 @@ extension JSValue: ExpressibleByFloatLiteral {
170
177
}
171
178
172
179
extension JSValue : ExpressibleByNilLiteral {
173
- public init ( nilLiteral: ( ) ) {
180
+ public init ( nilLiteral _ : ( ) ) {
174
181
self = . null
175
182
}
176
183
}
@@ -205,14 +212,28 @@ public func setJSValue(this: JSObject, index: Int32, value: JSValue) {
205
212
}
206
213
}
207
214
208
- extension JSValue {
209
- /// Return `true` if this value is an instance of the passed `constructor` function.
210
- /// Returns `false` for everything except objects and functions.
211
- /// - Parameter constructor: The constructor function to check.
212
- /// - Returns: The result of `instanceof` in the JavaScript environment.
213
- public func isInstanceOf( _ constructor: JSFunction ) -> Bool {
215
+ public func getJSValue( this: JSObject , symbol: JSSymbol ) -> JSValue {
216
+ var rawValue = RawJSValue ( )
217
+ _get_prop ( this. id, symbol. id,
218
+ & rawValue. kind,
219
+ & rawValue. payload1, & rawValue. payload2)
220
+ return rawValue. jsValue
221
+ }
222
+
223
+ public func setJSValue( this: JSObject , symbol: JSSymbol , value: JSValue ) {
224
+ value. withRawJSValue { rawValue in
225
+ _set_prop ( this. id, symbol. id, rawValue. kind, rawValue. payload1, rawValue. payload2)
226
+ }
227
+ }
228
+
229
+ public extension JSValue {
230
+ /// Return `true` if this value is an instance of the passed `constructor` function.
231
+ /// Returns `false` for everything except objects and functions.
232
+ /// - Parameter constructor: The constructor function to check.
233
+ /// - Returns: The result of `instanceof` in the JavaScript environment.
234
+ func isInstanceOf( _ constructor: JSFunction ) -> Bool {
214
235
switch self {
215
- case . boolean, . string, . number, . null, . undefined:
236
+ case . boolean, . string, . number, . null, . undefined, . symbol :
216
237
return false
217
238
case let . object( ref) :
218
239
return ref. isInstanceOf ( constructor)
@@ -227,11 +248,12 @@ extension JSValue: CustomStringConvertible {
227
248
switch self {
228
249
case let . boolean( boolean) :
229
250
return boolean. description
230
- case . string( let string) :
251
+ case let . string( string) :
231
252
return string. description
232
- case . number( let number) :
253
+ case let . number( number) :
233
254
return number. description
234
- case . object( let object) , . function( let object as JSObject ) :
255
+ case let . object( object) , let . function( object as JSObject ) ,
256
+ . symbol( let object as JSObject ) :
235
257
return object. toString!( ) . fromJSValue ( ) !
236
258
case . null:
237
259
return " null "
0 commit comments