gloo 7.1.3 → 7.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +23 -0
  3. data/docs/application.md +65 -4
  4. data/docs/development.md +10 -0
  5. data/docs/iterators.md +66 -1
  6. data/docs/language_objects.md +34 -22
  7. data/docs/language_scripting.md +23 -2
  8. data/docs/language_syntax.md +86 -3
  9. data/docs/objects.md +6 -6
  10. data/docs/operators.md +36 -2
  11. data/docs/plugins.md +26 -0
  12. data/docs/testing.md +35 -9
  13. data/docs/verbs.md +31 -3
  14. data/lib/VERSION +1 -1
  15. data/lib/VERSION_NOTES +23 -0
  16. data/lib/dependencies.rb +4 -1
  17. data/lib/gloo/app/engine.rb +151 -23
  18. data/lib/gloo/app/prompt.rb +8 -0
  19. data/lib/gloo/convert/string_to_decimal.rb +3 -6
  20. data/lib/gloo/convert/string_to_integer.rb +3 -2
  21. data/lib/gloo/core/dictionary.rb +23 -12
  22. data/lib/gloo/core/obj.rb +77 -23
  23. data/lib/gloo/core/op.rb +99 -0
  24. data/lib/gloo/core/parser.rb +15 -0
  25. data/lib/gloo/core/tokens.rb +18 -0
  26. data/lib/gloo/core/verb.rb +130 -1
  27. data/lib/gloo/exec/dispatch.rb +7 -3
  28. data/lib/gloo/exec/runaway_recursion.rb +34 -0
  29. data/lib/gloo/exec/runner.rb +46 -0
  30. data/lib/gloo/exec/script.rb +16 -2
  31. data/lib/gloo/expr/expression.rb +77 -6
  32. data/lib/gloo/expr/l_decimal.rb +18 -2
  33. data/lib/gloo/expr/l_integer.rb +5 -2
  34. data/lib/gloo/expr/op_div.rb +4 -0
  35. data/lib/gloo/expr/op_eq.rb +4 -0
  36. data/lib/gloo/expr/op_gt.rb +4 -0
  37. data/lib/gloo/expr/op_gteq.rb +4 -0
  38. data/lib/gloo/expr/op_ineq.rb +4 -0
  39. data/lib/gloo/expr/op_lt.rb +4 -0
  40. data/lib/gloo/expr/op_lteq.rb +4 -0
  41. data/lib/gloo/expr/op_minus.rb +4 -0
  42. data/lib/gloo/expr/op_mult.rb +3 -7
  43. data/lib/gloo/expr/op_plus.rb +8 -1
  44. data/lib/gloo/objs/basic/alias.rb +6 -1
  45. data/lib/gloo/objs/basic/boolean.rb +15 -6
  46. data/lib/gloo/objs/basic/decimal.rb +25 -3
  47. data/lib/gloo/objs/basic/script.rb +3 -24
  48. data/lib/gloo/objs/basic/string_msgs.rb +50 -68
  49. data/lib/gloo/objs/ctrl/until.rb +219 -0
  50. data/lib/gloo/persist/declaration_ledger.rb +9 -9
  51. data/lib/gloo/persist/declaration_writer.rb +208 -0
  52. data/lib/gloo/persist/disc_mech.rb +70 -39
  53. data/lib/gloo/persist/file_loader.rb +125 -66
  54. data/lib/gloo/persist/file_saver.rb +130 -198
  55. data/lib/gloo/persist/file_storage.rb +15 -4
  56. data/lib/gloo/persist/file_style.rb +89 -0
  57. data/lib/gloo/persist/line_splitter.rb +16 -1
  58. data/lib/gloo/persist/persist_man.rb +112 -30
  59. data/lib/gloo/persist/save_index.rb +211 -0
  60. data/lib/gloo/persist/script_body_collector.rb +16 -21
  61. data/lib/gloo/persist/source/blank_node.rb +10 -3
  62. data/lib/gloo/persist/source/comment_node.rb +10 -2
  63. data/lib/gloo/persist/source/directive_node.rb +10 -2
  64. data/lib/gloo/persist/source/obj_node.rb +174 -9
  65. data/lib/gloo/persist/source/source_doc.rb +54 -0
  66. data/lib/gloo/persist/subtree_extractor.rb +9 -37
  67. data/lib/gloo/persist/trivia_buffer.rb +7 -7
  68. data/lib/gloo/plugin/callback.rb +54 -10
  69. data/lib/gloo/plugin/ext_manager.rb +44 -0
  70. data/lib/gloo/plugin/lib_manager.rb +65 -1
  71. data/lib/gloo/shell/runner.rb +36 -9
  72. data/lib/gloo/verbs/break.rb +6 -4
  73. data/lib/gloo/verbs/check.rb +8 -2
  74. data/lib/gloo/verbs/create.rb +13 -4
  75. data/lib/gloo/verbs/if.rb +33 -10
  76. data/lib/gloo/verbs/invoke.rb +6 -1
  77. data/lib/gloo/verbs/list.rb +9 -2
  78. data/lib/gloo/verbs/load.rb +31 -22
  79. data/lib/gloo/verbs/move.rb +50 -5
  80. data/lib/gloo/verbs/put.rb +9 -2
  81. data/lib/gloo/verbs/reload.rb +2 -1
  82. data/lib/gloo/verbs/run.rb +8 -1
  83. data/lib/gloo/verbs/save.rb +30 -4
  84. data/lib/gloo/verbs/tell.rb +9 -2
  85. data/lib/gloo/verbs/unless.rb +34 -10
  86. data/lib/gloo/verbs/unload.rb +81 -16
  87. data/test.gloo/app/exit_status.test.gloo +34 -0
  88. data/test.gloo/app/fixtures/exit_after_error.gloo +9 -0
  89. data/test.gloo/ctrl/until.test.gloo +100 -0
  90. data/test.gloo/lang/computed_paths.test.gloo +98 -0
  91. data/test.gloo/lang/declaration.test.gloo +36 -0
  92. data/test.gloo/lang/fixtures/mixed_indent.gloo +5 -0
  93. data/test.gloo/lang/literal.test.gloo +43 -0
  94. data/test.gloo/lang/naming.test.gloo +44 -6
  95. data/test.gloo/lang/ops.test.gloo +193 -0
  96. data/test.gloo/lang/runaway_recursion.test.gloo +44 -0
  97. data/test.gloo/objs/decimal.test.gloo +23 -0
  98. data/test.gloo/objs/string.test.gloo +30 -0
  99. data/test.gloo/verbs/create.test.gloo +6 -21
  100. data/test.gloo/verbs/fixtures/load_beside.gloo +6 -0
  101. data/test.gloo/verbs/fixtures/lossless_text.gloo +7 -0
  102. data/test.gloo/verbs/fixtures/lossless_text_saved.gloo +9 -0
  103. data/test.gloo/verbs/if.test.gloo +41 -0
  104. data/test.gloo/verbs/load.test.gloo +22 -3
  105. data/test.gloo/verbs/move.test.gloo +39 -0
  106. data/test.gloo/verbs/save.test.gloo +239 -0
  107. data/test.gloo/verbs/unless.test.gloo +40 -0
  108. data/test.gloo/verbs/unload.test.gloo +61 -3
  109. metadata +15 -1
