Skip to content
17 changes: 13 additions & 4 deletions internal/vm/arith.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,11 @@ func binary(op bytecode.Op, a, b object.Value) object.Value {
return matrixOp(op, a, b)
}

// Time arithmetic: t + secs / t - secs (shift by a Duration) and t - other
// (the seconds between two instants) reach the operator fast path.
if at, ok := a.(*Time); ok {
return timeOp(op, at, b)
// Time reaches the VM-less path only for an operator other than + / - (those
// are intercepted in binaryOp, which has the VM needed for MRI's num_exact
// coercion). Every other arithmetic operator on a Time is undefined.
if _, ok := a.(*Time); ok {
return raise("NoMethodError", "undefined method '%s' for an instance of Time", arithOpName(op))
}

// Date arithmetic: d + n / d - n (shift by a whole number of days) and
Expand Down Expand Up @@ -399,6 +400,14 @@ func (vm *VM) binaryOp(op bytecode.Op, a, b object.Value) object.Value {
if _, isSB := a.(*SafeBufferVal); isSB {
return vm.send(a, arithOpName(op), []object.Value{b}, nil)
}
// Time arithmetic (t + n / t - n, and t - other) needs a live VM: MRI's
// num_exact coercion of the shift amount (Integer/Rational kept exact, a
// Float taken through its exact #to_r, any other object via #to_r, while a
// String or nil is rejected) and the nanosecond-exact result both require
// dispatch the VM-less binary path cannot do.
if at, ok := a.(*Time); ok && (op == bytecode.OpAdd || op == bytecode.OpSub) {
return vm.timeArith(op, at, b)
}
// Numeric coercion protocol: when a built-in number is combined with a
// non-numeric object that answers #coerce, MRI calls other.coerce(self)
// — expecting a [a2, b2] pair of same-type numbers — then re-runs the
Expand Down
8 changes: 7 additions & 1 deletion internal/vm/file_ops_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ func TestFileMetaOps(t *testing.T) {
cases := []struct{ src, want string }{
// chmod returns the number of paths; multiple paths sum.
{`p File.chmod(0o600, "` + f + `")`, "1\n"},
// utime returns the count; accepts a Time and an Integer second count.
// utime returns the count; accepts a Time, and Integer/Float/Rational
// second counts (each coerced to whole Unix seconds).
{`p File.utime(Time.now, Time.now, "` + f + `")`, "1\n"},
{`p File.utime(0, 0, "` + f + `")`, "1\n"},
{`p File.utime(Rational(3, 2), 2.0, "` + f + `")`, "1\n"},
// chown with nil ids leaves them unchanged and still reports the count.
{`p File.chown(nil, nil, "` + f + `")`, "1\n"},
{`p File.lchown(nil, nil, "` + f + `")`, "1\n"},
Expand All @@ -47,6 +49,10 @@ func TestFileMetaOps(t *testing.T) {
t.Errorf("src=%q got=%q want=%q", c.src, got, c.want)
}
}
// A non-numeric utime second count is a TypeError (numFloat rejects it).
if got := runFSErr(t, `File.utime(:bad, 0, "`+f+`")`); got != "TypeError" {
t.Errorf("utime(:bad): got %q want TypeError", got)
}
}

// TestFileMetaOpsErrors drives the chmod/chown/lchown/utime error branches via
Expand Down
Loading