Class: Unmarshal

Inherits:
Object
  • Object
show all
Defined in:
lib/rmarshal/unmarshal.rb

Instance Method Summary collapse

Constructor Details

#initialize(fp) ⇒ Unmarshal

Returns a new instance of Unmarshal.



26
27
28
29
30
31
# File 'lib/rmarshal/unmarshal.rb', line 26

def initialize(fp)
	@fp = fp
	@interned = []
	
	@value = unmarshal
end

Instance Method Details

#byteObject



34
# File 'lib/rmarshal/unmarshal.rb', line 34

def byte() @fp.read 1 end

#doubleObject



36
# File 'lib/rmarshal/unmarshal.rb', line 36

def double() @fp.read(8).unpack('E')[0] end

#longObject



35
# File 'lib/rmarshal/unmarshal.rb', line 35

def long() @fp.read(4).unpack('i')[0] end

#unmarshalObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rmarshal/unmarshal.rb', line 38

def unmarshal
	type = byte
	case type
		when '.' then :Ellipsis
		when '0' then :null # Special!
		when 'N' then nil
		when 'T' then true
		when 'F' then false
		
		when 'c'
			argcount, nlocals, stacksize, flags = long, long, long, long
			
			code_, consts, names, varnames, freevars = unmarshal, unmarshal, unmarshal, unmarshal, unmarshal
			cellvars, filename, name = unmarshal, unmarshal, unmarshal
			firstlineno, lnotab = long, unmarshal
			
			Code.new( 
					argcount, nlocals, stacksize, flags, 
					code_, consts, names, varnames, freevars, 
					cellvars, filename, name, firstlineno, lnotab
				)
		
		when 'f' then @fp.read(byte).to_f
		when 'g' then double
		when 'i' then long
		when 'I' then @fp.read(8).unpack('q')[0]
		
		when 'R'
			@interned[long]
		
		when 's' then @fp.read long
		
		when 'S' then :StopIteration
		
		when 't'
			str = @fp.read long
			@interned[@interned.size] = str
			str
		
		when 'u' then @fp.read(long).force_encoding 'utf-8'
		
		when 'x'
			Complex @fp.read(byte).to_f(), @fp.read(byte).to_f()
		when 'y'
			Complex double, double
		
		when '(', '['
			(0...long).map { unmarshal }
		
		when '{'
			val = {}
			
			while true
				key = unmarshal
				break if key == :null
				
				val[key] = unmarshal
			end
			
			val
		
		when '<', '>'
			Set.new((0...long).map { unmarshal })
		
		when '?'
			raise 'TYPE_UNKNOWN encountered.  Incomplete marshal.'
		
		else
			raise "Unknown type '#{type}'"
	end
end

#valueObject



33
# File 'lib/rmarshal/unmarshal.rb', line 33

def value() @value end