@@ -1906,30 +1906,80 @@ public let WasmCodeGenerators: [CodeGenerator] = [
19061906 let labelType = b. type ( of: label) . wasmLabelType!
19071907 let lastParamType = labelType. parameters. last!
19081908 assert ( lastParamType. wasmReferenceType != nil , " This should never be a wasmGenericRef " )
1909- let isIndexType = !lastParamType. wasmReferenceType!. isAbstract ( )
1910- let typeDef = isIndexType ? b. getWasmTypeDef ( for: lastParamType) : nil
19111909 let sourceType = lastParamType. wasmReferenceType!. kind. topType ( )
19121910 let v = function. findOrGenerateWasmVar ( ofType: sourceType)
19131911 let args = labelType. parameters. dropLast ( ) . map ( function. findOrGenerateWasmVar)
1914- function. wasmBranchOnCast (
1915- v, targetRefType: lastParamType, to: label, args: args, typeDef: typeDef)
1912+ let isIndexType = !lastParamType. wasmReferenceType!. isAbstract ( )
1913+ if isIndexType {
1914+ let typeDef = b. getWasmTypeDef ( for: lastParamType)
1915+ let unlinkedLastParamType = ILType . wasmRef (
1916+ . Index( ) , nullability: lastParamType. wasmReferenceType!. nullability)
1917+ function. wasmBranchOnCast (
1918+ v, targetRefType: unlinkedLastParamType, to: label, args: args, typeDef: typeDef
1919+ )
1920+ } else {
1921+ function. wasmBranchOnCast ( v, targetRefType: lastParamType, to: label, args: args)
1922+ }
19161923 } else {
1917- let hierarchies : [ ILType ] = [
1918- . wasmAnyRef ( ) , . wasmFuncRef ( ) , . wasmExternRef ( ) , . wasmExnRef ( ) ,
1919- ]
1920- let topType = hierarchies . randomElement ( ) !
1921- let targetRefType = function . randomWasmReferenceType ( withAbstractSuperType : topType )
1922- let isIndexType = !targetRefType . wasmReferenceType! . isAbstract ( )
1923- let typeDef = isIndexType ? b . getWasmTypeDef ( for : targetRefType ) : nil
1924+ let topType = ILType . wasmRefHierarchyTopTypes . randomElement ( ) !
1925+ let ( targetRefType , typeDef ) = function . randomWasmReferenceType (
1926+ withAbstractSuperType : topType )
1927+ var blockOutputTypes = b . randomWasmBlockOutputTypes ( upTo : 2 ) + [ targetRefType ]
1928+ let signatureDef = b . wasmDefineAdHocSignatureType (
1929+ signature : [ ] => blockOutputTypes ,
1930+ indexTypes : typeDef != nil ? [ typeDef! ] : nil )
19241931
1925- let blockParamTypes = b. randomWasmBlockOutputTypes ( upTo: 2 ) + [ targetRefType]
1932+ let signature = b. type ( of: signatureDef) . wasmFunctionSignatureDefSignature
1933+
1934+ function. wasmBuildBlockWithResults ( with: signatureDef, args: [ ] ) {
1935+ blockLabel, _ in
1936+ let sourceVar = function. findOrGenerateWasmVar ( ofType: topType)
1937+ let args = signature. outputTypes. map ( function. findOrGenerateWasmVar)
1938+
1939+ function. wasmBranchOnCast (
1940+ sourceVar, targetRefType: targetRefType, to: blockLabel, args: args. dropLast ( ) ,
1941+ typeDef: typeDef)
1942+
1943+ return args
1944+ }
1945+ }
1946+ } ,
1947+
1948+ CodeGenerator (
1949+ " WasmBranchOnCastFailGenerator " , inContext: . single( . wasmFunction)
1950+ ) { b in
1951+ let function = b. currentWasmModule. currentWasmFunction
1952+
1953+ let label = b. findVariable { label in
1954+ if let params = b. type ( of: label) . wasmLabelType? . parameters, let last = params. last {
1955+ return ILType . wasmRefHierarchyTopTypes. contains ( last)
1956+ }
1957+ return false
1958+ }
1959+
1960+ if let label {
1961+ let labelType = b. type ( of: label) . wasmLabelType!
1962+ let lastParamType = labelType. parameters. last!
1963+
1964+ let sourceVar = function. findOrGenerateWasmVar ( ofType: lastParamType)
1965+ let ( targetRefType, typeDef) = function. randomWasmReferenceType (
1966+ withAbstractSuperType: lastParamType)
1967+
1968+ let args = labelType. parameters. dropLast ( ) . map ( function. findOrGenerateWasmVar)
1969+ function. wasmBranchOnCastFail (
1970+ sourceVar, targetRefType: targetRefType, to: label, args: args, typeDef: typeDef)
1971+ } else {
1972+ let topType = ILType . wasmRefHierarchyTopTypes. randomElement ( ) !
1973+ let blockParamTypes = b. randomWasmBlockOutputTypes ( upTo: 2 ) + [ topType]
19261974
19271975 function. wasmBuildBlockWithResults ( with: [ ] => blockParamTypes, args: [ ] ) {
19281976 blockLabel, _ in
19291977 let sourceVar = function. findOrGenerateWasmVar ( ofType: topType)
1978+ let ( targetRefType, typeDef) = function. randomWasmReferenceType (
1979+ withAbstractSuperType: topType)
19301980 let args = blockParamTypes. map ( function. findOrGenerateWasmVar)
19311981
1932- function. wasmBranchOnCast (
1982+ function. wasmBranchOnCastFail (
19331983 sourceVar, targetRefType: targetRefType, to: blockLabel, args: args. dropLast ( ) ,
19341984 typeDef: typeDef)
19351985
0 commit comments