forked from s-u/rJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathotables.c
More file actions
241 lines (194 loc) · 5.58 KB
/
Copy pathotables.c
File metadata and controls
241 lines (194 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include "rJava.h"
#define RJAVA_LOOKUP 23
/* This uses the mechanism of the RObjectTables package
see: http://www.omegahat.org/RObjectTables/ */
/**
* Returns the R_UnboundValue
*/
HIDE SEXP R_getUnboundValue() {
return(R_UnboundValue);
}
/**
* @param name name of a java class
* @param canCache Can R cache this object
* @param tb
* @return TRUE if the a class called name exists in on of the packages
*/
/* not actually used by R */
HIDE Rboolean rJavaLookupTable_exists(const char * const name, Rboolean *canCache, R_ObjectTable *tb){
#ifdef LOOKUP_DEBUG
Rprintf( " >> rJavaLookupTable_exists\n" );
#endif
if(tb->active == FALSE)
return(FALSE);
tb->active = FALSE;
Rboolean val = classNameLookupExists( tb, name );
tb->active = TRUE;
return( val );
}
/**
* Returns a new jclassName object if the class exists or the
* unbound value if it does not
*
* @param name class name
* @param canCache ??
* @param tb lookup table
*/
SEXP rJavaLookupTable_get(const char * const name, Rboolean *canCache, R_ObjectTable *tb){
#ifdef LOOKUP_DEBUG
Rprintf( " >> rJavaLookupTable_get\n" );
#endif
SEXP val;
if(tb->active == FALSE)
return(R_UnboundValue);
tb->active = FALSE;
val = PROTECT( classNameLookup( tb, name ) );
tb->active = TRUE;
UNPROTECT(1); /* val */
return(val);
}
/**
* Does nothing. Not applicable to java packages
*/
int rJavaLookupTable_remove(const char * const name, R_ObjectTable *tb){
#ifdef LOOKUP_DEBUG
Rprintf( " >> rJavaLookupTable_remove( %s) \n", name );
#endif
error( "cannot remove from java package" ) ;
return 0;
}
/**
* Indicates if R can cahe the variable name.
* Currently allways return FALSE
*
* @param name name of the class
* @param tb lookup table
* @return allways FALSE (for now)
*/
HIDE Rboolean rJavaLookupTable_canCache(const char * const name, R_ObjectTable *tb){
#ifdef LOOKUP_DEBUG
Rprintf( " >> rJavaLookupTable_canCache\n" );
#endif
return( FALSE );
}
/**
* Generates an error. assign is not possible on our lookup table
*/
HIDE SEXP rJavaLookupTable_assign(const char * const name, SEXP value, R_ObjectTable *tb){
#ifdef LOOKUP_DEBUG
Rprintf( " >> rJavaLookupTable_assign( %s ) \n", name );
#endif
error("can't assign to java package lookup");
return R_NilValue;
}
/**
* Returns the list of classes known to be included in the
* packages. Currently returns NULL
*
* @param tb lookup table
*/
HIDE SEXP rJavaLookupTable_objects(R_ObjectTable *tb) {
#ifdef LOOKUP_DEBUG
Rprintf( " >> rJavaLookupTable_objects\n" );
#endif
tb->active = FALSE;
SEXP res = PROTECT( getKnownClasses( tb ) ) ;
tb->active = TRUE;
UNPROTECT(1); /* res */
return( res );
}
REPC SEXP newRJavaLookupTable(SEXP importer){
#ifdef LOOKUP_DEBUG
Rprintf( "<newRJavaLookupTable>\n" );
#endif
R_ObjectTable *tb;
SEXP val, klass;
tb = (R_ObjectTable *) malloc(sizeof(R_ObjectTable));
if(!tb)
error( "cannot allocate space for an internal R object table" );
tb->type = RJAVA_LOOKUP ; /* FIXME: not sure what this should be */
tb->cachedNames = NULL;
R_PreserveObject(importer);
tb->privateData = importer;
tb->exists = rJavaLookupTable_exists;
tb->get = rJavaLookupTable_get;
tb->remove = rJavaLookupTable_remove;
tb->assign = rJavaLookupTable_assign;
tb->objects = rJavaLookupTable_objects;
tb->canCache = rJavaLookupTable_canCache;
tb->onAttach = NULL;
tb->onDetach = NULL;
PROTECT(val = R_MakeExternalPtr(tb, install("UserDefinedDatabase"), R_NilValue));
PROTECT(klass = NEW_CHARACTER(1));
SET_STRING_ELT(klass, 0, COPY_TO_USER_STRING("UserDefinedDatabase"));
SET_CLASS(val, klass);
UNPROTECT(2);
#ifdef LOOKUP_DEBUG
Rprintf( "</newRJavaLookupTable>\n" );
#endif
return(val);
}
HIDE jobject getImporterReference(R_ObjectTable *tb ){
jobject res = (jobject)EXTPTR_PTR( GET_SLOT( (SEXP)(tb->privateData), install( "jobj" ) ) );
#ifdef LOOKUP_DEBUG
Rprintf( " >> getImporterReference : [%d]\n", res );
#endif
return res ;
}
HIDE SEXP getKnownClasses( R_ObjectTable *tb ){
#ifdef LOOKUP_DEBUG
Rprintf( " >> getKnownClasses\n" );
#endif
jobject importer = getImporterReference(tb);
JNIEnv *env=getJNIEnv();
jarray a = (jarray) (*env)->CallObjectMethod(env, importer, mid_RJavaImport_getKnownClasses ) ;
SEXP res = PROTECT( getStringArrayCont( a ) ) ;
#ifdef LOOKUP_DEBUG
Rprintf( " %d known classes\n", LENGTH(res) );
#endif
UNPROTECT(1);
return res ;
}
HIDE SEXP classNameLookup( R_ObjectTable *tb, const char * const name ){
#ifdef LOOKUP_DEBUG
Rprintf( " >> classNameLookup\n" );
#endif
JNIEnv *env=getJNIEnv();
jobject importer = getImporterReference(tb);
jobject clazz = newString(env, name ) ;
jstring s ; /* Class */
s = (jstring) (*env)->CallObjectMethod(env, importer, mid_RJavaImport_lookup, clazz ) ;
SEXP res ;
int np ;
if( !s ){
res = R_getUnboundValue() ;
np = 0;
} else{
PROTECT( res = new_jclassName( env, s ) );
np = 1;
}
releaseObject(env, clazz);
releaseObject(env, s);
if( np ) UNPROTECT(1);
#ifdef LOOKUP_DEBUG
Rprintf( "</classNameLookup>\n" );
#endif
return res ;
}
HIDE Rboolean classNameLookupExists(R_ObjectTable *tb, const char * const name ){
#ifdef LOOKUP_DEBUG
Rprintf( " classNameLookupExists\n" );
#endif
JNIEnv *env=getJNIEnv();
jobject importer = getImporterReference(tb);
jobject clazz = newString(env, name ) ;
jboolean s ; /* Class */
s = (jboolean) (*env)->CallBooleanMethod(env, importer,
mid_RJavaImport_exists , clazz ) ;
Rboolean res = (s) ? TRUE : FALSE;
#ifdef LOOKUP_DEBUG
Rprintf( " exists( %s ) = %d \n", name, res );
#endif
releaseObject(env, clazz);
return res ;
}