Module: Police::DataFlow::Proxies

Defined in:
lib/police/dataflow/proxies.rb

Overview

Namespace for the auto-generated proxy classes.

Class Method Summary collapse

Class Method Details

.clear_cacheBoolean

Clears the cache of proxy classes associated with Ruby classes.

This method has a terrible impact on VM performance, and is only intended for testing the Police::DataFlow implementation.

Returns:

  • (Boolean)

    true



68
69
70
71
# File 'lib/police/dataflow/proxies.rb', line 68

def self.clear_cache
  @classes.clear
  true
end

.for(proxied_class, label_set) ⇒ Class

A class whose instances proxy instances of a Ruby class.

Parameters:

  • proxied_class (Class)

    the class whose instances will be proxied by instances of the returned class

  • label_set (Hash<Integer,Hash<Police::DataFlow::Label,Boolean>>)

    the set of all labels that will be held by the proxy

Returns:

  • (Class)

    a Police::DataFlow::ProxyBase subclass that can proxy instances of the given class



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/police/dataflow/proxies.rb', line 15

def self.for(proxied_class, label_set)
  unless class_cache = @classes[proxied_class]
    class_cache = {}
    @classes[proxied_class] = class_cache
  end

  cache_key = label_set.keys.sort!.freeze
  return class_cache[cache_key] if class_cache.has_key? cache_key

  label_classes = label_set.map { |label_key, label_hash|
    label_hash.first.first.class
  }.sort_by!(&:__id__).freeze

  proxy_class = for! proxied_class, label_classes
  class_cache[cache_key] = proxy_class
  proxy_class
end

.for!(proxied_class, label_classes) ⇒ Class

A class whose instances proxy instances of a Ruby class.

Use for instead of calling this directly.

Parameters:

  • proxied_class (Class)

    the class whose instances will be proxied by instances of the returned class

  • label_classes (Array<Class>)

    classes for all the labels that will be held by proxies instantiated from this class; the array should not have duplicates

Returns:

  • (Class)

    a Police::DataFlow::ProxyBase subclass that can proxy instances of the given class



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/police/dataflow/proxies.rb', line 45

def self.for!(proxied_class, label_classes)
  proxy_class = nil
  klass = proxied_class
  until klass == nil
    if klass == String
      # TODO(pwnall): String-specific proxying
      break
    elsif klass == Numeric
      proxy_class = Class.new Police::DataFlow::ProxyNumeric
    end
    klass = klass.superclass
  end
  proxy_class ||= Class.new Police::DataFlow::ProxyBase
  proxy_class.__police_classes__ = label_classes
  proxy_class
end