import _CJavaScriptKit
/// `JSValue` represents a value in JavaScript.
@dynamicMemberLookup
public enum JSValue: Equatable {
case boolean(Bool)
case string(JSString)
case number(Double)
case object(JSObject)
case null
case undefined
case function(JSFunction)
case symbol(JSSymbol)
case bigInt(JSBigInt)
/// Returns the `Bool` value of this JS value if its type is boolean.
/// If not, returns `nil`.
public var boolean: Bool? {
switch self {
case let .boolean(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 self {
case let .string(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 self {
case let .number(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 self {
case let .object(object): return object
default: return nil
}
}
/// Returns the `JSFunction` of this JS value if its type is function.
/// If not, returns `nil`.
public var function: JSFunction? {
switch self {
case let .function(function): return function
default: return nil
}
}
/// Returns the `JSSymbol` of this JS value if its type is function.
/// If not, returns `nil`.
public var symbol: JSSymbol? {
switch self {
case let .symbol(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 self {
case let .bigInt(bigInt): return bigInt
default: return nil
}
}
/// Returns the `true` if this JS value is null.
/// If not, returns `false`.
public var isNull: Bool {
return self == .null
}
/// Returns the `true` if this JS value is undefined.
/// If not, returns `false`.
public var isUndefined: Bool {
return self == .undefined
}
}
public extension JSValue {
/// 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.
subscript(dynamicMember name: String) -> ((ConvertibleToJSValue...) -> JSValue) {
object![dynamicMember: name]!
}
/// An unsafe convenience method of `JSObject.subscript(_ index: Int) -> JSValue`
/// - Precondition: `self` must be a JavaScript Object.
subscript(dynamicMember name: String) -> JSValue {
get { self.object![name] }
set { self.object![name] = newValue }
}
/// An unsafe convenience method of `JSObject.subscript(_ index: Int) -> JSValue`
/// - Precondition: `self` must be a JavaScript Object.
subscript(_ index: Int) -> JSValue {
get { object![index] }
set { object![index] = newValue }
}
}
extension JSValue: Swift.Error {}
public extension JSValue {
func fromJSValue