@@ -134,8 +134,8 @@ trait GenWitInterop[G <: Global with Singleton] extends SubComponent {
134134 implicit val pos = tree.pos
135135 val sym = tree.symbol
136136 withNewLocalNameScope {
137- val funcType = jsInterop.witFunctionTypeOf (sym)
138- val baseParams = funcType.params .map(toWIT(_))
137+ val (paramTypes, resultType) = witMethodSignatureOf (sym)
138+ val baseParams = paramTypes .map(toWIT(_))
139139 val params = name match {
140140 case _:js.WitFunctionName .Function |
141141 _:js.WitFunctionName .ResourceConstructor |
@@ -146,7 +146,7 @@ trait GenWitInterop[G <: Global with Singleton] extends SubComponent {
146146 }
147147 val witFuncType = wit.FuncType (
148148 params,
149- toResultWIT(funcType. resultType)
149+ toResultWIT(resultType)
150150 )
151151 js.WitNativeMemberDef (flags, moduleName, name,
152152 encodeMethodSym(sym), witFuncType)
@@ -158,8 +158,7 @@ trait GenWitInterop[G <: Global with Singleton] extends SubComponent {
158158 val sym = tree.symbol
159159
160160 val flags = js.MemberFlags .empty.withNamespace(js.MemberNamespace .PublicStatic )
161- val funcType = jsInterop.witFunctionTypeOf(sym)
162-
161+ val (paramTypes, resultType) = witMethodSignatureOf(sym)
163162 for {
164163 methodAnnot <- sym.getAnnotation(WitResourceStaticMethodAnnotation )
165164 resourceAnnot <- sym.owner.companionClass.getAnnotation(WitResourceImportAnnotation )
@@ -170,8 +169,8 @@ trait GenWitInterop[G <: Global with Singleton] extends SubComponent {
170169 val name = js.WitFunctionName .ResourceStaticMethod (
171170 func = methodName, resource = resourceName)
172171 withNewLocalNameScope {
173- val params = funcType.params. map(p => toWIT(p ))
174- val ft = wit.FuncType (params, toResultWIT(funcType. resultType))
172+ val params = paramTypes. map(toWIT(_ ))
173+ val ft = wit.FuncType (params, toResultWIT(resultType))
175174 js.WitNativeMemberDef (flags, moduleName, name, encodeMethodSym(sym), ft)
176175 }
177176 }
@@ -182,7 +181,7 @@ trait GenWitInterop[G <: Global with Singleton] extends SubComponent {
182181 val sym = tree.symbol
183182
184183 val flags = js.MemberFlags .empty.withNamespace(js.MemberNamespace .PublicStatic )
185- val funcType = jsInterop.witFunctionTypeOf (sym)
184+ val (paramTypes, resultType) = witMethodSignatureOf (sym)
186185
187186 for {
188187 methodAnnot <- sym.getAnnotation(WitResourceConstructorAnnotation )
@@ -192,19 +191,20 @@ trait GenWitInterop[G <: Global with Singleton] extends SubComponent {
192191 } yield {
193192 val name = js.WitFunctionName .ResourceConstructor (resourceName)
194193 withNewLocalNameScope {
195- val params = funcType.params. map(p => toWIT(p ))
196- val ft = wit.FuncType (params, toResultWIT(funcType. resultType))
194+ val params = paramTypes. map(toWIT(_ ))
195+ val ft = wit.FuncType (params, toResultWIT(resultType))
197196 js.WitNativeMemberDef (flags, moduleName, name, encodeMethodSym(sym), ft)
198197 }
199198 }
200199 }
201200
202- def genWitExportDef (info : jsInterop.WitExportInfo ,
201+ def genWitExportDef (info : jsInterop.WitExportInfo , sym : Symbol ,
203202 methodDef : js.MethodDef ): js.WitExportDef = {
204203 withNewLocalNameScope {
204+ val (paramTypes, resultType) = witMethodSignatureOf(sym)
205205 val signature = wit.FuncType (
206- info.signature.params .map(toWIT(_)),
207- toResultWIT(info.signature. resultType)
206+ paramTypes .map(toWIT(_)),
207+ toResultWIT(resultType)
208208 )
209209 js.WitExportDef (
210210 info.moduleName,
@@ -215,15 +215,46 @@ trait GenWitInterop[G <: Global with Singleton] extends SubComponent {
215215 }
216216 }
217217
218+ private def witMethodSignatureOf (sym : Symbol ): (List [Type ], Type ) = {
219+ exitingPhase(currentRun.typerPhase) {
220+ val methodType = sym.tpe
221+ val params =
222+ if (methodType.paramss.isEmpty) Nil
223+ else methodType.paramss.head.map(_.tpe)
224+ (params, methodType.resultType)
225+ }
226+ }
227+
228+ private def witVariantValueTypeOf (sym : Symbol ): Type = {
229+ exitingPhase(currentRun.typerPhase) {
230+ if (sym.isModuleClass) {
231+ UnitTpe
232+ } else if (sym.isClass && sym.isFinal && ! sym.isTrait) {
233+ sym.primaryConstructor.paramss.flatten match {
234+ case Nil =>
235+ UnitTpe
236+ case param :: Nil =>
237+ param.tpe
238+ case _ =>
239+ throw new AssertionError (s " Invalid WIT variant case shape for $sym" )
240+ }
241+ } else {
242+ throw new AssertionError (s " Invalid WIT variant case symbol $sym" )
243+ }
244+ }
245+ }
246+
218247 private def toWIT (tpe : Type ): wit.ValType = {
219- unsigned2WIT.get(tpe.typeSymbolDirect).orElse {
220- toWITMaybeArray(tpe.dealiasWiden)
248+ val widenedTpe = exitingPhase(currentRun.typerPhase)(tpe.dealiasWiden)
249+
250+ unsigned2WIT.get(widenedTpe.typeSymbolDirect).orElse {
251+ toWITMaybeArray(widenedTpe)
221252 }.orElse {
222- primitiveIRWIT.get(toIRType(tpe.dealiasWiden ))
253+ primitiveIRWIT.get(toIRType(widenedTpe ))
223254 }.getOrElse {
224- tpe.dealiasWiden .typeSymbol match {
255+ widenedTpe .typeSymbol match {
225256 case tsym if isWasmComponentTupleClass(tsym) =>
226- wit.TupleType (tpe .typeArgs.map(toWIT(_)))
257+ wit.TupleType (widenedTpe.baseType(tsym) .typeArgs.map(toWIT(_)))
227258
228259 case tsym if tsym.hasAnnotation(WitFlagsAnnotation ) =>
229260 // Read numFlags from annotation parameter
@@ -236,26 +267,26 @@ trait GenWitInterop[G <: Global with Singleton] extends SubComponent {
236267 wit.FlagsType (className, numFlags)
237268
238269 case tsym if isWasmWitRecordClass(tsym) =>
239- // TODO: it needs to be sorted by the order of record in wit definition
240270 val className = encodeClassName(tsym)
241- val fields : List [wit.FieldType ] = tsym.info.decls.collect {
242- case f if f.isField =>
243- val label = encodeFieldSym(f)(f.pos).name
244- val fieldType = jsInterop.witVariantValueTypeOf(f)
245- val valueType = toWIT(fieldType)
246- wit.FieldType (label, valueType)
247- }.toList
271+ val fields = exitingPhase(currentRun.typerPhase) {
272+ tsym.primaryConstructor.paramss.flatten.map { param =>
273+ (param.name.dropLocal.toString(), param.tpe)
274+ }
275+ }.map { case (fieldName, fieldType) =>
276+ val label = Names .FieldName (className, Names .SimpleFieldName (fieldName))
277+ wit.FieldType (label, toWIT(fieldType))
278+ }
248279 wit.RecordType (className, fields)
249280
250281 case tsym if isWasmWitResourceType(tsym) =>
251282 wit.ResourceType (encodeClassName(tsym))
252283
253284 case tsym if tsym.isSubClass(ComponentResultClass ) && tsym.isSealed =>
254- val List (ok, err) = tpe .typeArgs
285+ val List (ok, err) = widenedTpe.baseType( ComponentResultClass ) .typeArgs
255286 wit.ResultType (toResultWIT(ok), toResultWIT(err))
256287
257288 case tsym if tsym.fullName == " java.util.Optional" =>
258- val List (t) = tpe.dealiasWiden .typeArgs
289+ val List (t) = widenedTpe.baseType(tsym) .typeArgs
259290 wit.OptionType (toWIT(t))
260291
261292 case tsym if tsym.hasAnnotation(WitVariantAnnotation ) && tsym.isSealed =>
@@ -266,7 +297,7 @@ trait GenWitInterop[G <: Global with Singleton] extends SubComponent {
266297 val cases = tsym.sealedChildren.toList.sortBy(_.pos.line) map { child =>
267298 // assert(child.isFinal)
268299 // assert(child.isClass)
269- val valueType = jsInterop. witVariantValueTypeOf(child)
300+ val valueType = witVariantValueTypeOf(child)
270301 val caseTyp = if (toIRType(valueType) == jstpe.VoidType ) {
271302 None
272303 } else {
0 commit comments