Class: RDL::Config

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/rdl/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



11
12
13
14
15
16
17
18
19
20
# File 'lib/rdl/config.rb', line 11

def initialize
  @nowrap = Set.new # Set of symbols
  @gather_stats = false
  @report = false # if this is enabled by default, modify @at_exit_installed
  @guess_types = [] # same as above
  @at_exit_installed = false
  @type_defaults = { wrap: true, typecheck: false }
  @pre_defaults = { wrap: true }
  @post_defaults = { wrap: true }
end

Instance Attribute Details

#gather_statsObject

Returns the value of attribute gather_stats.



7
8
9
# File 'lib/rdl/config.rb', line 7

def gather_stats
  @gather_stats
end

#nowrapObject

Returns the value of attribute nowrap.



6
7
8
# File 'lib/rdl/config.rb', line 6

def nowrap
  @nowrap
end

#post_defaultsObject

Returns the value of attribute post_defaults.



9
10
11
# File 'lib/rdl/config.rb', line 9

def post_defaults
  @post_defaults
end

#pre_defaultsObject

Returns the value of attribute pre_defaults.



9
10
11
# File 'lib/rdl/config.rb', line 9

def pre_defaults
  @pre_defaults
end

#reportObject

writer is custom defined



8
9
10
# File 'lib/rdl/config.rb', line 8

def report
  @report
end

#type_defaultsObject

Returns the value of attribute type_defaults.



9
10
11
# File 'lib/rdl/config.rb', line 9

def type_defaults
  @type_defaults
end

Instance Method Details

#add_nowrap(*klasses) ⇒ Object



37
38
39
40
41
42
# File 'lib/rdl/config.rb', line 37

def add_nowrap(*klasses)
  klasses.each { |klass|
    @nowrap.add klass.to_s.to_sym
    @nowrap.add RDL::Util.add_singleton_marker(klass.to_s).to_sym
  }
end

#do_guess_typesObject



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/rdl/config.rb', line 235

def do_guess_types
  return if @guess_types.empty?
  puts "------------------------------"
  puts "TYPE GUESSES"
  RDL::Config.instance.guess_types.each { |klass|
    puts
    puts "class #{klass}"
    the_klass = RDL::Util.to_class(klass)
    sklass = RDL::Util.add_singleton_marker(klass.to_s)
    the_klass.singleton_methods(false).each { |meth|
      next unless meth.to_s =~ /^__rdl_(.*)_old/
      guess_meth(sklass, $1, true, the_klass.singleton_method(meth))
    }
    the_klass.instance_methods(false).each { |meth|
      next unless meth.to_s =~ /^__rdl_(.*)_old/
      guess_meth(klass, $1, false, the_klass.instance_method(meth))
    }
    puts "end"
  }
end

#do_reportObject



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/rdl/config.rb', line 151

def do_report
  return unless @report
  puts "------------------------------"
  typechecked = []
  missing = []
  RDL::Globals.info.info.each_pair { |klass, meths|
    meths.each { |meth, kinds|
      if kinds[:typecheck]
        if kinds[:typechecked]
          typechecked << [klass, meth]
        else
          missing << [klass, meth]
        end
      elsif kinds[:typechecked]
        raise RuntimeError, "#{RDL::Util.pp_klass_method(klass, meth)} typechecked but not annotated to do so?!"
      end
    }
  }
  unless typechecked.empty?
    puts "TYPECHECKED METHODS:"
    typechecked.each { |klass, meth| puts RDL::Util.pp_klass_method(klass, meth) }
  end
  unless missing.empty?
    puts unless typechecked.empty?
    puts "METHODS ANNOTATED TO BE TYPECHECKED BUT NOT TYPECHECKED:"
    missing.each { |klass, meth| puts RDL::Util.pp_klass_method(klass, meth) }
  end
end

#guess_meth(klass, meth, is_sing, the_meth) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/rdl/config.rb', line 180

def guess_meth(klass, meth, is_sing, the_meth)
  # first print based on signature according to Ruby
  first = true
  block = false
  print "  type #{if is_sing then '\'self.' + meth + '\'' else ':' + meth end}, '("
  params = the_meth.parameters
  params.each_with_index { |param, i|
    kind, name = param
    print ", " unless first || kind == :block
    case kind
    when :req
      print "XXXX #{name}"
    when :opt
      print "?XXXX #{name}"
    when :rest
      print "*#XXXX #{name}"
    when :key
      print "#{name}: XXXX"
    when :block
      block = true
    else
      print "???? param of kind #{kind} for #{name}"
    end
    first = false
  }
  print ")"
  print " { BLOCK }" if block
  puts " -> XXXX'"

  # next print based on observed types
  otypes = RDL::Globals.info.get(klass, meth, :otype) if RDL::Globals.info.has?(klass, meth, :otype) # observed types
  return if otypes.nil?
  first = true
  print "  type #{if is_sing then '\'self.' + meth + '\'' else ':' + meth end}, '("
  otargs = []
  otret = RDL::Globals.types[:bot]
  otblock = false
  otypes.each { |ot|
    ot[:args].each_with_index { |t, i|
      otargs[i] = RDL::Globals.types[:bot] if otargs[i].nil?
      otargs[i] = RDL::Type::UnionType.new(otargs[i], RDL::Type::NominalType.new(t)).canonical
    }
    otret = RDL::Type::UnionType.new(otret, RDL::Type::NominalType.new(ot[:ret])).canonical
    otblock = otblock || ot[:block]
  }
  otargs.each { |t|
    print ", " unless first
    print t
    first = false
  }
  print ")"
  print " { BLOCK }" if otblock
  puts " -> #{otret}'"
