Class: Laser::Analysis::LaserModule

Inherits:
LaserObject show all
Defined in:
lib/laser/analysis/bootstrap/laser_module.rb

Overview

Laser representation of a module. Named LaserModule to avoid naming conflicts. It has lists of methods, instance variables, and so on.

Direct Known Subclasses

LaserClass

Instance Attribute Summary (collapse)

Attributes inherited from LaserObject

#klass, #scope, #singleton_class

Instance Method Summary (collapse)

Methods inherited from LaserObject

#instance_variable_defined?, #instance_variable_get, #instance_variable_set, #laser_simulate, #normal_class

Methods included from ModuleExtensions

#attr_accessor_with_default, #cattr_accessor, #cattr_accessor_with_default, #cattr_get_and_setter, #cattr_reader, #cattr_writer, #opposite_method

Constructor Details

- (LaserModule) initialize(klass = ClassRegistry['Module'], scope = Scope::GlobalScope, full_path = (@name_set = :no; "#{klass.path}:Anonymous:#{object_id.to_s(16)}")) {|_self| ... }

Returns a new instance of LaserModule

Yields:

  • (_self)

Yield Parameters:



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 10

def initialize(klass = ClassRegistry['Module'], scope = Scope::GlobalScope,
               full_path=(@name_set = :no; "#{klass.path}:Anonymous:#{object_id.to_s(16)}"))
  super(klass, scope, full_path.split('::').last)
  full_path = submodule_path(full_path) if scope && scope.parent
  validate_module_path!(full_path) unless LaserSingletonClass === self

  @name_set = :yes unless @name_set == :no
  @path = full_path
  @instance_methods = {}
  @visibility_table = {}
  @constant_table = {}
  @scope = scope
  @ivar_types = {}
  @superclass ||= nil
  initialize_protocol
  @binding = Bindings::ConstantBinding.new(name, self)
  initialize_scope
  yield self if block_given?
  LaserModule.all_modules << self
end

Instance Attribute Details

- (Object) binding (readonly)

Returns the value of attribute binding



6
7
8
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 6

def binding
  @binding
end

- (Object) path

Returns the value of attribute path



7
8
9
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 7

def path
  @path
end

- (Object) superclass

Returns the value of attribute superclass



6
7
8
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 6

def superclass
  @superclass
end

Instance Method Details

- (Object) <=>(other)



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 31

def <=>(other)
  if self == other
    return 0
  elsif ancestors.include?(other)
    return -1
  elsif other.ancestors.include?(self)
    return 1
  else
    return nil
  end
end

- (Object) ===(other)

simulation methods



284
285
286
287
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 284

def ===(other)
  klass = (LaserObject === other ? other.klass : ClassRegistry[other.class.name])
  klass.ancestors.include?(self)
end

- (Object) __all_instance_methods(include_super = true)



135
136
137
138
139
140
141
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 135

def __all_instance_methods(include_super = true)
  mine = @instance_methods.keys
  if include_super && @superclass
  then @superclass.instance_methods | mine
  else mine
  end
end

- (Object) __instance_methods_with_privacy(include_super, *allowed)



143
144
145
146
147
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 143

def __instance_methods_with_privacy(include_super, *allowed)
  methods = __all_instance_methods(include_super)
  table = visibility_table
  methods.select { |name| allowed.include?(table[name.to_sym]) }
end

- (Object) __make_module_function__(method_name)



377
378
379
380
381
382
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 377

def __make_module_function__(method_name)
  set_visibility!(method_name, :private)
  found_method = instance_method(method_name).dup
  singleton_class.add_instance_method!(found_method)
  singleton_class.set_visibility!(method_name, :public)
end

- (Object) __visibility_modifier__(args, kind)



368
369
370
371
372
373
374
375
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 368

def __visibility_modifier__(args, kind)
  if args.empty?
    Bootstrap::VISIBILITY_STACK.value[-1] = kind
  else
    args.each { |method| set_visibility!(method.to_sym, kind) }
  end
  self
end

- (Object) add_instance_method!(method)



112
113
114
115
116
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 112

def add_instance_method!(method)
  @instance_methods[method.name.to_sym] = method
  @visibility_table[method.name.to_sym] = :public
  method.owner = self
end

- (Object) add_instance_variable!(binding)



188
189
190
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 188

def add_instance_variable!(binding)
  @instance_variables[binding.name] = binding
end

- (Object) alias_method(new, old)



353
354
355
356
357
358
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 353

def alias_method(new, old)
  newsym = new.to_sym
  oldsym = old.to_sym
  @instance_methods[newsym] = instance_method(oldsym)
  @visibility_table[newsym] = visibility_for(oldsym)
end

- (Object) ancestors

The set of all superclasses (including the class itself)



218
219
220
221
222
223
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 218

def ancestors
  if @superclass.nil?
  then [self]
  else [self] + @superclass.ancestors
  end
end

- (Object) as_type



54
55
56
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 54

