Cherry pick threading, atomic, and list changes from wip-scritchui.
[SquirrelJME.git] / emulators / emulator-base-native / c / jniHelper.c
blobf87649edf959453bbdb899f63e4e92c0c45ecc13
1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // -------------------------------------------------------------------------*/
10 #include <string.h>
11 #include <jni.h>
13 #include "squirreljme.h"
14 #include "sjme/debug.h"
16 sjme_jboolean sjme_jni_checkVMException(JNIEnv* env)
18 /* Was there an exception? */
19 if ((*env)->ExceptionCheck(env))
21 /* Debug print it. */
22 (*env)->ExceptionDescribe(env);
24 /* Did fail! */
25 return SJME_JNI_TRUE;
28 return SJME_JNI_FALSE;
31 void sjme_jni_throwMLECallError(JNIEnv* env, sjme_errorCode code)
33 sjme_jni_throwThrowable(env, code,
34 "cc/squirreljme/jvm/mle/exceptions/MLECallError");
37 void sjme_jni_throwThrowable(JNIEnv* env, sjme_errorCode code,
38 sjme_lpcstr type)
40 #define BUF_SIZE 512
41 jclass tossingClass;
42 char buf[BUF_SIZE];
44 /* Get the class where the exception is. */
45 tossingClass = (*env)->FindClass(env, type);
46 if (tossingClass == NULL)
48 sjme_die("Could not find exception class?");
49 return;
52 /* Generate a message accordingly. */
53 memset(buf, 0, sizeof(buf));
54 snprintf(buf, BUF_SIZE - 1, "Native error: %d",
55 (int)sjme_error_default(code));
56 buf[BUF_SIZE - 1] = 0;
58 /* Throw it. */
59 if ((*env)->ThrowNew(env, tossingClass, buf) != 0)
60 sjme_die("Could not throw a new throwable?");
61 #undef BUF_SIZE
64 void sjme_jni_throwVMException(JNIEnv* env, sjme_errorCode code)
66 sjme_jni_throwThrowable(env, code,
67 "cc/squirreljme/emulator/vm/VMException");