1 // Copyright 2022-present 650 Industries. All rights reserved.
2 
3 // MARK: - Arguments
4 
5 /**
6  Tries to cast given argument to the type that is wrapped by the argument type.
7  - Parameters:
8   - argument: A value to be cast. If it's a ``JavaScriptValue``, it's first unpacked to the raw value.
9   - argumentType: Something that implements ``AnyArgumentType`` and knows how to cast the argument.
10  - Returns: A new value converted according to the argument type.
11  - Throws: Rethrows various exceptions that could be thrown by the argument type wrappers.
12  */
13 internal func castArgument(_ argument: Any, toType argumentType: AnyArgumentType) throws -> Any {
14   // TODO: Accept JavaScriptValue and JavaScriptObject as argument types.
15   if let argument = argument as? JavaScriptValue {
16     return try argumentType.cast(argument.getRaw())
17   }
18   return try argumentType.cast(argument)
19 }
20 
21 /**
22  Same as ``castArgument(_:argumentType:)`` but for an array of arguments.
23  - Parameters:
24    - arguments: An array of arguments to be cast.
25    - argumentTypes: An array of argument types in the same order as the array of arguments.
26  - Returns: An array of arguments after casting. Its size is the same as the input arrays.
27  - Throws: ``InvalidArgsNumberException`` when the sizes of arrays passed as parameters are not equal.
28    Rethrows exceptions thrown by ``castArgument(_:argumentType:)``.
29  */
30 internal func castArguments(_ arguments: [Any], toTypes argumentTypes: [AnyArgumentType]) throws -> [Any] {
31   if arguments.count != argumentTypes.count {
32     throw InvalidArgsNumberException((received: arguments.count, expected: argumentTypes.count))
33   }
34   return try arguments.enumerated().map { index, argument in
35     let argumentType = argumentTypes[index]
36 
37     do {
38       return try castArgument(argument, toType: argumentType)
39     } catch {
40       throw ArgumentCastException((index: index, type: argumentType)).causedBy(error)
41     }
42   }
43 }
44 
45 // MARK: - Exceptions
46 
47 internal class InvalidArgsNumberException: GenericException<(received: Int, expected: Int)> {
48   override var reason: String {
49     "Received \(param.received) arguments, but \(param.expected) was expected"
50   }
51 }
52 
53 internal class ArgumentCastException: GenericException<(index: Int, type: AnyArgumentType)> {
54   override var reason: String {
55     "Argument at index '\(param.index)' couldn't be cast to type \(param.type.description)"
56   }
57 }
58 
59 private class ModuleUnavailableException: GenericException<String> {
60   override var reason: String {
61     "Module '\(param)' is no longer available"
62   }
63 }
64