@@ -10,6 +10,7 @@ public enum JSValue: Equatable {
1010 case null
1111 case undefined
1212 case function( JSFunction )
13+ case symbol( JSSymbol )
1314
1415 /// Returns the `Bool` value of this JS value if its type is boolean.
1516 /// If not, returns `nil`.
@@ -67,6 +68,13 @@ public enum JSValue: Equatable {
6768 }
6869 }
6970
71+ public var symbol : JSSymbol ? {
72+ switch self {
73+ case let . symbol( symbol) : return symbol
74+ default : return nil
75+ }
76+ }
77+
7078 /// Returns the `true` if this JS value is null.
7179 /// If not, returns `false`.
7280 public var isNull : Bool {
@@ -80,39 +88,38 @@ public enum JSValue: Equatable {
8088 }
8189}
8290
83- extension JSValue {
91+ public extension JSValue {
8492 /// An unsafe convenience method of `JSObject.subscript(_ name: String) -> ((ConvertibleToJSValue...) -> JSValue)?`
8593 /// - 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 ) {
8795 object![ dynamicMember: name] !
8896 }
8997
9098 /// An unsafe convenience method of `JSObject.subscript(_ index: Int) -> JSValue`
9199 /// - Precondition: `self` must be a JavaScript Object.
92- public subscript( dynamicMember name: String ) -> JSValue {
100+ subscript( dynamicMember name: String ) -> JSValue {
93101 get { self . object![ name] }
94102 set { self . object![ name] = newValue }
95103 }
96104
97105 /// An unsafe convenience method of `JSObject.subscript(_ index: Int) -> JSValue`
98106 /// - Precondition: `self` must be a JavaScript Object.
99- public subscript( _ index: Int ) -> JSValue {
107+ subscript( _ index: Int ) -> JSValue {
100108 get { object![ index] }
101109 set { object![ index] = newValue }
102110 }
103111}
104112
105113extension JSValue : Swift . Error { }
106114
107- extension JSValue {
108- public func fromJSValue< Type> ( ) -> Type ? where Type: ConstructibleFromJSValue {
115+ public extension JSValue {
116+ func fromJSValue< Type> ( ) -> Type ? where Type: ConstructibleFromJSValue {
109117 return Type . construct ( from: self )
110118 }
111119}
112120
113- extension JSValue {
114-
115- public static func string( _ value: String ) -> JSValue {
121+ public extension JSValue {
122+ static func string( _ value: String ) -> JSValue {
116123 . string( JSString ( value) )
117124 }
118125
@@ -141,12 +148,12 @@ extension JSValue {
141148 /// eventListenter.release()
142149 /// ```
143150 @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 {
145152 . object( JSClosure ( body) )
146153 }
147154
148155 @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 {
150157 . object( closure)
151158 }
152159}
@@ -170,7 +177,7 @@ extension JSValue: ExpressibleByFloatLiteral {
170177}
171178
172179extension JSValue : ExpressibleByNilLiteral {
173- public init ( nilLiteral: ( ) ) {
180+ public init ( nilLiteral _ : ( ) ) {
174181 self = . null
175182 }
176183}
@@ -205,14 +212,28 @@ public func setJSValue(this: JSObject, index: Int32, value: JSValue) {
205212 }
206213}
207214
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 {
214235 switch self {
215- case . boolean, . string, . number, . null, . undefined:
236+ case . boolean, . string, . number, . null, . undefined, . symbol :
216237 return false
217238 case let . object( ref) :
218239 return ref. isInstanceOf ( constructor)
@@ -227,11 +248,12 @@ extension JSValue: CustomStringConvertible {
227248 switch self {
228249 case let . boolean( boolean) :
229250 return boolean. description
230- case . string( let string) :
251+ case let . string( string) :
231252 return string. description
232- case . number( let number) :
253+ case let . number( number) :
233254 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 ) :
235257 return object. toString!( ) . fromJSValue ( ) !
236258 case . null:
237259 return " null "
0 commit comments