summaryrefslogtreecommitdiff
path: root/tools/shared/src/main/scala/scala/scalajs/tools/javascript/ScalaJSClassEmitter.scala
blob: b249f88370aa1a6c5e09746340bcc1b6254c0896 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/*                     __                                               *\
**     ________ ___   / /  ___      __ ____  Scala.js tools             **
**    / __/ __// _ | / /  / _ | __ / // __/  (c) 2014, LAMP/EPFL        **
**  __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \    http://scala-js.org/       **
** /____/\___/_/ |_/____/_/ | |__/ /____/                               **
**                          |/____/                                     **
\*                                                                      */


package scala.scalajs.tools.javascript

import scala.scalajs.ir._
import Position._
import Transformers._
import scala.scalajs.ir.Trees._
import Types._

import scala.scalajs.tools.sem._
import CheckedBehavior.Unchecked

import scala.scalajs.tools.javascript.{Trees => js}

/** Defines methods to emit Scala.js classes to JavaScript code.
 *  The results are completely desugared.
 */
final class ScalaJSClassEmitter(semantics: Semantics) {

  import JSDesugaring._

  /** Desugar a Scala.js class into ECMAScript 5 constructs */
  def genClassDef(tree: ClassDef): js.Tree = {
    implicit val pos = tree.pos
    val kind = tree.kind

    var reverseParts: List[js.Tree] = Nil

    if (kind == ClassKind.TraitImpl) {
      reverseParts ::= genTraitImpl(tree)
    } else {
      if (kind.isClass)
        reverseParts ::= genClass(tree)
      if (kind.isClass || kind == ClassKind.Interface ||
          tree.name.name == Definitions.StringClass)
        reverseParts ::= genInstanceTests(tree)
      reverseParts ::= genArrayInstanceTests(tree)
      reverseParts ::= genTypeData(tree)
      if (kind.isClass)
        reverseParts ::= genSetTypeData(tree)
      if (kind == ClassKind.ModuleClass)
        reverseParts ::= genModuleAccessor(tree)
      if (kind.isClass)
        reverseParts ::= genClassExports(tree)
    }

    js.Block(reverseParts.reverse)
  }

  def genClass(tree: ClassDef): js.Tree = {
    val className = tree.name.name
    val typeFunctionDef = genConstructor(tree)
    val memberDefs = tree.defs collect {
      case m: MethodDef =>
        genMethod(className, m)
      case p: PropertyDef =>
        genProperty(className, p)
    }

    js.Block(typeFunctionDef :: memberDefs)(tree.pos)
  }

  /** Generates the JS constructor for a class. */
  def genConstructor(tree: ClassDef): js.Tree = {
    assert(tree.kind.isClass)

    val classIdent = tree.name
    val className = classIdent.name
    val tpe = ClassType(className)

    assert(tree.parent.isDefined || className == Definitions.ObjectClass,
        "Class $className is missing a parent class")

    val ctorFun = {
      val superCtorCall = tree.parent.fold[js.Tree] {
        js.Skip()(tree.pos)
      } { parentIdent =>
        implicit val pos = tree.pos
        js.Apply(
            js.DotSelect(encodeClassVar(parentIdent.name), js.Ident("call")),
            List(js.This()))
      }
      val fieldDefs = for {
        field @ VarDef(name, vtpe, mutable, rhs) <- tree.defs
      } yield {
        implicit val pos = field.pos
        desugarJavaScript(
            Assign(Select(This()(tpe), name, mutable)(vtpe), rhs),
            semantics)
      }
      js.Function(Nil,
          js.Block(superCtorCall :: fieldDefs)(tree.pos))(tree.pos)
    }

    {
      implicit val pos = tree.pos
      val typeVar = encodeClassVar(className)
      val docComment = js.DocComment("@constructor")
      val ctorDef = js.Assign(typeVar, ctorFun)

      val chainProto = tree.parent.fold[js.Tree] {
        js.Skip()
      } { parentIdent =>
        js.Block(
          js.Assign(typeVar.prototype,
              js.New(js.DotSelect(envField("h"), parentIdent), Nil)),
          genAddToPrototype(className, js.Ident("constructor"), typeVar)
        )
      }

      val inheritableCtorDef = {
        val inheritableCtorVar =
          js.DotSelect(envField("h"), classIdent)
        js.Block(
          js.DocComment("@constructor"),
          js.Assign(inheritableCtorVar, js.Function(Nil, js.Skip())),
          js.Assign(inheritableCtorVar.prototype, typeVar.prototype)
        )
      }

      js.Block(docComment, ctorDef, chainProto, inheritableCtorDef)
    }
  }

