SPB Git

Repository Public License

ScriptValue.swift 3.6 KB 77 lines swift

1import Foundation23/// A JSON-like value crossing the JavaScript boundary. `Any` results of WebKit are converted to this `Sendable`4/// form immediately, so nothing untyped travels through the module.5public enum ScriptValue: Sendable, Hashable {6    case null7    case bool(Bool)8    case number(Double)9    case string(String)10    case array([ScriptValue])11    case object([String: ScriptValue])1213    /// Converts what WebKit hands back (`NSNumber`, `NSString`, `NSArray`, `NSDictionary`, `NSNull`, `NSDate`).14    /// Unknown object kinds become `.null`. Depth is bounded so a hostile structure cannot recurse forever.15    public init(any value: Any?, depth: Int = 0) {16        guard let value, depth < 32 else { self = .null; return }17        switch value {18        case is NSNull:19            self = .null20        case let number as NSNumber:21            // CFBoolean is the only reliable way to tell a JavaScript boolean from a number.22            if CFGetTypeID(number) == CFBooleanGetTypeID() { self = .bool(number.boolValue) } else { self = .number(number.doubleValue) }23        case let string as String:24            self = .string(string)25        case let array as [Any]:26            self = .array(array.map { ScriptValue(any: $0, depth: depth + 1) })27        case let dictionary as [String: Any]:28            self = .object(dictionary.mapValues { ScriptValue(any: $0, depth: depth + 1) })29        case let date as Date:30            self = .number(date.timeIntervalSince1970 * 1000)31        default:32            self = .null33        }34    }3536    public var stringValue: String? { if case .string(let value) = self { value } else { nil } }37    public var boolValue: Bool? { if case .bool(let value) = self { value } else { nil } }38    public var doubleValue: Double? { if case .number(let value) = self { value } else { nil } }39    public var intValue: Int? {40        guard case .number(let value) = self, value.isFinite, abs(value) < 9_007_199_254_740_992 else { return nil }41        return Int(value)42    }43    public var arrayValue: [ScriptValue]? { if case .array(let value) = self { value } else { nil } }44    public var objectValue: [String: ScriptValue]? { if case .object(let value) = self { value } else { nil } }45    public var isNull: Bool { self == .null }4647    public subscript(key: String) -> ScriptValue? { objectValue?[key] }4849    /// Foundation representation, for passing arguments to `callAsyncJavaScript`.50    public var foundationValue: Any {51        switch self {52        case .null: NSNull()53        case .bool(let value): value54        case .number(let value): value55        case .string(let value): value56        case .array(let values): values.map(\.foundationValue)57        case .object(let values): values.mapValues(\.foundationValue)58        }59    }60}6162extension ScriptValue: ExpressibleByStringLiteral, ExpressibleByBooleanLiteral, ExpressibleByIntegerLiteral,63                       ExpressibleByFloatLiteral, ExpressibleByNilLiteral {64    public init(stringLiteral value: String) { self = .string(value) }65    public init(booleanLiteral value: Bool) { self = .bool(value) }66    public init(integerLiteral value: Int) { self = .number(Double(value)) }67    public init(floatLiteral value: Double) { self = .number(value) }68    public init(nilLiteral: ()) { self = .null }69}7071extension ScriptValue: ExpressibleByArrayLiteral, ExpressibleByDictionaryLiteral {72    public init(arrayLiteral elements: ScriptValue...) { self = .array(elements) }73    public init(dictionaryLiteral elements: (String, ScriptValue)...) {74        self = .object(Dictionary(elements, uniquingKeysWith: { _, last in last }))75    }76}77
Files main
Menu

Theme