Skip to content

Commit 29a956d

Browse files
committed
Scala.js: Throw an NPE if the receiver of a synchronized is null.
1 parent 03a47b4 commit 29a956d

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

compiler/src/dotty/tools/backend/sjs/JSCodeGen.scala

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1976,10 +1976,11 @@ class JSCodeGen()(using genCtx: Context) {
19761976
genArg
19771977
case _ =>
19781978
implicit val pos = tree.span
1979-
/* TODO Check for a null receiver?
1980-
* In theory, it's UB, but that decision should be left for link time.
1981-
*/
1982-
js.Block(genReceiver, genArg)
1979+
js.Block(
1980+
js.If(js.BinaryOp(js.BinaryOp.===, genReceiver, js.Null()),
1981+
js.Throw(js.New(NullPointerExceptionClass, js.MethodIdent(jsNames.NoArgConstructorName), Nil)),
1982+
js.Skip())(jstpe.NoType),
1983+
genArg)
19831984
}
19841985
}
19851986

@@ -3472,6 +3473,7 @@ class JSCodeGen()(using genCtx: Context) {
34723473

34733474
object JSCodeGen {
34743475

3476+
private val NullPointerExceptionClass = ClassName("java.lang.NullPointerException")
34753477
private val JSObjectClassName = ClassName("scala.scalajs.js.Object")
34763478

34773479
private val newSimpleMethodName = SimpleMethodName("new")

0 commit comments

Comments
 (0)