Created
February 26, 2016 11:20
-
-
Save olafurpg/dbfdd552c49f748dd6e9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def genArrayInstanceTests(tree: LinkedClass): js.Tree = { | |
import Definitions._ | |
import TreeDSL._ | |
implicit val pos = tree.pos | |
val className = tree.name.name | |
val displayName = decodeClassName(className) | |
val objParam = js.ParamDef(Ident("obj"), rest = false) | |
val obj = objParam.ref | |
val depthParam = js.ParamDef(Ident("depth"), rest = false) | |
val depth = depthParam.ref | |
val createIsArrayOfStat = { | |
envFieldDef("isArrayOf", | |
className, | |
js.Function(List(objParam, depthParam), className match { | |
case Definitions.ObjectClass => | |
val dataVarDef = genLet(Ident("data"), mutable = false, { | |
obj && (obj DOT "$classData") | |
}) | |
val data = dataVarDef.ref | |
js.Block(dataVarDef, js.If(!data, { | |
js.Return(js.BooleanLiteral(false)) | |
}, { | |
val arrayDepthVarDef = | |
genLet(Ident("arrayDepth"), mutable = false, { | |
(data DOT "arrayDepth") || js.IntLiteral(0) | |
}) | |
val arrayDepth = arrayDepthVarDef.ref | |
js.Block(arrayDepthVarDef, js.Return { | |
// Array[A] </: Array[Array[A]] | |
!js.BinaryOp(JSBinaryOp.<, arrayDepth, depth) && | |
(// Array[Array[A]] <: Array[Object] | |
js.BinaryOp(JSBinaryOp.>, arrayDepth, depth) || | |
// Array[Int] </: Array[Object] | |
!genIdentBracketSelect(data DOT "arrayBase", | |
"isPrimitive")) | |
}) | |
})) | |
case _ => | |
js.Return(! (! ({ | |
genIsScalaJSObject(obj) && (( | |
obj DOT "$classData" DOT "arrayDepth") === depth) && | |
genIsClassNameInAncestors( | |
className, | |
obj DOT "$classData" DOT "arrayBase" DOT "ancestors") | |
}))) | |
})) | |
} | |
val createAsArrayOfStat = | |
if (semantics.asInstanceOfs == Unchecked) { | |
js.Skip() | |
} else { | |
envFieldDef("asArrayOf", | |
className, | |
js.Function(List(objParam, depthParam), js.Return { | |
js.If(js.Apply(envField("isArrayOf", className), | |
List(obj, depth)) || (obj === js.Null()), { | |
obj | |
}, { | |
genCallHelper( | |
"throwArrayCastException", | |
obj, | |
js.StringLiteral("L" + displayName + ";"), | |
depth) | |
}) | |
})) | |
} | |
js.Block(createIsArrayOfStat, createAsArrayOfStat) | |
} |
val newColumn =
tok.length +
(if (split == Newline) newIndent
else column + split.length)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Original https://github.com/scala-js/scala-js/blob/29e46c2284afd48dcfe2dcacbcfe9f9fdbadf617/tools/shared/src/main/scala/org/scalajs/core/tools/linker/backend/emitter/ScalaJSClassEmitter.scala#L584