@@ -13,6 +13,7 @@ import Denotations._
1313import Flags ._
1414import Names ._
1515import NameKinds .DefaultGetterName
16+ import NameOps ._
1617import Periods ._
1718import Phases ._
1819import StdNames ._
@@ -681,7 +682,7 @@ final class JSExportsGen(jsCodeGen: JSCodeGen)(using Context) {
681682 val varDefs = new mutable.ListBuffer [js.VarDef ]
682683
683684 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 )(
685686 prevArgsCount => varDefs.take(prevArgsCount).toList.map(_.ref))
686687
687688 varDefs += js.VarDef (freshLocalIdent(" prep" + i), NoOriginalName , rhs.tpe, mutable = false , rhs)
@@ -698,7 +699,7 @@ final class JSExportsGen(jsCodeGen: JSCodeGen)(using Context) {
698699 * (unboxing and default parameter handling).
699700 */
700701 def genScalaArg (exported : Exported , paramIndex : Int , formalArgsRegistry : FormalArgsRegistry ,
701- param : JSParamInfo , static : Boolean )(
702+ param : JSParamInfo , static : Boolean , captures : List [js. Tree ] )(
702703 previousArgsValues : Int => List [js.Tree ])(
703704 implicit pos : SourcePosition ): js.Tree = {
704705
@@ -713,7 +714,7 @@ final class JSExportsGen(jsCodeGen: JSCodeGen)(using Context) {
713714 if (exported.hasDefaultAt(paramIndex)) {
714715 // If argument is undefined and there is a default getter, call it
715716 js.If (js.BinaryOp (js.BinaryOp .=== , jsArg, js.Undefined ()), {
716- genCallDefaultGetter(exported.sym, paramIndex, static)(previousArgsValues)
717+ genCallDefaultGetter(exported.sym, paramIndex, static, captures )(previousArgsValues)
717718 }, {
718719 unboxedArg
719720 })(unboxedArg.tpe)
@@ -724,7 +725,8 @@ final class JSExportsGen(jsCodeGen: JSCodeGen)(using Context) {
724725 }
725726 }
726727
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 ])(
728730 previousArgsValues : Int => List [js.Tree ])(
729731 implicit pos : SourcePosition ): js.Tree = {
730732
@@ -735,9 +737,30 @@ final class JSExportsGen(jsCodeGen: JSCodeGen)(using Context) {
735737 assert(! defaultGetterDenot.isOverloaded, i " found overloaded default getter $defaultGetterDenot" )
736738 val defaultGetter = defaultGetterDenot.symbol
737739
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+ }
741764
742765 // Pass previous arguments to defaultGetter
743766 val defaultGetterArgs = previousArgsValues(defaultGetter.info.paramInfoss.head.size)
0 commit comments