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/ConvertibleToJSValue.swift at main · aehlke/JavaScriptKit Expand file tree/ Copy pathConvertibleToJSValue.swiftMore file actions 277 lines (240 loc) · 8.14 KB / Copy pathConvertibleToJSValue.swiftFile metadata and controls277 lines (240 loc) · 8.14 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 import _CJavaScriptKit /// Objects that can be converted to a JavaScript value, preferably in a lossless manner. public protocol ConvertibleToJSValue { /// Create a JSValue that represents this object var jsValue: JSValue { get } } extension ConvertibleToJSValue { @available(*, deprecated, message: "Use the .jsValue property instead") public func jsValue() -> JSValue { jsValue } } public typealias JSValueCompatible = ConvertibleToJSValue & ConstructibleFromJSValue extension JSValue: JSValueCompatible { public static func construct(from value: JSValue) -> Self? { return value } public var jsValue: JSValue { self } } extension Bool: ConvertibleToJSValue { public var jsValue: JSValue { .boolean(self) } } extension Int: ConvertibleToJSValue { public var jsValue: JSValue { assert(Self.bitWidth == 32) return .number(Double(self)) } } extension UInt: ConvertibleToJSValue { public var jsValue: JSValue { assert(Self.bitWidth == 32) return .number(Double(self)) } } extension Float: ConvertibleToJSValue { public var jsValue: JSValue { .number(Double(self)) } } extension Double: ConvertibleToJSValue { public var jsValue: JSValue { .number(self) } } extension String: ConvertibleToJSValue { public var jsValue: JSValue { .string(JSString(self)) } } extension UInt8: ConvertibleToJSValue { public var jsValue: JSValue { .number(Double(self)) } } extension UInt16: ConvertibleToJSValue { public var jsValue: JSValue { .number(Double(self)) } } extension UInt32: ConvertibleToJSValue { public var jsValue: JSValue { .number(Double(self)) } } extension Int8: ConvertibleToJSValue { public var jsValue: JSValue { .number(Double(self)) } } extension Int16: ConvertibleToJSValue { public var jsValue: JSValue { .number(Double(self)) } } extension Int32: ConvertibleToJSValue { public var jsValue: JSValue { .number(Double(self)) } } extension JSString: ConvertibleToJSValue { public var jsValue: JSValue { .string(self) } } extension JSObject: JSValueCompatible {} private let _objectConstructor = LazyThreadLocal(initialize: { JSObject.global.Object.object! }) private var objectConstructor: JSObject { _objectConstructor.wrappedValue } private let _arrayConstructor = LazyThreadLocal(initialize: { JSObject.global.Array.object! }) private var arrayConstructor: JSObject { _arrayConstructor.wrappedValue } #if !hasFeature(Embedded) extension Dictionary where Value == ConvertibleToJSValue, Key == String { public var jsValue: JSValue { let object = objectConstructor.new() for (key, value) in self { object[key] = value.jsValue } return .object(object) } } #endif extension Dictionary: ConvertibleToJSValue where Value: ConvertibleToJSValue, Key == String { public var jsValue: JSValue { let object = objectConstructor.new() for (key, value) in self { object[key] = value.jsValue } return .object(object) } } extension Dictionary: ConstructibleFromJSValue where Value: ConstructibleFromJSValue, Key == String { public static func construct(from value: JSValue) -> Self? { guard let objectRef = value.object, let keys: [String] = objectConstructor.keys!(objectRef.jsValue).fromJSValue() else { return nil } var entries = [(String, Value)]() entries.reserveCapacity(keys.count) for key in keys { guard let value: Value = objectRef[key].fromJSValue() else { return nil } entries.append((key, value)) } return Dictionary(uniqueKeysWithValues: entries) } } extension Optional: ConstructibleFromJSValue where Wrapped: ConstructibleFromJSValue { public static func construct(from value: JSValue) -> Self? { switch value { case .null, .undefined: return .some(nil) default: guard let wrapped = Wrapped.construct(from: value) else { return nil } return .some(wrapped) } } } extension Optional: ConvertibleToJSValue where Wrapped: ConvertibleToJSValue { public var jsValue: JSValue { switch self { case .none: return .null case .some(let wrapped): return wrapped.jsValue } } } extension Array: ConvertibleToJSValue where Element: ConvertibleToJSValue { public var jsValue: JSValue { let array = arrayConstructor.new(count) for (index, element) in enumerated() { array[index] = element.jsValue } return .object(array) } } #if !hasFeature(Embedded) extension Array where Element == ConvertibleToJSValue { public var jsValue: JSValue { let array = arrayConstructor.new(count) for (index, element) in enumerated() { array[index] = element.jsValue } return .object(array) } } #endif extension Array: ConstructibleFromJSValue where Element: ConstructibleFromJSValue { public static func construct(from value: JSValue) -> [Element]? { guard let objectRef = value.object, objectRef.isInstanceOf(JSObject.global.Array.object!) else { return nil } let count: Int = objectRef.length.fromJSValue()! var array = [Element]() array.reserveCapacity(count) for i in 0..<count { guard let value: Element = objectRef[i].fromJSValue() else { return nil } array.append(value) } return array } } extension RawJSValue: ConvertibleToJSValue { public var jsValue: JSValue { switch kind { case .boolean: return .boolean(payload1 != 0) case .number: return .number(payload2) case .string: return .string(JSString(jsRef: payload1)) case .object: return .object(JSObject(id: UInt32(payload1))) case .null: return .null case .undefined: return .undefined case .symbol: return .symbol(JSSymbol(id: UInt32(payload1))) case .bigInt: return .bigInt(JSBigInt(id: UInt32(payload1))) } } } extension JSValue { func withRawJSValue<T>(_ body: (RawJSValue) -> T) -> T { body(convertToRawJSValue()) } fileprivate func convertToRawJSValue() -> RawJSValue { let kind: JavaScriptValueKind let payload1: JavaScriptPayload1 var payload2: JavaScriptPayload2 = 0 switch self.storage { case .boolean(let boolValue): kind = .boolean payload1 = boolValue ? 1 : 0 case .number(let numberValue): kind = .number payload1 = 0 payload2 = numberValue case .string(let string): kind = .string payload1 = string.asInternalJSRef() payload2 = 0 case .object(let ref): kind = .object payload1 = JavaScriptPayload1(ref.id) case .null: kind = .null payload1 = 0 case .undefined: kind = .undefined payload1 = 0 case .symbol(let symbolRef): kind = .symbol payload1 = JavaScriptPayload1(symbolRef.id) case .bigInt(let bigIntRef): kind = .bigInt payload1 = JavaScriptPayload1(bigIntRef.id) } return RawJSValue(kind: kind, payload1: payload1, payload2: payload2) } } extension Array where Element: ConvertibleToJSValue { func withRawJSValues<T>(_ body: ([RawJSValue]) -> T) -> T { let jsValues = map { $0.jsValue } // Ensure the jsValues live longer than the temporary raw JS values return withExtendedLifetime(jsValues) { body(jsValues.map { $0.convertToRawJSValue() }) } } } #if !hasFeature(Embedded) extension Array where Element == ConvertibleToJSValue { func withRawJSValues<T>(_ body: ([RawJSValue]) -> T) -> T { let jsValues = map { $0.jsValue } // Ensure the jsValues live longer than the temporary raw JS values return withExtendedLifetime(jsValues) { body(jsValues.map { $0.convertToRawJSValue() }) } } } #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. |