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
32 changes: 26 additions & 6 deletions internal/vm/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -4496,14 +4496,14 @@ func (vm *VM) bootstrap() {
vm.cInteger.define(">>", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value {
return vm.integerShift(bigVal(self), args[0], true)
})
vm.cInteger.define("&", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value {
return object.NormInt(new(big.Int).And(bigVal(self), bigArg(args[0])))
vm.cInteger.define("&", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value {
return vm.integerBitOp(bigVal(self), args[0], "&", (*big.Int).And)
})
vm.cInteger.define("|", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value {
return object.NormInt(new(big.Int).Or(bigVal(self), bigArg(args[0])))
vm.cInteger.define("|", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value {
return vm.integerBitOp(bigVal(self), args[0], "|", (*big.Int).Or)
})
vm.cInteger.define("^", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value {
return object.NormInt(new(big.Int).Xor(bigVal(self), bigArg(args[0])))
vm.cInteger.define("^", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value {
return vm.integerBitOp(bigVal(self), args[0], "^", (*big.Int).Xor)
})
vm.cInteger.define("~", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value {
return object.NormInt(new(big.Int).Not(bigVal(self)))
Expand Down Expand Up @@ -7964,6 +7964,26 @@ func bigArg(v object.Value) *big.Int {
return nil
}

// integerBitOp applies a bitwise operator (&, |, ^) to an Integer receiver. An
// Integer or Bignum right operand is combined directly; any other operand except
// a Float runs Ruby's coerce protocol — rhs.coerce(self) yields a [x, y] pair and
// the operator is re-dispatched on it — while a Float (and anything without
// #coerce) raises a TypeError, matching MRI, which does not coerce a Float here.
func (vm *VM) integerBitOp(a *big.Int, arg object.Value, name string, op func(z, x, y *big.Int) *big.Int) object.Value {
if b, ok := object.BigOf(arg); ok {
return object.NormInt(op(new(big.Int), a, b))
}
if _, isFloat := arg.(object.Float); !isFloat && vm.respondsToDynamic(arg, "coerce") {
pair := vm.send(arg, "coerce", []object.Value{object.NormInt(a)}, nil)
if arr, ok := pair.(*object.Array); ok && len(arr.Elems) == 2 {
return vm.send(arr.Elems[0], name, []object.Value{arr.Elems[1]}, nil)
}
raise("TypeError", "coerce must return [x, y]")
}
raise("TypeError", "%s can't be coerced into Integer", vm.classOf(arg).name)
return nil
}

// shiftInt shifts a left by n bits (right by -n when n is negative), promoting
// to a Bignum as needed and demoting a result that fits back into an Integer.
func shiftInt(a *big.Int, n int64) object.Value {
Expand Down
42 changes: 42 additions & 0 deletions internal/vm/integer_bit_coerce_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) the go-embedded-ruby/ruby authors
//
// SPDX-License-Identifier: BSD-3-Clause

package vm

import "testing"

// TestIntegerBitwiseCoerce covers the coerce protocol of Integer#&, #| and #^:
// an Integer or Bignum operand is combined directly, any other operand that
// defines #coerce is coerced (rhs.coerce(self) -> [x, y], then the operator is
// re-dispatched on the pair), and a Float — or an operand without #coerce —
// raises a TypeError. Verified against ruby 4.0.6.
func TestIntegerBitwiseCoerce(t *testing.T) {
const c = `class C; def coerce(o); [o, 5]; end; end; `
cases := []struct{ src, want string }{
// Plain integer operands still work, promoting to Bignum as needed.
{`p 6 & 3`, `2`},
{`p 6 | 3`, `7`},
{`p 6 ^ 3`, `5`},
{`p (2**64) | 1`, `18446744073709551617`},
{`p (2**70) & (2**70 - 1)`, `0`},
// A non-Integer operand with #coerce is coerced, then re-dispatched.
{c + `p 6 | C.new`, `7`},
{c + `p 6 & C.new`, `4`},
{c + `p 3 ^ C.new`, `6`},
// A Float never runs the coerce protocol here; it is a TypeError.
{`begin; 3 | 3.4; rescue TypeError => e; p e.class; end`, `TypeError`},
{`begin; 3 & 3.4; rescue TypeError => e; p e.class; end`, `TypeError`},
// An operand without #coerce is a TypeError naming its class.
{`begin; 3 | Object.new; rescue TypeError => e; p e.message; end`, `"Object can't be coerced into Integer"`},
{`class D; end; begin; 3 ^ D.new; rescue TypeError => e; p e.message; end`, `"D can't be coerced into Integer"`},
{`begin; 3 & "x"; rescue TypeError => e; p e.class; end`, `TypeError`},
// A #coerce that does not return a two-element array is a TypeError.
{`class E; def coerce(o); [1]; end; end; begin; 3 | E.new; rescue TypeError => e; p e.message; end`, `"coerce must return [x, y]"`},
}
for _, tc := range cases {
if got := eval(t, tc.src); got != tc.want+"\n" {
t.Errorf("src=%q got=%q want=%q", tc.src, got, tc.want+"\n")
}
}
}