GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — JavaScriptKit/Sources/JavaScriptKit/JSValue.swift at main · wfltaylor/JavaScriptKit Expand file tree/ Copy pathJSValue.swiftMore file actions 406 lines (363 loc) · 12.6 KB / Copy pathJSValue.swiftFile metadata and controls406 lines (363 loc) · 12.6 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 import _CJavaScriptKit /// `JSValue` represents a value in JavaScript. @dynamicMemberLookup public struct JSValue: Equatable { /// The internal storage of the JSValue, which is intentionally not public /// to leave the flexibility to change the storage. internal enum Storage: Equatable { case boolean(Bool) case string(JSString) case number(Double) case object(JSObject) case null case undefined case symbol(JSSymbol) case bigInt(JSBigInt) } internal var storage: Storage internal init(storage: Storage) { self.storage = storage } public static func boolean(_ value: Bool) -> JSValue { .init(storage: .boolean(value)) } public static func string(_ value: JSString) -> JSValue { .init(storage: .string(value)) } public static func number(_ value: Double) -> JSValue { .init(storage: .number(value)) } public static func object(_ value: JSObject) -> JSValue { .init(storage: .object(value)) } public static var null: JSValue { .init(storage: .null) } public static var undefined: JSValue { .init(storage: .undefined) } public static func symbol(_ value: JSSymbol) -> JSValue { .init(storage: .symbol(value)) } public static func bigInt(_ value: JSBigInt) -> JSValue { .init(storage: .bigInt(value)) } /// Returns the `Bool` value of this JS value if its type is boolean. /// If not, returns `nil`. public var boolean: Bool? { switch storage { case .boolean(let boolean): return boolean default: return nil } } /// Returns the `String` value of this JS value if the type is string. /// If not, returns `nil`. /// /// Note that this accessor may copy the JS string value into Swift side memory. /// /// To avoid the copying, please consider the `jsString` instead. public var string: String? { jsString.map(String.init) } /// Returns the `JSString` value of this JS value if the type is string. /// If not, returns `nil`. /// public var jsString: JSString? { switch storage { case .string(let string): return string default: return nil } } /// Returns the `Double` value of this JS value if the type is number. /// If not, returns `nil`. public var number: Double? { switch storage { case .number(let number): return number default: return nil } } /// Returns the `JSObject` of this JS value if its type is object. /// If not, returns `nil`. public var object: JSObject? { switch storage { case .object(let object): return object default: return nil } } // @available(*, deprecated, renamed: "object", message: "Use the .object property instead") public var function: JSObject? { object } /// Returns the `JSSymbol` of this JS value if its type is function. /// If not, returns `nil`. public var symbol: JSSymbol? { switch storage { case .symbol(let symbol): return symbol default: return nil } } /// Returns the `JSBigInt` of this JS value if its type is function. /// If not, returns `nil`. public var bigInt: JSBigInt? { switch storage { case .bigInt(let bigInt): return bigInt default: return nil } } /// Returns the `true` if this JS value is null. /// If not, returns `false`. public var isNull: Bool { return storage == .null } /// Returns the `true` if this JS value is undefined. /// If not, returns `false`. public var isUndefined: Bool { return storage == .undefined } } /// JSValue is intentionally not `Sendable` because accessing a JSValue living in a different /// thread is invalid. Although there are some cases where Swift allows sending a non-Sendable /// values to other isolation domains, not conforming `Sendable` is still useful to prevent /// accidental misuse. @available(*, unavailable) extension JSValue: Sendable {} extension JSValue { #if !hasFeature(Embedded) /// An unsafe convenience method of `JSObject.subscript(_ name: String) -> ((ConvertibleToJSValue...) -> JSValue)?` /// - Precondition: `self` must be a JavaScript Object and specified member should be a callable object. public subscript(dynamicMember name: String) -> ((ConvertibleToJSValue...) -> JSValue) { object![dynamicMember: name]! } #endif /// An unsafe convenience method of `JSObject.subscript(_ index: Int) -> JSValue` /// - Precondition: `self` must be a JavaScript Object. public subscript(dynamicMember name: String) -> JSValue { get { self.object![name] } nonmutating set { self.object![name] = newValue } } /// An unsafe convenience method of `JSObject.subscript(_ index: Int) -> JSValue` /// - Precondition: `self` must be a JavaScript Object. public subscript(_ index: Int) -> JSValue { get { object![index] } nonmutating set { object![index] = newValue } } } extension JSValue { public func fromJSValue<Type>() -> Type? where Type: ConstructibleFromJSValue { return Type.construct(from: self) } } extension JSValue { public static func string(_ value: String) -> JSValue { .init(storage: .string(JSString(value))) } /// Deprecated: Please create `JSClosure` directly and manage its lifetime manually. /// /// Migrate this usage /// /// ```swift /// button.addEventListener!("click", JSValue.function { _ in /// ... /// return JSValue.undefined /// }) /// ``` /// /// into below code. /// /// ```swift /// let eventListener = JSClosure { _ in /// ... /// return JSValue.undefined /// } /// /// button.addEventListener!("click", JSValue.function(eventListener)) /// ... /// button.removeEventListener!("click", JSValue.function(eventListener)) /// eventListener.release() /// ``` @available(*, deprecated, message: "Please create JSClosure directly and manage its lifetime manually.") public static func function(_ body: @escaping ([JSValue]) -> JSValue) -> JSValue { .init(storage: .object(JSClosure(body))) } @available( *, deprecated, renamed: "object", message: "JSClosure is no longer a subclass of JSFunction. Use .object(closure) instead." ) public static func function(_ closure: JSClosure) -> JSValue { .init(storage: .object(closure)) } @available(*, deprecated, renamed: "object", message: "Use .object(function) instead") public static func function(_ function: JSObject) -> JSValue { .init(storage: .object(function)) } } extension JSValue: ExpressibleByStringLiteral { public init(stringLiteral value: String) { self = .init(storage: .string(JSString(value))) } } extension JSValue: ExpressibleByIntegerLiteral { public init(integerLiteral value: Int32) { self = .init(storage: .number(Double(value))) } } extension JSValue: ExpressibleByFloatLiteral { public init(floatLiteral value: Double) { self = .init(storage: .number(value)) } } extension JSValue: ExpressibleByNilLiteral { public init(nilLiteral _: ()) { self = .init(storage: .null) } } public func getJSValue(this: JSObject, name: JSString) -> JSValue { var rawValue = RawJSValue() let rawBitPattern = swjs_get_prop( this.id, name.asInternalJSRef(), &rawValue.payload1, &rawValue.payload2 ) rawValue.kind = unsafeBitCast(rawBitPattern, to: JavaScriptValueKind.self) return rawValue.jsValue } public func setJSValue(this: JSObject, name: JSString, value: JSValue) { value.withRawJSValue { rawValue in swjs_set_prop(this.id, name.asInternalJSRef(), rawValue.kind, rawValue.payload1, rawValue.payload2) } } public func getJSValue(this: JSObject, index: Int32) -> JSValue { var rawValue = RawJSValue() let rawBitPattern = swjs_get_subscript( this.id, index, &rawValue.payload1, &rawValue.payload2 ) rawValue.kind = unsafeBitCast(rawBitPattern, to: JavaScriptValueKind.self) return rawValue.jsValue } public func setJSValue(this: JSObject, index: Int32, value: JSValue) { value.withRawJSValue { rawValue in swjs_set_subscript( this.id, index, rawValue.kind, rawValue.payload1, rawValue.payload2 ) } } public func getJSValue(this: JSObject, symbol: JSSymbol) -> JSValue { var rawValue = RawJSValue() let rawBitPattern = swjs_get_prop( this.id, symbol.id, &rawValue.payload1, &rawValue.payload2 ) rawValue.kind = unsafeBitCast(rawBitPattern, to: JavaScriptValueKind.self) return rawValue.jsValue } public func setJSValue(this: JSObject, symbol: JSSymbol, value: JSValue) { value.withRawJSValue { rawValue in swjs_set_prop(this.id, symbol.id, rawValue.kind, rawValue.payload1, rawValue.payload2) } } extension JSValue { /// Return `true` if this value is an instance of the passed `constructor` function. /// Returns `false` for everything except objects and functions. /// - Parameter constructor: The constructor function to check. /// - Returns: The result of `instanceof` in the JavaScript environment. public func isInstanceOf(_ constructor: JSObject) -> Bool { switch storage { case .boolean, .string, .number, .null, .undefined, .symbol, .bigInt: return false case .object(let ref): return ref.isInstanceOf(constructor) } } } extension JSValue: CustomStringConvertible { public var description: String { // per https://tc39.es/ecma262/multipage/text-processing.html#sec-string-constructor-string-value // this always returns a string JSObject.global.String.object!(self).string! } } #if hasFeature(Embedded) // Overloads of `JSValue.subscript(dynamicMember name: String) -> ((ConvertibleToJSValue...) -> JSValue)` // for 0 through 7 arguments for Embedded Swift. // // These are required because the `ConvertibleToJSValue...` subscript is not // available in Embedded Swift due to lack of support for existentials. // // Note: Once Embedded Swift supports parameter packs/variadic generics, we can // replace all of these with a single method that takes a generic pack. extension JSValue { @_disfavoredOverload public subscript(dynamicMember name: String) -> (() -> JSValue) { object![dynamicMember: name]! } @_disfavoredOverload public subscript<A0: ConvertibleToJSValue>(dynamicMember name: String) -> ((A0) -> JSValue) { object![dynamicMember: name]! } @_disfavoredOverload public subscript< A0: ConvertibleToJSValue, A1: ConvertibleToJSValue >(dynamicMember name: String) -> ((A0, A1) -> JSValue) { object![dynamicMember: name]! } @_disfavoredOverload public subscript< A0: ConvertibleToJSValue, A1: ConvertibleToJSValue, A2: ConvertibleToJSValue >(dynamicMember name: String) -> ((A0, A1, A2) -> JSValue) { object![dynamicMember: name]! } @_disfavoredOverload public subscript< A0: ConvertibleToJSValue, A1: ConvertibleToJSValue, A2: ConvertibleToJSValue, A3: ConvertibleToJSValue >(dynamicMember name: String) -> ((A0, A1, A2, A3) -> JSValue) { object![dynamicMember: name]! } @_disfavoredOverload public subscript< A0: ConvertibleToJSValue, A1: ConvertibleToJSValue, A2: ConvertibleToJSValue, A3: ConvertibleToJSValue, A4: ConvertibleToJSValue >(dynamicMember name: String) -> ((A0, A1, A2, A3, A4) -> JSValue) { object![dynamicMember: name]! } @_disfavoredOverload public subscript< A0: ConvertibleToJSValue, A1: ConvertibleToJSValue, A2: ConvertibleToJSValue, A3: ConvertibleToJSValue, A4: ConvertibleToJSValue, A5: ConvertibleToJSValue >(dynamicMember name: String) -> ((A0, A1, A2, A3, A4, A5) -> JSValue) { object![dynamicMember: name]! } @_disfavoredOverload public subscript< A0: ConvertibleToJSValue, A1: ConvertibleToJSValue, A2: ConvertibleToJSValue, A3: ConvertibleToJSValue, A4: ConvertibleToJSValue, A5: ConvertibleToJSValue, A6: ConvertibleToJSValue >(dynamicMember name: String) -> ((A0, A1, A2, A3, A4, A5, A6) -> JSValue) { object![dynamicMember: name]! } } #endif |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |