Class: RDL::Config

Inherits:
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.



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

def initialize
  @nowrap = Set.new
  @gather_stats = false
  @report = false
  @guess_types = []
  @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

#guess_typesObject

Returns the value of attribute guess_types.



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

def guess_types
  @guess_types
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.



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

def post_defaults
  @post_defaults
end

#pre_defaultsObject

Returns the value of attribute pre_defaults.



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

def pre_defaults
  @pre_defaults
end

#reportObject

Returns the value of attribute report.



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

def report
  @report
end

#type_defaultsObject

Returns the value of attribute type_defaults.



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

def type_defaults
  @type_defaults
end

Instance Method Details

#add_nowrap(*klasses) ⇒ Object



22
23
24
# File 'lib/rdl/config.rb', line 22

def add_nowrap(*klasses)
  klasses.each { |klass| @nowrap.add klass }
end

#do_guess_typesObject



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 214

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



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/rdl/config.rb', line 130

def do_report
  return unless @report
  puts "------------------------------"
  typechecked = []
  missing = []
  $__rdl_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



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
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
# File 'lib/rdl/config.rb', line 159

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_info.get(klass, meth, :otype) if $__rdl_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_bot_type
  otblock = false
  otypes.each { |ot|
    ot[:args].each_with_index { |t, i|
      otargs[i] = $__rdl_bot_type 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

#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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
# File 'lib/rdl/config.rb', line 36

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



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

def remove_nowrap(*klasses)
  klasses.each { |klass| @nowrap.delete klass }
end