Class: Wardite::I32
- Inherits:
-
Object
- Object
- Wardite::I32
- Includes:
- ValueHelper
- Defined in:
- lib/wardite/value.rb
Constant Summary collapse
- I32_MAX =
: Integer
(1<<32) - 1
Instance Attribute Summary collapse
-
#value ⇒ Object
value should be stored as unsigned Integer, even in I32/I64 when we want to access signed value, it’d be done via #value_s.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #convert_s(to:) ⇒ Object
- #convert_u(to:) ⇒ Object
- #demote(to:) ⇒ Object
- #extend_s(to:) ⇒ Object
- #extend_u(to:) ⇒ Object
- #extendN_s(from:, to:) ⇒ Object
-
#initialize(value = 0) ⇒ I32
constructor
A new instance of I32.
-
#inspect ⇒ Object
I32#inspect shows signed value for convinience.
- #memsize ⇒ Object
-
#packed(size: nil) ⇒ Object
TODO: eliminate use of pack, to support mruby - in this file!.
- #promote(to:) ⇒ Object
- #reinterpret(to:) ⇒ Object
- #trunc_s(to:) ⇒ Object
- #trunc_sat_s(to:) ⇒ Object
- #trunc_sat_u(to:) ⇒ Object
- #trunc_u(to:) ⇒ Object
-
#value_s ⇒ Object
returns a value interpreted as signed integer.
- #wrap(to:) ⇒ Object
Methods included from ValueHelper
Constructor Details
#initialize(value = 0) ⇒ I32
Returns a new instance of I32.
44 45 46 |
# File 'lib/wardite/value.rb', line 44 def initialize(value=0) @value = value end |
Instance Attribute Details
#value ⇒ Object
value should be stored as unsigned Integer, even in I32/I64 when we want to access signed value, it’d be done via #value_s
41 42 43 |
# File 'lib/wardite/value.rb', line 41 def value @value end |
Class Method Details
.from_bytes(str, size: nil, signed: false) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/wardite/value.rb', line 52 def self.from_bytes(str, size: nil, signed: false) v = case size when nil str.unpack("I!")[0] when 8 signed ? str.unpack("c")[0] : str.unpack("C")[0] when 16 signed ? str.unpack("s!")[0] : str.unpack("S!")[0] end if !v.is_a?(Integer) raise "broken string or unsupported size: #{str.inspect} -> #{size}" end Wardite::I32(v) end |
Instance Method Details
#==(other) ⇒ Object
225 226 227 |
# File 'lib/wardite/value.rb', line 225 def ==(other) return self.class == other.class && self.value == other.value end |
#convert_s(to:) ⇒ Object
130 131 132 133 134 135 136 137 138 139 |
# File 'lib/wardite/value.rb', line 130 def convert_s(to:) case to when :f32 F32(value_s.to_f) when :f64 F64(value_s.to_f) else raise EvalError, "unsupported operation" end end |
#convert_u(to:) ⇒ Object
143 144 145 146 147 148 149 150 151 152 |
# File 'lib/wardite/value.rb', line 143 def convert_u(to:) case to when :f32 F32(value.to_f) when :f64 F64(value.to_f) else raise EvalError, "unsupported operation" end end |
#demote(to:) ⇒ Object
156 157 158 159 |
# File 'lib/wardite/value.rb', line 156 def demote(to:) raise EvalError, "unsupported operation" end |
#extend_s(to:) ⇒ Object
104 105 106 107 |
# File 'lib/wardite/value.rb', line 104 def extend_s(to:) raise EvalError, "unsupported operation" if to != :i64 I64(value_s) end |
#extend_u(to:) ⇒ Object
111 112 113 114 |
# File 'lib/wardite/value.rb', line 111 def extend_u(to:) raise EvalError, "unsupported operation" if to != :i64 I64(value) end |
#extendN_s(from:, to:) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/wardite/value.rb', line 180 def extendN_s(from:, to:) src_value = case from when :i8 base = value & 0xff (base >> 8).zero? ? base : ((-base) ^ 0xff) + 1 when :i16 base = value & 0xffff (base >> 15).zero? ? base : ((-base) ^ 0xffff) + 1 when :i32 value_s else raise EvalError, "unsupported value size" end case to when :i32 I32(src_value) when :i64 I64(src_value) else raise EvalError, "unsupported value size" end end |
#inspect ⇒ Object
I32#inspect shows signed value for convinience
221 222 223 |
# File 'lib/wardite/value.rb', line 221 def inspect "I32(#{value_s})" end |
#memsize ⇒ Object
68 69 70 |
# File 'lib/wardite/value.rb', line 68 def memsize 32 end |
#packed(size: nil) ⇒ Object
TODO: eliminate use of pack, to support mruby - in this file!
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/wardite/value.rb', line 83 def packed(size: nil) case size when nil [self.value].pack("I!") when 8 [self.value & 0xff].pack("C") when 16 [self.value & 0xffff].pack("S!") else raise EvalError, "unsupported size #{size}" end end |
#promote(to:) ⇒ Object
163 164 165 166 |
# File 'lib/wardite/value.rb', line 163 def promote(to:) raise EvalError, "unsupported operation" end |
#reinterpret(to:) ⇒ Object
170 171 172 173 174 175 |
# File 'lib/wardite/value.rb', line 170 def reinterpret(to:) raise EvalError, "unsupported operation" if to != :f32 v = [value].pack("I!").unpack("f")[0] raise EvalError, "[BUG] String#unpack is broke, really?" if !v.is_a?(Float) F32(v) end |
#trunc_s(to:) ⇒ Object
118 119 120 |
# File 'lib/wardite/value.rb', line 118 def trunc_s(to:) raise EvalError, "unsupported operation" end |
#trunc_sat_s(to:) ⇒ Object
216 217 218 |
# File 'lib/wardite/value.rb', line 216 def trunc_sat_s(to:) raise EvalError, "unsupported operation" end |
#trunc_sat_u(to:) ⇒ Object
210 211 212 |
# File 'lib/wardite/value.rb', line 210 def trunc_sat_u(to:) raise EvalError, "unsupported operation" end |
#trunc_u(to:) ⇒ Object
124 125 126 |
# File 'lib/wardite/value.rb', line 124 def trunc_u(to:) raise EvalError, "unsupported operation" end |
#value_s ⇒ Object
returns a value interpreted as signed integer
74 75 76 77 78 |
# File 'lib/wardite/value.rb', line 74 def value_s (@value >> 31).zero? ? @value : ((-@value) ^ I32_MAX) + 1 end |
#wrap(to:) ⇒ Object
98 99 100 |
# File 'lib/wardite/value.rb', line 98 def wrap(to:) raise EvalError, "unsupported operation" end |