package org.jruby.runtime;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import org.jruby.Ruby;
import org.jruby.anno.JRubyMethod;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.ArraySupport;
public final class Arity implements Serializable {
private static final long serialVersionUID = 1L;
private static final Map<Integer, Arity> arities = new HashMap<>();
private final int value;
public final static Arity NO_ARGUMENTS = newArity(0);
public final static Arity ONE_ARGUMENT = newArity(1);
public final static Arity TWO_ARGUMENTS = newArity(2);
public final static Arity THREE_ARGUMENTS = newArity(3);
public final static Arity OPTIONAL = newArity(-1);
public final static Arity ONE_REQUIRED = newArity(-2);
public final static Arity TWO_REQUIRED = newArity(-3);
public final static Arity THREE_REQUIRED = newArity(-4);
public final static int UNLIMITED_ARGUMENTS = -1;
private Arity(int value) {
this.value = value;
}
private static Arity createArity(int required, int optional, boolean rest) {
return createArity((optional > 0 || rest) ? -(required + 1) : required);
}
public static Arity createArity(int value) {
switch (value) {
case -4:
return THREE_REQUIRED;
case -3:
return TWO_REQUIRED;
case -2:
return ONE_REQUIRED;
case -1:
return OPTIONAL;
case 0:
return NO_ARGUMENTS;
case 1:
return ONE_ARGUMENT;
case 2:
return TWO_ARGUMENTS;
case 3:
return THREE_ARGUMENTS;
}
return newArity(value);
}
public static Arity fromAnnotation(JRubyMethod anno) {
return createArity(anno.required(), anno.optional(), anno.rest());
}
public static Arity fromAnnotation(JRubyMethod anno, int required) {
return createArity(required, anno.optional(), anno.rest());
}
public static Arity fromAnnotation(JRubyMethod anno, Class[] parameterTypes, boolean isStatic) {
int required;
if (anno.optional() == 0 && !anno.rest() && anno.required() == 0) {
int i = parameterTypes.length;
if (isStatic) i--;
if (parameterTypes.length > 0) {
if (parameterTypes[0] == ThreadContext.class) i--;
if (parameterTypes[parameterTypes.length - 1] == Block.class) i--;
}
required = i;
} else {
required = anno.required();
}
return createArity(required, anno.optional(), anno.rest());
}
private static Arity newArity(int value) {
Arity result = arities.get(value);
if (result == null) {
synchronized (arities) {
result = arities.get(value);
if (result == null) {
arities.put(value, result = new Arity(value));
}
}
}
return result;
}
public static Arity fixed(int arity) {
assert arity >= 0;
return createArity(arity);
}
public static Arity optional() {
return OPTIONAL;
}
public static Arity required(int minimum) {
assert minimum >= 0;
return createArity(-(1 + minimum));
}
public static Arity noArguments() {
return NO_ARGUMENTS;
}
public static Arity singleArgument() {
return ONE_ARGUMENT;
}
public static Arity twoArguments() {
return TWO_ARGUMENTS;
}
public int getValue() {
return value;
}
public void checkArity(Ruby runtime, IRubyObject[] args) {
checkArity(runtime, args.length);
}
public void checkArity(Ruby runtime, int length) {
if (isFixed()) {
if (length != required()) {
throw runtime.newArgumentError(length, required());
}
} else {
if (length < required()) {
throw runtime.newArgumentError(length, required());
}
}
}
public boolean isFixed() {
return value >= 0;
}
public int required() {
return value < 0 ? -(1 + value) : value;
}
@Override
public boolean equals(Object other) {
return this == other;
}
@Override
public int hashCode() {
return value;
}
@Override
public String toString() {
return isFixed() ? "Fixed" + required() : "Opt";
}
public static int checkArgumentCount(Ruby runtime, IRubyObject[] args, int min, int max) {
return checkArgumentCount(runtime, args.length, min, max);
}
public static int checkArgumentCount(ThreadContext context, IRubyObject[] args, int min, int max) {
return checkArgumentCount(context, args.length, min, max);
}
public static int checkArgumentCount(ThreadContext context, String name, IRubyObject[] args, int min, int max) {
return checkArgumentCount(context.runtime, name, args.length, min, max);
}
public static int checkArgumentCount(Ruby runtime, String name, IRubyObject[] args, int min, int max) {
return checkArgumentCount(runtime, name, args.length, min, max);
}
public static int checkArgumentCount(Ruby runtime, int length, int min, int max) {
raiseArgumentError(runtime, length, min, max);
return length;
}
public static int checkArgumentCount(ThreadContext context, int length, int min, int max) {
raiseArgumentError(context, length, min, max);
return length;
}
public static int checkArgumentCount(Ruby runtime, int length, int min, int max, boolean hasKwargs) {
raiseArgumentError(runtime, length, min, max, hasKwargs);
return length;
}
public static int checkArgumentCount(Ruby runtime, String name, int length, int min, int max) {
raiseArgumentError(runtime, name, length, min, max);
return length;
}
public static int checkArgumentCount(Ruby runtime, String name, int length, int min, int max, boolean hasKwargs) {
raiseArgumentError(runtime, name, length, min, max, hasKwargs);
return length;
}
public static void raiseArgumentError(Ruby runtime, IRubyObject[] args, int min, int max) {
raiseArgumentError(runtime, args.length, min, max);
}
public static void raiseArgumentError(Ruby runtime, int length, int min, int max) {
if (length < min || (max > UNLIMITED_ARGUMENTS && length > max))
throw runtime.newArgumentError(length, min, max);
}
public static void raiseArgumentError(ThreadContext context, int length, int min, int max) {
raiseArgumentError(context.runtime, length, min, max);
}
public static void raiseArgumentError(Ruby runtime, int length, int min, int max, boolean hasKwargs) {
if (length < min) throw runtime.newArgumentError(length, min, max);
if (max > UNLIMITED_ARGUMENTS && length > max) {
if (hasKwargs && length == max + 1) {
return;
}
throw runtime.newArgumentError(length, min, max);
}
}
public static void raiseArgumentError(Ruby runtime, String name, int length, int min, int max) {
if (length < min || (max > UNLIMITED_ARGUMENTS && length > max))
throw runtime.newArgumentError(name, length, min, max);
}
public static void raiseArgumentError(Ruby runtime, String name, int length, int min, int max, boolean hasKwargs) {
if (length < min) throw runtime.newArgumentError(name, length, min, max);
if (max > UNLIMITED_ARGUMENTS && length > max) {
if (hasKwargs && length == max + 1) {
return;
}
throw runtime.newArgumentError(name, length, min, max);
}
}
public static IRubyObject[] scanArgs(Ruby runtime, IRubyObject[] args, int required, int optional) {
final int total = required + optional;
int len = checkArgumentCount(runtime, args, required, total);
args = ArraySupport.newCopy(args, total);
for (int i=len; i<total; i++) args[i] = runtime.getNil();
return args;
}
}