Class: EvalHook::TreeProcessor

Inherits:
SexpProcessor
  • Object
show all
Defined in:
lib/evalhook/tree_processor.rb

Instance Method Summary collapse

Constructor Details

#initialize(hook_handler, global_variable_name) ⇒ TreeProcessor

Returns a new instance of TreeProcessor.



26
27
28
29
30
31
32
# File 'lib/evalhook/tree_processor.rb', line 26

def initialize(hook_handler, global_variable_name)
  super()
  @hh_ref_global_name = global_variable_name
  @hook_handler = hook_handler
  @def_scope = Array.new
  self.require_empty = false
end

Instance Method Details

#class_scope(class_name) ⇒ Object



152
153
154
155
156
157
# File 'lib/evalhook/tree_processor.rb', line 152

def class_scope(class_name)
  @def_scope.push(class_name)
  ret = yield
  @def_scope.pop
  ret
end

#const_path_emul(code) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/evalhook/tree_processor.rb', line 34

def const_path_emul(code)
  if code.instance_of? Array
    if (code.size == 1)
      s(:const, code[-1].to_sym)
    else
      s(:colon2, const_path_emul(code[0..-2]), code[-1].to_sym)
    end
  else
    const_path_emul code.split("::")
  end
end

#hook_handler_referenceObject



46
47
48
# File 'lib/evalhook/tree_processor.rb', line 46

def hook_handler_reference
  s(:gvar, @hh_ref_global_name)
end

#process_call(tree) ⇒ Object



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
# File 'lib/evalhook/tree_processor.rb', line 58

def process_call(tree)
  original_receiver = tree[1]

  receiver = if original_receiver
    s(:call, hook_handler_reference, :private_method_check, s(:arglist,process(original_receiver),s(:lit,tree[2])))
  else
    s(:self)
  end

  firstcall = nil
  if tree[1] == nil and (tree[3] == s(:arglist) or tree[3] == nil)
    firstcall = s(:call,
        hook_handler_reference,
        :hooked_variable_method,
        s(:arglist, receiver, s(:lit, tree[2]), s(:call, nil, :binding, s(:arglist)))
        )
  else
    firstcall = s(:call,
        hook_handler_reference,
        :hooked_method,
        s(:arglist, receiver, s(:lit, tree[2]))
        )
  end

  arglist = s(:arglist)
  if tree[3]
    if tree[3][0] == :arglist
      arglist = tree[3]
    else
      tree[3..-1].each do |st|
        arglist << st
      end
    end
  end

  s(:call, firstcall, :call, process(arglist) || s(:arglist))
end

#process_cdecl(tree) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/evalhook/tree_processor.rb', line 96

def process_cdecl(tree)

      const_tree = tree[1]
      value_tree = tree[2]

      base_class_tree = nil
      const_id = nil

      unless const_tree.instance_of? Symbol
        if const_tree[0] == :colon2
          base_class_tree = const_tree[1]
          const_id = const_tree[2]
        elsif const_tree[0] == :colon3
          base_class_tree = s(:lit, Object)
          const_id = const_tree[1]
        end
      else
        base_class_tree = s(:lit, Object)
        const_id = const_tree
      end

      args1 = s(:arglist, base_class_tree, s(:lit, const_id), process(value_tree))

      s(:call, hook_handler_reference, :hooked_cdecl, args1 )
end

#process_class(tree) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/evalhook/tree_processor.rb', line 159

def process_class(tree)
  class_name_tree = tree[1]
  if class_name_tree.instance_of? Sexp
    if class_name_tree.first == :colon3
      class_name_tree = process_colon3(class_name_tree)
    end
  end

  if tree[2]
    class_scope(tree[1]) do
      class_tree = s(:class, class_name_tree, process(tree[2]))
      tree[3..-1].map(&method(:process)).each(&class_tree.method(:<<))
      class_tree
    end
  else
    class_scope(tree[1]) do
      class_tree = s(:class, class_name_tree, nil)
      tree[3..-1].map(&method(:process)).each(&class_tree.method(:<<))
      class_tree
    end
  end
end

#process_colon2(tree) ⇒ Object



182
183
184
185
186
# File 'lib/evalhook/tree_processor.rb', line 182

def process_colon2(tree)
  name = tree[2].to_s
  args = s(:arglist, process(tree[1]), s(:str, name))
  s(:call, hook_handler_reference, :hooked_colon2, args)
end

#process_colon3(tree) ⇒ Object



188
189
190
191
192
193
194
# File 'lib/evalhook/tree_processor.rb', line 188

def process_colon3(tree)
  if @hook_handler.base_namespace
    s(:colon2, const_path_emul(@hook_handler.base_namespace.to_s), tree[1])
  else
    s(:colon3, tree[1])
  end
