13514
|
1 /*
|
|
2 * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. This
|
|
3 * code is released under a tri EPL/GPL/LGPL license. You can use it,
|
|
4 * redistribute it and/or modify it under the terms of the:
|
|
5 *
|
|
6 * Eclipse Public License version 1.0
|
|
7 * GNU General Public License version 2
|
|
8 * GNU Lesser General Public License version 2.1
|
|
9 */
|
|
10 package com.oracle.truffle.ruby.runtime.core;
|
|
11
|
|
12 import java.math.*;
|
|
13
|
|
14 import com.oracle.truffle.api.*;
|
|
15 import com.oracle.truffle.ruby.runtime.*;
|
|
16
|
|
17 public class GeneralConversions {
|
|
18
|
|
19 /**
|
|
20 * Convert a value to a boolean, without doing any lookup.
|
|
21 */
|
|
22 public static boolean toBoolean(Object value) {
|
|
23 assert value != null;
|
|
24
|
|
25 if (value instanceof NilPlaceholder) {
|
|
26 return false;
|
|
27 }
|
|
28
|
|
29 if (value instanceof Boolean) {
|
|
30 return (boolean) value;
|
|
31 }
|
|
32
|
|
33 if (value instanceof RubyTrueClass) {
|
|
34 return true;
|
|
35 }
|
|
36
|
|
37 if (value instanceof RubyFalseClass) {
|
|
38 return false;
|
|
39 }
|
|
40
|
|
41 return true;
|
|
42 }
|
|
43
|
|
44 /**
|
|
45 * Convert a value to a {@code Fixnum}, without doing any lookup.
|
|
46 */
|
|
47 public static int toFixnum(Object value) {
|
|
48 assert value != null;
|
|
49
|
|
50 if (value instanceof NilPlaceholder || value instanceof RubyNilClass) {
|
|
51 return 0;
|
|
52 }
|
|
53
|
|
54 if (value instanceof Integer) {
|
|
55 return (int) value;
|
|
56 }
|
|
57
|
|
58 if (value instanceof RubyFixnum) {
|
|
59 return ((RubyFixnum) value).getValue();
|
|
60 }
|
|
61
|
|
62 if (value instanceof BigInteger) {
|
|
63 throw new UnsupportedOperationException();
|
|
64 }
|
|
65
|
|
66 if (value instanceof RubyBignum) {
|
|
67 throw new UnsupportedOperationException();
|
|
68 }
|
|
69
|
|
70 if (value instanceof Double) {
|
|
71 return (int) (double) value;
|
|
72 }
|
|
73
|
|
74 if (value instanceof RubyFloat) {
|
|
75 return (int) ((RubyFloat) value).getValue();
|
|
76 }
|
|
77
|
|
78 CompilerDirectives.transferToInterpreter();
|
|
79
|
|
80 throw new UnsupportedOperationException(value.getClass().toString());
|
|
81 }
|
|
82
|
|
83 /**
|
|
84 * Convert a value to a {@code Float}, without doing any lookup.
|
|
85 */
|
|
86 public static double toFloat(Object value) {
|
|
87 assert value != null;
|
|
88
|
|
89 if (value instanceof NilPlaceholder || value instanceof RubyNilClass) {
|
|
90 return 0;
|
|
91 }
|
|
92
|
|
93 if (value instanceof Integer) {
|
|
94 return (int) value;
|
|
95 }
|
|
96
|
|
97 if (value instanceof RubyFixnum) {
|
|
98 return ((RubyFixnum) value).getValue();
|
|
99 }
|
|
100
|
|
101 if (value instanceof BigInteger) {
|
|
102 return ((BigInteger) value).doubleValue();
|
|
103 }
|
|
104
|
|
105 if (value instanceof RubyBignum) {
|
|
106 return ((RubyBignum) value).getValue().doubleValue();
|
|
107 }
|
|
108
|
|
109 if (value instanceof Double) {
|
|
110 return (double) value;
|
|
111 }
|
|
112
|
|
113 if (value instanceof RubyFloat) {
|
|
114 return ((RubyFloat) value).getValue();
|
|
115 }
|
|
116
|
|
117 CompilerDirectives.transferToInterpreter();
|
|
118
|
|
119 throw new UnsupportedOperationException();
|
|
120 }
|
|
121
|
|
122 /**
|
|
123 * Given a {@link BigInteger} value, produce either a {@code Fixnum} or {@code Bignum} .
|
|
124 */
|
|
125 public static Object fixnumOrBignum(BigInteger value) {
|
|
126 assert value != null;
|
|
127
|
|
128 if (value.compareTo(RubyFixnum.MIN_VALUE_BIG) >= 0 && value.compareTo(RubyFixnum.MAX_VALUE_BIG) <= 0) {
|
|
129 return value.intValue();
|
|
130 } else {
|
|
131 return value;
|
|
132 }
|
|
133 }
|
|
134
|
|
135 /**
|
|
136 * Given a {@code long} value, produce either a {@code Fixnum} or {@code Bignum} .
|
|
137 */
|
|
138 public static Object fixnumOrBignum(long value) {
|
|
139 if (value >= RubyFixnum.MIN_VALUE && value <= RubyFixnum.MAX_VALUE) {
|
|
140 return (int) value;
|
|
141 } else {
|
|
142 return BigInteger.valueOf(value);
|
|
143 }
|
|
144 }
|
|
145
|
|
146 /**
|
|
147 * Given a reference, produce either {@code nil} or the object. .
|
|
148 */
|
|
149 public static Object instanceOrNil(Object object) {
|
|
150 if (object == null) {
|
|
151 return NilPlaceholder.INSTANCE;
|
|
152 } else {
|
|
153 return object;
|
|
154 }
|
|
155 }
|
|
156
|
|
157 }
|