@@ -125,12 +125,12 @@ public let CodeGenerators: [CodeGenerator] = [
125125 let fctName = ( useProperty ? prototypeType. properties : prototypeType. methods) . randomElement ( ) !
126126 let fct = b. getProperty ( fctName, of: prototype)
127127 let fctType = b. type ( of: fct)
128- let arguments = b. randomArguments ( forCalling : fct)
128+ let ( arguments, matches ) = b. randomArguments ( forCallingGuardableFunction : fct)
129129 let receiverType = fctType. receiver ?? prototypeType
130130 let desiredReceiverType = fctType. receiver ?? prototypeType
131131 let receiver = b. randomVariable ( forUseAs: desiredReceiverType)
132132 let needGuard = ( !fctType. Is ( . function( ) ) && !fctType. Is ( . unboundFunction( ) ) )
133- || !b. type ( of: receiver) . Is ( receiverType)
133+ || !b. type ( of: receiver) . Is ( receiverType) || !matches
134134 if Bool . random ( ) {
135135 b. callMethod ( " call " , on: fct, withArgs: [ receiver] + arguments, guard: needGuard)
136136 } else {
@@ -918,7 +918,8 @@ public let CodeGenerators: [CodeGenerator] = [
918918 b. buildRecursive ( )
919919 b. doReturn ( b. randomJsVariable ( ) )
920920 }
921- b. callFunction ( f, withArgs: b. randomArguments ( forCalling: f) )
921+ let ( arguments, matches) = b. randomArguments ( forCallingGuardableFunction: f)
922+ b. callFunction ( f, withArgs: arguments, guard: !matches)
922923 } ,
923924
924925 RecursiveCodeGenerator ( " StrictModeFunctionGenerator " ) { b in
@@ -929,7 +930,8 @@ public let CodeGenerators: [CodeGenerator] = [
929930 b. buildRecursive ( )
930931 b. doReturn ( b. randomJsVariable ( ) )
931932 }
932- b. callFunction ( f, withArgs: b. randomArguments ( forCalling: f) )
933+ let ( arguments, matches) = b. randomArguments ( forCallingGuardableFunction: f)
934+ b. callFunction ( f, withArgs: arguments, guard: !matches)
933935 } ,
934936
935937 RecursiveCodeGenerator ( " ArrowFunctionGenerator " ) { b in
@@ -952,7 +954,8 @@ public let CodeGenerators: [CodeGenerator] = [
952954 }
953955 b. doReturn ( b. randomJsVariable ( ) )
954956 }
955- b. callFunction ( f, withArgs: b. randomArguments ( forCalling: f) )
957+ let ( arguments, matches) = b. randomArguments ( forCallingGuardableFunction: f)
958+ b. callFunction ( f, withArgs: arguments, guard: !matches)
956959 } ,
957960
958961 RecursiveCodeGenerator ( " AsyncFunctionGenerator " ) { b in
@@ -961,7 +964,8 @@ public let CodeGenerators: [CodeGenerator] = [
961964 b. await ( b. randomJsVariable ( ) )
962965 b. doReturn ( b. randomJsVariable ( ) )
963966 }
964- b. callFunction ( f, withArgs: b. randomArguments ( forCalling: f) )
967+ let ( arguments, matches) = b. randomArguments ( forCallingGuardableFunction: f)
968+ b. callFunction ( f, withArgs: arguments, guard: !matches)
965969 } ,
966970
967971 RecursiveCodeGenerator ( " AsyncArrowFunctionGenerator " ) { b in
@@ -986,7 +990,8 @@ public let CodeGenerators: [CodeGenerator] = [
986990 }
987991 b. doReturn ( b. randomJsVariable ( ) )
988992 }
989- b. callFunction ( f, withArgs: b. randomArguments ( forCalling: f) )
993+ let ( arguments, matches) = b. randomArguments ( forCallingGuardableFunction: f)
994+ b. callFunction ( f, withArgs: arguments, guard: !matches)
990995 } ,
991996
992997 CodeGenerator ( " PropertyRetrievalGenerator " , inputs: . preferred( . object( ) ) ) { b, obj in
@@ -1227,16 +1232,13 @@ public let CodeGenerators: [CodeGenerator] = [
12271232 } ,
12281233
12291234 CodeGenerator ( " FunctionCallGenerator " , inputs: . preferred( . function( ) ) ) { b, f in
1230- let arguments = b. randomArguments ( forCalling: f)
1231- // TODO: we may also need guarding if the arguments aren't compatible with the expected ones
1232- let needGuard = b. type ( of: f) . MayNotBe ( . function( ) )
1233- b. callFunction ( f, withArgs: arguments, guard: needGuard)
1235+ let ( arguments, matches) = b. randomArguments ( forCallingGuardableFunction: f)
1236+ b. callFunction ( f, withArgs: arguments, guard: !matches)
12341237 } ,
12351238
12361239 CodeGenerator ( " ConstructorCallGenerator " , inputs: . preferred( . constructor( ) ) ) { b, c in
1237- let arguments = b. randomArguments ( forCalling: c)
1238- // TODO: we may also need guarding if the arguments aren't compatible with the expected ones
1239- let needGuard = b. type ( of: c) . MayNotBe ( . constructor( ) )
1240+ let ( arguments, matches) = b. randomArguments ( forCallingGuardableFunction: c)
1241+ let needGuard = b. type ( of: c) . MayNotBe ( . constructor( ) ) || !matches
12401242 b. construct ( c, withArgs: arguments, guard: needGuard)
12411243 } ,
12421244
@@ -1261,22 +1263,20 @@ public let CodeGenerators: [CodeGenerator] = [
12611263 } ,
12621264
12631265 CodeGenerator ( " UnboundFunctionCallGenerator " , inputs: . preferred( . unboundFunction( ) ) ) { b, f in
1264- let arguments = b. randomArguments ( forCalling : f)
1266+ let ( arguments, argsMatch ) = b. randomArguments ( forCallingGuardableFunction : f)
12651267 let fctType = b. type ( of: f)
1266- // TODO: we may also need guarding if the arguments aren't compatible with the expected ones
1267- let needGuard = fctType. MayNotBe ( . unboundFunction( ) )
1268- let receiver = b. randomVariable ( forUseAs: fctType. receiver ?? . object( ) )
1268+ let ( receiver, recMatches) = b. randomVariable ( forUseAsGuarded: fctType. receiver ?? . object( ) )
1269+ let needGuard = fctType. MayNotBe ( . unboundFunction( ) ) || !argsMatch || !recMatches
12691270 // For simplicity we just hard-code the call function. If this was a separate IL
12701271 // instruction, the JSTyper could infer the result type.
12711272 b. callMethod ( " call " , on: f, withArgs: [ receiver] + arguments, guard: needGuard)
12721273 } ,
12731274
12741275 CodeGenerator ( " UnboundFunctionApplyGenerator " , inputs: . preferred( . unboundFunction( ) ) ) { b, f in
1275- let arguments = b. randomArguments ( forCalling : f)
1276+ let ( arguments, argsMatch ) = b. randomArguments ( forCallingGuardableFunction : f)
12761277 let fctType = b. type ( of: f)
1277- // TODO: we may also need guarding if the arguments aren't compatible with the expected ones
1278- let needGuard = fctType. MayNotBe ( . unboundFunction( ) )
1279- let receiver = b. randomVariable ( forUseAs: fctType. receiver ?? . object( ) )
1278+ let ( receiver, recMatches) = b. randomVariable ( forUseAsGuarded: fctType. receiver ?? . object( ) )
1279+ let needGuard = fctType. MayNotBe ( . unboundFunction( ) ) || !argsMatch || !recMatches
12801280 // For simplicity we just hard-code the apply function. If this was a separate IL
12811281 // instruction, the JSTyper could infer the result type.
12821282 b. callMethod ( " apply " , on: f, withArgs: [ receiver, b. createArray ( with: arguments) ] , guard: needGuard)
0 commit comments