  /** Generates a method. */
  def genMethod(className: String, method: MethodDef): js.Tree = {
    implicit val pos = method.pos
    val methodFun = js.Function(method.args.map(transformParamDef),
        desugarBody(method.body, method.resultType == NoType))
    genAddToPrototype(className, method.name, methodFun)
  }

  /** Generates a property. */
  def genProperty(className: String, property: PropertyDef): js.Tree = {
    implicit val pos = property.pos
    val classType = ClassType(className)

    // defineProperty method
    val defProp =
      js.BracketSelect(js.VarRef(js.Ident("Object"), false),
          js.StringLiteral("defineProperty"))

    // class prototype
    val proto = encodeClassVar(className).prototype

    // property name
    val name = property.name match {
      case StringLiteral(value) =>
        js.StringLiteral(value)
      case id: Ident =>
        // We need to work around the closure compiler. Call propertyName to
        // get a string representation of the optimized name
        genCallHelper("propertyName",
            js.ObjectConstr(transformIdent(id) -> js.IntLiteral(0) :: Nil))
    }

    // Options passed to the defineProperty method
    val descriptor = js.ObjectConstr {
      // Basic config
      val base =
        js.StringLiteral("enumerable") -> js.BooleanLiteral(true) :: Nil

      // Optionally add getter
      val wget =
        if (property.getterBody == EmptyTree) base
        else js.StringLiteral("get") ->
          js.Function(Nil, desugarBody(property.getterBody, isStat = false)) :: base

      // Optionally add setter
      if (property.setterBody == EmptyTree) wget
      else js.StringLiteral("set") ->
          js.Function(transformParamDef(property.setterArg) :: Nil,
              desugarBody(property.setterBody, isStat = true)) :: wget
    }

    js.Apply(defProp, proto :: name :: descriptor :: Nil)
  }

  /** Generate `classVar.prototype.name = value` */
  def genAddToPrototype(className: String, name: js.PropertyName,
      value: js.Tree)(implicit pos: Position): js.Tree = {
    val proto = encodeClassVar(className).prototype
    val select = name match {
      case name: js.Ident         => js.DotSelect(proto, name)
      case name: js.StringLiteral => js.BracketSelect(proto, name)
    }
    js.Assign(select, value)
  }

  /** Generate `classVar.prototype.name = value` */
  def genAddToPrototype(className: String, name: PropertyName,
      value: js.Tree)(implicit pos: Position): js.Tree = {
    val newName = name match {
      case ident: Ident         => transformIdent(ident)
      case StringLiteral(value) => js.StringLiteral(value)
    }
    genAddToPrototype(className, newName, value)
  }