end

#process_const(tree) ⇒ Object



203
204
205
206
207
# File 'lib/evalhook/tree_processor.rb', line 203

def process_const(tree)
  name = tree[1].to_s
  args = s(:arglist, s(:str, name))
  s(:call, hook_handler_reference, :hooked_const, args)
end

#process_defn(tree) ⇒ Object



209
210
211
212
213
214
# File 'lib/evalhook/tree_processor.rb', line 209

def process_defn(tree)
  @last_args = tree[2]
  @last_method_name = tree[1]

  s(tree[0],tree[1],tree[2],*tree[3..-1].map(&method(:process)))
end

#process_defs(tree) ⇒ Object



246
247
248
249
250
251
# File 'lib/evalhook/tree_processor.rb', line 246

def process_defs(tree)
  block_tree = class_scope( "" ) {
    s(:block, *tree[4..-1].map(&method(:process)))
  }
  s(:defs, process(tree[1]), tree[2], tree[3], block_tree)
end

#process_dxstr(tree) ⇒ Object



122
123
124
125
126
127
128
# File 'lib/evalhook/tree_processor.rb', line 122

def process_dxstr(tree)
   dstr_tree = tree.dup
   dstr_tree[0] = :dstr

   args = s(:arglist, process(dstr_tree) )
   s(:call, hook_handler_reference, :hooked_xstr, args)
end

#process_gasgn(tree) ⇒ Object



50
51
52
53
54
55
# File 'lib/evalhook/tree_processor.rb', line 50

def process_gasgn(tree)

  args1 = s(:arglist, s(:lit, tree[1]), process(tree[2]))

  s(:call, hook_handler_reference, :hooked_gasgn, args1)
end

#process_gvar(tree) ⇒ Object



196
197
198
199
200
201
# File 'lib/evalhook/tree_processor.rb', line 196

def process_gvar(tree)
  name = tree[1]

  args = s(:arglist, s(:lit, name))
  s(:call, hook_handler_reference, :hooked_gvar, args)
end

#process_module(tree) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/evalhook/tree_processor.rb', line 135

def process_module(tree)
  module_name_tree = tree[1]
  if module_name_tree.instance_of? Sexp
    if module_name_tree.first == :colon3
      module_name_tree = process_colon3(module_name_tree)
    end
  end

  class_scope(nil) {
    module_tree = s(:module, module_name_tree)
    tree[2..-1].each do |subtree|
      module_tree << process(subtree)
    end
    module_tree
  }
end

#process_super(tree) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
# File 'lib/evalhook/tree_processor.rb', line 234

def process_super(tree)

      receiver = s(:self)

      firstcall = s(:call, hook_handler_reference,
            :hooked_method,
            s(:arglist, receiver, s(:lit,@last_method_name), superclass_call_tree))

      # pass the args passed to super
      s(:call, firstcall, :call, process(s(:arglist, *tree[1..-1])))
end

#process_xstr(tree) ⇒ Object



130
131
132
133
# File 'lib/evalhook/tree_processor.rb', line 130

def process_xstr(tree)
  args = s(:arglist, s(:lit, tree[1]) )
  s(:call, hook_handler_reference, :hooked_xstr, args)
end

#process_zsuper(tree) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/evalhook/tree_processor.rb', line 253

def process_zsuper(tree)

      receiver = s(:self)

      firstcall = s(:call, hook_handler_reference,
            :hooked_method,
            s(:arglist, receiver, s(:lit,@last_method_name), superclass_call_tree))

      # pass the args of the current defn
      args = s(:arglist)
      @last_args[1..-1].each do |arg_sym|
        if arg_sym.to_s[0] == "*"
          args << s(:splat, s(:lvar, arg_sym.to_s[1..-1].to_sym))
        else
          args << s(:lvar, arg_sym)
        end
      end

      s(:call, firstcall, :call, args)
end

#superclass_call_treeObject



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/evalhook/tree_processor.rb', line 216

def superclass_call_tree
  current_class_call = nil

  def_class = @def_scope.last
  if def_class.instance_of? Symbol
    current_class_call = s(:const, def_class)
    s(:call, current_class_call, :superclass, s(:arglist))
  elsif def_class.instance_of? Sexp
    current_class_call = def_class
    s(:call, current_class_call, :superclass, s(:arglist))
  elsif def_class.instance_of? String
    s(:call, s(:self), :class, s(:arglist))
  else
    current_class_call = s(:call, nil, :raise, s(:arglist, s(:const, :SecurityError)))
    s(:call, current_class_call, :superclass, s(:arglist))
  end
end