Class: Wardite::I32
- Inherits:
-
Object
- Object
- Wardite::I32
- Includes:
- ValueHelper
- Defined in:
- lib/wardite/value.rb
Constant Summary collapse
- I32_MAX =
(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
- #convert_s(to:) ⇒ Object
- #convert_u(to:) ⇒ Object
- #demote(to:) ⇒ Object
- #extend_s(to:) ⇒ Object
- #extend_u(to:) ⇒ Object
-
#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_u(to:) ⇒ Object
-
#value_s ⇒ Object
returns a value interpreted as signed integer.
- #wrap(to:) ⇒ Object
Methods included from ValueHelper
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
59 60 61 |
# File 'lib/wardite/value.rb', line 59 def value @value end |
Class Method Details
.from_bytes(str, size: nil, signed: false) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/wardite/value.rb', line 65 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
#convert_s(to:) ⇒ Object
143 144 145 146 147 148 149 150 151 152 |
# File 'lib/wardite/value.rb', line 143 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
156 157 158 159 160 161 162 163 164 165 |
# File 'lib/wardite/value.rb', line 156 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
169 170 171 172 |
# File 'lib/wardite/value.rb', line 169 def demote(to:) raise EvalError, "unsupported operation" end |
#extend_s(to:) ⇒ Object
117 118 119 120 |
# File 'lib/wardite/value.rb', line 117 def extend_s(to:) raise EvalError, "unsupported operation" if to != :i64 I64(value_s) end |
#extend_u(to:) ⇒ Object
124 125 126 127 |
# File 'lib/wardite/value.rb', line 124 def extend_u(to:) raise EvalError, "unsupported operation" if to != :i64 I64(value) end |
#inspect ⇒ Object
I32#inspect shows signed value for convinience
191 192 193 |
# File 'lib/wardite/value.rb', line 191 def inspect "I32(#{value_s})" end |
#memsize ⇒ Object
81 82 83 |
# File 'lib/wardite/value.rb', line 81 def memsize 32 end |
#packed(size: nil) ⇒ Object
TODO: eliminate use of pack, to support mruby - in this file!
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/wardite/value.rb', line 96 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
176 177 178 179 |
# File 'lib/wardite/value.rb', line 176 def promote(to:) raise EvalError, "unsupported operation" end |
#reinterpret(to:) ⇒ Object
183 184 185 186 187 188 |
# File 'lib/wardite/value.rb', line 183 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
131 132 133 |
# File 'lib/wardite/value.rb', line 131 def trunc_s(to:) raise EvalError, "unsupported operation" end |
#trunc_u(to:) ⇒ Object
137 138 139 |
# File 'lib/wardite/value.rb', line 137 def trunc_u(to:) raise EvalError, "unsupported operation" end |
#value_s ⇒ Object
returns a value interpreted as signed integer
87 88 89 90 91 |
# File 'lib/wardite/value.rb', line 87 def value_s (@value >> 31).zero? ? @value : ((-@value) ^ I32_MAX) + 1 end |
#wrap(to:) ⇒ Object
111 112 113 |
# File 'lib/wardite/value.rb', line 111 def wrap(to:) raise EvalError, "unsupported operation" end |