@@ -13,6 +13,7 @@ import Denotations._
13
13
import Flags ._
14
14
import Names ._
15
15
import NameKinds .DefaultGetterName
16
+ import NameOps ._
16
17
import Periods ._
17
18
import Phases ._
18
19
import StdNames ._
@@ -681,7 +682,7 @@ final class JSExportsGen(jsCodeGen: JSCodeGen)(using Context) {
681
682
val varDefs = new mutable.ListBuffer [js.VarDef ]
682
683
683
684
for ((param, i) <- exported.params.zipWithIndex) {
684
- val rhs = genScalaArg(exported, i, formalArgsRegistry, param, static)(
685
+ val rhs = genScalaArg(exported, i, formalArgsRegistry, param, static, captures = Nil )(
685
686
prevArgsCount => varDefs.take(prevArgsCount).toList.map(_.ref))
686
687
687
688
varDefs += js.VarDef (freshLocalIdent(" prep" + i), NoOriginalName , rhs.tpe, mutable = false , rhs)
@@ -698,7 +699,7 @@ final class JSExportsGen(jsCodeGen: JSCodeGen)(using Context) {
698
699
* (unboxing and default parameter handling).
699
700
*/
700
701
def genScalaArg (exported : Exported , paramIndex : Int , formalArgsRegistry : FormalArgsRegistry ,
701
- param : JSParamInfo , static : Boolean )(
702
+ param : JSParamInfo , static : Boolean , captures : List [js. Tree ] )(
702
703
previousArgsValues : Int => List [js.Tree ])(
703
704
implicit pos : SourcePosition ): js.Tree = {
704
705
@@ -713,7 +714,7 @@ final class JSExportsGen(jsCodeGen: JSCodeGen)(using Context) {
713
714
if (exported.hasDefaultAt(paramIndex)) {
714
715
// If argument is undefined and there is a default getter, call it
715
716
js.If (js.BinaryOp (js.BinaryOp .=== , jsArg, js.Undefined ()), {
716
- genCallDefaultGetter(exported.sym, paramIndex, static)(previousArgsValues)
717
+ genCallDefaultGetter(exported.sym, paramIndex, static, captures )(previousArgsValues)
717
718
}, {
718
719
unboxedArg
719
720
})(unboxedArg.tpe)
@@ -724,7 +725,8 @@ final class JSExportsGen(jsCodeGen: JSCodeGen)(using Context) {
724
725
}
725
726
}
726
727
727
- private def genCallDefaultGetter (sym : Symbol , paramIndex : Int , static : Boolean )(
728
+ private def genCallDefaultGetter (sym : Symbol , paramIndex : Int ,
729
+ static : Boolean , captures : List [js.Tree ])(
728
730
previousArgsValues : Int => List [js.Tree ])(
729
731
implicit pos : SourcePosition ): js.Tree = {
730
732
@@ -735,9 +737,30 @@ final class JSExportsGen(jsCodeGen: JSCodeGen)(using Context) {
735
737
assert(! defaultGetterDenot.isOverloaded, i " found overloaded default getter $defaultGetterDenot" )
736
738
val defaultGetter = defaultGetterDenot.symbol
737
739
738
- val targetTree =
739
- if (sym.isClassConstructor || static) genLoadModule(targetSym)
740
- else js.This ()(encodeClassType(targetSym))
740
+ val targetTree = {
741
+ if (sym.isClassConstructor || static) {
742
+ if (targetSym.isStatic) {
743
+ assert(captures.isEmpty, i " expected empty captures for ${targetSym.fullName} at $pos" )
744
+ genLoadModule(targetSym)
745
+ } else {
746
+ assert(captures.sizeIs == 1 , " expected exactly one capture" )
747
+
748
+ // Find the module accessor.
749
+ val outer = targetSym.originalOwner
750
+ val name = atPhase(typerPhase)(targetSym.name.unexpandedName).sourceModuleName
751
+ val modAccessor = outer.info.memberBasedOnFlags(name, required = Module ).symbol
752
+ assert(modAccessor.exists, i " could not find module accessor for ${targetSym.fullName} at $pos" )
753
+
754
+ val receiver = captures.head
755
+ if (outer.isJSType)
756
+ genApplyJSClassMethod(receiver, modAccessor, Nil )
757
+ else
758
+ genApplyMethodMaybeStatically(receiver, modAccessor, Nil )
759
+ }
760
+ } else {
761
+ js.This ()(encodeClassType(targetSym))
762
+ }
763
+ }
741
764
742
765
// Pass previous arguments to defaultGetter
743
766
val defaultGetterArgs = previousArgsValues(defaultGetter.info.paramInfoss.head.size)
0 commit comments