end

#guess_typesObject



27
28
29
30
# File 'lib/rdl/config.rb', line 27

def guess_types
  install_at_exit
  return @guess_types
end

#guess_types=(val) ⇒ Object



32
33
34
35
# File 'lib/rdl/config.rb', line 32

def guess_types=(val)
  install_at_exit
  @guess_types = val
end

#profile_stats(outname = "/#{__FILE__[0...-3]}", outdir = "") ⇒ Object

require_relative ‘../rdl3/rdl/lib/rdl.rb’ require_relative ‘../rdl3/rdl/lib/rdl_types.rb’ RDL::Config.instance.profile_stats



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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/rdl/config.rb', line 57

def profile_stats(outname="/#{__FILE__[0...-3]}",outdir="")
  require 'profile'
  Profiler__.stop_profile # Leave setup out of stats

  at_exit do
    Profiler__.stop_profile
    RDL::Globals.contract_switch.off {
      puts "START."
      puts "Performing Profile Analysis"
      # Class Name => [Times Contract Called | Times Called | Time | Time | Class Profile]
      # Implications:
      #   [nil] -> Method exists in object space, but not used
      #   [-1] -> Contract exists for method, but method not profiled
      #   [-1, ...] -> Method profiled, but no contract exists
      totals = {}

      puts "Retrieving Profiler Data"
      Profiler__.class_variable_get(:@@maps).values.each do |threadmap|
        threadmap.each do |key, data|
          total_data = (totals[key.to_s] ||= [-1, 0, 0.0, 0.0, key])
          total_data[1] += data[0]
          total_data[2] += data[1]
          total_data[3] += data[2]
        end
      end

      puts "Scanning Object Space"
      kls = []
      ObjectSpace.each_object { |obj|
        if kls.include? obj.class then
          next
        end
        kls << obj.class
        mthds = obj.public_methods(false) + obj.private_methods(false) + obj.protected_methods(false)
        puts "Class #{obj.class}"
        mthds.each{ |mthd|
          puts "    :#{mthd}"
          totals["#{obj.class}::#{mthd.to_s}".gsub('::','#')] ||= [nil] unless mthd.to_s =~ /new/
        }
      }

      p "Scanning RDL Contract Log"
      RDL::Globals.wrapped_calls.each{ |mname,ct|
        if (totals[mname]) then
          if (totals[mname][0]) then
            totals[mname][0] = ct
          else
            totals[mname][0] = -1
          end
        end
      }

      puts "Analyzing Statistics"
      filtered = {}
      totals.each{ |k,v|
        if (not (k=~/(rdl)|(RDL)/)) and (v[0].nil? or v[0]==-1) then filtered[k]=v end
      }

      puts "Writing Output"
      require 'json'
      fpath = "#{outdir}/#{outname}_rdlstat.json".gsub('//','')
      File.open(fpath,'w') do |file|
        file.puts "POTENTIAL PROBLEMS"
        filtered.each{ |k,v|
          begin
            k =~ /((?:.+\#)*)(.+)/
            x = $1[0...-1] # Store $1 and $2 before overriden
            y = $2
            if (x =~ /\<Class\:/) then
              puts "Cannot evaluate user-defined class #{x}"
            else
              kls = eval x.gsub('#','::')
              mthds = kls.public_methods(false) + kls.protected_methods(false) # Ignoring private methods
              if (mthds.include? y.to_sym)
                file.printf "%-20s %-s", k, v.to_s
                file.puts ""
              else
                puts "Ignoring inheritance problem for #{k}"
              end
            end
          rescue
          end
        }
        file.puts " "
        file.puts "JSON OBJECT"
        file.puts totals.to_json
      end
      puts "DONE."
    }
  end

  Profiler__.start_profile # Restart profiler after setup
end

#remove_nowrap(*klasses) ⇒ Object



44
45
46
47
48
49
# File 'lib/rdl/config.rb', line 44

def remove_nowrap(*klasses)
  klasses.each { |klass|
    @nowrap.delete klass.to_s.to_sym
    @nowrap.delete RDL::Util.add_singleton_marker(klass.to_s).to_sym
  }
end