@@ -3,10 +3,10 @@
3
3
  #
4
4
  # One object declaration in a source file, eg.
5
5
  # name [type] : value
6
- # Carries the raw text needed to reproduce the declaration unchanged,
7
- # plus a back-reference to the heap object it created (or updated),
8
- # so a save can tell whether the live value still matches what's on
9
- # disk and, if so, leave the raw text alone.
6
+ # Carries the declaration's exact source text, plus a back-reference
7
+ # to the heap object it created (or named again), so a save can tell
8
+ # whether the object is still there and its value unchanged and, if
9
+ # so, write the text back exactly as it was read.
10
10
  #
11
11
 
12
12
  module Gloo
@@ -19,8 +19,23 @@ module Gloo
19
19
  # :begin_end -- name [type] : BEGIN ... END
20
20
  # :body -- name [type] :, followed by an indented,
21
21
  # unparsed body (script commands)
22
+ #
23
+ # raw is the declaration's own exact text: its line (with any
24
+ # continuation lines), and its BEGIN/END block or script body,
25
+ # line endings included -- not its leading_doc or children.
26
+ # head is the declaration line up to its value (indent, name and
27
+ # [type], as written), raw_tail what follows it.
28
+ #
29
+ # repaired_head is set when gloo had to guess at the head (a
30
+ # missing name or ']'): a save writes the head as understood.
31
+ #
32
+ # home is the object's parent and snapshot a copy of its value,
33
+ # both taken when the file finished loading: the object is still
34
+ # in its place if its parent is home, and unchanged if its value
35
+ # equals the snapshot.
22
36
  attr_accessor :name, :raw_type, :raw_indent, :raw_tail, :block_style,