def as_type
  Types::ClassObjectType.new(self)
end

- (Object) class_name



86
87
88
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 86

def class_name
  'Module'
end

- (Object) classes_including



229
230
231
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 229

def classes_including
  @classes_including ||= []
end

- (Boolean) const_defined?(constant, inherit = true)

Fuck you, that's why



312
313
314
315
316
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 312

def const_defined?(constant, inherit=true)
  !!const_get(constant, inherit)
rescue
  false
end

- (Object) const_get(constant, inherit = true)



300
301
302
303
304
305
306
307
308
309
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 300

def const_get(constant, inherit=true)
  if inherit && superclass
    @constant_table[constant] || superclass.const_get(constant, true)
  elsif LaserClass === self
    @constant_table[constant] or raise ArgumentError.new("Class #{@path} has no constant #{constant}")
  else
    (@constant_table[constant] || ClassRegistry['Object'].const_get(constant, false)) or
        raise ArgumentError.new("Class #{@path} has no constant #{constant}")
  end
end

- (Object) const_set(string, value)



289
290
291
292
293
294
295
296
297
298
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 289

def const_set(string, value)
  @constant_table[string] = value
  if LaserModule === value && !value.name_set?
    if self == ClassRegistry['Object']
      value.set_name(string)
    else
      value.set_name("#{@path}::#{string}")
    end
  end
end

- (Object) define_method(name, proc)



322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 322

def define_method(name, proc)
  str_name = name.to_s
  name = name.to_sym
  new_method = LaserMethod.new(str_name, proc)
  new_method.owner = self
  @instance_methods[name] = new_method
  if Bootstrap::VISIBILITY_STACK.value.last == :module_function
    __make_module_function__(name)
  else
    @visibility_table[name] = Bootstrap::VISIBILITY_STACK.value.last
  end
  new_method
end

- (Object) define_method_with_annotations(name, proc, opts = {})



336
337
338
339
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 336

def define_method_with_annotations(name, proc, opts={})
  method = define_method(name, proc)
  opts.each { |name, value| method.send("#{name}=", value) }
end

- (Object) extend(*mods)



364
365
366
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 364

def extend(*mods)
  singleton_class.include(*mods)
end

- (Object) include(*mods)



360
361
362
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 360

def include(*mods)
  mods.reverse.each { |mod| include_module(mod) }
end

- (Object) include_module(mod)

Directly translated from MRI's C implementation in class.c:650



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 238

def include_module(mod)
  if mod.klass == ClassRegistry['Class']
    raise ArgumentError.new("Tried to include #{mod.name}, which should "+
                            " be a Module or Module subclass, not a " +
                            "#{mod.klass.name}.")
  end
  original_mod = mod
  any_changes = false
  current = self
  while mod
    superclass_seen = false
    should_change = true
    if mod == self
      raise ArgumentError.new("Cyclic module inclusion: #{mod} mixed into #{self}")
    end
    ancestors.each do |parent|
      case parent
      when LaserModuleCopy
        if parent == mod
          current = parent unless superclass_seen
          should_change = false
          break
        end
      when LaserClass
        superclass_seen = true
      end
    end
    if should_change
      new_super = (current.superclass = LaserModuleCopy.new(mod, current.ancestors[1]))
      mod.classes_including << current
      current = new_super
      any_changes = true
    end
    mod = mod.superclass
  end
  unless any_changes
    raise DoubleIncludeError.new("Included #{original_mod.path} into #{self.path}"+
                                  " but it was already included.", nil)
  end
end

- (Object) included_modules



233
234
235
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 233

def included_modules
  ancestors.select { |mod| LaserModuleCopy === mod }
end

- (Object) initialize_protocol

Initializes the protocol for this LaserClass.



100
101
102
103
104
105
106
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 100

def initialize_protocol
  if ProtocolRegistry[path].any? && !TESTS_ACTIVATED
    $stderr.puts "Warning: creating new instance of #{class_name} #{path}"
  else
    ProtocolRegistry.add_class(self)
  end
end

- (Object) initialize_scope

If this is a new, custom module, we can update the constant table and perform module initialization.



92
93
94
95
96
97
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 92

def initialize_scope
  if @scope && !(@scope.parent.nil?)
    @scope.parent.constants[name] = self.binding if @scope.parent
    @scope.locals['self'] = Bindings::LocalVariableBinding.new('self', self)
  end
end

- (Object) inspect



279
280
281
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 279

def inspect
  "#<LaserModule: #{path}>"
end

- (Object) instance_method(name)



118
119
120
121
122
123
124
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 118

def instance_method(name)
  lookup = name.to_sym
  if @instance_methods.has_key?(lookup)
  then @instance_methods[lookup]
  else @superclass && @superclass.instance_method(lookup)
  end
end

- (Object) instance_methods(include_super = true)



149
150
151
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 149

def instance_methods(include_super = true)
  __instance_methods_with_privacy(include_super, :public, :protected)
end

