|
| 1 | +package dotty.tools |
| 2 | +package dotc |
| 3 | +package transform |
| 4 | +package sjs |
| 5 | + |
| 6 | +import MegaPhase._ |
| 7 | +import core.Constants |
| 8 | +import core.Contexts._ |
| 9 | +import core.Decorators._ |
| 10 | +import core.StdNames.nme |
| 11 | +import core.Phases._ |
| 12 | +import core.Symbols._ |
| 13 | +import core.Types._ |
| 14 | +import ast.Trees._ |
| 15 | +import dotty.tools.dotc.ast.tpd |
| 16 | + |
| 17 | +import dotty.tools.backend.sjs.JSDefinitions.jsdefn |
| 18 | + |
| 19 | +/** Adds fake calls to the constructors of local JS classes in calls to |
| 20 | + * `createLocalJSClass`. |
| 21 | + * |
| 22 | + * Given a call of the form |
| 23 | + * {{{ |
| 24 | + * scala.scalajs.runtime.createLocalJSClass(classOf[C], jsClassValue, ???) |
| 25 | + * }}} |
| 26 | + * this phase fills in the `???` with an array of calls to the constructors |
| 27 | + * of `C`, like |
| 28 | + * {{{ |
| 29 | + * [ new C(), new C(???, ???) : Object ] |
| 30 | + * }}} |
| 31 | + * |
| 32 | + * If the class `C` has an outer pointer, as determined by the `ExplicitOuter` |
| 33 | + * phase, the calls to the constructor insert a reference to the outer |
| 34 | + * instance: |
| 35 | + * {{{ |
| 36 | + * [ new C(Enclosing.this), new C(Enclosing.this, ???, ???) : Object ] |
| 37 | + * }}} |
| 38 | + * |
| 39 | + * The `LambdaLift` phase will further expand those constructor calls with |
| 40 | + * values for captures. The back-end will extract the values of the outer |
| 41 | + * pointer and/or the captures to introduce them as JS class captures. |
| 42 | + * |
| 43 | + * Since we need to insert fake `new C()` calls, this scheme does not work for |
| 44 | + * abstract local classes. We therefore reject them as implementation |
| 45 | + * restriction in `PrepJSInterop`. |
| 46 | + * |
| 47 | + * This phase complements `ExplicitJSClasses`. The latter cannot create the |
| 48 | + * fake new invocations because that would require inventing sound type |
| 49 | + * arguments for the class' type parameters in order not to break Ycheck. |
| 50 | + */ |
| 51 | +class AddLocalJSFakeNews extends MiniPhase { thisPhase => |
| 52 | + import ExplicitOuter.outer |
| 53 | + import ast.tpd._ |
| 54 | + |
| 55 | + override def phaseName: String = AddLocalJSFakeNews.name |
| 56 | + |
| 57 | + override def isEnabled(using Context): Boolean = |
| 58 | + ctx.settings.scalajs.value |
| 59 | + |
| 60 | + override def runsAfter: Set[String] = Set(Erasure.name) |
| 61 | + |
| 62 | + override def transformApply(tree: Apply)(using Context): Tree = { |
| 63 | + if (tree.symbol == jsdefn.Runtime_createLocalJSClass) { |
| 64 | + val classValueArg :: superClassValueArg :: _ :: Nil = tree.args |
| 65 | + val cls = classValueArg match { |
| 66 | + case Literal(constant) if constant.tag == Constants.ClazzTag => |
| 67 | + constant.typeValue.typeSymbol.asClass |
| 68 | + case _ => |
| 69 | + // this shouldn't happen |
| 70 | + report.error(i"unexpected $classValueArg for the first argument to `createLocalJSClass`", classValueArg) |
| 71 | + jsdefn.JSObjectClass |
| 72 | + } |
| 73 | + |
| 74 | + val fakeNews = { |
| 75 | + val ctors = cls.info.decls.lookupAll(nme.CONSTRUCTOR).toList.reverse |
| 76 | + val elems = ctors.map(ctor => fakeNew(cls, ctor.asTerm)) |
| 77 | + JavaSeqLiteral(elems, TypeTree(defn.ObjectType)) |
| 78 | + } |
| 79 | + |
| 80 | + cpy.Apply(tree)(tree.fun, classValueArg :: superClassValueArg :: fakeNews :: Nil) |
| 81 | + } else { |
| 82 | + tree |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /** Creates a fake invocation of the given class with the given constructor. */ |
| 87 | + private def fakeNew(cls: ClassSymbol, ctor: TermSymbol)(using Context): Tree = { |
| 88 | + val tycon = cls.typeRef |
| 89 | + val outerArgs = outer.argsForNew(cls, tycon) |
| 90 | + val nonOuterArgCount = ctor.info.firstParamTypes.size - outerArgs.size |
| 91 | + val nonOuterArgs = List.fill(nonOuterArgCount)(ref(defn.Predef_undefined).appliedToNone) |
| 92 | + |
| 93 | + New(tycon, ctor, outerArgs ::: nonOuterArgs) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +object AddLocalJSFakeNews { |
| 98 | + val name: String = "addLocalJSFakeNews" |
| 99 | +} |
0 commit comments