Make the Java jmercury.runtime.Exception class implement the getMessage()

Branches: main

Make the Java jmercury.runtime.Exception class implement the getMessage()
method for Throwable objects.  This is useful when Mercury code is called
from Java code, without the main/2 wrapper.

java/runtime/Exception.java:
        Add a method pointer as a class variable.

        Implement the getMessage() method by calling the method pointer
        if set.

library/exception.m:
        On module initialisation set the method pointer in the
        jmercury.runtime.Exception class.

        Factor out code from report_uncaught_exception_2.
This commit is contained in:
Peter Wang
2009-08-03 05:13:55 +00:00
parent c6b277dd3a
commit 26498eb230
2 changed files with 50 additions and 8 deletions

View File

@@ -7,11 +7,24 @@
package jmercury.runtime;
public class Exception extends java.lang.Error {
// Should be mercury.univ.Univ_0 but we don't want to depend on the
// standard library.
// This is to be set when the exception module is initialised, to avoid
// having the runtime depend on the standard library.
public static MethodPtr getMessageHook = null;
// Should be univ.Univ_0 but we don't want to depend on the standard
// library.
public Object exception;
public Exception(Object exception) {
this.exception = exception;
}
public String getMessage() {
if (getMessageHook != null) {
Object[] args = new Object[] { exception };
return (String) getMessageHook.call___0_0(args);
} else {
return null;
}
}
}