rjb 1.4.7-x86-mswin32-100

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/ext/rjb.h ADDED
@@ -0,0 +1,164 @@
1
+ /*
2
+ * Rjb - Ruby <-> Java Bridge
3
+ * Copyright(c) 2004,2005 arton
4
+ *
5
+ * This library is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * This library is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * $Id: rjb.h 180 2011-12-05 16:34:29Z arton $
16
+ * $Log: rjb.h,v $
17
+ * Revision 1.1 2005/01/16 17:36:10 arton
18
+ * Initial revision
19
+ *
20
+ *
21
+ */
22
+
23
+ #ifndef RJB_H
24
+ #define RJB_H
25
+
26
+ #if RJB_RUBY_VERSION_CODE < 190
27
+ #if !defined(RHASH_TBL)
28
+ #define RHASH_TBL(x) RHASH((x))->tbl
29
+ #endif
30
+ #endif
31
+
32
+ #if !defined(RSTRING_LEN)
33
+ #define RSTRING_LEN(s) (RSTRING(s)->len)
34
+ #endif
35
+ #if !defined(RSTRING_PTR)
36
+ #define RSTRING_PTR(s) (RSTRING(s)->ptr)
37
+ #endif
38
+ #if !defined(RARRAY_LEN)
39
+ #define RARRAY_LEN(s) (RARRAY(s)->len)
40
+ #endif
41
+ #if !defined(RARRAY_PTR)
42
+ #define RARRAY_PTR(s) (RARRAY(s)->ptr)
43
+ #endif
44
+
45
+ #if !defined(COUNTOF)
46
+ #define COUNTOF(x) (sizeof(x)/sizeof(x[0]))
47
+ #endif
48
+
49
+ #if !defined(_I64_MIN)
50
+ #define _I64_MIN (-9223372036854775807i64 - 1)
51
+ #endif
52
+ #if !defined(_I64_MAX)
53
+ #define _I64_MAX 9223372036854775807i64
54
+ #endif
55
+
56
+ #if !defined(HAVE_LONG_LONG) && defined(__LP64__)
57
+ #define HAVE_LONG_LONG 1
58
+ #endif
59
+
60
+ /* in load.c */
61
+ extern int rjb_create_jvm(JNIEnv** pjenv, JavaVMInitArgs*, char*, VALUE);
62
+
63
+ /* in rjb.c */
64
+ extern JavaVM* rjb_jvm;
65
+ extern jclass rjb_rbridge;
66
+ extern jmethodID rjb_register_bridge;
67
+ extern VALUE rjb_loaded_classes;
68
+ extern jmethodID rjb_class_getName;
69
+ extern jclass rjb_j_throwable;
70
+ extern jmethodID rjb_throwable_getMessage;
71
+ extern JNIEnv* rjb_attach_current_thread(void);
72
+ extern jclass rjb_find_class(JNIEnv* jenv, VALUE name);
73
+ extern void rjb_release_string(JNIEnv *jenv, jstring str, const char* chrs);
74
+ extern VALUE rjb_load_vm_default();
75
+ extern void rjb_unload_vm();
76
+ extern VALUE rjb_safe_funcall(VALUE args);
77
+ extern VALUE jv2rv(JNIEnv* jenv, jvalue val);
78
+ extern jobject get_systemloader(JNIEnv* jenv);
79
+ extern jclass rjb_find_class_by_name(JNIEnv* jenv, const char* name);
80
+
81
+ /* in rjbexception.c */
82
+ extern VALUE rjb_get_exception_class(JNIEnv* jenv, jstring str);
83
+ extern void rjb_check_exception(JNIEnv* jenv, int t);
84
+ extern VALUE rjb_s_throw(int, VALUE*, VALUE);
85
+
86
+ /* conversion functions */
87
+ typedef void (*R2J)(JNIEnv*, VALUE, jvalue*, const char*, int);
88
+ typedef VALUE (*J2R)(JNIEnv*, jvalue);
89
+ typedef jarray (*R2JARRAY)(JNIEnv*, VALUE, const char*);
90
+ typedef void (JNICALL *RELEASEARRAY)(JNIEnv*, jobject, void*, jint);
91
+ typedef jlong (JNICALL *INVOKEAL)(JNIEnv*, jobject, jmethodID, const jvalue*);
92
+ typedef jdouble (JNICALL *INVOKEAD)(JNIEnv*, jobject, jmethodID, const jvalue*);
93
+ typedef jfloat (JNICALL *INVOKEAF)(JNIEnv*, jobject, jmethodID, const jvalue*);
94
+ typedef jboolean (JNICALL *INVOKEAZ)(JNIEnv*, jobject, jmethodID, const jvalue*);
95
+ typedef jshort (JNICALL *INVOKEAS)(JNIEnv*, jobject, jmethodID, const jvalue*);
96
+ typedef jobject (JNICALL *INVOKEA)(JNIEnv*, jobject, jmethodID, const jvalue*);
97
+ typedef VALUE (*CONV)(JNIEnv*, void*);
98
+
99
+ /*
100
+ * internal method class
101
+ */
102
+ struct cls_constructor {
103
+ jmethodID id;
104
+ int arg_count;
105
+ R2J* arg_convert;
106
+ char* method_signature;
107
+ char result_signature;
108
+ char result_arraydepth;
109
+ };
110
+
111
+ struct cls_method {
112
+ struct cls_constructor basic;
113
+ ID name;
114
+ int static_method;
115
+ off_t method;
116
+ J2R result_convert;
117
+ /* overload only */
118
+ struct cls_method* next;
119
+ };
120
+
121
+ /*
122
+ * internal field class
123
+ */
124
+ struct cls_field {
125
+ ID name;
126
+ jfieldID id;
127
+ char* field_signature;
128
+ char result_signature;
129
+ char result_arraydepth;
130
+ R2J arg_convert;
131
+ J2R value_convert;
132
+ int readonly;
133
+ int static_field;
134
+ };
135
+
136
+ /*
137
+ * Object instance
138
+ */
139
+ struct jvi_data {
140
+ jclass klass; /* class */
141
+ jobject obj; /* instance */
142
+ st_table* methods;
143
+ st_table* fields;
144
+ };
145
+
146
+ /*
147
+ * Class instance
148
+ */
149
+ struct jv_data {
150
+ struct jvi_data idata;
151
+ st_table* static_methods;
152
+ struct cls_constructor** constructors;
153
+ };
154
+
155
+ /*
156
+ * Bridge instance
157
+ */
158
+ struct rj_bridge {
159
+ jobject bridge;
160
+ jobject proxy;
161
+ VALUE wrapped;
162
+ };
163
+
164
+ #endif
@@ -0,0 +1,167 @@
1
+ /*
2
+ * Rjb - Ruby <-> Java Bridge
3
+ * Copyright(c) 2004,2005,2006,2010 arton
4
+ *
5
+ * This library is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * This library is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * $Id: rjbexception.c 174 2011-11-09 13:47:43Z arton $
16
+ */
17
+
18
+ #include "ruby.h"
19
+ #include "extconf.h"
20
+ #if RJB_RUBY_VERSION_CODE < 190
21
+ #include "st.h"
22
+ #else
23
+ #include "ruby/st.h"
24
+ #endif
25
+ #include "jniwrap.h"
26
+ #include "riconv.h"
27
+ #include "rjb.h"
28
+
29
+ static VALUE missing_delegate(int argc, VALUE* argv, VALUE self)
30
+ {
31
+ ID rmid = rb_to_id(argv[0]);
32
+ return rb_funcall(rb_ivar_get(self, rb_intern("@cause")), rmid, argc - 1, argv + 1);
33
+ }
34
+
35
+ static VALUE exception_to_s(VALUE self)
36
+ {
37
+ return rb_funcall(rb_ivar_get(self, rb_intern("@cause")),
38
+ rb_intern("toString"), 0);
39
+ }
40
+
41
+ /*
42
+ * handle Java exception
43
+ * At this time, the Java exception is defined without the package name.
44
+ * This design may change in future release.
45
+ */
46
+ VALUE rjb_get_exception_class(JNIEnv* jenv, jstring str)
47
+ {
48
+ VALUE rexp;
49
+ char* pcls;
50
+ VALUE cname;
51
+ const char* p = (*jenv)->GetStringUTFChars(jenv, str, JNI_FALSE);
52
+ char* clsname = ALLOCA_N(char, strlen(p) + 1);
53
+ strcpy(clsname, p);
54
+ rjb_release_string(jenv, str, p);
55
+ pcls = strrchr(clsname, '.');
56
+ if (pcls)
57
+ {
58
+ pcls++;
59
+ }
60
+ else
61
+ {
62
+ pcls = clsname;
63
+ }
64
+ cname = rb_str_new2(pcls);
65
+ rexp = rb_hash_aref(rjb_loaded_classes, cname);
66
+ if (rexp == Qnil)
67
+ {
68
+ rexp = rb_define_class(pcls, rb_eStandardError);
69
+ rb_define_method(rexp, "method_missing", missing_delegate, -1);
70
+ rb_define_method(rexp, "to_str", exception_to_s, 0);
71
+ #if defined(HAVE_RB_HASH_ASET) || defined(RUBINIUS)
72
+ rb_hash_aset(rjb_loaded_classes, cname, rexp);
73
+ #else
74
+ #ifdef RHASH_TBL
75
+ st_insert(RHASH_TBL(rjb_loaded_classes), cname, rexp);
76
+ #else
77
+ st_insert(RHASH(rjb_loaded_classes)->tbl, cname, rexp);
78
+ #endif
79
+ #endif
80
+
81
+ }
82
+ return rexp;
83
+ }
84
+
85
+ /*
86
+ * throw newly created exception with supplied message.
87
+ */
88
+ VALUE rjb_s_throw(int argc, VALUE* argv, VALUE self)
89
+ {
90
+ VALUE klass;
91
+ VALUE message;
92
+ JNIEnv* jenv = NULL;
93
+
94
+ rjb_load_vm_default();
95
+
96
+ jenv = rjb_attach_current_thread();
97
+ (*jenv)->ExceptionClear(jenv);
98
+
99
+ if (rb_scan_args(argc, argv, "11", &klass, &message) == 2)
100
+ {
101
+ jclass excep = rjb_find_class(jenv, klass);
102
+ if (excep == NULL)
103
+ {
104
+ rb_raise(rb_eRuntimeError, "`%s' not found", StringValueCStr(klass));
105
+ }
106
+ (*jenv)->ThrowNew(jenv, excep, StringValueCStr(message));
107
+ }
108
+ else
109
+ {
110
+ struct jvi_data* ptr;
111
+ Data_Get_Struct(klass, struct jvi_data, ptr);
112
+ if (!(*jenv)->IsInstanceOf(jenv, ptr->obj, rjb_j_throwable))
113
+ {
114
+ rb_raise(rb_eRuntimeError, "arg1 must be a throwable");
115
+ }
116
+ else
117
+ {
118
+ (*jenv)->Throw(jenv, ptr->obj);
119
+ }
120
+ }
121
+ return Qnil;
122
+ }
123
+
124
+ void rjb_check_exception(JNIEnv* jenv, int t)
125
+ {
126
+ jthrowable exp = (*jenv)->ExceptionOccurred(jenv);
127
+ if (exp)
128
+ {
129
+ VALUE rexp = Qnil;
130
+ if (RTEST(ruby_verbose))
131
+ {
132
+ (*jenv)->ExceptionDescribe(jenv);
133
+ }
134
+ (*jenv)->ExceptionClear(jenv);
135
+ if(1)
136
+ {
137
+ char* msg = "unknown exception";
138
+ jclass cls = (*jenv)->GetObjectClass(jenv, exp);
139
+ jstring str = (*jenv)->CallObjectMethod(jenv, exp, rjb_throwable_getMessage);
140
+ if (str)
141
+ {
142
+ const char* p = (*jenv)->GetStringUTFChars(jenv, str, JNI_FALSE);
143
+ msg = ALLOCA_N(char, strlen(p) + 1);
144
+ strcpy(msg, p);
145
+ rjb_release_string(jenv, str, p);
146
+ }
147
+ str = (*jenv)->CallObjectMethod(jenv, cls, rjb_class_getName);
148
+ if (str)
149
+ {
150
+ rexp = rjb_get_exception_class(jenv, str);
151
+ }
152
+ if (rexp == Qnil)
153
+ {
154
+ (*jenv)->DeleteLocalRef(jenv, exp);
155
+ rb_raise(rb_eRuntimeError, "%s", msg);
156
+ }
157
+ else
158
+ {
159
+ VALUE rexpi = rb_funcall(rexp, rb_intern("new"), 1, rb_str_new2(msg));
160
+ jvalue val;
161
+ val.l = exp;
162
+ rb_ivar_set(rexpi, rb_intern("@cause"), jv2rv(jenv, val));
163
+ rb_exc_raise(rexpi);
164
+ }
165
+ }
166
+ }
167
+ }
data/lib/rjb.rb ADDED
@@ -0,0 +1,122 @@
1
+ =begin
2
+ Copyright(c) 2006-2010,2012 arton
3
+ =end
4
+
5
+ require 'rbconfig'
6
+
7
+ module RjbConf
8
+ if /darwin/ =~ RUBY_PLATFORM
9
+ if ENV['JVM_LIB'].nil? || ENV['JVM_LIB'] == ''
10
+ if ENV['JAVA_HOME'].nil? || ENV['JAVA_HOME'] == ''
11
+ jvms = Dir.glob("#{`/usr/libexec/java_home`.strip}/**/libjvm.dylib")
12
+ else
13
+ jvms = Dir.glob("#{ENV['JAVA_HOME']}/**/libjvm.dylib")
14
+ end
15
+ if jvms.size > 0
16
+ ENV['JVM_LIB'] = jvms[0]
17
+ end
18
+ end
19
+ end
20
+
21
+ dir = File.join(File.dirname(File.dirname(__FILE__)), 'data')
22
+ if File.exist?(dir)
23
+ datadir = dir
24
+ else
25
+ datadir = RbConfig::CONFIG['datadir']
26
+ end
27
+ BRIDGE_FILE = File.join(datadir, 'rjb', 'jp', 'co', 'infoseek', 'hp',
28
+ 'arton', 'rjb', 'RBridge.class')
29
+ unless File.exist?(BRIDGE_FILE)
30
+ raise 'bridge file not found'
31
+ end
32
+ end
33
+
34
+ require 'rjbcore'
35
+
36
+ module Rjb
37
+ module MODIFIER
38
+ def self.STATIC
39
+ 8
40
+ end
41
+ def self.PUBLIC
42
+ 1
43
+ end
44
+ end
45
+
46
+ module JMethod
47
+ def instance_method?(m)
48
+ m.modifiers & MODIFIER.STATIC == 0
49
+ end
50
+ def public_method?(m)
51
+ (m.modifiers & MODIFIER.PUBLIC) == MODIFIER.PUBLIC
52
+ end
53
+ def jmethods(org, klass, &blk)
54
+ (org + klass.getMethods.select do |m|
55
+ blk.call(m)
56
+ end.map do |m|
57
+ m.name
58
+ end).uniq
59
+ end
60
+ def format_sigs(s)
61
+ if s.size < 0
62
+ ''
63
+ elsif s.size == 1
64
+ s[0]
65
+ else
66
+ "[#{s.map{|m|m.nil? ? 'void' : m}.join(', ')}]"
67
+ end
68
+ end
69
+ end
70
+
71
+ class Rjb_JavaClass
72
+ include JMethod
73
+ def public_methods(inh = true)
74
+ jmethods(super(inh), self) do |m|
75
+ !instance_method?(m) && public_method?(m)
76
+ end
77
+ end
78
+ def methods(inh = true)
79
+ jmethods(super(inh), self) do |m|
80
+ !instance_method?(m) && public_method?(m)
81
+ end
82
+ end
83
+ def java_methods
84
+ jmethods([], self) do |m|
85
+ !instance_method?(m) && public_method?(m)
86
+ end.map do |m|
87
+ "#{m}(#{format_sigs(self.static_sigs(m))})"
88
+ end
89
+ end
90
+ end
91
+ class Rjb_JavaProxy
92
+ include JMethod
93
+ def initialize_proxy
94
+ end
95
+ def public_methods(inh = true)
96
+ jmethods(super(inh), getClass) do |m|
97
+ instance_method?(m) && public_method?(m)
98
+ end
99
+ end
100
+ def methods(inh = true)
101
+ jmethods(super(inh), getClass) do |m|
102
+ instance_method?(m) && public_method?(m)
103
+ end
104
+ end
105
+ def java_methods
106
+ jmethods([], getClass) do |m|
107
+ instance_method?(m) && public_method?(m)
108
+ end.map do |m|
109
+ "#{m}(#{format_sigs(getClass.sigs(m))})"
110
+ end
111
+ end
112
+ end
113
+ class Rjb_JavaBridge
114
+ def method_missing(name, *args)
115
+ @wrapped.__send__(name, *args)
116
+ end
117
+ def respond_to?(name, inc_private = false)
118
+ @wrapped.respond_to?(name, inc_private)
119
+ end
120
+ attr_reader :wrapped
121
+ end
122
+ end