@@ -8,10 +8,26 @@ class Kernel
8
8
@events = EventManager . new ( [ :initialized ] )
9
9
10
10
class << self
11
+ # Return the event manager defined in the `IRuby::Kernel` class.
12
+ # This event manager can handle the following event:
13
+ #
14
+ # - `initialized`: The event occurred after the initialization of
15
+ # a `IRuby::Kernel` instance is finished
16
+ #
17
+ # @example Registering initialized event
18
+ # IRuby::Kernel.events.register(:initialized) do |result|
19
+ # STDERR.puts "IRuby kernel has been initialized"
20
+ # end
21
+ #
22
+ # @see IRuby::EventManager
23
+ # @see IRuby::Kernel#events
11
24
attr_reader :events
25
+
26
+ # Returns the singleton kernel instance
12
27
attr_accessor :instance
13
28
end
14
29
30
+ # Returns a session object
15
31
attr_reader :session
16
32
17
33
EVENTS = [
@@ -40,22 +56,51 @@ def initialize(config_file, session_adapter_name=nil)
40
56
self . class . events . trigger ( :initialized , self )
41
57
end
42
58
59
+ # Returns the event manager defined in a `IRuby::Kernel` instance.
60
+ # This event manager can handle the following events:
61
+ #
62
+ # - `pre_execute`: The event occurred before running the code
63
+ #
64
+ # - `pre_run_cell`: The event occurred before running the code and
65
+ # if the code execution is not silent
66
+ #
67
+ # - `post_execute`: The event occurred after running the code
68
+ #
69
+ # - `post_run_cell`: The event occurred after running the code and
70
+ # if the code execution is not silent
71
+ #
72
+ # The callback functions of `pre_run_cell` event must take one argument
73
+ # to get an `ExecutionInfo` object.
74
+ # The callback functions of `post_run_cell` event must take one argument
75
+ # to get the result of the code execution.
76
+ #
77
+ # @example Registering post_run_cell event
78
+ # IRuby::Kernel.instance.events.register(:post_run_cell) do |result|
79
+ # STDERR.puts "The result of the last execution: %p" % result
80
+ # end
81
+ #
82
+ # @see IRuby::EventManager
83
+ # @see IRuby::ExecutionInfo
84
+ # @see IRuby::Kernel.events
43
85
attr_reader :events
44
86
87
+ # @private
45
88
def create_backend
46
89
PryBackend . new
47
90
rescue Exception => e
48
91
IRuby . logger . warn "Could not load PryBackend: #{ e . message } \n #{ e . backtrace . join ( "\n " ) } " unless LoadError === e
49
92
PlainBackend . new
50
93
end
51
94
95
+ # @private
52
96
def run
53
97
send_status :starting
54
98
while @running
55
99
dispatch
56
100
end
57
101
end
58
102
103
+ # @private
59
104
def dispatch
60
105
msg = @session . recv ( :reply )
61
106
IRuby . logger . debug "Kernel#dispatch: msg = #{ msg } "
@@ -72,6 +117,7 @@ def dispatch
72
117
@session . send ( :publish , :error , error_content ( e ) )
73
118
end
74
119
120
+ # @private
75
121
def kernel_info_request ( msg )
76
122
@session . send ( :reply , :kernel_info_reply ,
77
123
protocol_version : '5.0' ,
@@ -93,11 +139,13 @@ def kernel_info_request(msg)
93
139
status : :ok )
94
140
end
95
141
142
+ # @private
96
143
def send_status ( status )
97
144
IRuby . logger . debug "Send status: #{ status } "
98
145
@session . send ( :publish , :status , execution_state : status )
99
146
end
100
147
148
+ # @private
101
149
def execute_request ( msg )
102
150
code = msg [ :content ] [ 'code' ]
103
151
store_history = msg [ :content ] [ 'store_history' ]
@@ -147,6 +195,7 @@ def execute_request(msg)
147
195
@session . send ( :reply , :execute_reply , content )
148
196
end
149
197
198
+ # @private
150
199
def error_content ( e )
151
200
rindex = e . backtrace . rindex { |line | line . start_with? ( @backend . eval_path ) } || -1
152
201
backtrace = SyntaxError === e && rindex == -1 ? [ ] : e . backtrace [ 0 ..rindex ]
@@ -155,12 +204,14 @@ def error_content(e)
155
204
traceback : [ "#{ RED } #{ e . class } #{ RESET } : #{ e . message } " , *backtrace ] }
156
205
end
157
206
207
+ # @private
158
208
def is_complete_request ( msg )
159
209
# FIXME: the code completeness should be judged by using ripper or other Ruby parser
160
210
@session . send ( :reply , :is_complete_reply ,
161
211
status : :unknown )
162
212
end
163
213
214
+ # @private
164
215
def complete_request ( msg )
165
216
# HACK for #26, only complete last line
166
217
code = msg [ :content ] [ 'code' ]
@@ -176,36 +227,43 @@ def complete_request(msg)
176
227
status : :ok )
177
228
end
178
229
230
+ # @private
179
231
def connect_request ( msg )
180
232
@session . send ( :reply , :connect_reply , Hash [ %w( shell_port iopub_port stdin_port hb_port ) . map { |k | [ k , @config [ k ] ] } ] )
181
233
end
182
234
235
+ # @private
183
236
def shutdown_request ( msg )
184
237
@session . send ( :reply , :shutdown_reply , msg [ :content ] )
185
238
@running = false
186
239
end
187
240
241
+ # @private
188
242
def history_request ( msg )
189
243
# we will just send back empty history for now, pending clarification
190
244
# as requested in ipython/ipython#3806
191
245
@session . send ( :reply , :history_reply , history : [ ] )
192
246
end
193
247
248
+ # @private
194
249
def inspect_request ( msg )
195
250
# not yet implemented. See (#119).
196
251
@session . send ( :reply , :inspect_reply , status : :ok , found : false , data : { } , metadata : { } )
197
252
end
198
253
254
+ # @private
199
255
def comm_open ( msg )
200
256
comm_id = msg [ :content ] [ 'comm_id' ]
201
257
target_name = msg [ :content ] [ 'target_name' ]
202
258
Comm . comm [ comm_id ] = Comm . target [ target_name ] . new ( target_name , comm_id )
203
259
end
204
260
261
+ # @private
205
262
def comm_msg ( msg )
206
263
Comm . comm [ msg [ :content ] [ 'comm_id' ] ] . handle_msg ( msg [ :content ] [ 'data' ] )
207
264
end
208
265
266
+ # @private
209
267
def comm_close ( msg )
210
268
comm_id = msg [ :content ] [ 'comm_id' ]
211
269
Comm . comm [ comm_id ] . handle_close ( msg [ :content ] [ 'data' ] )
0 commit comments