forked from jruby/jruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinvoke.cpp
More file actions
337 lines (287 loc) · 10.7 KB
/
invoke.cpp
File metadata and controls
337 lines (287 loc) · 10.7 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/***** BEGIN LICENSE BLOCK *****
* Version: CPL 1.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Common Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.eclipse.org/legal/cpl-v10.html
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Copyright (C) 2008-2010 Wayne Meissner
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the CPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the CPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****/
#include <stdio.h>
#include <stdlib.h>
#ifdef __sun
#include <alloca.h>
#endif
#include <jni.h>
#include "ruby.h"
#include "jruby.h"
#include "Handle.h"
#include "JUtil.h"
#include "JavaException.h"
#include "org_jruby_cext_Native.h"
using namespace jruby;
static VALUE dispatch(void* func, int arity, int argCount, VALUE recv, VALUE* v);
static int invokeLevel = 0;
static void clearSyncQueue(DataSyncQueue* q);
class InvocationSession {
private:
JNIEnv* env;
public:
InvocationSession(JNIEnv* env_) {
++invokeLevel;
this->env = env_;
nsync(env);
}
~InvocationSession() {
--invokeLevel;
if (unlikely(!TAILQ_EMPTY(&jsyncq))) {
runSyncQueue(env, &jsyncq);
if (invokeLevel < 1) {
clearSyncQueue(&jsyncq);
}
}
if (likely(invokeLevel < 1)) {
if (unlikely(!TAILQ_EMPTY(&cleanq))) {
runSyncQueue(env, &cleanq);
clearSyncQueue(&cleanq);
}
if (unlikely(!TAILQ_EMPTY(&nsyncq))) {
clearSyncQueue(&nsyncq);
}
}
}
};
/*
* Class: org_jruby_cext_Native
* Method: callInit
* Signature: (Lorg/jruby/runtime/ThreadContext;J)J
*/
extern "C" JNIEXPORT jlong JNICALL
Java_org_jruby_cext_Native_callInit(JNIEnv* env, jobject self, jobject jThreadContext, jlong address)
{
jobject runtime = env->CallObjectMethod(jThreadContext, jruby::ThreadContext_getRuntime_method);
if (!env->IsSameObject(runtime, jruby::runtime)) {
jruby::throwExceptionByName(env, jruby::RuntimeException, "C extension initialized against invalid ruby runtime");
return 0;
}
try {
((void (*)(void)) address)();
} catch (jruby::JavaException& ex) {
env->Throw(ex.getCause());
} catch (std::exception& ex) {
jruby::throwExceptionByName(env, jruby::RuntimeException, "C runtime exception occurred: ", ex.what());
}
return 0;
}
/*
* Class: org_jruby_cext_Native
* Method: callMethod
* Signature: (Lorg/jruby/runtime/ThreadContext;JLorg/jruby/runtime/builtin/IRubyObject;I[Lorg/jruby/runtime/builtin/IRubyObject;)Lorg/jruby/runtime/builtin/IRubyObject;
*/
extern "C" JNIEXPORT jobject JNICALL
Java_org_jruby_cext_Native_callMethod(JNIEnv* env, jobject nativeClass, jobject jThreadContext,
jlong address, jlong recv, jint arity, jlongArray argArray)
{
try {
int argCount = env->GetArrayLength(argArray);
jlong* largs = (jlong *) alloca(argCount * sizeof(jlong));
env->GetLongArrayRegion(argArray, 0, argCount, largs);
VALUE* values = (VALUE *) alloca(argCount * sizeof(VALUE));
for (int i = 0; i < argCount; ++i) {
values[i] = makeStrongRef(env, (VALUE) largs[i]);
}
InvocationSession session(env);
makeStrongRef(env, (VALUE) recv);
VALUE v = dispatch((void *) address, arity, argCount, (VALUE) recv, values);
return valueToObject(env, v);
} catch (jruby::JavaException& ex) {
env->Throw(ex.getCause());
return NULL;
} catch (std::exception& ex) {
jruby::throwExceptionByName(env, jruby::RuntimeException, "C runtime exception occurred: ", ex.what());
return NULL;
}
}
/*
* Class: org_jruby_cext_Native
* Method: callMethod0
* Signature: (JJ)Lorg/jruby/runtime/builtin/IRubyObject;
*/
extern "C" JNIEXPORT jobject JNICALL
Java_org_jruby_cext_Native_callMethod0(JNIEnv* env, jobject self, jlong fn, jlong recv)
{
try {
InvocationSession session(env);
makeStrongRef(env, (VALUE) recv);
return valueToObject(env, ((VALUE (*)(VALUE)) fn)((VALUE) recv));
} catch (jruby::JavaException& ex) {
env->Throw(ex.getCause());
return NULL;
} catch (std::exception& ex) {
jruby::throwExceptionByName(env, jruby::RuntimeException, "C runtime exception occurred: ", ex.what());
return NULL;
}
}
/*
* Class: org_jruby_cext_Native
* Method: callMethod1
* Signature: (JJJ)Lorg/jruby/runtime/builtin/IRubyObject;
*/
extern "C" JNIEXPORT jobject JNICALL
Java_org_jruby_cext_Native_callMethod1(JNIEnv* env, jobject self, jlong fn, jlong recv, jlong arg1)
{
try {
InvocationSession session(env);
makeStrongRef(env, (VALUE) recv);
makeStrongRef(env, (VALUE) arg1);
return valueToObject(env, ((VALUE (*)(VALUE, VALUE)) fn)((VALUE) recv, (VALUE) arg1));
} catch (jruby::JavaException& ex) {
env->Throw(ex.getCause());
return NULL;
} catch (std::exception& ex) {
jruby::throwExceptionByName(env, jruby::RuntimeException, "C runtime exception occurred: ", ex.what());
return NULL;
}
}
/*
* Class: org_jruby_cext_Native
* Method: callMethod2
* Signature: (JJJJ)Lorg/jruby/runtime/builtin/IRubyObject;
*/
extern "C" JNIEXPORT jobject JNICALL
Java_org_jruby_cext_Native_callMethod2(JNIEnv* env, jobject self, jlong fn, jlong recv,
jlong arg1, jlong arg2)
{
try {
InvocationSession session(env);
makeStrongRef(env, (VALUE) recv);
makeStrongRef(env, (VALUE) arg1);
makeStrongRef(env, (VALUE) arg2);
return valueToObject(env, ((VALUE (*)(VALUE, VALUE, VALUE)) fn)((VALUE) recv, (VALUE) arg1, (VALUE) arg2));
} catch (jruby::JavaException& ex) {
env->Throw(ex.getCause());
return NULL;
} catch (std::exception& ex) {
jruby::throwExceptionByName(env, jruby::RuntimeException, "C runtime exception occurred: ", ex.what());
return NULL;
}
}
/*
* Class: org_jruby_cext_Native
* Method: callMethod3
* Signature: (JJJJJ)Lorg/jruby/runtime/builtin/IRubyObject;
*/
extern "C" JNIEXPORT jobject JNICALL
Java_org_jruby_cext_Native_callMethod3(JNIEnv* env, jobject self, jlong fn, jlong recv,
jlong arg1, jlong arg2, jlong arg3)
{
try {
InvocationSession session(env);
makeStrongRef(env, (VALUE) recv);
makeStrongRef(env, (VALUE) arg1);
makeStrongRef(env, (VALUE) arg2);
makeStrongRef(env, (VALUE) arg3);
return valueToObject(env, ((VALUE (*)(VALUE, VALUE, VALUE, VALUE)) fn)((VALUE) recv, (VALUE) arg1, (VALUE) arg2, (VALUE) arg3));
} catch (jruby::JavaException& ex) {
env->Throw(ex.getCause());
return NULL;
} catch (std::exception& ex) {
jruby::throwExceptionByName(env, jruby::RuntimeException, "C runtime exception occurred: ", ex.what());
return NULL;
}
}
static VALUE
dispatch(void* func, int arity, int argCount, VALUE recv, VALUE* v)
{
// printf("calling %p with arity=%d\n", func, arity);
if (arity < 0) {
return ((VALUE (*)(int, VALUE*, VALUE)) func)(argCount, v, recv);
}
switch (argCount) {
case 0:
return ((VALUE (*)(VALUE))func)(recv);
case 1:
return ((VALUE (*)(VALUE, VALUE))func)(recv, v[0]);
case 2:
return ((VALUE (*)(VALUE, VALUE, VALUE))func)(recv, v[0], v[1]);
case 3:
return ((VALUE (*)(VALUE, VALUE, VALUE, VALUE))func)(recv, v[0], v[1], v[2]);
case 4:
return ((VALUE (*)(VALUE, VALUE, VALUE, VALUE, VALUE))func)(recv, v[0], v[1], v[2], v[3]);
case 5:
return ((VALUE (*)(VALUE, VALUE, VALUE, VALUE, VALUE, VALUE))func)(recv, v[0], v[1], v[2], v[3], v[4]);
case 6:
return ((VALUE (*)(VALUE, VALUE, VALUE, VALUE, VALUE, VALUE, VALUE))func)(recv, v[0], v[1], v[2], v[3], v[4], v[5]);
case 7:
return ((VALUE (*)(VALUE, VALUE, VALUE, VALUE, VALUE, VALUE, VALUE, VALUE))func)(recv, v[0], v[1], v[2], v[3], v[4], v[5], v[6]);
case 8:
return ((VALUE (*)(VALUE, VALUE, VALUE, VALUE, VALUE, VALUE, VALUE, VALUE, VALUE))func)(recv, v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]);
default:
rb_raise(rb_eArgError, "Too many arguments (%d for max %d)", argCount, 8);
}
}
static void
clearSyncQueue(DataSyncQueue* q)
{
DataSync* d;
while ((d = TAILQ_FIRST(q))) {
TAILQ_REMOVE(q, d, syncq);
}
}
/*
* Class: org_jruby_cext_Native
* Method: callFunction
* Signature: (JJ)J
*/
JNIEXPORT jlong JNICALL
Java_org_jruby_cext_Native_callFunction(JNIEnv* env, jobject, jlong function, jlong data)
{
try {
if (function == (jlong)0) return (jlong)0; // Just in case somebody passes no unblock function
return ((VALUE (*)(void*)) function)((void*) data);
} catch (jruby::JavaException& ex) {
env->Throw(ex.getCause());
return 0x0;
} catch (std::exception& ex) {
jruby::throwExceptionByName(env, jruby::RuntimeException, "C runtime exception occurred: ", ex.what());
return 0x0;
}
}
/*
* Class: org_jruby_cext_Native
* Method: callProcMethod
* Signature: (JJ)Lorg/jruby/runtime/builtin/IRubyObject;
*/
extern "C" JNIEXPORT jobject JNICALL
Java_org_jruby_cext_Native_callProcMethod(JNIEnv* env, jobject, jlong fn, jlong args_ary)
{
try {
InvocationSession session(env);
makeStrongRef(env, (VALUE) args_ary);
return valueToObject(env, ((VALUE (*)(VALUE)) fn)((VALUE) args_ary));
} catch (jruby::JavaException& ex) {
env->Throw(ex.getCause());
return NULL;
} catch (std::exception& ex) {
jruby::throwExceptionByName(env, jruby::RuntimeException, "C runtime exception occurred: ", ex.what());
return NULL;
}
}