Skip to content

Commit

Permalink
Merge pull request jruby#8444 from enebo/api4
Browse files Browse the repository at this point in the history
Nearly all newArray is converted to API
  • Loading branch information
enebo authored Nov 22, 2024
2 parents 274cedd + c10467b commit dae3f9c
Show file tree
Hide file tree
Showing 96 changed files with 1,220 additions and 1,463 deletions.
9 changes: 4 additions & 5 deletions core/src/main/java/org/jruby/AbstractRubyMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@

import static org.jruby.api.Convert.asBoolean;
import static org.jruby.api.Convert.asFixnum;
import static org.jruby.api.Create.newArray;

/**
* @see RubyMethod
Expand Down Expand Up @@ -125,11 +126,9 @@ public IRubyObject owner(ThreadContext context) {
public IRubyObject source_location(ThreadContext context) {
String filename = getFilename();

if (filename != null) {
return context.runtime.newArray(Convert.asString(context, filename), asFixnum(context, getLine()));
}

return context.nil;
return filename != null ?
newArray(context, Convert.asString(context, filename), asFixnum(context, getLine())) :
context.nil;
}

public String getFilename() {
Expand Down
12 changes: 11 additions & 1 deletion core/src/main/java/org/jruby/Ruby.java
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ private Ruby(RubyInstanceConfig config) {

if (profile.allowClass("Array")) {
arrayClass = RubyArray.createArrayClass(this);
emptyFrozenArray = newEmptyArray();
emptyFrozenArray = Create.newEmptyArray(getCurrentContext());
emptyFrozenArray.setFrozen(true);
} else {
arrayClass = null;
Expand Down Expand Up @@ -3395,42 +3395,52 @@ public ProfilingService getProfilingService() {

// new factory methods ------------------------------------------------------------------------

@Deprecated(since = "10.0")
public RubyArray newEmptyArray() {
return RubyArray.newEmptyArray(this);
}

@Deprecated(since = "10.0")
public RubyArray newArray() {
return RubyArray.newArray(this);
}

@Deprecated(since = "10.0")
public RubyArray newArrayLight() {
return RubyArray.newArrayLight(this);
}

@Deprecated(since = "10.0")
public RubyArray newArray(IRubyObject object) {
return RubyArray.newArray(this, object);
}

@Deprecated(since = "10.0")
public RubyArray newArray(IRubyObject car, IRubyObject cdr) {
return RubyArray.newArray(this, car, cdr);
}

@Deprecated(since = "10.0")
public RubyArray newArray(IRubyObject... objects) {
return RubyArray.newArray(this, objects);
}

@Deprecated(since = "10.0")
public RubyArray newArrayNoCopy(IRubyObject... objects) {
return RubyArray.newArrayNoCopy(this, objects);
}

@Deprecated(since = "10.0")
public RubyArray newArrayNoCopyLight(IRubyObject... objects) {
return RubyArray.newArrayNoCopyLight(this, objects);
}

@Deprecated(since = "10.0")
public RubyArray newArray(List<IRubyObject> list) {
return RubyArray.newArray(this, list);
}

@Deprecated(since = "10.0")
public RubyArray newArray(int size) {
return RubyArray.newArray(this, size);
}
Expand Down
31 changes: 12 additions & 19 deletions core/src/main/java/org/jruby/RubyArgsFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import static org.jruby.RubyEnumerator.enumeratorize;
import static org.jruby.anno.FrameField.LASTLINE;
import static org.jruby.api.Convert.*;
import static org.jruby.api.Create.newString;
import static org.jruby.api.Create.*;
import static org.jruby.api.Error.argumentError;
import static org.jruby.runtime.ThreadContext.CALL_KEYWORD;
import static org.jruby.runtime.ThreadContext.resetCallInfo;
Expand All @@ -53,8 +53,6 @@
import org.jruby.anno.JRubyMethod;
import org.jruby.exceptions.RaiseException;
import org.jruby.internal.runtime.GlobalVariable;
import org.jruby.internal.runtime.ReadonlyAccessor;
import org.jruby.internal.runtime.ValueAccessor;
import org.jruby.runtime.Arity;
import org.jruby.runtime.Block;
import org.jruby.runtime.CallSite;
Expand All @@ -79,7 +77,7 @@ public static void initArgsFile(final Ruby runtime) {

argfClass.defineAnnotatedMethods(RubyArgsFile.class);

IRubyObject argsFile = argfClass.newInstance(runtime.getCurrentContext(), new IRubyObject[] { null }, (Block) null);
IRubyObject argsFile = argfClass.newInstance(runtime.getCurrentContext(), new IRubyObject[] { null }, null);

runtime.setArgsFile(argsFile);
runtime.getGlobalVariables().defineReadonly("$<", new ArgsFileAccessor(runtime), GlobalVariable.Scope.GLOBAL);
Expand All @@ -104,13 +102,10 @@ public IRubyObject setValue(IRubyObject newValue) {

@JRubyMethod(name = "initialize", visibility = PRIVATE, rest = true)
public IRubyObject initialize(ThreadContext context, IRubyObject[] args) {
final Ruby runtime = context.runtime;
final RubyArray argv;
if (args.length == 1 && args[0] == null) {
argv = runtime.getObject().getConstant("ARGV").convertToArray();
} else {
argv = runtime.newArray(args);
}
var runtime = context.runtime;
final var argv = args.length == 1 && args[0] == null ?
runtime.getObject().getConstant("ARGV").convertToArray() :
newArray(context, args);

// ARGF is intended to be a singleton from a Ruby perspective but it is still
// possible for someone to ARGF.class.new. We do still want a global view of
Expand Down Expand Up @@ -437,14 +432,13 @@ public static IRubyObject readline(ThreadContext context, IRubyObject recv, IRub
public static IRubyObject readlines(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
Arity.checkArgumentCount(context, args, 0, 1);

Ruby runtime = context.runtime;
ArgsFileData data = ArgsFileData.getArgsFileData(runtime);
ArgsFileData data = ArgsFileData.getArgsFileData(context.runtime);

if (!data.next_argv(context)) return runtime.newEmptyArray();
if (!data.next_argv(context)) return newEmptyArray(context);

if (!(data.currentFile instanceof RubyIO)) return data.currentFile.callMethod(context, "readlines", args);

var ary = runtime.newArray();
var ary = newArray(context);
IRubyObject line;
while(!(line = argf_getline(context, recv, args)).isNil()) {
ary.append(context, line);
Expand All @@ -456,13 +450,12 @@ public static IRubyObject readlines(ThreadContext context, IRubyObject recv, IRu
public static IRubyObject to_a(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
Arity.checkArgumentCount(context, args, 0, 1);

Ruby runtime = context.runtime;
ArgsFileData data = ArgsFileData.getArgsFileData(runtime);
ArgsFileData data = ArgsFileData.getArgsFileData(context.runtime);

if (!data.next_argv(context)) return runtime.newEmptyArray();
if (!data.next_argv(context)) return newEmptyArray(context);
if (!(data.currentFile instanceof RubyIO)) return data.currentFile.callMethod(context, "to_a", args);

var ary = runtime.newArray();
var ary = newArray(context);
IRubyObject line;
while ((line = argf_getline(context, recv, args)) != context.nil) {
ary.append(context, line);
Expand Down
31 changes: 9 additions & 22 deletions core/src/main/java/org/jruby/RubyArithmeticSequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public IRubyObject first(ThreadContext context, IRubyObject num) {
long n = numericToLong(context, num);

if (n < 0) throw argumentError(context, "attempt to take negative size");
if (n == 0) return context.runtime.newEmptyArray();
if (n == 0) return newEmptyArray(context);

boolean x = excludeEnd.isTrue();

Expand Down Expand Up @@ -275,12 +275,9 @@ public IRubyObject first(ThreadContext context, IRubyObject num) {
if (n > len) n = (long)len;

if (Double.isInfinite(unit)) {
if (len > 0) {
ary = newArray(context, 1);
ary.append(context, newFloat(context, beg));
} else {
ary = context.runtime.newEmptyArray();
}
ary = len > 0 ?
newArray(context, 1).append(context, newFloat(context, beg)) :
newEmptyArray(context);
} else if (unit == 0) {
IRubyObject val = newFloat(context, beg);
ary = newArray(context, n);
Expand Down Expand Up @@ -453,13 +450,12 @@ public IRubyObject last(ThreadContext context) {
@JRubyMethod
public IRubyObject last(ThreadContext context, IRubyObject num) {
IRubyObject b = begin, e = end, s = step, len_1, len;
RubyArray ary;
boolean last_is_adjusted;

if (e.isNil()) throw context.runtime.newRangeError("cannot get the last element of endless arithmetic sequence");

len_1 = ((RubyNumeric)((RubyNumeric)e).op_minus(context, b)).idiv(context, s);
if (Numeric.f_negative_p(context, len_1)) return num == null ? context.nil : context.runtime.newEmptyArray();
if (Numeric.f_negative_p(context, len_1)) return num == null ? context.nil : newEmptyArray(context);

IRubyObject last = ((RubyNumeric)b).op_plus(context, Numeric.f_mul(context, s, len_1));
if ((last_is_adjusted = excludeEnd.isTrue()) && Helpers.rbEqual(context, last, e).isTrue()) {
Expand All @@ -485,7 +481,7 @@ public IRubyObject last(ThreadContext context, IRubyObject num) {
long n = numericToLong(context, nv);
if (n < 0) throw argumentError(context, "negative array size");

ary = newArray(context, n);
var ary = newArray(context, n);
b = ((RubyNumeric)last).op_minus(context, Numeric.f_mul(context, s, nv));
while (n > 0) {
b = ((RubyNumeric)b).op_plus(context, s);
Expand All @@ -500,28 +496,19 @@ public IRubyObject last(ThreadContext context, IRubyObject num) {
public IRubyObject size(ThreadContext context) {
Ruby runtime = context.runtime;
IRubyObject len_1, len, last;
int x;

if (begin instanceof RubyFloat || end instanceof RubyFloat || step instanceof RubyFloat) {
double ee, n;

if (end.isNil()) {
if (Numeric.f_negative_p(context, step)) {
ee = Double.NEGATIVE_INFINITY;
} else {
ee = Double.POSITIVE_INFINITY;
}
ee = Numeric.f_negative_p(context, step) ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY;
} else {
ee = num2dbl(end);
}

n = floatStepSize(num2dbl(begin), ee, num2dbl(step), excludeEnd.isTrue());
if (Double.isInfinite(n)) {
return dbl2num(runtime, n);
}
if (RubyNumeric.posFixable(n)) {
return int2fix(runtime, (long)n);
}
if (Double.isInfinite(n)) return dbl2num(runtime, n);
if (RubyNumeric.posFixable(n)) return int2fix(runtime, (long)n);

return RubyBignum.newBignorm(runtime, n);
}
Expand Down
Loading

0 comments on commit dae3f9c

Please sign in to comment.