Skip to content

Commit

Permalink
Merge pull request jruby#8432 from enebo/argerr
Browse files Browse the repository at this point in the history
argumentError API conversions with a lot of runtime to tc replacement
  • Loading branch information
enebo authored Nov 19, 2024
2 parents fba073b + a4df1c5 commit 6c28127
Show file tree
Hide file tree
Showing 66 changed files with 1,308 additions and 1,611 deletions.
19 changes: 10 additions & 9 deletions core/src/main/java/org/jruby/FiberScheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.nio.ByteBuffer;

import static org.jruby.api.Convert.asFixnum;
import static org.jruby.api.Error.argumentError;

public class FiberScheduler {
// MRI: rb_fiber_scheduler_kernel_sleep
Expand Down Expand Up @@ -100,7 +101,7 @@ public static IRubyObject ioPWrite(ThreadContext context, IRubyObject scheduler,

// MRI: rb_fiber_scheduler_io_read_memory
public static IRubyObject ioReadMemory(ThreadContext context, IRubyObject scheduler, IRubyObject io, ByteBuffer base, int size, int length) {
RubyIOBuffer buffer = RubyIOBuffer.newBuffer(context.runtime, base, size, RubyIOBuffer.LOCKED);
RubyIOBuffer buffer = RubyIOBuffer.newBuffer(context, base, size, RubyIOBuffer.LOCKED);

IRubyObject result = ioRead(context, scheduler, io, buffer, length, 0);

Expand All @@ -112,7 +113,7 @@ public static IRubyObject ioReadMemory(ThreadContext context, IRubyObject schedu

// MRI: rb_fiber_scheduler_io_pread_memory
public static IRubyObject ioPReadMemory(ThreadContext context, IRubyObject scheduler, IRubyObject io, ByteBuffer base, int from, int size, int length) {
RubyIOBuffer buffer = RubyIOBuffer.newBuffer(context.runtime, base, size, RubyIOBuffer.LOCKED);
RubyIOBuffer buffer = RubyIOBuffer.newBuffer(context, base, size, RubyIOBuffer.LOCKED);

IRubyObject result = ioPRead(context, scheduler, io, buffer, from, length, 0);

Expand All @@ -124,7 +125,7 @@ public static IRubyObject ioPReadMemory(ThreadContext context, IRubyObject sched

// MRI: rb_fiber_scheduler_io_write_memory
public static IRubyObject ioWriteMemory(ThreadContext context, IRubyObject scheduler, IRubyObject io, ByteBuffer base, int size, int length) {
RubyIOBuffer buffer = RubyIOBuffer.newBuffer(context.runtime, base, size, RubyIOBuffer.LOCKED | RubyIOBuffer.READONLY);
RubyIOBuffer buffer = RubyIOBuffer.newBuffer(context, base, size, RubyIOBuffer.LOCKED | RubyIOBuffer.READONLY);

IRubyObject result = ioWrite(context, scheduler, io, buffer, length, 0);

Expand All @@ -136,7 +137,7 @@ public static IRubyObject ioWriteMemory(ThreadContext context, IRubyObject sched

// MRI: p
public static IRubyObject ioPWriteMemory(ThreadContext context, IRubyObject scheduler, IRubyObject io, ByteBuffer base, int from, int size, int length) {
RubyIOBuffer buffer = RubyIOBuffer.newBuffer(context.runtime, base, size, RubyIOBuffer.LOCKED | RubyIOBuffer.READONLY);
RubyIOBuffer buffer = RubyIOBuffer.newBuffer(context, base, size, RubyIOBuffer.LOCKED | RubyIOBuffer.READONLY);

IRubyObject result = ioPWrite(context, scheduler, io, buffer, from, length, 0);

Expand All @@ -157,21 +158,21 @@ public static IRubyObject addressResolve(ThreadContext context, IRubyObject sche
}

// MRI: verify_scheduler
static void verifyInterface(IRubyObject scheduler) {
static void verifyInterface(ThreadContext context, IRubyObject scheduler) {
if (!scheduler.respondsTo("block")) {
throw scheduler.getRuntime().newArgumentError("Scheduler must implement #block");
throw argumentError(context, "Scheduler must implement #block");
}

if (!scheduler.respondsTo("unblock")) {
throw scheduler.getRuntime().newArgumentError("Scheduler must implement #unblock");
throw argumentError(context, "Scheduler must implement #unblock");
}

if (!scheduler.respondsTo("kernel_sleep")) {
throw scheduler.getRuntime().newArgumentError("Scheduler must implement #kernel_sleep");
throw argumentError(context, "Scheduler must implement #kernel_sleep");
}

if (!scheduler.respondsTo("io_wait")) {
throw scheduler.getRuntime().newArgumentError("Scheduler must implement #io_wait");
throw argumentError(context, "Scheduler must implement #io_wait");
}
}

Expand Down
74 changes: 24 additions & 50 deletions core/src/main/java/org/jruby/RubyArithmeticSequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@

import static org.jruby.api.Convert.asFixnum;
import static org.jruby.api.Convert.numericToLong;
import static org.jruby.api.Create.newFixnum;
import static org.jruby.api.Error.argumentError;
import static org.jruby.runtime.Helpers.hashEnd;
import static org.jruby.runtime.Helpers.hashStart;
import static org.jruby.runtime.Helpers.murmurCombine;
Expand Down Expand Up @@ -186,42 +188,28 @@ public IRubyObject first(ThreadContext context, IRubyObject num) {
Ruby runtime = context.runtime;
IRubyObject b = begin, e = end, s = step;
RubyArray ary;
long n;
boolean x;

if (num == null) {
if (b.isNil()) {
return context.nil;
}
if (b.isNil()) return context.nil;
if (!e.isNil()) {
IRubyObject zero = int2fix(runtime, 0);
CallSite op_cmp = sites(context).op_cmp;
CallSite op_gt = sites(context).op_gt;
CallSite op_lt = sites(context).op_lt;
int r = RubyComparable.cmpint(context, ((RubyNumeric)step).coerceCmp(context, op_cmp, zero), s, zero);
if (r > 0 && RubyNumeric.numFuncall(context, b, op_gt, e).isTrue()) {
return context.nil;
}

if (r < 0 && RubyNumeric.numFuncall(context, b, op_lt, e).isTrue()) {
return context.nil;
}
if (r > 0 && RubyNumeric.numFuncall(context, b, op_gt, e).isTrue()) return context.nil;
if (r < 0 && RubyNumeric.numFuncall(context, b, op_lt, e).isTrue()) return context.nil;
}
return b;
}

/* TODO: the following code should be extracted as arith_seq_take */
n = numericToLong(context, num);
long n = numericToLong(context, num);

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

if (n == 0) {
return runtime.newEmptyArray();
}

x = excludeEnd.isTrue();
boolean x = excludeEnd.isTrue();

if (b instanceof RubyFixnum && e.isNil() && s instanceof RubyFixnum) {
long i = fix2long(b);
Expand Down Expand Up @@ -255,7 +243,7 @@ public IRubyObject first(ThreadContext context, IRubyObject num) {
if (len < 0) len = 0;
ary = RubyArray.newArray(runtime, (n < len) ? n : len);
while (n > 0 && i < end) {
ary.append(RubyFixnum.newFixnum(runtime, i));
ary.append(newFixnum(context, i));
if (i + unit < i) break;
i += unit;
--n;
Expand Down Expand Up @@ -465,53 +453,38 @@ public IRubyObject last(ThreadContext context) {
@JRubyMethod
public IRubyObject last(ThreadContext context, IRubyObject num) {
Ruby runtime = context.runtime;
IRubyObject b = begin, e = end, s = step, len_1, len, last, nv;
IRubyObject b = begin, e = end, s = step, len_1, len;
RubyArray ary;
boolean last_is_adjusted;
long n;

if (e.isNil()) {
throw runtime.newRangeError("cannot get the last element of endless arithmetic sequence");
}
if (e.isNil()) throw 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)) {
if (num == null) {
return context.nil;
}

return runtime.newEmptyArray();
}
if (Numeric.f_negative_p(context, len_1)) return num == null ? context.nil : runtime.newEmptyArray();

last = ((RubyNumeric)b).op_plus(context, Numeric.f_mul(context, s, len_1));
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()) {
last = ((RubyNumeric)last).op_minus(context, s);
}

if (num == null) {
return last;
}
if (num == null) return last;

if (last_is_adjusted) {
len = len_1;
} else {
len = ((RubyNumeric)len_1).op_plus(context, int2fix(runtime, 1));
}

nv = num;
if (!(nv instanceof RubyInteger)) {
nv = num.convertToInteger();
}
IRubyObject nv = num;
if (!(nv instanceof RubyInteger)) nv = num.convertToInteger();

CallSite op_gt = sites(context).op_gt;
if (RubyNumeric.numFuncall(context, nv, op_gt, len).isTrue()) {
nv = len;
}

n = numericToLong(context, nv);
if (n < 0) {
throw runtime.newArgumentError("negative array size");
}
long n = numericToLong(context, nv);
if (n < 0) throw argumentError(context, "negative array size");

ary = RubyArray.newArray(runtime, n);
b = ((RubyNumeric)last).op_minus(context, Numeric.f_mul(context, s, nv));
Expand Down Expand Up @@ -582,9 +555,10 @@ public IRubyObject size(ThreadContext context) {
}

/**
* A size method suitable for lambda method reference implementation of {@link SizeFn#size(ThreadContext, IRubyObject, IRubyObject[])}
* A size method suitable for lambda method reference implementation of
* {@link RubyEnumerator.SizeFn#size(ThreadContext, IRubyObject, IRubyObject[])}
*
* @see SizeFn#size(ThreadContext, IRubyObject, IRubyObject[])
* @see RubyEnumerator.SizeFn#size(ThreadContext, IRubyObject, IRubyObject[])
*/
private static IRubyObject size(ThreadContext context, RubyArithmeticSequence self, IRubyObject[] args) {
return self.size(context);
Expand All @@ -597,15 +571,15 @@ private static FiberSites fiberSites(ThreadContext context) {
@JRubyMethod(name = "each_cons")
public IRubyObject each_cons(ThreadContext context, IRubyObject arg, final Block block) {
int size = (int) numericToLong(context, arg);
if (size <= 0) throw context.runtime.newArgumentError("invalid size");
if (size <= 0) throw argumentError(context, "invalid size");
return block.isGiven() ? RubyEnumerable.each_consCommon(context, this, size, block) :
enumeratorize(context.runtime, this, "each_cons", arg);
}

@JRubyMethod(name = "each_slice")
public IRubyObject each_slice(ThreadContext context, IRubyObject arg, final Block block) {
int size = (int) numericToLong(context, arg);
if (size <= 0) throw context.runtime.newArgumentError("invalid size");
if (size <= 0) throw argumentError(context, "invalid size");

return block.isGiven() ? RubyEnumerable.each_sliceCommon(context, this, size, block) :
enumeratorizeWithSize(context, this, "each_slice", new IRubyObject[]{arg}, RubyArithmeticSequence::size);
Expand Down
Loading

0 comments on commit 6c28127

Please sign in to comment.