Class: Wardite::I64

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

Constant Summary collapse

I64_MAX =

: Integer

(1<<64) - 1

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) ⇒ I64

Returns a new instance of I64.



254
255
256
# File 'lib/wardite/value.rb', line 254

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

Instance Attribute Details

#valueObject

: Integer



251
252
253
# File 'lib/wardite/value.rb', line 251

def value
  @value
end

Class Method Details

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



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/wardite/value.rb', line 262

def self.from_bytes(str, size: nil, signed: false)
  v = case size
    when nil
      str.unpack("L!")[0]
    when 8
      signed ? str.unpack("c")[0] : str.unpack("C")[0]
    when 16
      signed ? str.unpack("s!")[0] : str.unpack("S!")[0]
    when 32
      signed ? str.unpack("i!")[0] : str.unpack("I!")[0]
    end
  if !v.is_a?(Integer)
    raise "broken string or unsupported size: #{str.inspect} -> #{size}"
  end
  Wardite::I64(v)
end

Instance Method Details

#==(other) ⇒ Object



413
414
415
# File 'lib/wardite/value.rb', line 413

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

#convert_s(to:) ⇒ Object



344
345
346
347
348
349
350
351
352
353
# File 'lib/wardite/value.rb', line 344

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



357
358
359
360
361
362
363
364
365
366
# File 'lib/wardite/value.rb', line 357

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:



370
371
372
# File 'lib/wardite/value.rb', line 370

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

#extend_s(to:) ⇒ Object

Raises:



320
321
322
# File 'lib/wardite/value.rb', line 320

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

#extend_u(to:) ⇒ Object

Raises:



326
327
328
# File 'lib/wardite/value.rb', line 326

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

#extendN_s(from:, to:) ⇒ Object

Raises:



392
393
394
# File 'lib/wardite/value.rb', line 392

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

#inspectObject

I64#inspect shows signed value



409
410
411
# File 'lib/wardite/value.rb', line 409

def inspect
  "I64(#{@value})"
end

#memsizeObject



280
281
282
# File 'lib/wardite/value.rb', line 280

def memsize
  64
end

#packed(size: nil) ⇒ Object



294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/wardite/value.rb', line 294

def packed(size: nil)
  case size
  when nil
    [self.value].pack("L!")
  when 8
    [self.value & 0xff].pack("C")
  when 16
    [self.value & 0xffff].pack("S!")
  when 32
    [self.value & 0xffffffff].pack("I!")
  else
    raise EvalError, "unsupported size #{size}"
  end
end

#promote(to:) ⇒ Object

Raises:



376
377
378
# File 'lib/wardite/value.rb', line 376

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

#reinterpret(to:) ⇒ Object

Raises:



382
383
384
385
386
387
# File 'lib/wardite/value.rb', line 382

def reinterpret(to:)
  raise EvalError, "unsupported operation" if to != :f64
  v = [value].pack("L!").unpack("d")[0]
  raise EvalError, "[BUG] String#unpack is broke, really?" if !v.is_a?(Float)
  F64(v)
end

#trunc_s(to:) ⇒ Object

Raises:



332
333
334
# File 'lib/wardite/value.rb', line 332

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

#trunc_sat_s(to:) ⇒ Object

Raises:



404
405
406
# File 'lib/wardite/value.rb', line 404

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

#trunc_sat_u(to:) ⇒ Object

Raises:



398
399
400
# File 'lib/wardite/value.rb', line 398

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

#trunc_u(to:) ⇒ Object

Raises:



338
339
340
# File 'lib/wardite/value.rb', line 338

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

#value_sObject

returns a value interpreted as signed integer



286
287
288
289
290
# File 'lib/wardite/value.rb', line 286

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

#wrap(to:) ⇒ Object



311
312
313
314
315
316
# File 'lib/wardite/value.rb', line 311

def wrap(to:)
  if to != :i32
    raise EvalError, "unsupported operation #{to}"
  end
  I32(value % (1 << 32))
end