Class: Proc

Inherits:
Object
  • Object
show all
Defined in:
lib/decompiler/proc/as_code.rb,
lib/decompiler/proc/signature.rb,
lib/decompiler/proc/as_expression.rb

Defined Under Namespace

Classes: Arguments, Signature

Instance Method Summary collapse

Instance Method Details

#argument_infoObject

Return a hash mapping each argument name to a description of that argument.



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/decompiler/proc/signature.rb', line 133

def argument_info
  args = self.arguments()

  info = {}
  args.each do |name|
    info[name] = name.to_s
  end

  # Rest arg
  if args.rest_arg then
    rest_name = args[args.rest_arg]
    if rest_name then
      info[rest_name] = "*#{rest_name}"
    end
  end

  return info
end

#argumentsObject

Return an Arguments object representing the arguments in the order in which they appear in the argument list.



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
109
# File 'lib/decompiler/proc/signature.rb', line 66

def arguments
  has_rest_arg = self.has_rest_arg

  if self.respond_to?(:var) then
    # pre-YARV
    case self.var
    when Node::DASGN_CURR
      return Arguments.new([ self.var.vid ], false, has_rest_arg ? 0 : nil)
    when Node::MASGN
      if self.var.head then
        a = self.var.head.to_a
        args = a.map { |n| n.vid }
      else
        args = []
      end
      if self.var.args then
        args.push(self.var.args.vid)
      end
      return Arguments.new(args, true, has_rest_arg ? args.size - 1: nil)
    when nil
      return Arguments.new(nil, false, has_rest_arg ? 0 : nil)
    when Fixnum
      return Arguments.new([], false, has_rest_arg ? 0 : nil)
    else
      raise "Unexpected node type: #{self.var.class}"
    end
  elsif
    # YARV
    iseq = self.body
    local_vars = iseq.local_table
    has_rest_arg = iseq.arg_rest != -1
    has_block_arg = iseq.arg_block != -1
    num_args = \
      iseq.argc + \
      iseq.arg_opt_table.size + \
      (has_rest_arg ? 1 : 0) + \
      (has_block_arg ? 1 : 0)
    names = local_vars[0...num_args]
    # TODO: masgn
    return Arguments.new(names, true, has_rest_arg ? -1 : nil)
  else
    return Arguments.new(nil, false, nil)
  end
end

#as_code(indent = 0) ⇒ Object

Return a string representation of a proc’s definition/body, similarly to Method#as_code.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/decompiler/proc/as_code.rb', line 8

def as_code(indent=0)
  sig = self.signature
  body_expression = self.body ? self.body.as_code(indent+1) : nil
  s = "#{'  '*indent}proc do"
  if not sig.args.unspecified then
    s += " #{sig}"
  end
  s += "\n"
  if body_expression then
    s += "#{body_expression}\n"
  end
  s += "#{'  '*indent}end"
  return s
end

#as_expressionObject

Return a single-line string representation of a proc’s definition/body, similarly to Method#as_expression.



8
9
10
11
12
13
14
# File 'lib/decompiler/proc/as_expression.rb', line 8

def as_expression
  sig = self.signature
  body_expression = self.body ? self.body.as_expression : nil
  s = sig.args.unspecified ? "" : sig.to_s + ' '
  b = body_expression ? body_expression + ' ' : ''
  return "proc { #{s}#{b}}"
end

#has_rest_argObject

Return true if the proc has a rest arg



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/decompiler/proc/signature.rb', line 112

def has_rest_arg
  if self.respond_to?(:var) then
    # pre-YARV
    has_rest_arg = false
    if self.var then
      if self.var.class == Node::MASGN then
        if self.var.args then
          has_rest_arg = true
        end
      end
    end
  else
    # YARV
    rest = self.body.arg_rest
    has_rest_arg = (rest >= 0 ? rest - 1 : nil)
  end
  return has_rest_arg
end

#signatureObject

Return a String representing the method’s signature.



178
179
180
181
182
# File 'lib/decompiler/proc/signature.rb', line 178

def signature
  return Signature.new(
      arguments(),
      argument_info)
end