23
- :raw_value, :raw_end_indent, :leading_doc, :trailing_comment, :obj
37
+ :raw_value, :leading_doc, :obj, :raw, :head,
38
+ :home, :snapshot, :repaired_head
24
39
  attr_reader :children
25
40
 
26
41
  #
@@ -35,15 +50,80 @@ module Gloo
35
50
  @raw_tail = ''
36
51
  @block_style = :inline
37
52
  @raw_value = nil
38
- # a begin_end block's closing END is not necessarily indented
39
- # to match its declaration -- kept separately, raw.
40
- @raw_end_indent = ''
41
53
  @leading_doc = nil
42
- @trailing_comment = nil
43
54
  @obj = nil
55
+ @raw = ''
56
+ @head = ''
57
+ @home = nil
58
+ @snapshot = nil
59
+ @repaired_head = nil
44
60
  @children = []
45
61
  end
46
62
 
63
+ #
64
+ # The exact source text of the declaration, its leading_doc and
65
+ # everything under it, as read.
66
+ #
67
+ def text
68
+ str = "#{@leading_doc}#{@raw}"
69
+ @children.each { |child| str << child.text }
70
+ return str
71
+ end
72
+
73
+ #
74
+ # A copy of this declaration with no children, and with the
75
+ # given text in place of its own (for a save's new document;
76
+ # this node is left as it is).
77
+ #
78
+ def detached( raw = @raw )
79
+ copy = dup
80
+ copy.raw = raw
81
+ copy.reset_children
82
+ return copy
83
+ end
84
+
85
+ #
86
+ # A copy with the head gloo understood in place of the one
87
+ # written (see repaired_head); this node if nothing was repaired.
88
+ #
89
+ def repaired
90
+ return self unless @repaired_head
91
+
92
+ copy = detached( @raw.sub( @head, @repaired_head ) )
93
+ copy.head = @repaired_head
94
+ copy.repaired_head = nil
95
+ return copy
96
+ end
97
+
98
+ #
99
+ # Record where the object is and what its value is, now that the
100
+ # file has finished loading.
101
+ #
102
+ def remember
103
+ return unless @obj
104
+
105
+ @home = @obj.parent
106
+ @snapshot = copy_of( @obj.value )
107
+ end
108
+
109
+ #
110
+ # Is the object still under the parent it was loaded under?
111
+ #
112
+ def in_place?
113
+ return false unless @obj && @home
114
+
115
+ return @obj.parent.equal?( @home ) && @home.children.any? { |c| c.equal?( @obj ) }
116
+ end
117
+
118
+ #
119
+ # Has the object's value changed since the file was loaded?
120
+ #
121
+ def changed?
122
+ return false unless @obj
123
+
124
+ return @obj.value != @snapshot
125
+ end
126
+
47
127
  #
48
128
  # The cleaned-up leading_doc: each line's leading whitespace and
49
129
  # its '#' marker (plus one following space, if any) stripped,
@@ -59,8 +139,93 @@ module Gloo
59
139
  return dedent( lines ).join( "\n" )
60
140
  end