- (Object) instance_variable(name)



177
178
179
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 177

def instance_variable(name)
  @instance_variables[name] || (@superclass && @superclass.instance_variable(name))
end

- (Object) instance_variables



181
182
183
184
185
186
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 181

def instance_variables
  if @superclass.nil?
  then @instance_variables
  else @instance_variables.merge(@superclass.instance_variables)
  end
end

- (Object) ivar_type(name)



169
170
171
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 169

def ivar_type(name)
  @ivar_types[name] || (@superclass && @superclass.ivar_type(name)) || Types::NILCLASS
end

- (Boolean) method_defined?(name)



126
127
128
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 126

def method_defined?(name)
  instance_method(name) && visibility_for(name) != :private
end

- (Object) module_function(*args)



396
397
398
399
400
401
402
403
404
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 396

def module_function(*args)
  if args.any?
    args.each do |method|
      __make_module_function__(method.to_sym)
    end
  else
    Bootstrap::VISIBILITY_STACK.value[-1] = :module_function
  end
end

- (Object) name



108
109
110
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 108

def name
  self.path.split('::').last
end

- (Boolean) name_set?



58
59
60
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 58

def name_set?
  @name_set == :yes
end

- (Object) parent



213
214
215
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 213

def parent
  @superclass
end

- (Object) private(*args)



392
393
394
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 392

def private(*args)
  __visibility_modifier__(args, :private)
end

- (Object) private_instance_methods(include_super = true)



165
166
167
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 165

def private_instance_methods(include_super = true)
  __instance_methods_with_privacy(include_super, :private)
end

- (Object) protected(*args)



388
389
390
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 388

def protected(*args)
  __visibility_modifier__(args, :protected)
end

- (Object) protected_instance_methods(include_super = true)



157
158
159
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 157

def protected_instance_methods(include_super = true)
  __instance_methods_with_privacy(include_super, :protected)
end

- (Boolean) protected_method_defined?(name)



161
162
163
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 161

def protected_method_defined?(name)
  !!__instance_methods_with_privacy(include_super, :protected)
end

- (Object) public(*args)



384
385
386
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 384

def public(*args)
  __visibility_modifier__(args, :public)
end

- (Object) public_instance_method(name)



130
131
132
133
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 130

def public_instance_method(name)
  result = instance_method(name)
  visibility_for(name) == :public ? result : nil
end

- (Object) public_instance_methods(include_super = true)



153
154
155
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 153

def public_instance_methods(include_super = true)
  __instance_methods_with_privacy(include_super, :public)
end

- (Object) remove_const(sym)



318
319
320
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 318

def remove_const(sym)
  @constant_table.delete(sym)
end

- (Object) remove_method(symbol)



347
348
349
350
351
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 347

def remove_method(symbol)
  sym = symbol.to_sym
  @instance_methods.delete(sym)
  @visibility_table.delete(sym)
end

- (Object) set_ivar_type(name, type)



173
174
175
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 173

def set_ivar_type(name, type)
  @ivar_types[name] = type
end

- (Object) set_name(new_path)



62
63
64
65
66
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 62

def set_name(new_path)
  @path = new_path
  ProtocolRegistry.add_class(self)
  @name_set = :yes
end

- (Object) set_visibility!(method, visibility)



205
206
207
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 205

def set_visibility!(method, visibility)
  @visibility_table[method] = visibility
end

- (Object) submodule_path(new_mod_name)

Returns the canonical path for a (soon-to-be-created) submodule of the given scope. This is computed before creating the module.



70
71
72
73
74
75
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 70

def submodule_path(new_mod_name)
  scope = self.scope.parent
  new_mod_full_path = scope.parent.nil? ? '' : scope.path
  new_mod_full_path += '::' unless new_mod_full_path.empty?
  new_mod_full_path += new_mod_name
end

- (Object) subset



225
226
227
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 225

def subset
  [self]
end

- (Object) undef_method(symbol)



341
342
343
344
345
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 341

def undef_method(symbol)
  sym = symbol.to_sym
  @instance_methods[sym] = nil
  @visibility_table[sym] = :undefined
end

- (Object) validate_module_path!(path)



77
78
79
80
81
82
83
84
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 77

def validate_module_path!(path)
  path.split('::').each do |component|
    if !component.empty? && component[0,1] !~ /[A-Z]/
      raise ArgumentError.new("Path component #{component} in #{path}" +
                              ' does not start with a capital letter, A-Z.')
    end
  end
end

- (Object) visibility_for(method)



192
193
194
195
196
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 192

def visibility_for(method)
  lookup = method.to_sym
  return @visibility_table[lookup] ||
    (@superclass && @superclass.visibility_for(lookup))
end

- (Object) visibility_table



198
199
200
201
202
203
# File 'lib/laser/analysis/bootstrap/laser_module.rb', line 198

def visibility_table
  if @superclass
  then @superclass.visibility_table.merge(@visibility_table)
  else @visibility_table
  end
end