class Marshal::THAW

Public Class Methods

new() click to toggle source
# File lib/nfreeze.rb, line 188
def initialize
   @io   = nil # assigned later
   @seen = Hash.new
end

Public Instance Methods

thaw(obj) click to toggle source
# File lib/nfreeze.rb, line 180
def thaw obj
   @io = (obj.to_io rescue StringIO.new(obj.to_str))
   version_check
   recur
end

Private Instance Methods

load_array() click to toggle source
# File lib/nfreeze.rb, line 276
def load_array
   load_int.times.map { recur }
end
load_binary(len) click to toggle source
# File lib/nfreeze.rb, line 246
def load_binary len
   raise "broken #{len}" if len < 0
   str = @io.read len
   str.force_encoding Encoding::BINARY
   str
end
load_binary_large() click to toggle source
# File lib/nfreeze.rb, line 253
def load_binary_large
   load_binary load_int
end
load_binary_tiny() click to toggle source
# File lib/nfreeze.rb, line 257
def load_binary_tiny
   load_binary load_byte + 128
end
load_byte() click to toggle source
# File lib/nfreeze.rb, line 236
def load_byte
   @io.getbyte - 128
end
load_hash() click to toggle source
# File lib/nfreeze.rb, line 280
def load_hash
   load_int.times.each_with_object Hash.new do |i, ret|
      # order matters
      v = recur
      k = load_binary_large
      ret.store k, v
   end
end
load_int() click to toggle source
# File lib/nfreeze.rb, line 240
def load_int
   str = @io.read 4
   len, = str.unpack 'N'
   len
end
load_string(len) click to toggle source
# File lib/nfreeze.rb, line 261
def load_string len
   raise "broken #{len}" if len < 0
   str = @io.read len
   str.force_encoding Encoding::UTF_8
   str
end
load_string_large() click to toggle source
# File lib/nfreeze.rb, line 268
def load_string_large
   load_string load_int
end
load_string_tiny() click to toggle source
# File lib/nfreeze.rb, line 272
def load_string_tiny
   load_string load_byte + 128
end
recur() click to toggle source
# File lib/nfreeze.rb, line 207
def recur
   case type = @io.getbyte
   when 0x01 then load_binary_large
   when 0x02 then load_array
   when 0x03 then load_hash
   when 0x04 then raise @@e # this is ref
   when 0x05 then nil
   when 0x06 then raise "Endian mismatch" # machine-natives
   when 0x07 then load_double
   when 0x08 then load_byte
   when 0x09 then load_int
   when 0x0a then load_binary_tiny
   # some cases here...
   when 0x0e then nil
   when 0x0f then true
   when 0x10 then false
   # some cases here...
   when 0x17 then load_string_tiny
   when 0x18 then load_string_large
   else raise TypeError, "can't understand type ##{type}"
   end
rescue Exception => e
   if e == @@e
      retry # ignore refs
   else
      raise
   end
end
version_check() click to toggle source
# File lib/nfreeze.rb, line 193
def version_check
   str = @io.read 2
   x, minor = str.unpack 'cc'
   netorder = x & 1
   major    = x >> 1
   if major != 2 or minor < 6
      raise "unsupported version #{major}.#{minor}"
   elsif netorder != 1
      raise "machine-endian unpredictalbe for this input"
   end
end