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

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

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

#bindingObject (readonly)

Returns the value of attribute binding



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

def binding
  @binding
end

#pathObject

Returns the value of attribute path



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

def path
  @path
end

#superclassObject

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

#<=>(other) ⇒ Object



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

#===(other) ⇒ Object

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

#__all_instance_methods(include_super = true) ⇒ Object



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

#__instance_methods_with_privacy(include_super, *allowed) ⇒ Object



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

#__make_module_function__(method_name) ⇒ Object



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

#__visibility_modifier__(args, kind) ⇒ Object



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

#add_instance_method!(method) ⇒ Object



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

#add_instance_variable!(binding) ⇒ Object



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

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

#alias_method(new, old) ⇒ Object



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

#ancestorsObject

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

#as_typeObject



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

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

#class_nameObject



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

def class_name
  'Module'
end

#classes_includingObject



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

def classes_including
  @classes_including ||= []
end

#const_defined?(constant, inherit = true) ⇒ Boolean

Fuck you, that's why

Returns:

  • (Boolean)


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

#const_get(constant, inherit = true) ⇒ Object



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

#const_set(string, value) ⇒ Object



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

#define_method(name, proc) ⇒ Object



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

#define_method_with_annotations(name, proc, opts = {}) ⇒ Object



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

#extend(*mods) ⇒ Object



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

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

#include(*mods) ⇒ Object



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

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

#include_module(mod) ⇒ Object

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

#included_modulesObject



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

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

#initialize_protocolObject

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

#initialize_scopeObject

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

#inspectObject



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

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

#instance_method(name) ⇒ Object



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

#instance_methods(include_super = true) ⇒ Object



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

#instance_variable(name) ⇒ Object



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

#instance_variablesObject



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

#ivar_type(name) ⇒ Object



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

#method_defined?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

#module_function(*args) ⇒ Object



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

#nameObject



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

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

#name_set?Boolean

Returns:

  • (Boolean)


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

def name_set?
  @name_set == :yes
end

#parentObject



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

def parent
  @superclass
end

#private(*args) ⇒ Object



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

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

#private_instance_methods(include_super = true) ⇒ Object



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

#protected(*args) ⇒ Object



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

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

#protected_instance_methods(include_super = true) ⇒ Object



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

#protected_method_defined?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

#public(*args) ⇒ Object



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

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

#public_instance_method(name) ⇒ Object



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

#public_instance_methods(include_super = true) ⇒ Object



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

#remove_const(sym) ⇒ Object



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

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

#remove_method(symbol) ⇒ Object



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

#set_ivar_type(name, type) ⇒ Object



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

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

#set_name(new_path) ⇒ Object



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

#set_visibility!(method, visibility) ⇒ Object



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

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

#submodule_path(new_mod_name) ⇒ Object

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

#subsetObject



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

def subset
  [self]
end

#undef_method(symbol) ⇒ Object



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

#validate_module_path!(path) ⇒ Object



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

#visibility_for(method) ⇒ Object



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

#visibility_tableObject



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