  def genInstanceTests(tree: ClassDef): js.Tree = {
    import Definitions._
    import TreeDSL._

    implicit val pos = tree.pos

    val classIdent = transformIdent(tree.name)
    val className = classIdent.name
    val displayName = decodeClassName(className)

    val isAncestorOfString =
      AncestorsOfStringClass.contains(className)
    val isAncestorOfHijackedNumberClass =
      AncestorsOfHijackedNumberClasses.contains(className)
    val isAncestorOfBoxedBooleanClass =
      AncestorsOfBoxedBooleanClass.contains(className)

    val objParam = js.ParamDef(Ident("obj"), mutable = false)
    val obj = objParam.ref

    val createIsStat = {
      envField("is") DOT classIdent :=
        js.Function(List(objParam), js.Return(className match {
          case Definitions.ObjectClass =>
            js.BinaryOp("!==", obj, js.Null())

          case Definitions.StringClass =>
            js.UnaryOp("typeof", obj) === js.StringLiteral("string")

          case _ =>
            var test = (obj && (obj DOT "$classData") &&
                (obj DOT "$classData" DOT "ancestors" DOT classIdent))

            if (isAncestorOfString)
              test = test || (
                  js.UnaryOp("typeof", obj) === js.StringLiteral("string"))
            if (isAncestorOfHijackedNumberClass)
              test = test || (
                  js.UnaryOp("typeof", obj) === js.StringLiteral("number"))
            if (isAncestorOfBoxedBooleanClass)
              test = test || (
                  js.UnaryOp("typeof", obj) === js.StringLiteral("boolean"))

            !(!test)
        }))
    }

    val createAsStat = if (semantics.asInstanceOfs == Unchecked) {
      js.Skip()
    } else {
      envField("as") DOT classIdent :=
        js.Function(List(objParam), js.Return(className match {
          case Definitions.ObjectClass =>
            obj

          case _ =>
            js.If(js.Apply(envField("is") DOT classIdent, List(obj)) ||
                (obj === js.Null()), {
              obj
            }, {
              genCallHelper("throwClassCastException",
                  obj, js.StringLiteral(displayName))
            })
      }))
    }

    js.Block(createIsStat, createAsStat)
  }

  def genArrayInstanceTests(tree: ClassDef): js.Tree = {
    import Definitions._
    import TreeDSL._

    implicit val pos = tree.pos

    val classIdent = transformIdent(tree.name)
    val className = classIdent.name
    val displayName = decodeClassName(className)

    val objParam = js.ParamDef(Ident("obj"), mutable = false)
    val obj = objParam.ref

    val depthParam = js.ParamDef(Ident("depth"), mutable = false)
    val depth = depthParam.ref

    val createIsArrayOfStat = {
      envField("isArrayOf") DOT classIdent :=
        js.Function(List(objParam, depthParam), className match {
          case Definitions.ObjectClass =>
            val dataVarDef = js.VarDef(Ident("data"), false, {
              obj && (obj DOT "$classData")
            })
            val data = dataVarDef.ref
            js.Block(
              dataVarDef,
              js.If(!data, {
                js.Return(js.BooleanLiteral(false))
              }, {
                val arrayDepthVarDef = js.VarDef(Ident("arrayDepth"), false, {
                  (data DOT "arrayDepth") || js.IntLiteral(0)
                })
                val arrayDepth = arrayDepthVarDef.ref
                js.Block(
                  arrayDepthVarDef,
                  js.Return {
                    // Array[A] </: Array[Array[A]]
                    !js.BinaryOp("<", arrayDepth, depth) && (
                      // Array[Array[A]] <: Array[Object]
                      js.BinaryOp(">", arrayDepth, depth) ||
                      // Array[Int] </: Array[Object]
                      !js.BracketSelect(data DOT "arrayBase", js.StringLiteral("isPrimitive"))
                    )
                  })
              }))

          case _ =>
            js.Return(!(!(obj && (obj DOT "$classData") &&
                ((obj DOT "$classData" DOT "arrayDepth") === depth) &&
                (obj DOT "$classData" DOT "arrayBase" DOT "ancestors" DOT classIdent))))
        })
    }

    val createAsArrayOfStat = if (semantics.asInstanceOfs == Unchecked) {
      js.Skip()
    } else {
      envField("asArrayOf") DOT classIdent :=
        js.Function(List(objParam, depthParam), js.Return {
          js.If(js.Apply(envField("isArrayOf") DOT classIdent, List(obj, depth)) ||
              (obj === js.Null()), {
            obj
          }, {
            genCallHelper("throwArrayCastException",
                obj, js.StringLiteral("L"+displayName+";"), depth)
          })
        })
    }

    js.Block(createIsArrayOfStat, createAsArrayOfStat)
  }

