Skip to content

Commit 535bc52

Browse files
committed
[Truffle] Have a try at implementing a not fully compliant Bignum#coerce.
* So it does not create Fixnum-range Bignums.
1 parent 05aad64 commit 535bc52

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
fails:Bignum#coerce coerces other to a Bignum and returns [other, self] when passed a Fixnum
2-
fails:Bignum#coerce raises a TypeError when passed a Float or String

truffle/src/main/java/org/jruby/truffle/nodes/core/BignumNodes.java

+41
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,47 @@ public Object abs(RubyBignum value) {
593593

594594
}
595595

596+
@CoreMethod(names = "coerce", required = 1)
597+
public abstract static class CoerceNode extends CoreMethodNode {
598+
599+
public CoerceNode(RubyContext context, SourceSection sourceSection) {
600+
super(context, sourceSection);
601+
}
602+
603+
public CoerceNode(CoerceNode prev) {
604+
super(prev);
605+
}
606+
607+
@Specialization
608+
public RubyArray coerce(RubyBignum a, int b) {
609+
notDesignedForCompilation();
610+
611+
// TODO (eregon, 16 Feb. 2015): This is NOT spec, but let's try to see if we can make it work.
612+
// b is converted to a Bignum here in other implementations.
613+
Object[] store = new Object[] { b, a };
614+
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), store, store.length);
615+
}
616+
617+
@Specialization
618+
public RubyArray coerce(RubyBignum a, long b) {
619+
notDesignedForCompilation();
620+
621+
// TODO (eregon, 16 Feb. 2015): This is NOT spec, but let's try to see if we can make it work.
622+
// b is converted to a Bignum here in other implementations.
623+
Object[] store = new Object[] { b, a };
624+
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), store, store.length);
625+
}
626+
627+
@Specialization
628+
public RubyArray coerce(RubyBignum a, RubyBignum b) {
629+
notDesignedForCompilation();
630+
631+
Object[] store = new Object[] { b, a };
632+
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), store, store.length);
633+
}
634+
635+
}
636+
596637
@CoreMethod(names = "divmod", required = 1)
597638
public abstract static class DivModNode extends CoreMethodNode {
598639

0 commit comments

Comments
 (0)