ext/rjb.c in rjb-1.1.2 vs ext/rjb.c in rjb-1.1.3
- old
+ new
@@ -10,14 +10,14 @@
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
- * $Id: rjb.c 59 2008-02-21 19:37:15Z arton $
+ * $Id: rjb.c 64 2008-03-05 14:24:22Z kuwa1 $
*/
-#define RJB_VERSION "1.1.2"
+#define RJB_VERSION "1.1.3"
#include "ruby.h"
#include "extconf.h"
#if RJB_RUBY_VERSION_CODE < 190
#include "st.h"
@@ -124,11 +124,10 @@
JNIEnv* env;
(*rjb_jvm)->AttachCurrentThread(rjb_jvm, (void**)&env, '\0');
return env;
}
-
void rjb_release_string(JNIEnv *jenv, jstring str, const char* chrs)
{
(*jenv)->ReleaseStringUTFChars(jenv, str, chrs);
(*jenv)->DeleteLocalRef(jenv, str);
}
@@ -1744,10 +1743,18 @@
return Qnil;
}
/*
+ * load Java Virtual Machine with default arguments.
+ */
+VALUE rjb_load_vm_default()
+{
+ return rjb_s_load(0, NULL, 0);
+}
+
+/*
* unload Java Virtual Machine
*
* def unload()
* classes.clear
* unload(jvm)
@@ -1760,10 +1767,12 @@
static VALUE rjb_s_unload(int argc, VALUE* argv, VALUE self)
{
st_foreach(RHASH_TBL(rjb_loaded_classes), clear_classes, 0);
if (rjb_jvm)
{
+ JNIEnv* jenv = rjb_attach_current_thread();
+ (*jenv)->ExceptionClear(jenv);
(*rjb_jvm)->DestroyJavaVM(rjb_jvm);
rjb_jvm = NULL;
}
return Qnil;
}
@@ -2020,13 +2029,17 @@
{
VALUE vsig, rest;
char* sig;
VALUE ret = Qnil;
struct jv_data* ptr;
- JNIEnv* jenv = rjb_attach_current_thread();
int found = 0;
+ JNIEnv* jenv = NULL;
+ rjb_load_vm_default();
+ jenv = rjb_attach_current_thread();
+ (*jenv)->ExceptionClear(jenv);
+
rb_scan_args(argc, argv, "1*", &vsig, &rest);
sig = StringValueCStr(vsig);
Data_Get_Struct(self, struct jv_data, ptr);
if (ptr->constructors)
{
@@ -2051,12 +2064,16 @@
static VALUE rjb_newinstance(int argc, VALUE* argv, VALUE self)
{
VALUE ret = Qnil;
struct jv_data* ptr;
struct cls_constructor** pc;
- JNIEnv* jenv = rjb_attach_current_thread();
int found = 0;
+ JNIEnv* jenv = NULL;
+
+ rjb_load_vm_default();
+ jenv = rjb_attach_current_thread();
+ (*jenv)->ExceptionClear(jenv);
Data_Get_Struct(self, struct jv_data, ptr);
if (ptr->constructors)
{
@@ -2109,12 +2126,15 @@
* jclass Rjb::bind(rbobj, interface_name)
*/
static VALUE rjb_s_bind(VALUE self, VALUE rbobj, VALUE itfname)
{
VALUE result = Qnil;
- JNIEnv* jenv = rjb_attach_current_thread();
-
+ JNIEnv* jenv = NULL;
+
+ rjb_load_vm_default();
+ jenv = rjb_attach_current_thread();
+ (*jenv)->ExceptionClear(jenv);
jclass itf = rjb_find_class(jenv, itfname);
rjb_check_exception(jenv, 1);
if (itf)
{
struct rj_bridge* ptr = ALLOC(struct rj_bridge);
@@ -2136,10 +2156,22 @@
}
return result;
}
/*
+ * jclass Rjb::bind(rbobj, interface_name)
+ */
+static VALUE rjb_s_unbind(VALUE self, VALUE rbobj)
+{
+ JNIEnv* jenv;
+ rjb_load_vm_default();
+ jenv = rjb_attach_current_thread();
+ (*jenv)->ExceptionClear(jenv);
+ return rb_ary_delete(proxies, rbobj);
+}
+
+/*
* Jclass Rjb::import(classname)
*/
static VALUE rjb_s_import(VALUE self, VALUE clsname)
{
JNIEnv* jenv;
@@ -2148,16 +2180,13 @@
if (v != Qnil)
{
return v;
}
- if (!rjb_jvm)
- {
- /* auto-load with default setting */
- rjb_s_load(0, NULL, 0);
- }
+ rjb_load_vm_default();
jenv = rjb_attach_current_thread();
+ (*jenv)->ExceptionClear(jenv);
jcls = rjb_find_class(jenv, clsname);
if (!jcls)
{
rjb_check_exception(jenv, 0);
rb_raise(rb_eRuntimeError, "`%s' not found", StringValueCStr(clsname));
@@ -2723,9 +2752,10 @@
rjb = rb_define_module("Rjb");
rb_define_module_function(rjb, "load", rjb_s_load, -1);
rb_define_module_function(rjb, "unload", rjb_s_unload, -1);
rb_define_module_function(rjb, "import", rjb_s_import, 1);
rb_define_module_function(rjb, "bind", rjb_s_bind, 2);
+ rb_define_module_function(rjb, "unbind", rjb_s_unbind, 1);
rb_define_module_function(rjb, "classes", rjb_s_classes, 0);
rb_define_module_function(rjb, "throw", rjb_s_throw, -1);
rb_define_module_function(rjb, "primitive_conversion=", rjb_s_set_pconversion, 1);
rb_define_module_function(rjb, "primitive_conversion", rjb_s_get_pconversion, 0);
rb_define_const(rjb, "VERSION", rb_str_new2(RJB_VERSION));