  def genTypeData(tree: ClassDef): js.Tree = {
    import Definitions._
    import TreeDSL._

    implicit val pos = tree.pos

    val classIdent = transformIdent(tree.name)
    val className = classIdent.name
    val kind = tree.kind
    assert(kind.isType)

    val isObjectClass =
      className == ObjectClass
    val isHijackedBoxedClass =
      HijackedBoxedClasses.contains(className)
    val isAncestorOfHijackedClass =
      AncestorsOfHijackedClasses.contains(className)

    val parentData = tree.parent.fold[js.Tree] {
      if (isObjectClass) js.Null()
      else js.Undefined()
    } { parent =>
      envField("d") DOT parent
    }

    val ancestorsRecord = js.ObjectConstr(
        for (ancestor <- classIdent :: tree.ancestors.map(transformIdent))
          yield (ancestor, js.IntLiteral(1)))

    val typeData = js.New(envField("ClassTypeData"), List(
        js.ObjectConstr(List(classIdent -> js.IntLiteral(0))),
        js.BooleanLiteral(kind == ClassKind.Interface),
        js.StringLiteral(decodeClassName(className)),
        parentData,
        ancestorsRecord
    ) ++ (
        // Optional parameter isInstance
        if (isObjectClass) {
          /* Object has special ScalaJS.is.O *and* ScalaJS.isArrayOf.O. */
          List(
            envField("is") DOT classIdent,
            envField("isArrayOf") DOT classIdent)
        } else if (isHijackedBoxedClass) {
          /* Hijacked boxed classes have a special isInstanceOf test. */
          val xParam = js.ParamDef(Ident("x"), mutable = false)
          List(js.Function(List(xParam), js.Return {
            genIsInstanceOf(xParam.ref, ClassType(className))
          }))
        } else if (isAncestorOfHijackedClass || className == StringClass) {
          /* java.lang.String and ancestors of hijacked classes have a normal
           * ScalaJS.is.pack_Class test but with a non-standard behavior. */
          List(envField("is") DOT classIdent)
        } else {
          // For other classes, the isInstance function can be inferred.
          Nil
        }
    ))

    envField("d") DOT classIdent := typeData
  }

  def genSetTypeData(tree: ClassDef): js.Tree = {
    import TreeDSL._

    implicit val pos = tree.pos

    assert(tree.kind.isClass)

    encodeClassVar(tree.name.name).prototype DOT "$classData" :=
      envField("d") DOT tree.name
  }

  def genModuleAccessor(tree: ClassDef): js.Tree = {
    import TreeDSL._

    implicit val pos = tree.pos

    val classIdent = transformIdent(tree.name)
    val className = classIdent.name
    val tpe = ClassType(className)

    require(tree.kind == ClassKind.ModuleClass,
        s"genModuleAccessor called with non-module class: $className")
    assert(className.endsWith("$"))

    val moduleName = className.dropRight(1)
    val moduleIdent = Ident(moduleName)

    val moduleInstanceVar = envField("n") DOT moduleIdent
    val accessorVar = envField("m") DOT moduleIdent

    val createModuleInstanceField = {
      moduleInstanceVar := js.Undefined()
    }

    val createAccessor = {
      accessorVar := js.Function(Nil, js.Block(
        js.If(!(moduleInstanceVar), {
          moduleInstanceVar :=
            js.Apply(js.New(encodeClassVar(className), Nil) DOT js.Ident("init___"), Nil)
        }, js.Skip()),
        js.Return(moduleInstanceVar)
      ))
    }

    js.Block(createModuleInstanceField, createAccessor)
  }

  def genClassExports(tree: ClassDef): js.Tree = {
    val exports = tree.defs collect {
      case e: ConstructorExportDef =>
        genConstructorExportDef(tree, e)
      case e: ModuleExportDef =>
        genModuleExportDef(tree, e)
    }

    js.Block(exports)(tree.pos)
  }

