/
kernel.cr
180 lines (156 loc) · 3.9 KB
/
kernel.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
require "c/unistd"
STDIN = IO::FileDescriptor.new(0, blocking: LibC.isatty(0) == 0)
STDOUT = (IO::FileDescriptor.new(1, blocking: LibC.isatty(1) == 0)).tap { |f| f.flush_on_newline = true }
STDERR = (IO::FileDescriptor.new(2, blocking: LibC.isatty(2) == 0)).tap { |f| f.flush_on_newline = true }
PROGRAM_NAME = String.new(ARGV_UNSAFE.value)
ARGV = (ARGV_UNSAFE + 1).to_slice(ARGC_UNSAFE - 1).map { |c_str| String.new(c_str) }
ARGF = IO::ARGF.new(ARGV, STDIN)
# Repeatedly executes the block, passing an incremental `Int32`
# that starts with 0.
#
# ```
# loop do |i|
# print "#{i}) "
# line = gets
# break unless line
# # ...
# end
# ```
def loop
i = 0
while true
yield i
i += 1
end
end
# Reads a line from STDIN. See `IO#gets`.
def gets(*args, **options)
STDIN.gets(*args, **options)
end
# Reads a line from STDIN. See `IO#read_line`.
def read_line(*args, **options)
STDIN.read_line(*args, **options)
end
# Prints objects to STDOUT and then invokes `STDOUT.flush`. See `IO#print`.
def print(*objects : _)
STDOUT.print *objects
STDOUT.flush
nil
end
# Prints a formatted string to STDOUT. See `IO#printf`.
def printf(format_string, *args)
printf format_string, args
end
# ditto
def printf(format_string, args : Array | Tuple)
STDOUT.printf format_string, args
end
# Returns a formatted string. See `IO#printf`.
def sprintf(format_string, *args) : String
sprintf format_string, args
end
# ditto
def sprintf(format_string, args : Array | Tuple) : String
String.build(format_string.bytesize) do |str|
String::Formatter(typeof(args)).new(format_string, args, str).format
end
end
# Prints objects to STDOUT, each followed by a newline. See `IO#puts`.
def puts(*objects)
STDOUT.puts *objects
end
# Pretty prints *object* to STDOUT followed
# by a newline. Returns *object*.
#
# See `Object#pretty_print(pp)`
def p(object)
PrettyPrint.format(object, STDOUT, 79)
puts
object
end
# Pretty prints each object in *objects* to STDOUT, followed
# by a newline. Returns *objects*.
#
# See `Object#pretty_print(pp)`
def p(*objects)
objects.each do |obj|
p obj
end
objects
end
# :nodoc:
module AtExitHandlers
@@running = false
def self.add(handler)
handlers = @@handlers ||= [] of Int32 ->
handlers << handler
end
def self.run(status)
return if @@running
@@running = true
@@handlers.try &.reverse_each do |handler|
begin
handler.call status
rescue handler_ex
STDERR.puts "Error running at_exit handler: #{handler_ex}"
end
end
end
end
# Registers the given `Proc` for execution when the program exits.
# If multiple handlers are registered, they are executed in reverse order of registration.
#
# ```
# def do_at_exit(str1)
# at_exit { print str1 }
# end
#
# at_exit { puts "cruel world" }
# do_at_exit("goodbye ")
# exit
# ```
#
# Produces:
#
# ```text
# goodbye cruel world
# ```
def at_exit(&handler : Int32 ->)
AtExitHandlers.add(handler)
end
# Terminates execution immediately, returning the given status code
# to the invoking environment.
#
# Registered `at_exit` procs are executed.
def exit(status = 0)
AtExitHandlers.run status
STDOUT.flush
STDERR.flush
Process.exit(status)
end
# Terminates execution immediately, printing *message* to STDERR and
# then calling `exit(status)`.
def abort(message, status = 1)
STDERR.puts message if message
exit status
end
class Process
# hooks defined here due to load order problems
def self.after_fork_child_callbacks
@@after_fork_child_callbacks ||= [
->{ Scheduler.after_fork; nil },
->{ Event::SignalHandler.after_fork; nil },
->{ Event::SignalChildHandler.instance.after_fork; nil },
->{ Random::DEFAULT.new_seed; nil },
] of -> Nil
end
end
Signal.setup_default_handlers
at_exit { Event::SignalHandler.close }
# Background loop to cleanup unused fiber stacks
spawn do
loop do
sleep 5
Fiber.stack_pool_collect
end
end