Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6e8a869

Browse files
committed
Address comments in compiler/*.
1 parent 5a6197d commit 6e8a869

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

compiler/src/main/scala/org/scalajs/nscplugin/GenJSCode.scala

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6196,7 +6196,7 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
61966196
* But that lowers the type to iterable.
61976197
*/
61986198
if (hasRepeatedParam) {
6199-
val (p, l) = genPatchedParam(params.last, genJSArrayToVarArgs(_), ObjectTpe)
6199+
val (p, l) = genPatchedParam(params.last, genJSArrayToVarArgs(_), jstpe.AnyType)
62006200
(Some(p), Some(l))
62016201
} else {
62026202
(None, None)
@@ -6266,32 +6266,45 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
62666266
* To translate them, we first construct a typed closure for the body:
62676267
* {{{
62686268
* typed-lambda<_this = this, capture1: U1 = capture1, ..., captureM: UM = captureM>(
6269-
* arg1: S1, ..., argN: SN): TR = {
6270-
* val arg1Unboxed: T1 = arg1.asInstanceOf[T1];
6269+
* arg1: T1, ..., argN: TN): TR = {
6270+
* val arg1Unboxed: S1 = arg1.asInstanceOf[S1];
62716271
* ...
6272-
* val argNUnboxed: TN = argN.asInstanceOf[TN];
6272+
* val argNUnboxed: SN = argN.asInstanceOf[SN];
62736273
* // inlined body of `someMethod`, boxed
62746274
* }
62756275
* }}}
62766276
* In the closure, input params are unboxed before use, and the result of
6277-
* the body of `someMethod` is boxed back.
6277+
* the body of `someMethod` is boxed back. The Si and SR are the types
6278+
* found in the target `someMethod`; the Ti and TR are the types found in
6279+
* the SAM method to be impleemnted. It is common for `Si` to be different
6280+
* from `Ti`. For example, in a Scala function `(x: Int) => x`,
6281+
* `S1 = SR = int`, but `T1 = TR = any`, because `scala.Function1` defines
6282+
* an `apply` method that erases to using `any`'s.
62786283
*
62796284
* Then, we wrap that closure in a class satisfying the expected type.
6280-
* For Scala function types, we use the existing
6281-
* `scala.scalajs.runtime.AnonFunctionN` from the library. For other
6282-
* LMF-capable types, we generate a class on the fly, which looks like
6283-
* this:
6285+
* For SAM types that do not need any bridges (including all Scala
6286+
* function types), we use a `NewLambda` node.
6287+
*
6288+
* When bridges are required (which is rare), we generate a class on the
6289+
* fly. In that case, we "inline" the captures of the typed closure as
6290+
* fields of the class, and its body as the body of the main SAM method
6291+
* implementation. Overall, it looks like this:
62846292
* {{{
62856293
* class AnonFun extends Object with FunctionalInterface {
62866294
* val ...captureI: UI
62876295
* def <init>(...captureI: UI) {
62886296
* super();
62896297
* ...this.captureI = captureI;
62906298
* }
6299+
* // main SAM method implementation
62916300
* def theSAMMethod(params: Ti...): TR = {
62926301
* val ...captureI = this.captureI;
62936302
* // inline body of the typed-lambda
62946303
* }
6304+
* // a bridge
6305+
* def theSAMMethod(params: Vi...): VR = {
6306+
* this.theSAMMethod(...params.asInstanceOf[Ti]).asInstanceOf[VR]
6307+
* }
62956308
* }
62966309
* }}}
62976310
*/
@@ -6623,19 +6636,20 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
66236636
((param, paramSym), fromParamType) <- params.zip(paramSyms).zip(fromParamTypes)
66246637
} yield {
66256638
val paramTpe = paramTpes.getOrElse(paramSym.name, paramSym.tpe)
6626-
genPatchedParam(param, adaptBoxes(_, fromParamType, paramTpe), fromParamType)
6639+
genPatchedParam(param, adaptBoxes(_, fromParamType, paramTpe),
6640+
toIRType(underlyingOfEVT(fromParamType)))
66276641
}).unzip
66286642
}
66296643

66306644
private def genPatchedParam(param: js.ParamDef, rhs: js.VarRef => js.Tree,
6631-
fromParamType: Type)(
6645+
fromParamType: jstpe.Type)(
66326646
implicit pos: Position): (js.ParamDef, js.VarDef) = {
66336647
val paramNameIdent = param.name
66346648
val origName = param.originalName
66356649
val newNameIdent = freshLocalIdent(paramNameIdent.name)(paramNameIdent.pos)
66366650
val newOrigName = origName.orElse(paramNameIdent.name)
6637-
val patchedParam = js.ParamDef(newNameIdent, newOrigName,
6638-
toIRType(underlyingOfEVT(fromParamType)), mutable = false)(param.pos)
6651+
val patchedParam = js.ParamDef(newNameIdent, newOrigName, fromParamType,
6652+
mutable = false)(param.pos)
66396653
val paramLocal = js.VarDef(paramNameIdent, origName, param.ptpe,
66406654
mutable = false, rhs(patchedParam.ref))
66416655
(patchedParam, paramLocal)

0 commit comments

Comments
 (0)