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.



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

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



159
160
161
162
163
164
# File 'lib/evalhook/tree_processor.rb', line 159

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

#const_path_emul(code) ⇒ Object



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

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



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/evalhook/tree_processor.rb', line 45

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



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

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



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

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



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

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



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

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



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

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



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

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



216
217
218
219
220
221
# File 'lib/evalhook/tree_processor.rb', line 216

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



253
254
255
256
257
258
# File 'lib/evalhook/tree_processor.rb', line 253

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



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

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



57
58
59
60
61
62
# File 'lib/evalhook/tree_processor.rb', line 57

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



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

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



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

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



241
242
243
244
245
246
247
248
249
250
251
# File 'lib/evalhook/tree_processor.rb', line 241

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



137
138
139
140
# File 'lib/evalhook/tree_processor.rb', line 137

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

#process_zsuper(tree) ⇒ Object



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

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



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

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