1 // Copyright 2022-present 650 Industries. All rights reserved. 2 3 // MARK: - Arguments 4 5 /** 6 Tries to cast a given value to the type that is wrapped by the dynamic type. 7 - Parameters: 8 - value: A value to be cast. If it's a ``JavaScriptValue``, it's first unpacked to the raw value. 9 - type: Something that implements ``AnyDynamicType`` and knows how to cast the argument. 10 - Returns: A new value converted according to the dynamic type. 11 - Throws: Rethrows various exceptions that could be thrown by the dynamic types. 12 */ 13 internal func cast(_ value: Any, toType type: AnyDynamicType, appContext: AppContext) throws -> Any { 14 // TODO: Accept JavaScriptValue and JavaScriptObject as argument types. 15 if !(type is DynamicTypedArrayType), let value = value as? JavaScriptValue { 16 return try type.cast(value.getRaw(), appContext: appContext) 17 } 18 return try type.cast(value, appContext: appContext) 19 } 20 21 /** 22 Tries to cast the given arguments to the types expected by the function. 23 - Parameters: 24 - arguments: An array of arguments to be cast. 25 - function: A function for which to cast the arguments. 26 - appContext: A context of the app. 27 - Returns: An array of arguments after casting. Its size is the same as the input arrays. 28 - Throws: `InvalidArgsNumberException` when the number of arguments is not equal to the actual number 29 of function's arguments (without an owner and promise). Rethrows exceptions thrown by `cast(_:toType:)`. 30 */ 31 internal func cast(arguments: [Any], forFunction function: AnyFunction, appContext: AppContext) throws -> [Any] { 32 let requiredArgumentsCount = function.requiredArgumentsCount 33 let argumentTypeOffset = function.takesOwner ? 1 : 0 34 35 if arguments.count < requiredArgumentsCount || arguments.count > function.argumentsCount { 36 throw InvalidArgsNumberException(( 37 received: arguments.count, 38 expected: function.argumentsCount, 39 required: requiredArgumentsCount 40 )) 41 } 42 return try arguments.enumerated().map { index, argument in 43 let argumentType = function.dynamicArgumentTypes[index + argumentTypeOffset] 44 45 do { 46 return try cast(argument, toType: argumentType, appContext: appContext) 47 } catch { 48 throw ArgumentCastException((index: index, type: argumentType)).causedBy(error) 49 } 50 } 51 } 52 53 /** 54 Ensures the provided array of arguments matches the number of arguments expected by the function. 55 - If the function takes the owner, it's added to the beginning. 56 - If the array is still too small, missing arguments are very likely to be optional so it puts `nil` in their place. 57 */ 58 internal func concat(arguments: [Any], withOwner owner: AnyObject?, forFunction function: AnyFunction, appContext: AppContext) -> [Any] { 59 var result = arguments 60 61 if function.takesOwner, let owner = try? function.dynamicArgumentTypes.first?.cast(owner, appContext: appContext) { 62 result = [owner] + arguments 63 } 64 if arguments.count < function.argumentsCount { 65 result += Array(repeating: Any?.none as Any, count: function.argumentsCount - arguments.count) 66 } 67 return result 68 } 69 70 // MARK: - Exceptions 71 72 internal class InvalidArgsNumberException: GenericException<(received: Int, expected: Int, required: Int)> { 73 override var reason: String { 74 if param.required < param.expected { 75 return "Received \(param.received) arguments, but \(param.expected) was expected and at least \(param.required) is required" 76 } else { 77 return "Received \(param.received) arguments, but \(param.expected) was expected" 78 } 79 } 80 } 81 82 internal class ArgumentCastException: GenericException<(index: Int, type: AnyDynamicType)> { 83 override var reason: String { 84 "The \(formatOrdinalNumber(param.index + 1)) argument cannot be cast to type \(param.type.description)" 85 } 86 87 func formatOrdinalNumber(_ number: Int) -> String { 88 let formatter = NumberFormatter() 89 formatter.numberStyle = .ordinal 90 formatter.locale = Locale(identifier: "en_US") 91 return formatter.string(from: NSNumber(value: number)) ?? "" 92 } 93 } 94 95 private class ModuleUnavailableException: GenericException<String> { 96 override var reason: String { 97 "Module '\(param)' is no longer available" 98 } 99 } 100