Class: Class

Inherits:
Object show all
Defined in:
lib/fOOrth/core/class.rb,
lib/fOOrth/library/introspection/class.rb

Overview

  • library/introspection/class.rb - Class support for introspection.

Instance Method Summary collapse

Instance Method Details

#create_foorth_instance(vm) ⇒ Object

Create an instance of this fOOrth class.
Parameters:

  • vm - The current fOOrth virtual machine.



59
60
61
62
# File 'lib/fOOrth/core/class.rb', line 59

def create_foorth_instance(vm)
  (obj = self.new).foorth_init(vm)
  obj
end

#create_foorth_proxy(proxy_name = nil) ⇒ Object

Add this class as a proxy class in the foorth class system.
Parameters:

  • proxy_name - the optional name of the proxy. Defaults to the Ruby name.


Returns:

  • The spec of the proxy class.



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/fOOrth/core/class.rb', line 89

def create_foorth_proxy(proxy_name = nil)
  if proxy_name
    self.foorth_name = proxy_name
  end

  validate_class_name(foorth_name)

  error "F10: The class #{foorth_name} already exists." if $FOORTH_GLOBALS[foorth_name]

  install_foorth_class(foorth_name, self)
end

#create_foorth_subclass(foorth_name) ⇒ Object

Create a new fOOrth subclass of this class.
Parameters:

  • foorth_name - The foorth_name of the new sub-class.


Returns:

  • The spec of the subclass.


Note:

  • If a sub-class with the given name already exists, an exception is raised.



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/fOOrth/core/class.rb', line 71

def create_foorth_subclass(foorth_name)
  if $FOORTH_GLOBALS[XfOOrth::SymbolMap.map(foorth_name)]
    error "F10: The class #{foorth_name} already exists."
  end

  validate_class_name(foorth_name)

  new_class = Class.new(self) {self.foorth_name = foorth_name}

  XfOOrth.const_set('XfOOrth_' + foorth_name, new_class)
  install_foorth_class(foorth_name, new_class)
end

#create_shared_method(name, spec_class, options, &block) ⇒ Object

Create a shared method on this fOOrth class.
Parameters:

  • name - The name of the method to create.

  • spec_class - The specification class to use.

  • options - An array of options.

  • block - A block to associate with the name.


Returns

  • The spec created for the shared method.



40
41
42
43
44
45
46
47
48
49
# File 'lib/fOOrth/core/class.rb', line 40

def create_shared_method(name, spec_class, options, &block)
  sym = XfOOrth::SymbolMap.add_entry(name)
  spec = spec_class.new(name, sym, options, &block)

  unless self == Object && options.include?(:stub)
    define_method(sym, &spec.does)
  end

  foorth_shared[sym] = spec
end

#foorth_class_nameObject

Get the name of the class or a safe default.



14
15
16
# File 'lib/fOOrth/core/class.rb', line 14

def foorth_class_name
  self.foorth_name || "AnonymousClass<#{self.object_id}>".freeze
end

#foorth_method_info(name) ⇒ Object

Investigate a method of this class.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/fOOrth/library/introspection/class.rb', line 28

def foorth_method_info(name)
  symbol, results = XfOOrth::SymbolMap.map_info(name)
  found = false

  if symbol
    spec, info = map_foorth_shared_info(symbol)

    if spec && !spec.has_tag?(:stub)
      (results << ["", ""]).concat(info).concat(spec.get_info)
      found = true
    end

    spec, info = map_foorth_exclusive_info(symbol)

    if spec && !spec.has_tag?(:stub)
      (results << ["", ""]).concat(info).concat(spec.get_info)
      found = true
    end

    results << ["Scope", "not found."] unless found
  end

  results
end

#foorth_nameObject

Get the foorth name of this class.
Decree!

  • These are to be the only references to @_private_foorth_name!



9
10
11
# File 'lib/fOOrth/core/class.rb', line 9

def foorth_name
  @_private_foorth_name ||= name.gsub(/.*::/, '').freeze
end

#foorth_name=(new_name) ⇒ Object

Set the foorth name of this class.
Decree!

  • These are to be the only references to @_private_foorth_name!



21
22
23
# File 'lib/fOOrth/core/class.rb', line 21

def foorth_name=(new_name)
  @_private_foorth_name = new_name.freeze
end

#foorth_sharedObject

Access/create the class’s shared fOOrth dictionary.
Decree!

  • This is to be the only reference to @_private_foorth_shared!



28
29
30
# File 'lib/fOOrth/core/class.rb', line 28

def foorth_shared
  @_private_foorth_shared ||= Hash.new
end

#get_infoObject

Get introspection info.



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
# File 'lib/fOOrth/library/introspection/class.rb', line 54

def get_info
  results = [["Lineage", lineage]]

  if foorth_has_exclusive?
    results.concat([["", ""], ["Methods", "Class"]])

    foorth_exclusive.extract_method_names(:all).sort.each do |name|
      symbol, info = XfOOrth::SymbolMap.map_info(name)
      (results << ["", ""]).concat(info)

      spec, info = map_foorth_exclusive_info(symbol, :shallow)
      results.concat(info).concat(spec.get_info)
    end
  end

  unless foorth_shared.empty?
    results.concat([["", ""], ["Methods", "Shared"]])

    foorth_shared.extract_method_names(:all).sort.each do |name|
      symbol, info = XfOOrth::SymbolMap.map_info(name)
      (results << ["", ""]).concat(info)

      spec, info = map_foorth_shared_info(symbol, :shallow)
      results.concat(info).concat(spec.get_info)
    end
  end

  results
end

#lineageObject

Get the lineage of this class.



18
19
20
21
22
23
24
25
# File 'lib/fOOrth/library/introspection/class.rb', line 18

def lineage
  #Ugly hack, sorry!
  if [Object, Module].include?(self)
    "Object"
  else
    foorth_class_name + " < " + superclass.lineage
  end
end

#map_foorth_shared(symbol) ⇒ Object

Map the symbol to a specification or nil if there is no mapping.



52
53
54
# File 'lib/fOOrth/core/class.rb', line 52

def map_foorth_shared(symbol)
  foorth_shared[symbol] || ((sc = superclass) && sc.map_foorth_shared(symbol))
end

#map_foorth_shared_info(symbol, shallow = nil) ⇒ Object

Get information about the mapping of the symbol.



7
8
9
10
11
12
13
14
15
# File 'lib/fOOrth/library/introspection/class.rb', line 7

def map_foorth_shared_info(symbol, shallow=nil)
  if (spec = foorth_shared[symbol])
    [spec, [["Class", foorth_class_name], ["Scope", "Shared"]]]
  elsif (sc = superclass) && !shallow
    sc.map_foorth_shared_info(symbol)
  else
    [nil, nil]
  end
end