Class: EvalHook::TreeProcessor

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

Instance Method Summary collapse

Constructor Details

#initialize(hook_handler) ⇒ TreeProcessor

Returns a new instance of TreeProcessor.



29
30
31
32
33
34
# File 'lib/evalhook/tree_processor.rb', line 29

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

Instance Method Details

#class_scope(class_name) ⇒ Object



162
163
164
165
166
167
# File 'lib/evalhook/tree_processor.rb', line 162

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

#const_path_emul(code) ⇒ Object



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

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



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/evalhook/tree_processor.rb', line 48

def hook_handler_reference
  # create a global_variable name
  unless @hh_ref_global_name
    global_variable_name = "$hook_handler_" + rand(10000000000).to_s
    eval("#{global_variable_name} = @hook_handler")

    @hh_ref_global_name = global_variable_name.to_sym
  end

  s(:gvar, @hh_ref_global_name)
end

#process_call(tree) ⇒ Object



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

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[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



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/evalhook/tree_processor.rb', line 106

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



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/evalhook/tree_processor.rb', line 169

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



192
193
194
195
196
# File 'lib/evalhook/tree_processor.rb', line 192

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



198
199
200
201
202
203
204
# File 'lib/evalhook/tree_processor.rb', line 198

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



213
214
215
216
217
# File 'lib/evalhook/tree_processor.rb', line 213

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



219
220
221
222
223
224
# File 'lib/evalhook/tree_processor.rb', line 219

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

  s(tree[0],tree[1],tree[2],process(tree[3]))
end

#process_defs(tree) ⇒ Object



256
257
258
259
260
261
# File 'lib/evalhook/tree_processor.rb', line 256

def process_defs(tree)
  block_tree = class_scope( "" ) {
    process(tree[4])
  }
  s(:defs, process(tree[1]), tree[2], tree[3], block_tree)
end

#process_dxstr(tree) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/evalhook/tree_processor.rb', line 132

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



60
61
62
63
64
65
# File 'lib/evalhook/tree_processor.rb', line 60

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



206
207
208
209
210
211
# File 'lib/evalhook/tree_processor.rb', line 206

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



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/evalhook/tree_processor.rb', line 145

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



244
245
246
247
248
249
250
251
252
253
254
# File 'lib/evalhook/tree_processor.rb', line 244

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



140
141
142
143
# File 'lib/evalhook/tree_processor.rb', line 140

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

#process_zsuper(tree) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/evalhook/tree_processor.rb', line 263

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



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/evalhook/tree_processor.rb', line 226

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