Class: Wardite::I32

Inherits:
Object
  • Object
show all
Includes:
ValueHelper
Defined in:
lib/wardite/value.rb

Constant Summary collapse

I32_MAX =

: Integer

(1<<32) - 1
@@i32_object_pool =

@rbs!

@@i32_object_pool: Hash[Integer, I32]
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ValueHelper

#F32, #F64, #I32, #I64

Constructor Details

#initialize(value = 0) ⇒ I32



54
55
56
# File 'lib/wardite/value.rb', line 54

def initialize(value=0)
  @value = value
end

Instance Attribute Details

#valueObject

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

.cached_or_initialize(value) ⇒ Object



49
50
51
# File 'lib/wardite/value.rb', line 49

def self.cached_or_initialize(value)
  @@i32_object_pool[value] || I32.new(value)
end

.from_bytes(str, size: nil, signed: false) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/wardite/value.rb', line 62

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



235
236
237
# File 'lib/wardite/value.rb', line 235

def ==(other)
  return self.class == other.class && self.value == other.value
end

#convert_s(to:) ⇒ Object



140
141
142
143
144
145
146
147
148
149
# File 'lib/wardite/value.rb', line 140

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



153
154
155
156
157
158
159
160
161
162
# File 'lib/wardite/value.rb', line 153

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

Raises:



166
167
168
169
# File 'lib/wardite/value.rb', line 166

def demote(to:)
  raise EvalError, "unsupported operation"

end

#extend_s(to:) ⇒ Object

Raises:



114
115
116
117
# File 'lib/wardite/value.rb', line 114

def extend_s(to:)
  raise EvalError, "unsupported operation" if to != :i64
  I64(value_s)
end

#extend_u(to:) ⇒ Object

Raises:



121
122
123
124
# File 'lib/wardite/value.rb', line 121

def extend_u(to:)
  raise EvalError, "unsupported operation" if to != :i64
  I64(value)
end

#extendN_s(from:, to:) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/wardite/value.rb', line 190

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

#inspectObject

I32#inspect shows signed value for convinience



231
232
233
# File 'lib/wardite/value.rb', line 231

def inspect
  "I32(#{value_s})"
end

#memsizeObject



78
79
80
# File 'lib/wardite/value.rb', line 78

def memsize
  32
end

#packed(size: nil) ⇒ Object

TODO: eliminate use of pack, to support mruby - in this file!



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/wardite/value.rb', line 93

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

Raises:



173
174
175
176
# File 'lib/wardite/value.rb', line 173

def promote(to:)
  raise EvalError, "unsupported operation"

end

#reinterpret(to:) ⇒ Object

Raises:



180
181
182
183
184
185
# File 'lib/wardite/value.rb', line 180

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

Raises:



128
129
130
# File 'lib/wardite/value.rb', line 128

def trunc_s(to:)
  raise EvalError, "unsupported operation"
end

#trunc_sat_s(to:) ⇒ Object

Raises:



226
227
228
# File 'lib/wardite/value.rb', line 226

def trunc_sat_s(to:)
  raise EvalError, "unsupported operation"
end

#trunc_sat_u(to:) ⇒ Object

Raises:



220
221
222
# File 'lib/wardite/value.rb', line 220

def trunc_sat_u(to:)
  raise EvalError, "unsupported operation"
end

#trunc_u(to:) ⇒ Object

Raises:



134
135
136
# File 'lib/wardite/value.rb', line 134

def trunc_u(to:)
  raise EvalError, "unsupported operation"
end

#value_sObject

returns a value interpreted as signed integer



84
85
86
87
88
# File 'lib/wardite/value.rb', line 84

def value_s
  (@value >> 31).zero? ?
    @value :
    ((-@value) ^ I32_MAX) + 1
end

#wrap(to:) ⇒ Object

Raises:



108
109
110
# File 'lib/wardite/value.rb', line 108

def wrap(to:)
  raise EvalError, "unsupported operation"
end