Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/about/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ priorities and future plans.
- Restore Perl smartmatch dispatch for arrays, hashes, regexes, predicates,
tied hashes, overloaded objects, and both execution backends.

- Preserve Perl's divisor-sign modulus semantics in dynamically compiled
methods under `no overloading`.

- Restore file-test error, stat-cache, glob-reference, and `tell` bareword
behavior while preserving `${^LAST_FH}` for ordinary scalar arguments.

Expand Down
17 changes: 14 additions & 3 deletions src/main/java/org/perlonjava/runtime/operators/MathOperators.java
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,15 @@ public static RuntimeScalar integerModulus(RuntimeScalar arg1, RuntimeScalar arg
return new RuntimeScalar(result);
}

/** Integer modulus with Perl's divisor-sign result rule. */
private static RuntimeScalar modulusFromLongs(long dividend, long divisor) {
long result = dividend % divisor;
if (result != 0 && ((divisor > 0 && result < 0) || (divisor < 0 && result > 0))) {
result += divisor;
}
return getScalarInt(result);
}

/** Modulus when at least one operand is already a DOUBLE (see {@link #modulus}). */
private static RuntimeScalar modulusFromDoubles(double dividend, double divisor) {
if (divisor == 0.0) {
Expand Down Expand Up @@ -1686,8 +1695,10 @@ private static RuntimeScalar arith(RuntimeScalar a, RuntimeScalar b, int op) {
case 3 -> y != 0 && x % y == 0
? getScalarInt(x / y)
: new RuntimeScalar((double) x / (double) y);
case 4 -> y != 0 ? getScalarInt(x % y)
: new RuntimeScalar((double) x % (double) y);
// Perl's modulus result has the divisor's sign. Do not use
// Java's raw remainder here: this no-overload path is used by
// dynamically compiled methods under `no overloading` too.
case 4 -> modulusFromLongs(x, y);
case 5 -> new RuntimeScalar(Math.pow(x, y));
default -> throw new IllegalStateException();
};
Expand All @@ -1697,7 +1708,7 @@ private static RuntimeScalar arith(RuntimeScalar a, RuntimeScalar b, int op) {
case 1 -> integerResult(BigInteger.valueOf(x).subtract(BigInteger.valueOf(y)));
case 2 -> integerResult(BigInteger.valueOf(x).multiply(BigInteger.valueOf(y)));
case 3 -> new RuntimeScalar((double) x / (double) y);
case 4 -> new RuntimeScalar((double) x % (double) y);
case 4 -> modulusFromDoubles((double) x, (double) y);
case 5 -> new RuntimeScalar(Math.pow((double) x, (double) y));
default -> throw new IllegalStateException();
};
Expand Down
45 changes: 45 additions & 0 deletions src/test/resources/unit/issue_1311_autoload_modulus.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use strict;
use warnings;
use Test::More tests => 2;

{
package Issue1311AutoLoader;

sub install_autoloaded_method {
my $code = q{
sub local_day_of_week {
my $self = shift;
return 1 + (($self->day_of_week - $self->{locale}->first_day_of_week) % 7);
}
};
no overloading;
eval $code;
die $@ if $@;
}

sub new {
my ($class, $day_of_week, $first_day_of_week) = @_;
return bless {
day_of_week => $day_of_week,
locale => bless({ first_day_of_week => $first_day_of_week }, 'Issue1311AutoLoader::Locale'),
}, $class;
}

sub day_of_week {
return $_[0]->{day_of_week};
}

package Issue1311AutoLoader::Locale;

sub first_day_of_week {
return $_[0]->{first_day_of_week};
}
}

my $date = Issue1311AutoLoader->new(2, 7);
Issue1311AutoLoader::install_autoloaded_method();

is($date->day_of_week - $date->{locale}->first_day_of_week, -5,
'fixture has a negative weekday offset');
is($date->local_day_of_week, 3,
'method compiled by AUTOLOAD under no overloading uses Perl modulus semantics');
Loading