Signed-off-by: Tomek Grabiec <tgrab...@gmail.com>
---
 arch/x86/insn-selector_32.brg      |    1 -
 regression/jvm/ExceptionsTest.java |   32 ++++++++++++++++++++++++-
 vm/class.c                         |   45 +++++++++++++++++++++++++++++++++--
 3 files changed, 73 insertions(+), 5 deletions(-)

diff --git a/arch/x86/insn-selector_32.brg b/arch/x86/insn-selector_32.brg
index b3995ff..f900515 100644
--- a/arch/x86/insn-selector_32.brg
+++ b/arch/x86/insn-selector_32.brg
@@ -1247,7 +1247,6 @@ stmt:     STMT_CHECKCAST(reg)
        select_insn(s, tree, rel_insn(INSN_CALL_REL, (unsigned 
long)check_cast));
 
        method_args_cleanup(s, tree, 2);
-       select_exception_test(s, tree);
 }
 
 %%
diff --git a/regression/jvm/ExceptionsTest.java 
b/regression/jvm/ExceptionsTest.java
index 60a119b..b13d8bf 100644
--- a/regression/jvm/ExceptionsTest.java
+++ b/regression/jvm/ExceptionsTest.java
@@ -44,6 +44,9 @@ public class ExceptionsTest extends TestCase {
     public static void takeLong(long val) {
     }
 
+    public static void takeObject(Object obj) {
+    }
+
     public static void testTryBlockDoesNotThrowAnything() {
         boolean caught;
         try {
@@ -309,6 +312,32 @@ public class ExceptionsTest extends TestCase {
         assertTrue(caught);
     }
 
+    public static void testCheckcast() {
+        boolean caught = false;
+        Object o = null;
+        String s = null;
+
+        try {
+            s = (String)o;
+        } catch (ClassCastException e) {
+            caught = true;
+        }
+
+        assertFalse(caught);
+
+        o = new Object();
+
+        try {
+            s = (String)o;
+        } catch (ClassCastException e) {
+            caught = true;
+        }
+
+        assertTrue(caught);
+
+        takeObject(s);
+    }
+
     public static void main(String args[]) {
         testTryBlockDoesNotThrowAnything();
         testThrowAndCatchInTheSameMethod();
@@ -326,11 +355,12 @@ public class ExceptionsTest extends TestCase {
         testGetfield();
         testPutfield();
         testMonitorenter();
-       /* TODO: testMonitorexit() */
+        /* TODO: testMonitorexit() */
         testIdiv();
         testIrem();
         testLdiv();
         testLrem();
+        testCheckcast();
 
         exit();
     }
diff --git a/vm/class.c b/vm/class.c
index abb5620..c232b53 100644
--- a/vm/class.c
+++ b/vm/class.c
@@ -24,10 +24,13 @@
  * Please refer to the file LICENSE for details.
  */
 
+#include <jit/exception.h>
 #include <jit/compiler.h>
+#include <vm/string.h>
 #include <vm/die.h>
 #include <vm/vm.h>
 #include <stdlib.h>
+#include <errno.h>
 
 typedef void (*exception_init_fn)(struct object *, struct object *);
 
@@ -84,9 +87,45 @@ void check_array(struct object *obj, unsigned int index)
 
 void check_cast(struct object *obj, struct object *type)
 {
-       if (!obj)
+       struct string *str;
+       int err;
+
+       if (!obj || isInstanceOf(type, obj->class))
                return;
 
-       if (!isInstanceOf(type, obj->class))
-               abort();
+       if (exception_occurred())
+               goto throw;
+
+       str = alloc_str();
+       if (str == NULL) {
+               err = -ENOMEM;
+               goto error;
+       }
+
+       err = str_append(str, slash2dots(CLASS_CB(obj->class)->name));
+       if (err)
+               goto error;
+
+       err = str_append(str, " cannot be cast to ");
+       if (err)
+               goto error;
+
+       err = str_append(str, slash2dots(CLASS_CB(type)->name));
+       if (err)
+               goto error;
+
+       signal_new_exception("java/lang/ClassCastException", str->value);
+       free_str(str);
+ throw:
+       throw_from_jato_func(2 * sizeof(struct object *));
+       return;
+
+ error:
+       if (str)
+               free_str(str);
+
+       if (err == -ENOMEM) /* TODO: throw OutOfMemoryError */
+               die("%s: out of memory", __func__);
+
+       die("%s: error %d", __func__, err);
 }
-- 
1.6.0.6


------------------------------------------------------------------------------
OpenSolaris 2009.06 is a cutting edge operating system for enterprises 
looking to deploy the next generation of Solaris that includes the latest 
innovations from Sun and the OpenSource community. Download a copy and 
enjoy capabilities such as Networking, Storage and Virtualization. 
Go to: http://p.sf.net/sfu/opensolaris-get
_______________________________________________
Jatovm-devel mailing list
Jatovm-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jatovm-devel

Reply via email to