Skip to content

Instantly share code, notes, and snippets.

@olafurpg
Created February 26, 2016 11:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olafurpg/dbfdd552c49f748dd6e9 to your computer and use it in GitHub Desktop.
Save olafurpg/dbfdd552c49f748dd6e9 to your computer and use it in GitHub Desktop.
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)
}
@olafurpg
Copy link
Author

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