From d2e20d7c9f41fd51a79823654e83df6bdb80feb3 Mon Sep 17 00:00:00 2001
From: Alexey Andreev <konsoletyper@gmail.com>
Date: Fri, 6 Mar 2015 14:01:02 +0400
Subject: [PATCH] Add OutOfMemoryError. When catching exceptions that are not
 defined, report errors at compile time.

---
 .../classlib/java/lang/TOutOfMemoryError.java | 32 +++++++++++++++++++
 .../java/lang/TVirtualMachineError.java       | 32 +++++++++++++++++++
 .../model/util/MissingItemsProcessor.java     |  7 ++++
 3 files changed, 71 insertions(+)
 create mode 100644 teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TOutOfMemoryError.java
 create mode 100644 teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TVirtualMachineError.java

diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TOutOfMemoryError.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TOutOfMemoryError.java
new file mode 100644
index 000000000..ffdf97f56
--- /dev/null
+++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TOutOfMemoryError.java
@@ -0,0 +1,32 @@
+/*
+ *  Copyright 2015 Alexey Andreev.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.teavm.classlib.java.lang;
+
+/**
+ *
+ * @author Alexey Andreev
+ */
+public class TOutOfMemoryError extends TVirtualMachineError {
+    private static final long serialVersionUID = -1891949851728458692L;
+
+    public TOutOfMemoryError() {
+        super();
+    }
+
+    public TOutOfMemoryError(TString message) {
+        super(message);
+    }
+}
diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TVirtualMachineError.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TVirtualMachineError.java
new file mode 100644
index 000000000..33deeae40
--- /dev/null
+++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TVirtualMachineError.java
@@ -0,0 +1,32 @@
+/*
+ *  Copyright 2015 Alexey Andreev.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.teavm.classlib.java.lang;
+
+/**
+ *
+ * @author Alexey Andreev
+ */
+public class TVirtualMachineError extends TError {
+    private static final long serialVersionUID = -4246822614122675559L;
+
+    public TVirtualMachineError() {
+        super();
+    }
+
+    public TVirtualMachineError(TString message) {
+        super(message);
+    }
+}
diff --git a/teavm-core/src/main/java/org/teavm/model/util/MissingItemsProcessor.java b/teavm-core/src/main/java/org/teavm/model/util/MissingItemsProcessor.java
index e9beafe5c..6d09daf75 100644
--- a/teavm-core/src/main/java/org/teavm/model/util/MissingItemsProcessor.java
+++ b/teavm-core/src/main/java/org/teavm/model/util/MissingItemsProcessor.java
@@ -61,15 +61,22 @@ public class MissingItemsProcessor {
         for (int i = 0; i < program.basicBlockCount(); ++i) {
             BasicBlock block = program.basicBlockAt(i);
             instructionsToAdd.clear();
+            boolean missing = false;
             for (int j = 0; j < block.getInstructions().size(); ++j) {
                 Instruction insn = block.getInstructions().get(j);
                 insn.acceptVisitor(instructionProcessor);
                 if (!instructionsToAdd.isEmpty()) {
                     wasModified = true;
                     truncateBlock(block, j);
+                    missing = true;
                     break;
                 }
             }
+            if (!missing) {
+                for (TryCatchBlock tryCatch : block.getTryCatchBlocks()) {
+                    checkClass(null, tryCatch.getExceptionType());
+                }
+            }
         }
         if (wasModified) {
             new UnreachableBasicBlockEliminator().optimize(program);