Hacks to have our source code compatible with the compiler internals of all the versions of Scala that we support.
Makes the references to inner JS class values explicit.
Makes the references to inner JS class values explicit.
Roughly, for every inner JS class of the form:
class Outer { class Inner extends ParentJSClass }
this phase creates a field Inner$jsclass
in Outer
to hold the JS class
value for Inner
. The rhs of that field is a call to a magic method, used
to retain information that the back-end will need.
class Outer { <synthetic> val Inner$jsclass: AnyRef = createJSClass(classOf[Inner], js.constructorOf[ParentJSClass]) class Inner extends ParentJSClass }
These fields will be read by code generated in ExplicitLocalJS
.
Note that this field must also be added to outer classes and traits coming
from separate compilation, therefore this phase is an InfoTransform
.
Since the transformInfo
also applies to classes defined in the current
compilation unit, the tree traversal must not create the field symbols a
second time when synthesizing the ValDef
. Instead, it must reuse the same
symbols that transformInfo
will create.
It seems the easiest way to do that is to run the entire transform
"in
the future", with exitingPhase(ExplicitInnerJS)
. This design is similar
to how explicitouter
works.
Makes the references to local JS classes explicit and desugars calls to
js.constructorOf
.
Makes the references to local JS classes explicit and desugars calls to
js.constructorOf
.
It also makes explicit all references to inner JS classes, using the
pointers created by ExplicitInnerJS
, and otherwise makes sure the
back-end will receive all the information it needs to translate inner- and
local JS classes and objects.
Note that in this comment, by "class" we mean *only* class
es. trait
s
and object
s are not implied.
Similarly to how ExplicitInnerJS
creates explicit fields in the enclosing
templates of inner JS classes to hold the JS class values, this phase
creates local vals for local JS classes in the enclosing statement list.
For every local JS class of the form:
def outer() = { class Local extends ParentJSClass }
this phase creates a local val Local$jslass
in the body of outer()
to
hold the JS class value for Local
. The rhs of that val is a call to a
magic method, used to retain information that the back-end will need:
class Local
, in the form of a classOf
js.constructorOf[ParentJSClass]
new
expressions for all overloaded constructors. The latter will be augmented by lambdalift
with the appropriate actual
parameters for the captures of Local
, which will be needed by the
back-end. In code, this looks like:
def outer() = { class Local extends ParentJSClass val Local$jsclass: AnyRef = createLocalJSClass( classOf[Local], js.constructorOf[ParentJSClass], Array[AnyRef](new Local(), ...)) }
Since we need to insert fake new Inner()
s, this scheme does not work for
abstract local classes. We therefore reject them as implementation
restriction.
If the body of Local
references itself, then the val Local$jsclass
is
instead declared as a var
to work around the cyclic dependency:
def outer() = { var Local$jsclass: AnyRef = null class Local extends ParentJSClass { ... } Local$jsclass = createLocalJSClass(...) }
In addition to the above, ExplicitLocalJS
transforms all *call sites* of
local JS classes *and* inner JS classes, so that they refer to the
synthesized local vals and fields.
The primary transformation is the desugaring of js.constructorOf[C]
,
which depends on the nature of C
:
C
is a statically accessible class, desugar to
runtime.constructorOf(classOf[C])
so that the reified symbol survives
erasure and reaches the back-end.C
is an inner JS class, it must be of the form path.D
for some
pair (path
, D
), and we desugar it to path.D$jsclass
, using the
field created by ExplicitInnerJS
(it is an error if C
is of the form
Enclosing#D
).C
is a local JS class, desugar to C$jsclass
, using the local val
created by this phase. The other transformations build on top of the desugaring of
js.constructorOf[C]
, and apply only to inner JS classes and local JS
classes (not for statically accessible classes):
x.isInstanceOf[C]
desugars into
js.special.instanceof(x, js.constructorOf[C])
.new C(...args)
desugars into
withContextualJSClassValue(js.constructorOf[C], new C(...args))
, so
that the back-end receives a reified reference to the JS class value.D extends C
, D.super.m(...args)
desugars into
withContextualJSClassValue(js.constructorOf[C], D.super.m(...args))
. Finally, for inner- and local JS *objects*, their (only) instantiation
point of the form new O.type()
is rewritten as
withContextualJSClassValue(js.constructorOf[ParentClassOfO], new O.type())
,
so that the back-end receives a reified reference to the parent class of
O
. A similar treatment is applied on anonymous JS classes, which
basically define something very similar to an object
, although without
its own JS class.
Generate JavaScript code and output it to disk
Generation of exports for JavaScript
Send JS ASTs to files
Core definitions for Scala.js
Encoding of symbol names for JavaScript
Encoding of symbol names for JavaScript
Some issues that this encoding solves: * Overloading: encode the full signature in the JS name * Same scope for fields and methods of a class * Global access to classes and modules (by their full name)
Additions to Global meaningful for the JavaScript backend
Extension of ScalaPrimitives for primitives only relevant to the JS backend
This jspretyper
phase prepares a fix for issue SI-9487 in the case of
anonymous classes that extend js.Any.
This jspretyper
phase prepares a fix for issue SI-9487 in the case of
anonymous classes that extend js.Any.
During typer
, due to a bug (SI-9487), Scalac transfroms some public method
definitions of a quite specific syntactic form of anonymous classes into
private methods. This affects both methods and field accessors. This phase
identifies any anonymous class and adds a @WasPublicBeforeTyper
annotation
on its public methods and fields. After the typer
in jsinterop
the
anonymous classes are fixed if they extend js.Any using the annotations as
reference.
As an example:
class $anon extends ... { val foo = ??? var bar = ??? def baz = ??? private val foo2 = ??? private var bar2 = ??? private def baz2 = ??? }
Would become:
class $anon extends ... { @WasPublicBeforeTyper val foo = ??? @WasPublicBeforeTyper var bar = ??? @WasPublicBeforeTyper def baz = ??? private val foo2 = ??? private var bar2 = ??? private def baz2 = ??? }
And after typer
(if has SI-9487) will be:
class $anon extends ... { @WasPublicBeforeTyper private[this] var foo = ??? private <stable> <accessor> def foo = ??? // Needs fix @WasPublicBeforeTyper private[this] var bar = ??? private <accessor> def bar = ??? // Needs fix private <accessor> def bar_=(...) = ??? // Needs fix @WasPublicBeforeTyper private def baz = ??? // Needs fix ... }
Prepare export generation
Prepare export generation
Helpers for transformation of @JSExport annotations
Prepares classes extending js.Any for JavaScript interop
Prepares classes extending js.Any for JavaScript interop
This phase does: - Sanity checks for js.Any hierarchy - Annotate subclasses of js.Any to be treated specially - Rewrite calls to scala.Enumeration.Value (include name string) - Create JSExport methods: Dummy methods that are propagated through the whole compiler chain to mark exports. This allows exports to have the same semantics than methods.
This trait allows to query all options to the ScalaJS plugin
This trait allows to query all options to the ScalaJS plugin
Also see the help text in ScalaJSPlugin for information about particular options.
Main entry point for the Scala.js compiler plugin
Glue representation of types as seen from the IR but still with a reference to the Symbols.
Useful extractors for JavaScript trees
Hacks to have our source code compatible with the compiler internals of all the versions of Scala that we support.
In general, it tries to provide the newer APIs on top of older APIs.