Class: Peruby::Runtime
- Inherits:
-
Object
- Object
- Peruby::Runtime
- Defined in:
- lib/peruby/runtime.rb
Overview
Process-wide interpreter state.
Instance Attribute Summary collapse
-
#current_file ⇒ Object
readonly
rubocop:disable Metrics/ClassLength.
-
#file_stat_path ⇒ Object
readonly
rubocop:disable Metrics/ClassLength.
-
#last_input_handle ⇒ Object
readonly
rubocop:disable Metrics/ClassLength.
-
#local_stack ⇒ Object
readonly
rubocop:disable Metrics/ClassLength.
-
#match_state ⇒ Object
readonly
rubocop:disable Metrics/ClassLength.
-
#module_loader ⇒ Object
readonly
rubocop:disable Metrics/ClassLength.
-
#mro ⇒ Object
readonly
rubocop:disable Metrics/ClassLength.
-
#stash ⇒ Object
readonly
rubocop:disable Metrics/ClassLength.
-
#stderr ⇒ Object
readonly
rubocop:disable Metrics/ClassLength.
-
#stdout ⇒ Object
readonly
rubocop:disable Metrics/ClassLength.
-
#test_builder ⇒ Object
readonly
rubocop:disable Metrics/ClassLength.
Instance Method Summary collapse
- #call(code, arguments, context) ⇒ Object
- #call_compiled(code, arguments, context) ⇒ Object
- #configure_warnings(categories, disable: false) ⇒ Object
- #destroy(reference) ⇒ Object
- #die(values, file: @current_file, line: 1) ⇒ Object
- #emit_warning(message, file: @current_file, line: 1) ⇒ Object
- #env ⇒ Object
- #error_message(message, file: @current_file, line: 1) ⇒ Object
- #file_stat(path, lstat: false) ⇒ Object
-
#initialize(stdin: $stdin, stdout: $stdout, stderr: $stderr, refcount: false) ⇒ Runtime
constructor
rubocop:disable-next Metrics/AbcSize, Metrics/MethodLength.
- #install_signal(name, value) ⇒ Object
- #match!(match, subject) ⇒ Object
- #note_input(handle) ⇒ Object
- #number(value) ⇒ Object
- #open_next_argv ⇒ Object
- #output(handle, env = nil) ⇒ Object
- #overload_table(reference) ⇒ Object
- #overloaded(reference, operator, other = nil, swapped: false) ⇒ Object
- #overloads? ⇒ Boolean
- #prototype_arity(prototype) ⇒ Object
- #read_argv(separator) ⇒ Object
- #register_object(reference) ⇒ Object
- #register_overload(package, pairs, disable: false) ⇒ Object
- #register_phase(kind, operation, env: self.env) ⇒ Object
- #release(reference) ⇒ Object
- #run_phase(kind, reverse: false) ⇒ Object
- #select_output(handle = nil) ⇒ Object
- #shutdown ⇒ Object
- #update_environment(name, value) ⇒ Object
- #validate_prototype(code, count) ⇒ Object
- #warning(category, message) ⇒ Object
- #warning_categories ⇒ Object
- #warning_enabled?(category) ⇒ Boolean
- #warnings_active? ⇒ Boolean
- #with_file(file) ⇒ Object
- #with_match_scope ⇒ Object
- #with_warning_scope ⇒ Object
Constructor Details
#initialize(stdin: $stdin, stdout: $stdout, stderr: $stderr, refcount: false) ⇒ Runtime
rubocop:disable-next Metrics/AbcSize, Metrics/MethodLength
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 |
# File 'lib/peruby/runtime.rb', line 15 def initialize(stdin: $stdin, stdout: $stdout, stderr: $stderr, refcount: false) @stash = Stash.new @stdout = stdout @stderr = stderr @match_state = MatchState.new @local_stack = LocalStack.new @mro = MRO.new(@stash) @refcount = refcount @objects = {}.compare_by_identity @destroyed = {}.compare_by_identity @module_loader = ModuleLoader.new(self) @test_builder = TestBuilder.new(stdout) @current_file = '-e' @phases = Hash.new { |phases, name| phases[name] = [] } @overloads = {} @warnings = Set.new @warning_switch = @stash.glob('^W').scalar @stash.glob('$').scalar.set(Process.pid) @stash.glob('SIG').hash = PerlHash.new { |name, value| install_signal(name, value) } env_cells = ENV.to_h { |name, value| [name, Scalar.new(value)] } @stash.glob('ENV').hash = PerlHash.new(env_cells) { |name, value| update_environment(name, value) } @stash.glob('"').scalar.set(' ') @stash.glob(',').scalar.set(nil) @stash.glob('\\').scalar.set(nil) @stash.glob('/').scalar.set("\n") @stdin_handle = IOHandle.new(stdin) @stash.glob('STDIN').io = @stdin_handle @stash.glob('STDOUT').io = IOHandle.new(stdout) @stash.glob('STDERR').io = IOHandle.new(stderr) @stash.glob('ARGVOUT').io = IOHandle.new(stdout) @selected_output = @stash.glob('STDOUT').io @stash.glob('|').scalar = Scalar.new { |value| @selected_output.io.sync = Conv.truthy?(value) } @stash.glob('INC').array.cells << Scalar.new('.') end |
Instance Attribute Details
#current_file ⇒ Object (readonly)
rubocop:disable Metrics/ClassLength
10 11 12 |
# File 'lib/peruby/runtime.rb', line 10 def current_file @current_file end |
#file_stat_path ⇒ Object (readonly)
rubocop:disable Metrics/ClassLength
10 11 12 |
# File 'lib/peruby/runtime.rb', line 10 def file_stat_path @file_stat_path end |
#last_input_handle ⇒ Object (readonly)
rubocop:disable Metrics/ClassLength
10 11 12 |
# File 'lib/peruby/runtime.rb', line 10 def last_input_handle @last_input_handle end |
#local_stack ⇒ Object (readonly)
rubocop:disable Metrics/ClassLength
10 11 12 |
# File 'lib/peruby/runtime.rb', line 10 def local_stack @local_stack end |
#match_state ⇒ Object (readonly)
rubocop:disable Metrics/ClassLength
10 11 12 |
# File 'lib/peruby/runtime.rb', line 10 def match_state @match_state end |
#module_loader ⇒ Object (readonly)
rubocop:disable Metrics/ClassLength
10 11 12 |
# File 'lib/peruby/runtime.rb', line 10 def module_loader @module_loader end |
#mro ⇒ Object (readonly)
rubocop:disable Metrics/ClassLength
10 11 12 |
# File 'lib/peruby/runtime.rb', line 10 def mro @mro end |
#stash ⇒ Object (readonly)
rubocop:disable Metrics/ClassLength
10 11 12 |
# File 'lib/peruby/runtime.rb', line 10 def stash @stash end |
#stderr ⇒ Object (readonly)
rubocop:disable Metrics/ClassLength
10 11 12 |
# File 'lib/peruby/runtime.rb', line 10 def stderr @stderr end |
#stdout ⇒ Object (readonly)
rubocop:disable Metrics/ClassLength
10 11 12 |
# File 'lib/peruby/runtime.rb', line 10 def stdout @stdout end |
#test_builder ⇒ Object (readonly)
rubocop:disable Metrics/ClassLength
10 11 12 |
# File 'lib/peruby/runtime.rb', line 10 def test_builder @test_builder end |
Instance Method Details
#call(code, arguments, context) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/peruby/runtime.rb', line 129 def call(code, arguments, context) return call_compiled(code, arguments, context) if code.body.is_a?(Op::Compiled) validate_prototype(code, arguments.length) with_match_scope do @local_stack.within do loop do env = code.environment.fork(want: context, package: code.package, state_owner: code) env.bind(:array, '_', PerlArray.new(arguments)) result = code.body.run_subroutine(env, context) if result.is_a?(GotoRequest) code = result.code next end return context == :list ? Array(result).map(&:copy) : result end end end end |
#call_compiled(code, arguments, context) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/peruby/runtime.rb', line 149 def call_compiled(code, arguments, context) validate_prototype(code, arguments.length) if code.prototype previous_match = @match_state @local_stack.within do loop do env = code.environment.call_frame(arguments, want: context, package: code.package, state_owner: code) result = code.body.run_subroutine(env, context) if result.is_a?(GotoRequest) code = result.code next end return context == :list ? Array(result).map(&:copy) : result end end ensure @match_state = previous_match end |
#configure_warnings(categories, disable: false) ⇒ Object
190 191 192 193 |
# File 'lib/peruby/runtime.rb', line 190 def configure_warnings(categories, disable: false) selected = categories.empty? ? %w[uninitialized numeric once redefine] : categories disable ? @warnings.subtract(selected) : @warnings.merge(selected) end |
#destroy(reference) ⇒ Object
316 317 318 319 320 321 322 323 |
# File 'lib/peruby/runtime.rb', line 316 def destroy(reference) return unless reference.is_a?(Ref) && reference.blessed return if @destroyed[reference] @destroyed[reference] = true entry = @mro.resolve(reference.blessed, 'DESTROY') call(entry.last, [Scalar.new(reference)], :void) if entry end |
#die(values, file: @current_file, line: 1) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/peruby/runtime.rb', line 115 def die(values, file: @current_file, line: 1) object = values.one? && values.first.is_a?(Ref) ? values.first : nil text = values.map { |value| Conv.to_str(value) }.join = object ? Conv.to_str(object) : (text, file:, line:) handler = @stash.glob('SIG').hash.fetch('__DIE__') handler = handler.target if handler.is_a?(Ref) && handler.kind == 'CODE' previous = @handling_die @handling_die = true call(handler, [Scalar.new(object || )], :void) if handler.is_a?(Code) && !previous raise PerlError.new(, value: object || , formatted: true) ensure @handling_die = previous end |
#emit_warning(message, file: @current_file, line: 1) ⇒ Object
211 212 213 214 215 216 217 218 |
# File 'lib/peruby/runtime.rb', line 211 def emit_warning(, file: @current_file, line: 1) formatted = (, file:, line:) handler = @stash.glob('SIG').hash.fetch('__WARN__') handler = handler.target if handler.is_a?(Ref) && handler.kind == 'CODE' return call(handler, [Scalar.new(formatted)], :void) if handler.is_a?(Code) @stderr.print(formatted) end |
#error_message(message, file: @current_file, line: 1) ⇒ Object
111 112 113 |
# File 'lib/peruby/runtime.rb', line 111 def (, file: @current_file, line: 1) .end_with?("\n") ? : "#{} at #{file} line #{line}.\n" end |
#file_stat(path, lstat: false) ⇒ Object
226 227 228 229 230 231 232 |
# File 'lib/peruby/runtime.rb', line 226 def file_stat(path, lstat: false) return @file_stat if path == '_' @file_stat_path = path @file_stat = nil @file_stat = File.public_send(lstat ? :lstat : :stat, path) end |
#install_signal(name, value) ⇒ Object
247 248 249 250 251 252 253 254 255 256 |
# File 'lib/peruby/runtime.rb', line 247 def install_signal(name, value) return if %w[__DIE__ __WARN__].include?(name) handler = value.is_a?(Ref) && value.kind == 'CODE' ? value.target : value return Signal.trap(name, handler || 'DEFAULT') unless handler.is_a?(Code) Signal.trap(name) { call(handler, [Scalar.new(name)], :void) } rescue ArgumentError => e raise PerlError, e. end |
#match!(match, subject) ⇒ Object
92 93 94 |
# File 'lib/peruby/runtime.rb', line 92 def match!(match, subject) @match_state = MatchState.new(match, subject) end |
#note_input(handle) ⇒ Object
87 88 89 90 |
# File 'lib/peruby/runtime.rb', line 87 def note_input(handle) @last_input_handle = handle @stash.glob('.').scalar.set(handle.lineno) end |
#number(value) ⇒ Object
220 221 222 223 224 |
# File 'lib/peruby/runtime.rb', line 220 def number(value) warning('uninitialized', 'Use of uninitialized value') if value.nil? warning('numeric', %(Argument "#{value}" isn't numeric)) if value.is_a?(String) && !Conv::NUM_RE.match?(value) Conv.to_num(value) end |
#open_next_argv ⇒ Object
291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/peruby/runtime.rb', line 291 def open_next_argv cell = @stash.glob('ARGV').array.cells.shift if cell path = Conv.to_str(cell.get) @stash.glob('ARGV').scalar.set(path) @argv_handle = path == '-' ? @stdin_handle : IOHandle.new(File.open(path)) elsif !defined?(@argv_started) @argv_started = true @stash.glob('ARGV').scalar.set('-') @argv_handle = @stdin_handle else @argv_finished = true end end |
#output(handle, env = nil) ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/peruby/runtime.rb', line 54 def output(handle, env = nil) return @selected_output.io unless handle if env && handle lexical = env.fetch(:scalar, handle).get return lexical.io if lexical.is_a?(IOHandle) end @stash.glob(handle).io&.io || (handle == 'STDERR' ? @stderr : @stdout) end |
#overload_table(reference) ⇒ Object
285 286 287 288 289 |
# File 'lib/peruby/runtime.rb', line 285 def overload_table(reference) return unless reference.is_a?(Ref) && reference.blessed @mro.lineage(reference.blessed).filter_map { |package| @overloads[package] }.first end |
#overloaded(reference, operator, other = nil, swapped: false) ⇒ Object
270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/peruby/runtime.rb', line 270 def overloaded(reference, operator, other = nil, swapped: false) table = overload_table(reference) return [false, nil] unless table method = table[operator] || table['nomethod'] return [false, nil] unless method entry = @mro.resolve(reference.blessed, Conv.to_str(method), autoload: false) return [false, nil] unless entry arguments = [Scalar.new(reference), Scalar.new(other), Scalar.new(swapped ? 1 : '')] arguments << Scalar.new(operator) if table[operator].nil? [true, call(entry.last, arguments, :scalar)] end |
#overloads? ⇒ Boolean
268 |
# File 'lib/peruby/runtime.rb', line 268 def overloads? = !@overloads.empty? |
#prototype_arity(prototype) ⇒ Object
179 180 181 |
# File 'lib/peruby/runtime.rb', line 179 def prototype_arity(prototype) prototype.scan(/\\?[$@%&*+_]/).length end |
#read_argv(separator) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/peruby/runtime.rb', line 70 def read_argv(separator) loop do open_next_argv unless @argv_handle || @argv_finished return nil unless @argv_handle value = @argv_handle.read_record(separator) if value @last_input_handle = @argv_handle @argv_lineno = (@argv_lineno || 0) + 1 @stash.glob('.').scalar.set(@argv_lineno) return value end @argv_handle.close unless @argv_handle.equal?(@stdin_handle) @argv_handle = nil end end |
#register_object(reference) ⇒ Object
234 235 236 |
# File 'lib/peruby/runtime.rb', line 234 def register_object(reference) @objects[reference] = true end |
#register_overload(package, pairs, disable: false) ⇒ Object
262 263 264 265 266 |
# File 'lib/peruby/runtime.rb', line 262 def register_overload(package, pairs, disable: false) return @overloads.delete(package) if disable @overloads[package] = pairs end |
#register_phase(kind, operation, env: self.env) ⇒ Object
306 307 308 |
# File 'lib/peruby/runtime.rb', line 306 def register_phase(kind, operation, env: self.env) @phases[kind] << [operation, env] end |
#release(reference) ⇒ Object
238 239 240 |
# File 'lib/peruby/runtime.rb', line 238 def release(reference) destroy(reference) if @refcount end |
#run_phase(kind, reverse: false) ⇒ Object
310 311 312 313 314 |
# File 'lib/peruby/runtime.rb', line 310 def run_phase(kind, reverse: false) operations = @phases.delete(kind) || [] operations.reverse! if reverse operations.each { |operation, phase_env| operation.run(phase_env, :void) } end |
#select_output(handle = nil) ⇒ Object
64 65 66 67 68 |
# File 'lib/peruby/runtime.rb', line 64 def select_output(handle = nil) previous = @selected_output @selected_output = handle if handle previous end |
#shutdown ⇒ Object
242 243 244 245 |
# File 'lib/peruby/runtime.rb', line 242 def shutdown run_phase(:end, reverse: true) @objects.each_key { |reference| destroy(reference) } end |
#update_environment(name, value) ⇒ Object
258 259 260 |
# File 'lib/peruby/runtime.rb', line 258 def update_environment(name, value) value.nil? ? ENV.delete(name) : ENV[name] = Conv.to_str(value) end |
#validate_prototype(code, count) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/peruby/runtime.rb', line 167 def validate_prototype(code, count) prototype = code.prototype return if prototype.nil? required, _, optional = prototype.partition(';') minimum = prototype_arity(required.delete_suffix('@').delete_suffix('%')) maximum = required.match?(/[@%]\z/) ? nil : minimum + prototype_arity(optional) return if count >= minimum && (maximum.nil? || count <= maximum) raise PerlError, "Wrong number of arguments for #{code.name || 'anonymous subroutine'}" end |
#warning(category, message) ⇒ Object
205 206 207 208 209 |
# File 'lib/peruby/runtime.rb', line 205 def warning(category, ) return unless warning_enabled?(category) emit_warning() end |
#warning_categories ⇒ Object
203 |
# File 'lib/peruby/runtime.rb', line 203 def warning_categories = @warnings.dup |
#warning_enabled?(category) ⇒ Boolean
195 196 197 |
# File 'lib/peruby/runtime.rb', line 195 def warning_enabled?(category) @warnings.include?(category) || Conv.truthy?(@warning_switch.get) end |
#warnings_active? ⇒ Boolean
199 200 201 |
# File 'lib/peruby/runtime.rb', line 199 def warnings_active? !@warnings.empty? || Conv.truthy?(@warning_switch.get) end |
#with_file(file) ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/peruby/runtime.rb', line 103 def with_file(file) previous = @current_file @current_file = file yield ensure @current_file = previous end |
#with_match_scope ⇒ Object
96 97 98 99 100 101 |
# File 'lib/peruby/runtime.rb', line 96 def with_match_scope previous = @match_state yield ensure @match_state = previous end |
#with_warning_scope ⇒ Object
183 184 185 186 187 188 |
# File 'lib/peruby/runtime.rb', line 183 def with_warning_scope previous = @warnings.dup yield ensure @warnings = previous end |