Added support for native formatted exception messages

This commit is contained in:
Elias Naur 2005-11-24 11:31:26 +00:00
parent 42406fcf8f
commit 747f0850a0
6 changed files with 51 additions and 24 deletions

View File

@ -78,27 +78,33 @@ bool isDebugEnabled(void) {
return debug; return debug;
} }
void printfDebugJava(JNIEnv *env, const char *format, ...) { static jstring sprintfJavaString(JNIEnv *env, const char *format, va_list ap) {
#define BUFFER_SIZE 4000 #define BUFFER_SIZE 4000
char buffer[BUFFER_SIZE]; char buffer[BUFFER_SIZE];
jstring str; jstring str;
#ifdef WIN32
_vsnprintf(buffer, BUFFER_SIZE, format, ap);
#else
vsnprintf(buffer, BUFFER_SIZE, format, ap);
#endif
buffer[BUFFER_SIZE - 1] = '\0';
str = (*env)->NewStringUTF(env, buffer);
return str;
}
void printfDebugJava(JNIEnv *env, const char *format, ...) {
jstring str;
jclass org_lwjgl_LWJGLUtil_class; jclass org_lwjgl_LWJGLUtil_class;
jmethodID log_method; jmethodID log_method;
va_list ap; va_list ap;
va_start(ap, format);
if (isDebugEnabled()) { if (isDebugEnabled()) {
#ifdef WIN32 va_start(ap, format);
_vsnprintf(buffer, BUFFER_SIZE, format, ap); str = sprintfJavaString(env, format, ap);
#else va_end(ap);
vsnprintf(buffer, BUFFER_SIZE, format, ap);
#endif
buffer[BUFFER_SIZE - 1] = '\0';
str = (*env)->NewStringUTF(env, buffer);
org_lwjgl_LWJGLUtil_class = (*env)->FindClass(env, "org/lwjgl/LWJGLUtil"); org_lwjgl_LWJGLUtil_class = (*env)->FindClass(env, "org/lwjgl/LWJGLUtil");
log_method = (*env)->GetStaticMethodID(env, org_lwjgl_LWJGLUtil_class, "log", "(Ljava/lang/String;)V"); log_method = (*env)->GetStaticMethodID(env, org_lwjgl_LWJGLUtil_class, "log", "(Ljava/lang/String;)V");
(*env)->CallStaticVoidMethod(env, org_lwjgl_LWJGLUtil_class, log_method, str); (*env)->CallStaticVoidMethod(env, org_lwjgl_LWJGLUtil_class, log_method, str);
} }
va_end(ap);
} }
void printfDebug(const char *format, ...) { void printfDebug(const char *format, ...) {
@ -169,6 +175,21 @@ int copyEvents(event_queue_t *queue, jint *output_event_buffer, int buffer_size)
return num_events; return num_events;
} }
static void throwFormattedGeneralException(JNIEnv * env, const char *exception_name, const char *format, va_list ap) {
jclass cls;
jstring str;
jmethodID exception_constructor;
jobject exception;
if ((*env)->ExceptionCheck(env) == JNI_TRUE)
return; // The JVM crashes if we try to throw two exceptions from one native call
str = sprintfJavaString(env, format, ap);
cls = (*env)->FindClass(env, exception_name);
exception_constructor = (*env)->GetMethodID(env, cls, "<init>", "(Ljava/lang/String;)V");
exception = (*env)->NewObject(env, cls, exception_constructor, str);
(*env)->Throw(env, exception);
}
void throwGeneralException(JNIEnv * env, const char *exception_name, const char * err) { void throwGeneralException(JNIEnv * env, const char *exception_name, const char * err) {
jclass cls; jclass cls;
@ -176,13 +197,19 @@ void throwGeneralException(JNIEnv * env, const char *exception_name, const char
return; // The JVM crashes if we try to throw two exceptions from one native call return; // The JVM crashes if we try to throw two exceptions from one native call
cls = (*env)->FindClass(env, exception_name); cls = (*env)->FindClass(env, exception_name);
(*env)->ThrowNew(env, cls, err); (*env)->ThrowNew(env, cls, err);
(*env)->DeleteLocalRef(env, cls);
} }
void throwFMODException(JNIEnv * env, const char * err) { void throwFMODException(JNIEnv * env, const char * err) {
throwGeneralException(env, "org/lwjgl/fmod3/FMODException", err); throwGeneralException(env, "org/lwjgl/fmod3/FMODException", err);
} }
void throwFormattedException(JNIEnv * env, const char *format, ...) {
va_list ap;
va_start(ap, format);
throwFormattedGeneralException(env, "org/lwjgl/LWJGLException", format, ap);
va_end(ap);
}
void throwException(JNIEnv * env, const char * err) { void throwException(JNIEnv * env, const char * err) {
throwGeneralException(env, "org/lwjgl/LWJGLException", err); throwGeneralException(env, "org/lwjgl/LWJGLException", err);
} }

View File

@ -40,6 +40,7 @@
*/ */
#include <malloc.h> #include <malloc.h>
#include <jni.h>
#include "common_tools.h" #include "common_tools.h"
#include "extgl.h" #include "extgl.h"
#include "extgl_wgl.h" #include "extgl_wgl.h"
@ -78,14 +79,16 @@ static LRESULT CALLBACK dummyWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPAR
return DefWindowProc(hwnd, msg, wParam, lParam); return DefWindowProc(hwnd, msg, wParam, lParam);
} }
bool applyPixelFormat(HDC hdc, int iPixelFormat) { bool applyPixelFormat(JNIEnv *env, HDC hdc, int iPixelFormat) {
PIXELFORMATDESCRIPTOR desc; PIXELFORMATDESCRIPTOR desc;
if (DescribePixelFormat(hdc, iPixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &desc) == 0) { if (DescribePixelFormat(hdc, iPixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &desc) == 0) {
throwFormattedException(env, "DescribePixelFormat failed (%d)", GetLastError());
return false; return false;
} }
// make that the pixel format of the device context // make that the pixel format of the device context
if (SetPixelFormat(hdc, iPixelFormat, &desc) == FALSE) { if (SetPixelFormat(hdc, iPixelFormat, &desc) == FALSE) {
throwFormattedException(env, "SetPixelFormat failed (%d)", GetLastError());
return false; return false;
} }
return true; return true;
@ -358,8 +361,7 @@ int findPixelFormatOnDC(JNIEnv *env, HDC hdc, jobject pixel_format, jobject pixe
bool use_arb_selection = samples > 0 || pbuffer || pixelFormatCaps != NULL; bool use_arb_selection = samples > 0 || pbuffer || pixelFormatCaps != NULL;
pixel_format_id = findPixelFormatDefault(env, hdc, pixel_format, use_hdc_bpp, double_buffer); pixel_format_id = findPixelFormatDefault(env, hdc, pixel_format, use_hdc_bpp, double_buffer);
if (use_arb_selection) { if (use_arb_selection) {
if (!applyPixelFormat(hdc, pixel_format_id)) { if (!applyPixelFormat(env, hdc, pixel_format_id)) {
throwException(env, "Could not apply pixel format to window");
return -1; return -1;
} }
dummy_hglrc = wglCreateContext(hdc); dummy_hglrc = wglCreateContext(hdc);

View File

@ -43,6 +43,7 @@
#define __LWJGL_CONTEXT_H #define __LWJGL_CONTEXT_H
#include <windows.h> #include <windows.h>
#include <jni.h>
#include "common_tools.h" #include "common_tools.h"
#include "extgl.h" #include "extgl.h"
#include "extgl_wgl.h" #include "extgl_wgl.h"
@ -67,7 +68,7 @@ typedef struct {
*/ */
extern bool registerWindow(); extern bool registerWindow();
extern bool applyPixelFormat(HDC hdc, int iPixelFormat); extern bool applyPixelFormat(JNIEnv *env, HDC hdc, int iPixelFormat);
/* /*
* Close the window * Close the window

View File

@ -71,9 +71,8 @@ static bool getExtensions(JNIEnv *env, WGLExtensions *extensions, jobject pixel_
return false; return false;
} }
dummy_hdc = GetDC(dummy_hwnd); dummy_hdc = GetDC(dummy_hwnd);
if (!applyPixelFormat(dummy_hdc, pixel_format_id)) { if (!applyPixelFormat(env, dummy_hdc, pixel_format_id)) {
closeWindow(&dummy_hwnd, &dummy_hdc); closeWindow(&dummy_hwnd, &dummy_hdc);
throwException(env, "Could not apply pixel format");
return false; return false;
} }
dummy_context = wglCreateContext(dummy_hdc); dummy_context = wglCreateContext(dummy_hdc);

View File

@ -73,6 +73,6 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Win32DisplayPeerInfo_nInitDC
throwException(env, "Could not get pixel format from dummy hdc"); throwException(env, "Could not get pixel format from dummy hdc");
return; return;
} }
if (!applyPixelFormat(peer_info->drawable_hdc, pixel_format)) // If applyPixelFormat fails, just let it throw
throwException(env, "Could not apply pixel format to drawable"); applyPixelFormat(env, peer_info->drawable_hdc, pixel_format);
} }

View File

@ -53,8 +53,6 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Win32PeerInfo_nChoosePixelFormat
int pixel_format_id = findPixelFormat(env, origin_x, origin_y, pixel_format, pixel_format_caps, use_hdc_bpp, window, pbuffer, double_buffer); int pixel_format_id = findPixelFormat(env, origin_x, origin_y, pixel_format, pixel_format_caps, use_hdc_bpp, window, pbuffer, double_buffer);
if (pixel_format_id == -1) if (pixel_format_id == -1)
return; return;
if (!applyPixelFormat(peer_info->format_hdc, pixel_format_id)) { // Let it throw
throwException(env, "Could not apply pixel format"); applyPixelFormat(env, peer_info->format_hdc, pixel_format_id);
return;
}
} }