  def genConstructorExportDef(cd: ClassDef, tree: ConstructorExportDef): js.Tree = {
    import TreeDSL._

    implicit val pos = tree.pos
    val classType = ClassType(cd.name.name)
    val ConstructorExportDef(fullName, args, body) = tree

    val baseCtor = envField("c") DOT cd.name
    val (createNamespace, expCtorVar) = genCreateNamespaceInExports(fullName)

    js.Block(
      createNamespace,
      js.DocComment("@constructor"),
      expCtorVar := js.Function(args.map(transformParamDef), js.Block(
        js.Apply(js.DotSelect(baseCtor, js.Ident("call")), List(js.This())),
        desugarBody(body, isStat = true)
      )),
      expCtorVar DOT "prototype" := baseCtor DOT "prototype"
    )
  }

  def genModuleExportDef(cd: ClassDef, tree: ModuleExportDef): js.Tree = {
    import TreeDSL._

    implicit val pos = tree.pos

    val baseAccessor =
      envField("m") DOT cd.name.name.dropRight(1)
    val (createNamespace, expAccessorVar) =
      genCreateNamespaceInExports(tree.fullName)

    js.Block(
      createNamespace,
      expAccessorVar := baseAccessor
    )
  }

  def genTraitImpl(tree: ClassDef): js.Tree = {
    val traitImplName = tree.name.name
    val defs = tree.defs collect {
      case m: MethodDef =>
        genTraitImplMethod(traitImplName, m)
    }
    js.Block(defs)(tree.pos)
  }

  def genTraitImplMethod(traitImplName: String, tree: MethodDef): js.Tree = {
    implicit val pos = tree.pos
    val MethodDef(name: Ident, args, resultType, body) = tree
    js.Assign(
        js.DotSelect(envField("i"), name),
        js.Function(args.map(transformParamDef),
            desugarBody(body, resultType == NoType)))
  }

  /** Generate a dummy parent. Used by ScalaJSOptimizer */
  def genDummyParent(name: String): js.Tree = {
    implicit val pos = Position.NoPosition

    js.Block(
        js.DocComment("@constructor (dummy parent)"))
        js.Assign(js.DotSelect(envField("h"), js.Ident(name)),
            js.Function(Nil, js.Skip())
    )
  }

  // Helpers

  /** Desugars a function body of the IR into ES5 JavaScript. */
  private def desugarBody(tree: Tree, isStat: Boolean): js.Tree = {
    implicit val pos = tree.pos
    val withReturn =
      if (isStat) tree
      else Return(tree)
    desugarJavaScript(withReturn, semantics) match {
      case js.Block(stats :+ js.Return(js.Undefined())) => js.Block(stats)
      case other                                        => other
    }
  }

  /** Gen JS code for assigning an rhs to a qualified name in the exports scope.
   *  For example, given the qualified name "foo.bar.Something", generates:
   *
   *  ScalaJS.e["foo"] = ScalaJS.e["foo"] || {};
   *  ScalaJS.e["foo"]["bar"] = ScalaJS.e["foo"]["bar"] || {};
   *
   *  Returns (statements, ScalaJS.e["foo"]["bar"]["Something"])
   */
  private def genCreateNamespaceInExports(qualName: String)(
      implicit pos: Position): (js.Tree, js.Tree) = {
    val parts = qualName.split("\\.")
    val statements = List.newBuilder[js.Tree]
    var namespace = envField("e")
    for (i <- 0 until parts.length-1) {
      namespace = js.BracketSelect(namespace, js.StringLiteral(parts(i)))
      statements +=
        js.Assign(namespace, js.BinaryOp("||", namespace, js.ObjectConstr(Nil)))
    }
    val lhs = js.BracketSelect(namespace, js.StringLiteral(parts.last))
    (js.Block(statements.result()), lhs)
  }

}