forked from jruby/jruby
/
CallableSelector.java
442 lines (370 loc) · 17.4 KB
/
CallableSelector.java
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
package org.jruby.java.dispatch;
import java.lang.reflect.Member;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.jruby.Ruby;
import org.jruby.javasupport.JavaCallable;
import org.jruby.javasupport.JavaClass;
import org.jruby.javasupport.JavaUtil;
import org.jruby.javasupport.ParameterTypes;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.CodegenUtils;
/**
* Method selection logic for calling from Ruby to Java.
*/
public class CallableSelector {
private CallableSelector() { /* no-instances */ }
//private static final boolean DEBUG = true;
public static ParameterTypes matchingCallableArityN(Ruby runtime, Map cache, ParameterTypes[] methods, IRubyObject[] args, int argsLength) {
final int signatureCode = argsHashCode(args);
ParameterTypes method = (ParameterTypes) cache.get(signatureCode);
if (method == null) {
method = findMatchingCallableForArgs(runtime, methods, args);
if (method != null) cache.put(signatureCode, method);
}
return method;
}
// NOTE: The five match methods are arity-split to avoid the cost of boxing arguments
// when there's already a cached match. Do not condense them into a single
// method.
public static JavaCallable matchingCallableArityN(Ruby runtime, Map cache, JavaCallable[] methods, IRubyObject[] args, int argsLength) {
final int signatureCode = argsHashCode(args);
JavaCallable method = (JavaCallable) cache.get(signatureCode);
if (method == null) {
method = (JavaCallable) findMatchingCallableForArgs(runtime, methods, args);
if (method != null) cache.put(signatureCode, method);
}
return method;
}
public static JavaCallable matchingCallableArityOne(Ruby runtime, Map cache, JavaCallable[] methods, IRubyObject arg0) {
final int signatureCode = argsHashCode(arg0);
JavaCallable method = (JavaCallable) cache.get(signatureCode);
if (method == null) {
method = (JavaCallable) findMatchingCallableForArgs(runtime, methods, arg0);
if (method != null) cache.put(signatureCode, method);
}
return method;
}
public static JavaCallable matchingCallableArityTwo(Ruby runtime, Map cache, JavaCallable[] methods, IRubyObject arg0, IRubyObject arg1) {
final int signatureCode = argsHashCode(arg0, arg1);
JavaCallable method = (JavaCallable) cache.get(signatureCode);
if (method == null) {
method = (JavaCallable) findMatchingCallableForArgs(runtime, methods, arg0, arg1);
if (method != null) cache.put(signatureCode, method);
}
return method;
}
public static JavaCallable matchingCallableArityThree(Ruby runtime, Map cache, JavaCallable[] methods, IRubyObject arg0, IRubyObject arg1, IRubyObject arg2) {
final int signatureCode = argsHashCode(arg0, arg1, arg2);
JavaCallable method = (JavaCallable) cache.get(signatureCode);
if (method == null) {
method = (JavaCallable) findMatchingCallableForArgs(runtime, methods, arg0, arg1, arg2);
if (method != null) cache.put(signatureCode, method);
}
return method;
}
public static JavaCallable matchingCallableArityFour(Ruby runtime, Map cache, JavaCallable[] methods, IRubyObject arg0, IRubyObject arg1, IRubyObject arg2, IRubyObject arg3) {
final int signatureCode = argsHashCode(arg0, arg1, arg2, arg3);
JavaCallable method = (JavaCallable) cache.get(signatureCode);
if (method == null) {
method = (JavaCallable) findMatchingCallableForArgs(runtime, methods, arg0, arg1, arg2, arg3);
if (method != null) cache.put(signatureCode, method);
}
return method;
}
private static ParameterTypes findMatchingCallableForArgs(final Ruby runtime,
final ParameterTypes[] methods, final IRubyObject... args) {
ParameterTypes method = null;
// try the new way first
final List<ParameterTypes> candidates = findCallableCandidates(methods, args);
final int size = candidates.size();
if ( size > 0 ) {
// new way found one, so let's go with that
if ( size == 1 ) method = candidates.get(0);
else { // narrow to most specific version (or first version, if none are more specific)
ParameterTypes mostSpecific = candidates.get(0);
Class<?>[] msTypes = mostSpecific.getParameterTypes();
boolean ambiguous = false;
OUTER: for ( int c = 1; c < size; c++ ) {
final ParameterTypes candidate = candidates.get(c);
final Class<?>[] cTypes = candidate.getParameterTypes();
for ( int i = 0; i < msTypes.length; i++ ) {
final Class<?> msType = msTypes[i], cType = cTypes[i];
if ( msType != cType && msType.isAssignableFrom(cType) ) {
mostSpecific = candidate;
msTypes = cTypes;
ambiguous = false; continue OUTER;
}
}
// none more specific; check for ambiguities
for ( int i = 0; i < msTypes.length; i++ ) {
final Class<?> msType = msTypes[i], cType = cTypes[i];
if ( msType == cType || msType.isAssignableFrom(cType) || cType.isAssignableFrom(msType) ) {
ambiguous = false; continue OUTER;
} else {
ambiguous = true;
}
}
}
method = mostSpecific;
if (ambiguous) {
runtime.getWarnings().warn("ambiguous Java methods found, using " + ((Member) ((JavaCallable) method).accessibleObject()).getName() + CodegenUtils.prettyParams(msTypes));
}
}
}
// fall back on old ways
if (method == null) {
method = findCallable(methods, Exact, args);
if (method == null) {
method = findCallable(methods, AssignableAndPrimitivable, args);
if (method == null) {
method = findCallable(methods, AssignableOrDuckable, args);
if (method == null) {
method = findCallable(methods, AssignableOrDuckable, args);
if (method == null) {
method = findCallable(methods, AssignableAndPrimitivableWithVarargs, args);
}
}
}
}
}
return method;
}
private static ParameterTypes findCallable(ParameterTypes[] callables, CallableAcceptor acceptor, IRubyObject[] args) {
ParameterTypes bestCallable = null;
int bestScore = -1;
for ( int i = 0; i < callables.length; i++ ) {
ParameterTypes callable = callables[i];
if ( acceptor.accept(callable, args) ) {
int currentScore = getExactnessScore(callable, args);
if (currentScore > bestScore) {
bestCallable = callable;
bestScore = currentScore;
}
}
}
return bestCallable;
}
private static List<ParameterTypes> findCallableCandidates(final ParameterTypes[] callables,
final IRubyObject[] args) {
// in case of an exact match prefer to return it early :
for ( int c = 0; c < callables.length; c++ ) {
final ParameterTypes callable = callables[c];
if ( exactMatch(callable, args ) ) return Collections.singletonList(callable);
}
final ArrayList<ParameterTypes> retained = new ArrayList<ParameterTypes>(callables.length);
ParameterTypes[] incoming = callables.clone();
for ( int i = 0; i < args.length; i++ ) {
retained.clear();
for ( final Matcher matcher : NON_EXACT_MATCH_SEQUENCE ) {
for ( int c = 0; c < incoming.length; c++ ) {
ParameterTypes callable = incoming[c];
if ( callable == null ) continue; // removed (matched)
Class[] types = callable.getParameterTypes();
if ( matcher.match( types[i], args[i] ) ) {
retained.add(callable);
incoming[c] = null; // retaining - remove
}
}
}
incoming = retained.toArray( new ParameterTypes[retained.size()] );
}
return retained;
}
private static int getExactnessScore(ParameterTypes paramTypes, IRubyObject[] args) {
final Class[] types = paramTypes.getParameterTypes();
int count = 0;
if ( paramTypes.isVarArgs() ) {
// varargs exactness gives the last N args as +1 since they'll already
// have been determined to fit
// dig out as many trailing args as possible that match varargs type
final int nonVarargs = types.length - 1;
count += 1; // add one for vararg
// check remaining args
for (int i = 0; i < nonVarargs && i < args.length; i++) {
if ( types[i] == getJavaClass(args[i]) ) count++;
}
}
else {
for (int i = 0; i < args.length; i++) {
if ( types[i] == getJavaClass(args[i]) ) count++;
}
}
return count;
}
private static interface CallableAcceptor {
public boolean accept(ParameterTypes types, IRubyObject[] args);
}
private static final CallableAcceptor Exact = new CallableAcceptor() {
public boolean accept(ParameterTypes types, IRubyObject[] args) {
return exactMatch(types, args);
}
};
private static final CallableAcceptor AssignableAndPrimitivable = new CallableAcceptor() {
public boolean accept(ParameterTypes types, IRubyObject[] args) {
return assignableAndPrimitivable(types, args);
}
};
private static final CallableAcceptor AssignableOrDuckable = new CallableAcceptor() {
public boolean accept(ParameterTypes types, IRubyObject[] args) {
return assignableOrDuckable(types, args);
}
};
private static final CallableAcceptor AssignableAndPrimitivableWithVarargs = new CallableAcceptor() {
public boolean accept(ParameterTypes types, IRubyObject[] args) {
return assignableAndPrimitivableWithVarargs(types, args);
}
};
private static interface Matcher {
public boolean match(final Class<?> type, final IRubyObject arg);
}
private static final Matcher EXACT = new Matcher() {
public boolean match(final Class<?> type, final IRubyObject arg) {
final Class<?> argClass = getJavaClass(arg);
return type == argClass || (type.isPrimitive() && CodegenUtils.getBoxType(type) == argClass);
}
};
private static final Matcher ASSIGNABLE = new Matcher() {
public boolean match(Class type, IRubyObject arg) {
return assignable(type, arg);
}
};
private static final Matcher PRIMITIVABLE = new Matcher() {
public boolean match(Class type, IRubyObject arg) {
return primitivable(type, arg);
}
};
private static final Matcher DUCKABLE = new Matcher() {
public boolean match(Class type, IRubyObject arg) {
return duckable(type, arg);
}
};
//private static final Matcher[] MATCH_SEQUENCE = new Matcher[] { EXACT, PRIMITIVABLE, ASSIGNABLE, DUCKABLE };
private static final Matcher[] NON_EXACT_MATCH_SEQUENCE = new Matcher[] { PRIMITIVABLE, ASSIGNABLE, DUCKABLE };
private static boolean exactMatch(ParameterTypes paramTypes, IRubyObject... args) {
Class[] types = paramTypes.getParameterTypes();
if (args.length != types.length) return false;
for (int i = 0; i < types.length; i++) {
if (!EXACT.match(types[i], args[i])) {
return false;
}
}
return true;
}
private static boolean assignableAndPrimitivable(ParameterTypes paramTypes, IRubyObject... args) {
Class[] types = paramTypes.getParameterTypes();
if (args.length != types.length) return false;
for (int i = 0; i < types.length; i++) {
if (!(ASSIGNABLE.match(types[i], args[i]) && PRIMITIVABLE.match(types[i], args[i]))) {
return false;
}
}
return true;
}
private static boolean assignableOrDuckable(ParameterTypes paramTypes, IRubyObject... args) {
Class[] types = paramTypes.getParameterTypes();
if (args.length != types.length) return false;
for (int i = 0; i < types.length; i++) {
if (!(ASSIGNABLE.match(types[i], args[i]) || DUCKABLE.match(types[i], args[i]))) {
return false;
}
}
return true;
}
private static boolean assignableAndPrimitivableWithVarargs(ParameterTypes paramTypes, IRubyObject... args) {
// bail out if this is not a varargs method
if (!paramTypes.isVarArgs()) return false;
Class[] types = paramTypes.getParameterTypes();
Class varArgArrayType = types[types.length - 1];
Class varArgType = varArgArrayType.getComponentType();
// if there's no args, we only match when there's just varargs
if ( args.length == 0 ) return types.length <= 1;
// dig out as many trailing args as will fit, ensuring they match varargs type
int nonVarargs = types.length - 1;
for (int i = args.length - 1; i >= nonVarargs; i--) {
if (!(ASSIGNABLE.match(varArgType, args[i]) || PRIMITIVABLE.match(varArgType, args[i]))) {
return false;
}
}
// check remaining args
for (int i = 0; i < nonVarargs; i++) {
if (!(ASSIGNABLE.match(types[i], args[i]) || PRIMITIVABLE.match(types[i], args[i]))) {
return false;
}
}
return true;
}
private static boolean assignable(Class<?> type, final IRubyObject arg) {
return JavaClass.assignable(type, getJavaClass(arg));
}
/**
* This method checks whether an argument can be *directly* converted into
* the target primitive, i.e. without changing from integral to floating-point.
*
* @param type The target type
* @param arg The argument to convert
* @return Whether the argument can be directly converted to the target primitive type
*/
private static boolean primitivable(final Class<?> type, final IRubyObject arg) {
final Class<?> argClass = getJavaClass(arg);
if (type.isPrimitive()) {
// TODO: This is where we would want to do precision checks to see
// if it's non-destructive to coerce a given type into the target
// integral primitive
if (type == Integer.TYPE || type == Long.TYPE || type == Short.TYPE || type == Character.TYPE) {
return argClass == long.class || // long first because it's what Fixnum claims to be
argClass == byte.class ||
argClass == short.class ||
argClass == char.class ||
argClass == int.class ||
argClass == Long.class ||
argClass == Byte.class ||
argClass == Short.class ||
argClass == Character.class ||
argClass == Integer.class;
}
if (type == Float.TYPE || type == Double.TYPE) {
return argClass == double.class || // double first because it's what float claims to be
argClass == float.class ||
argClass == Float.class ||
argClass == Double.class;
}
if (type == Boolean.TYPE) {
return argClass == boolean.class ||
argClass == Boolean.class;
}
}
return false;
}
private static boolean duckable(final Class<?> type, final IRubyObject arg) {
return JavaUtil.isDuckTypeConvertable(getJavaClass(arg), type);
}
private static int argsHashCode(IRubyObject a0) {
return 31 + javaClassHashCode(a0);
}
private static int argsHashCode(IRubyObject a0, IRubyObject a1) {
return 31 * argsHashCode(a0) + javaClassHashCode(a1);
}
private static int argsHashCode(IRubyObject a0, IRubyObject a1, IRubyObject a2) {
return 31 * argsHashCode(a0, a1) + javaClassHashCode(a2);
}
private static int argsHashCode(IRubyObject a0, IRubyObject a1, IRubyObject a2, IRubyObject a3) {
return 31 * argsHashCode(a0, a1, a2) + javaClassHashCode(a3);
}
private static int argsHashCode(IRubyObject[] args) {
if ( args == null ) return 0;
int result = 1;
for ( int i = 0; i < args.length; i++ ) {
result = 31 * result + javaClassHashCode(args[i]);
}
return result;
}
private static int javaClassHashCode(final IRubyObject arg) {
return arg == null ? 0 : arg.getJavaClass().hashCode();
}
private static Class<?> getJavaClass(final IRubyObject arg) {
return arg != null ? arg.getJavaClass() : void.class;
}
}