@SuppressWarnings("unchecked") // NULL_CACHE RubyToJavaInvoker(RubyModule host, Member member, String name) { super(host, Visibility.PUBLIC, name); this.runtime = host.getRuntime(); final T callable; T[] varargsCallables = null; int minVarArgsArity = -1; callable = createCallable(runtime, member); int minArity = callable.getArity(); if ( callable.isVarArgs() ) { // TODO does it need to happen? varargsCallables = createCallableArray(callable); minVarArgsArity = getMemberArity(member) - 1; } cache = NULL_CACHE; // if there's a single callable - matching (and thus the cache) won't be used this.javaCallable = callable; this.javaCallables = null; this.javaVarargsCallables = varargsCallables; setArity(minArity, minArity, minVarArgsArity); setupNativeCall(); }
final int getMemberArity(Member member) { return getMemberParameterTypes(member).length; }
private boolean setNativeCallIfPublic(final Method method) { final int mod = method.getModifiers(); // only public, since non-public don't bind if ( Modifier.isPublic(mod) && Modifier.isPublic( method.getDeclaringClass().getModifiers() ) ) { setNativeCall(method.getDeclaringClass(), method.getName(), method.getReturnType(), method.getParameterTypes(), Modifier.isStatic(mod), true); return true; } return false; }
protected final T findCallableArityZero(IRubyObject self, String name) { T callable = this.javaCallable; if ( callable == null ) { final T[] callablesForArity; if ( javaCallables.length == 0 || (callablesForArity = javaCallables[0]) == null ) { if ( ( callable = matchVarArgsCallableArityZero(self) ) == null ) { throw newErrorDueNoMatchingCallable(self, name); } return callable; } callable = CallableSelector.matchingCallableArityZero(runtime, this, callablesForArity); if ( callable == null ) { if ((callable = matchVarArgsCallableArityZero(self)) == null ) { throw newErrorDueArgumentTypeMismatch(self, callablesForArity); } } } else { if (!callable.isVarArgs()) checkCallableArity(callable, 0); } return callable; }
static <T extends AccessibleObject> T[] setAccessible(T[] accessibles) { // TODO: Replace flag that's false on 9 with proper module checks if (!allAreAccessible(accessibles) && !Ruby.isSecurityRestricted() && Options.JI_SETACCESSIBLE.load() && allAreMember(accessibles)) { try { AccessibleObject.setAccessible(accessibles, true); } catch (SecurityException e) {} catch (RuntimeException re) { rethrowIfNotInaccessibleObject(re); } } return accessibles; }
setArity(Arity.OPTIONAL); callable = createCallable(runtime, members[0]); if (callable.isVarArgs()) { varargsCallables = createCallableArray(callable); int maxArity = 0; for (Member method: members) { int currentArity = getMemberParameterTypes(method).length; maxArity = Math.max(currentArity, maxArity); List<JavaCallable> methodsForArity = methodsMap.get(currentArity); JavaCallable javaMethod = createCallable(runtime,method); methodsForArity.add(javaMethod); if (isMemberVarArgs(method)) { varargsArity = Math.min(currentArity - 1, varargsArity); varargsMethods.add(javaMethod); callables = createCallableArrayArray(maxArity + 1); for (Map.Entry<Integer,List<JavaCallable>> entry : methodsMap.entrySet()) { List<JavaCallable> methodsForArity = (List<JavaCallable>)entry.getValue(); JavaCallable[] methodsArray = methodsForArity.toArray(createCallableArray(methodsForArity.size())); callables[((Integer)entry.getKey()).intValue()] = methodsArray; varargsCallables = createCallableArray(varargsMethods.size()); varargsMethods.toArray(varargsCallables);
protected final T findCallableArityTwo(IRubyObject self, String name, IRubyObject arg0, IRubyObject arg1) { T callable = this.javaCallable; if ( callable == null ) { // TODO: varargs? final T[] callablesForArity; if ( javaCallables.length <= 2 || (callablesForArity = javaCallables[2]) == null ) { if ((callable = matchVarArgsCallableArityTwo(self, arg0, arg1)) == null ) { throw runtime.newArgumentError(2, javaCallables.length - 1); } return callable; } callable = CallableSelector.matchingCallableArityTwo(runtime, this, callablesForArity, arg0, arg1); if ( callable == null ) { if ((callable = matchVarArgsCallableArityTwo(self, arg0, arg1)) == null ) { throw newErrorDueArgumentTypeMismatch(self, callablesForArity, arg0, arg1); } } } else { if (!callable.isVarArgs()) checkCallableArity(callable, 2); } return callable; }
protected final T findCallableArityOne(IRubyObject self, String name, IRubyObject arg0) { T callable = this.javaCallable; if ( callable == null ) { // TODO: varargs? final T[] callablesForArity; if ( javaCallables.length <= 1 || (callablesForArity = javaCallables[1]) == null ) { if ((callable = matchVarArgsCallableArityOne(self, arg0)) == null) { throw runtime.newArgumentError(1, javaCallables.length - 1); } return callable; } callable = CallableSelector.matchingCallableArityOne(runtime, this, callablesForArity, arg0); if ( callable == null ) { if ((callable = matchVarArgsCallableArityOne(self, arg0)) == null ) { throw newErrorDueArgumentTypeMismatch(self, callablesForArity, arg0); } } } else { if (!callable.isVarArgs()) checkCallableArity(callable, 1); } return callable; }
protected final T findCallableArityThree(IRubyObject self, String name, IRubyObject arg0, IRubyObject arg1, IRubyObject arg2) { T callable = this.javaCallable; if ( callable == null ) { // TODO: varargs? final T[] callablesForArity; if ( javaCallables.length <= 3 || (callablesForArity = javaCallables[3]) == null ) { if ( ( callable = matchVarArgsCallableArityThree(self, arg0, arg1, arg2) ) == null ) { throw runtime.newArgumentError(3, javaCallables.length - 1); } return callable; } callable = CallableSelector.matchingCallableArityThree(runtime, this, callablesForArity, arg0, arg1, arg2); if ( callable == null ) { if ( ( callable = matchVarArgsCallableArityThree(self, arg0, arg1, arg2) ) == null ) { throw newErrorDueArgumentTypeMismatch(self, callablesForArity, arg0, arg1, arg2); } } } else { if (!callable.isVarArgs()) checkCallableArity(callable, 3); } return callable; }
protected final T findCallableArityFour(IRubyObject self, String name, IRubyObject arg0, IRubyObject arg1, IRubyObject arg2, IRubyObject arg3) { T callable = this.javaCallable; if ( callable == null ) { // TODO: varargs? final T[] callablesForArity; if ( javaCallables.length <= 4 || (callablesForArity = javaCallables[4]) == null ) { if ( ( callable = matchVarArgsCallableArityFour(self, arg0, arg1, arg2, arg3) ) == null ) { throw runtime.newArgumentError(4, javaCallables.length - 1); } return callable; } callable = CallableSelector.matchingCallableArityFour(runtime, this, callablesForArity, arg0, arg1, arg2, arg3); if ( callable == null ) { if ( ( callable = matchVarArgsCallableArityFour(self, arg0, arg1, arg2, arg3) ) == null ) { throw newErrorDueArgumentTypeMismatch(self, callablesForArity, arg0, arg1, arg2, arg3); } } } else { if (!callable.isVarArgs()) checkCallableArity(callable, 4); } return callable; }
private T findCallableArityN(IRubyObject self, String name, IRubyObject[] args, int arity) { T callable = this.javaCallable; if ( callable == null ) { final T[] callablesForArity; if ( arity >= javaCallables.length || (callablesForArity = javaCallables[arity]) == null ) { if ( ( callable = matchVarArgsCallableArityN(self, args) ) == null ) { throw runtime.newArgumentError(args.length, javaCallables.length - 1); } return callable; } callable = CallableSelector.matchingCallableArityN(runtime, this, callablesForArity, args); if ( callable == null ) { if ( ( callable = matchVarArgsCallableArityN(self, args) ) == null ) { throw newErrorDueArgumentTypeMismatch(self, callablesForArity, args); } } } else { if (!callable.isVarArgs()) checkCallableArity(callable, args.length); } return callable; }
private void setArity(final int minArity, final int maxArity, final int minVarArgsArity) { if ( minVarArgsArity == -1 ) { // no var-args if ( minArity == maxArity ) { setArity( Arity.fixed(minArity) ); } else { // multiple overloads setArity(Arity.required(minArity)); // but <= maxArity } } else { setArity( Arity.required(minVarArgsArity < minArity ? minVarArgsArity : minArity) ); } }
public static Object[] convertArguments(final ParameterTypes method, final IRubyObject[] args) { return convertArguments(method, args, 0); // 0 - no additional space }
setArity(Arity.OPTIONAL); callable = createCallable(runtime, members[0]); if (callable.isVarArgs()) { varargsCallables = createCallableArray(callable); int maxArity = 0; for (Member method: members) { int currentArity = getMemberParameterTypes(method).length; maxArity = Math.max(currentArity, maxArity); List<JavaCallable> methodsForArity = methodsMap.get(currentArity); JavaCallable javaMethod = createCallable(runtime,method); methodsForArity.add(javaMethod); if (isMemberVarArgs(method)) { varargsArity = Math.min(currentArity - 1, varargsArity); varargsMethods.add(javaMethod); callables = createCallableArrayArray(maxArity + 1); for (Map.Entry<Integer,List<JavaCallable>> entry : methodsMap.entrySet()) { List<JavaCallable> methodsForArity = (List<JavaCallable>)entry.getValue(); JavaCallable[] methodsArray = methodsForArity.toArray(createCallableArray(methodsForArity.size())); callables[((Integer)entry.getKey()).intValue()] = methodsArray; varargsCallables = createCallableArray(varargsMethods.size()); varargsMethods.toArray(varargsCallables);
protected final T findCallableArityZero(IRubyObject self, String name) { T callable = this.javaCallable; if ( callable == null ) { final T[] callablesForArity; if ( javaCallables.length == 0 || (callablesForArity = javaCallables[0]) == null ) { if ( ( callable = matchVarArgsCallableArityZero(self) ) == null ) { throw newErrorDueNoMatchingCallable(self, name); } return callable; } callable = CallableSelector.matchingCallableArityZero(runtime, this, callablesForArity); if ( callable == null ) { if ((callable = matchVarArgsCallableArityZero(self)) == null ) { throw newErrorDueArgumentTypeMismatch(self, callablesForArity); } } } else { if (!callable.isVarArgs()) checkCallableArity(callable, 0); } return callable; }
static <T extends AccessibleObject> T[] setAccessible(T[] accessibles) { // TODO: Replace flag that's false on 9 with proper module checks if (!allAreAccessible(accessibles) && !Ruby.isSecurityRestricted() && Options.JI_SETACCESSIBLE.load() && allAreMember(accessibles)) { try { AccessibleObject.setAccessible(accessibles, true); } catch (SecurityException e) {} catch (RuntimeException re) { rethrowIfNotInaccessibleObject(re); } } return accessibles; }
protected final T findCallableArityTwo(IRubyObject self, String name, IRubyObject arg0, IRubyObject arg1) { T callable = this.javaCallable; if ( callable == null ) { // TODO: varargs? final T[] callablesForArity; if ( javaCallables.length <= 2 || (callablesForArity = javaCallables[2]) == null ) { if ((callable = matchVarArgsCallableArityTwo(self, arg0, arg1)) == null ) { throw runtime.newArgumentError(2, javaCallables.length - 1); } return callable; } callable = CallableSelector.matchingCallableArityTwo(runtime, this, callablesForArity, arg0, arg1); if ( callable == null ) { if ((callable = matchVarArgsCallableArityTwo(self, arg0, arg1)) == null ) { throw newErrorDueArgumentTypeMismatch(self, callablesForArity, arg0, arg1); } } } else { if (!callable.isVarArgs()) checkCallableArity(callable, 2); } return callable; }
protected final T findCallableArityOne(IRubyObject self, String name, IRubyObject arg0) { T callable = this.javaCallable; if ( callable == null ) { // TODO: varargs? final T[] callablesForArity; if ( javaCallables.length <= 1 || (callablesForArity = javaCallables[1]) == null ) { if ((callable = matchVarArgsCallableArityOne(self, arg0)) == null) { throw runtime.newArgumentError(1, javaCallables.length - 1); } return callable; } callable = CallableSelector.matchingCallableArityOne(runtime, this, callablesForArity, arg0); if ( callable == null ) { if ((callable = matchVarArgsCallableArityOne(self, arg0)) == null ) { throw newErrorDueArgumentTypeMismatch(self, callablesForArity, arg0); } } } else { if (!callable.isVarArgs()) checkCallableArity(callable, 1); } return callable; }
protected final T findCallableArityThree(IRubyObject self, String name, IRubyObject arg0, IRubyObject arg1, IRubyObject arg2) { T callable = this.javaCallable; if ( callable == null ) { // TODO: varargs? final T[] callablesForArity; if ( javaCallables.length <= 3 || (callablesForArity = javaCallables[3]) == null ) { if ( ( callable = matchVarArgsCallableArityThree(self, arg0, arg1, arg2) ) == null ) { throw runtime.newArgumentError(3, javaCallables.length - 1); } return callable; } callable = CallableSelector.matchingCallableArityThree(runtime, this, callablesForArity, arg0, arg1, arg2); if ( callable == null ) { if ( ( callable = matchVarArgsCallableArityThree(self, arg0, arg1, arg2) ) == null ) { throw newErrorDueArgumentTypeMismatch(self, callablesForArity, arg0, arg1, arg2); } } } else { if (!callable.isVarArgs()) checkCallableArity(callable, 3); } return callable; }
protected final T findCallableArityFour(IRubyObject self, String name, IRubyObject arg0, IRubyObject arg1, IRubyObject arg2, IRubyObject arg3) { T callable = this.javaCallable; if ( callable == null ) { // TODO: varargs? final T[] callablesForArity; if ( javaCallables.length <= 4 || (callablesForArity = javaCallables[4]) == null ) { if ( ( callable = matchVarArgsCallableArityFour(self, arg0, arg1, arg2, arg3) ) == null ) { throw runtime.newArgumentError(4, javaCallables.length - 1); } return callable; } callable = CallableSelector.matchingCallableArityFour(runtime, this, callablesForArity, arg0, arg1, arg2, arg3); if ( callable == null ) { if ( ( callable = matchVarArgsCallableArityFour(self, arg0, arg1, arg2, arg3) ) == null ) { throw newErrorDueArgumentTypeMismatch(self, callablesForArity, arg0, arg1, arg2, arg3); } } } else { if (!callable.isVarArgs()) checkCallableArity(callable, 4); } return callable; }