61
141
 
142
+ #
143
+ # Give the declaration a new name as written (eg. a dotted path
144
+ # when it moves into a file of its own). Only the name on its
145
+ # first line changes.
146
+ #
147
+ def rename( new_name )
148
+ @raw = @raw.sub( /\A([ \t]*)#{Regexp.escape( @name )}/ ) { "#{$1}#{new_name}" }
149
+ @head = @head.sub( /\A([ \t]*)#{Regexp.escape( @name )}/ ) { "#{$1}#{new_name}" }
150
+ @name = new_name
151
+ end
152
+
153
+ #
154
+ # Move the declaration (and everything under it) to a new
155
+ # indentation. Its own lines and its script body shift by the
156
+ # same amount; a BEGIN/END block's content is the value, so it
157
+ # stays exactly as it is, and END lines up with the declaration.
158
+ # A leading_doc's lines are set to the new indentation.
159
+ #
160
+ def reindent( new_indent )
161
+ old = @raw_indent
162
+ @leading_doc = reindent_doc( @leading_doc, new_indent ) if @leading_doc
163
+ @raw = reindent_own( @raw, old, new_indent )
164
+ @head = shift( @head, old, new_indent )
165
+ @raw_indent = new_indent
166
+ @children.each do |child|
167
+ next unless child.is_a?( ObjNode )
168
+
169
+ child.reindent( shift( child.raw_indent, old, new_indent ) )
170
+ end
171
+ end
172
+
173
+ protected
174
+
175
+ #
176
+ # Start with no children (for a detached copy).
177
+ #
178
+ def reset_children
179
+ @children = []
180
+ end
181
+
62
182
  private
63
183
 
184
+ #
185
+ # A copy of a value that can't be changed through the original
186
+ # (a script's lines are an Array that grows in place).
187
+ #
188
+ def copy_of( value )
189
+ return Marshal.load( Marshal.dump( value ) )
190
+ rescue TypeError
191
+ return value.dup
192
+ end
193
+
194
+ #
195
+ # The declaration's own lines at a new indentation.
196
+ #
197
+ def reindent_own( text, old, new_indent )
198
+ lines = text.lines
199
+ return text if lines.empty?
200
+
201
+ if @block_style == :begin_end
202
+ ending = lines.last[ /\r?\n\z/ ].to_s
203
+ lines[ 0 ] = shift( lines[ 0 ], old, new_indent )
204
+ lines[ -1 ] = "#{new_indent}END#{ending}" if lines.size > 1
205
+ return lines.join
206
+ end
207
+
208
+ return lines.map { |l| l.strip.empty? ? l : shift( l, old, new_indent ) }.join
209
+ end
210
+
211
+ #
212
+ # Replace a line's old indentation prefix with the new one; a
213
+ # line that doesn't start with it gets the new one added.
214
+ #
215
+ def shift( line, old, new_indent )
216
+ return "#{new_indent}#{line.delete_prefix( old )}" if line.start_with?( old )
217
+
218
+ return "#{new_indent}#{line.lstrip}"
219
+ end
220
+
221
+ #
222
+ # Re-indent a leading_doc's lines to new_indent, replacing
223
+ # whatever leading whitespace each '#' line had.
224
+ #
225
+ def reindent_doc( raw, new_indent )
226
+ return raw.lines.map { |line| "#{new_indent}#{line.lstrip}" }.join
227
+ end
228
+
64
229
  #
65
230
  # One raw comment line -> its text past the '#': leading
66
231
  # whitespace dropped, then the '#' and at most one space after
@@ -19,11 +19,53 @@ module Gloo
19
19
 
20
20
  attr_reader :children
21
21
 
22
+ # A file whose last line has no line ending: every node's text
23
+ # ends with one anyway (so new text can follow any node), and
24
+ # the document leaves the last one off.
25
+ attr_accessor :no_final_newline
26
+
22
27
  #
23
28
  # Set up an empty source document.
24
29
  #
25
30
  def initialize
26
31
  @children = []
32
+ @no_final_newline = false
33
+ end
34
+
35
+ #
36
+ # The file's exact text: every node's text, in order. Right
37
+ # after a load this is the file, byte for byte.
38
+ #
39
+ def text
40
+ str = @children.map( &:text ).join
41
+ str = str.delete_suffix( "\n" ) if @no_final_newline
42
+ return str
43
+ end
44
+
45
+ #
46
+ # Note that the file's last line had no line ending, and give it
47
+ # one in the node that holds it (see no_final_newline).
48
+ #
49
+ def missing_final_newline!
50
+ leaf = last_text_node( @children )
51
+ return unless leaf
52
+
53
+ leaf.raw << "\n"
54
+ @no_final_newline = true
55
+ end
56
+
57
+ #
58
+ # Every object declaration in the document, at any depth, in
59
+ # file order.
60
+ #
61
+ def obj_nodes( nodes = @children, found = [] )
62
+ nodes.each do |node|
63
+ next unless node.is_a?( Source::ObjNode )
64
+
65
+ found << node
66
+ obj_nodes( node.children, found )
67
+ end
68
+ return found
27
69
  end
28
70
 
29
71
  #
@@ -51,6 +93,18 @@ module Gloo
51
93
 
52
94
  private
53
95
 
96
+ #
97
+ # The node whose text comes last in the file: the last node, or
98
+ # its last child (at any depth) if it has children.
99
+ #
100
+ def last_text_node( nodes )
101
+ last = nodes.last
102
+ return nil unless last
103
+ return last unless last.is_a?( Source::ObjNode ) && last.children.any?
104
+
105
+ return last_text_node( last.children )
106
+ end
107
+
54
108
  #
55
109
  # Depth-first search of nodes (and their children) for the one
56
110
  # linked to obj; removes it from whichever children array it's
@@ -46,11 +46,13 @@ module Gloo
46
46
  return err( SPANS_MULTIPLE_FILES_ERR ) if owners.count > 1
47
47
 
48
48
  source_fs = owners.first
49
- node = source_fs ? remove_node( obj, source_fs ) : fresh_node( obj )
49
+ return [ fresh_node( obj ), nil ] unless source_fs
50
+
51
+ node = remove_node( obj, source_fs )
50
52
  return err( NO_OWN_DECLARATION_ERR ) unless node
51
53
 
52
- node.name = dotted_name( obj )
53
- reindent( node, 0 )
54
+ node.rename( dotted_name( obj ) )
55
+ node.reindent( '' )
54
56
  return [ node, source_fs ]
55
57
  end
56
58
 
@@ -110,13 +112,12 @@ module Gloo
110
112
 
111
113
  #
112
114
  # obj has no file of its own (never loaded, or created at
113
- # runtime since) -- build a plain node with no raw text of its
114
- # own. FileSaver renders it fresh from the live object, the same
115
- # way it already does for any brand-new child.
115
+ # runtime since) -- write it, and everything under it, from
116
+ # scratch, named by its dotted path.
116
117
  #
117
118
  def fresh_node( obj )
118
- node = Source::ObjNode.new( :name => obj.name, :raw_type => obj.type_display )
119
- node.obj = obj
119
+ node = FileSaver.new( @engine, nil, obj ).new_node( obj, dotted_name( obj ) )
120
+ Source::SourceDoc.new.obj_nodes( [ node ] ).each( &:remember )
120
121
  return node
121
122
  end
122
123
 
@@ -150,35 +151,6 @@ module Gloo
150
151
  return names.join( '.' )
151
152
  end
152
153
 
153
- #
154
- # Recompute raw_indent (and, for a BEGIN/END node, raw_end_indent
155
- # to match) from scratch for the moved node and every descendant
156
- # ObjNode. Their old indentation depth described nesting inside
157
- # the file they came from; here obj itself is depth 0, in a
158
- # brand-new file. Floating trivia (a blank line or comment not
159
- # attached as some declaration's leading_doc) travels along
160
- # unindented-adjusted -- purely cosmetic, since indentation never
161
- # affects how a comment/blank line is recognized on reload.
162
- #
163
- def reindent( node, depth )
164
- new_indent = "\t" * depth
165
- node.raw_indent = new_indent
166
- node.raw_end_indent = new_indent if node.block_style == :begin_end
167
- node.leading_doc = reindent_doc( node.leading_doc, new_indent ) if node.leading_doc
168
- node.children.each do |child|
169
- reindent( child, depth + 1 ) if child.is_a?( Source::ObjNode )
170
- end
171
- end
172
-
173
- #
174
- # Re-indent a raw leading_doc's lines to new_indent, replacing
175
- # whatever leading whitespace each '#'-prefixed line had in the
176
- # file it came from.
177
- #
178
- def reindent_doc( raw, new_indent )
179
- return raw.split( "\n" ).map { |line| "#{new_indent}#{line.lstrip}" }.join( "\n" )
180
- end
181
-
182
154
  end
183
155
  end
184
156
  end
@@ -29,16 +29,16 @@ module Gloo
29
29
  end
30
30
 
31
31
  #
32
- # Buffer one comment line. raw is the full source line (minus
33
- # its trailing newline); tabs is its indentation level.
32
+ # Buffer one comment line. raw is the full source line, with its
33
+ # line ending; tabs is its indentation level.
34
34
  #
35
35
  def push_comment( raw, tabs )
36
36
  @pending << { :kind => :comment, :raw => raw, :tabs => tabs }
37
37
  end
38
38
 
39
39
  #
40
- # Buffer one blank line. raw is its exact source text (may be
41
- # non-empty if it had trailing whitespace) -- a blank line always
40
+ # Buffer one blank line. raw is its exact source text, with its
41
+ # line ending and any trailing whitespace -- a blank line always
42
42
  # breaks a comment run's association with whatever declaration
43
43
  # follows, the same as it always has.
44
44
  #
@@ -61,8 +61,8 @@ module Gloo
61
61
  #
62
62
  # If the run since the last blank (or the whole run, if it has no
63
63
  # blank in it) is one or more comment lines all at the same
64
- # indent as line_tabs, claim it as a leading_doc (raw,
65
- # newline-joined). Anything before that -- an earlier blank, and
64
+ # indent as line_tabs, claim it as a leading_doc (the raw lines,
65
+ # line endings included). Anything before that -- an earlier blank, and
66
66
  # whatever preceded it -- floats into children first, in order,
67
67
  # since a blank always breaks the association. If the trailing
68
68
  # run doesn't qualify (wrong indent, or empty), it floats into
@@ -74,7 +74,7 @@ module Gloo
74
74
  flush_list( before, children )
75
75
 
76
76
  if after.any? && after.last[ :tabs ] == line_tabs
77
- doc = after.map { |t| t[ :raw ] }.join( "\n" )
77
+ doc = after.map { |t| t[ :raw ] }.join
78
78
  @pending = []
79
79
  return doc
80
80
  end
@@ -1,33 +1,77 @@
1
- #
1
+ #
2
2
  # A mechanism for registering extension components.
3
- #
3
+ #
4
+ # Each library or extension registers through its own callback, which
5
+ # remembers what it registered, so the library can be unloaded later:
6
+ # its verbs and object types taken out of the dictionary again.
7
+ #
4
8
  module Gloo
5
9
  module Plugin
6
10
  class Callback
7
11
 
8
- #
12
+ attr_reader :verbs, :objs
13
+
14
+ #
9
15
  # Initialize the callback.
10
- #
16
+ #
11
17
  def initialize( engine )
12
18
  @engine = engine
19
+ @verbs = []
20
+ @objs = []
13
21
  end
14
22
 
15
- #
23
+ #
16
24
  # Register a verb.
17
- #
25
+ #
18
26
  def register_verb( verb_class )
19
27
  @engine.log.debug "Registering verb: #{verb_class} from callback helper"
28
+ @verbs << verb_class
20
29
  @engine.dictionary.register_verb_post_start( verb_class, @engine )
21
30
  end
22
31
 
23
- #
32
+ #
24
33
  # Register an object.
25
- #
34
+ #
26
35
  def register_obj( object_class )
27
36
  @engine.log.debug "Registering object: #{object_class} from callback helper"
37
+ @objs << object_class
28
38
  @engine.dictionary.register_obj_post_start( object_class, @engine )
29
39
  end
30
-
40
+
41
+ #
42
+ # Take everything this callback registered out of the dictionary.
43
+ # The Ruby classes stay loaded (Ruby can't un-require), but gloo
44
+ # decides what exists from the dictionary, so they're gone.
45
+ #
46
+ def unregister
47
+ @verbs.each { |v| @engine.dictionary.unregister_verb( v ) }
48
+ @objs.each { |o| @engine.dictionary.unregister_obj( o ) }
49
+ end
50
+
51
+ #
52
+ # The objects on the heap whose type is one this callback
53
+ # registered (or a subclass of one), in the order they're found.
54
+ #
55
+ def objects_in_use
56
+ found = []
57
+ each_obj( @engine.heap.root ) do |obj|
58
+ found << obj if @objs.any? { |klass| obj.is_a?( klass ) }
59
+ end
60
+ return found
61
+ end
62
+
63
+ private
64
+
65
+ #
66
+ # Call the block with every object below the given one.
67
+ #
68
+ def each_obj( parent, &block )
69
+ parent.children.each do |child|
70
+ block.call( child )
71
+ each_obj( child, &block )
72
+ end
73
+ end
74
+
31
75
  end
32
76
  end
33
- end
77
+ end
@@ -12,12 +12,16 @@ module Gloo
12
12
  # Constants for extension management
13
13
  EXT_FILE = '_ext.rb'
14
14
 
15
+ IN_USE_ERR = "Could not unload extension '%s': objects of its types are on the heap (%s).".freeze
16
+
15
17
  #
16
18
  # Set up the extension manager.
17
19
  #
18
20
  def initialize( engine )
19
21
  @engine = engine
20
22
  @extensions = {}
23
+ # What each extension registered (its Callback), for unloading.
24
+ @registrations = {}
21
25
  @engine.log.debug 'extension manager initialized...'
22
26
  end
23
27
 
@@ -82,12 +86,52 @@ module Gloo
82
86
  plugin_class = Object.const_get( class_name )
83
87
  inst = plugin_class.new
84
88
  ext_cb = Callback.new( @engine )
89
+ @registrations[ name ] = ext_cb
85
90
  inst.register( ext_cb )
86
91
  rescue NameError
87
92
  @engine.err "Could not find class '#{class_name}' in extension file '#{full_path}'."
88
93
  end
89
94
  end
90
95
 
96
+
97
+ #
98
+ # Unload the extension of the given name: take its verbs and
99
+ # object types out of the dictionary, and forget it's loaded. The
100
+ # Ruby code stays loaded, so load_ext registers it again. Not
101
+ # done, and an error, while objects of its types are on the heap.
102
+ # Unloading an extension that isn't loaded does nothing.
103
+ # Returns false if it couldn't be unloaded, true otherwise.
104
+ #
105
+ def unload_ext( name )
106
+ unless @extensions.key?( name )
107
+ @engine.log.info "Extension #{name} isn't loaded."
108
+ return true
109
+ end
110
+
111
+ cb = @registrations[ name ]
112
+ in_use = cb ? cb.objects_in_use : []
113
+ unless in_use.empty?
114
+ @engine.err format( IN_USE_ERR, name, in_use.map( &:pn ).join( ', ' ) )
115
+ return false
116
+ end
117
+
118
+ cb&.unregister
119
+ @registrations.delete( name )
120
+ @extensions.delete( name )
121
+ @engine.log.debug "Extension unloaded: #{name}"
122
+ return true
123
+ end
124
+
125
+
126
+ #
127
+ # Unload every extension, as for a session reset. Called with the
128
+ # heap already emptied, so no object of an extension's types can
129
+ # stop it.
130
+ #
131
+ def unload_all
132
+ @extensions.keys.each { |name| unload_ext( name ) }
133
+ end
134
+
91
135
  end
92
136
  end
93
137
  end
@@ -13,12 +13,19 @@ module Gloo
13
13
  GEM_PREFIX = 'gloo-'
14
14
  INIT_CLASS_SUFFIX = 'Init'
15
15
 
16
+ PINNED_ERR = "Could not unload library '%s': gloo loaded it itself, so it stays loaded.".freeze
17
+ IN_USE_ERR = "Could not unload library '%s': objects of its types are on the heap (%s).".freeze
18
+
16
19
  #
17
20
  # Set up the library manager.
18
21
  #
19
22
  def initialize( engine )
20
23
  @engine = engine
21
24
  @libraries = {}
25
+ # What each library registered (its Callback), for unloading.
26
+ @registrations = {}
27
+ # Libraries gloo loaded itself; unloading them is an error.
28
+ @pinned = []
22
29
  @engine.log.debug 'library manager initialized...'
23
30
  end
24
31
 
@@ -47,8 +54,12 @@ module Gloo
47
54
  # Load the library of the given name.
48
55
  # The name will correspond with the name of a directory
49
56
  # within the gloo libraries directory.
57
+ # A pinned library is one gloo loads itself (gloo-test, in test
58
+ # mode): it can't be unloaded, and survives a session reset.
50
59
  #
51
- def load_lib( name )
60
+ def load_lib( name, pinned: false )
61
+ @pinned << name if pinned && !@pinned.include?( name )
62
+
52
63
  # Check to see if the library is already loaded.
53
64
  if @libraries.key?( name )
54
65
  @engine.log.info "Library #{name} is already loaded."
@@ -92,12 +103,65 @@ module Gloo
92
103
  plugin_class = Object.const_get( class_name )
93
104
  inst = plugin_class.new
94
105
  lib_cb = Callback.new( @engine )
106
+ @registrations[ name ] = lib_cb
95
107
  inst.register( lib_cb )
96
108
  rescue NameError
97
109
  @engine.err "Could not find class '#{class_name}' in library '#{name}'."
98
110
  end
99
111
  end
100
112
 
113
+
114
+ #
115
+ # Is the library one gloo loaded itself (see load_lib)?
116
+ #
117
+ def pinned?( name )
118
+ return @pinned.include?( name )
119
+ end
120
+
121
+ #
122
+ # Unload the library of the given name: take its verbs and object
123
+ # types out of the dictionary, and forget it's loaded. The Ruby
124
+ # code stays loaded (it can't be un-required), so load_lib
125
+ # registers it again. Not done, and an error, for a pinned
126
+ # library or while objects of its types are on the heap.
127
+ # Unloading a library that isn't loaded does nothing.
128
+ # Returns false if it couldn't be unloaded, true otherwise.
129
+ #
130
+ def unload_lib( name )
131
+ unless @libraries.key?( name )
132
+ @engine.log.info "Library #{name} isn't loaded."
133
+ return true
134
+ end
135
+
136
+ if pinned?( name )
137
+ @engine.err format( PINNED_ERR, name )
138
+ return false
139
+ end
140
+
141
+ cb = @registrations[ name ]
142
+ in_use = cb ? cb.objects_in_use : []
143
+ unless in_use.empty?
144
+ @engine.err format( IN_USE_ERR, name, in_use.map( &:pn ).join( ', ' ) )
145
+ return false
146
+ end
147
+
148
+ cb&.unregister
149
+ @registrations.delete( name )
150
+ @libraries.delete( name )
151
+ @engine.log.debug "Core library unloaded: #{name}"
152
+ return true
153
+ end
154
+
155
+
156
+ #
157
+ # Unload every library except the pinned ones (see load_lib), as
158
+ # for a session reset. Called with the heap already emptied, so no
159
+ # object of a library's types can stop it.
160
+ #
161
+ def unload_unpinned
162
+ ( @libraries.keys - @pinned ).each { |name| unload_lib( name ) }
163
+ end
164
+
101
165
  end
102
166
  end
103
167
  end