Class: SyntaxTree::YARV::VM
- Inherits:
-
Object
- Object
- SyntaxTree::YARV::VM
- Extended by:
- Forwardable
- Defined in:
- lib/syntax_tree/yarv/vm.rb
Defined Under Namespace
Classes: BlockFrame, BreakError, ClassFrame, Frame, FrozenCore, Jump, Leave, MethodFrame, NextError, NullEvents, RescueFrame, ReturnError, STDOUTEvents, Stack, ThrownError, TopFrame
Constant Summary collapse
- FROZEN_CORE =
FrozenCore.new.freeze
- DLEXT =
Methods for overriding runtime behavior
".#{RbConfig::CONFIG["DLEXT"]}"- SOEXT =
".#{RbConfig::CONFIG["SOEXT"]}"
Instance Attribute Summary collapse
-
#events ⇒ Object
readonly
Returns the value of attribute events.
-
#frame ⇒ Object
readonly
Returns the value of attribute frame.
-
#stack ⇒ Object
readonly
Returns the value of attribute stack.
Instance Method Summary collapse
- #catch(tag, &block) ⇒ Object
-
#const_base ⇒ Object
Helper methods for instructions.
- #eval(source, binding = TOPLEVEL_BINDING, filename = "(eval)", lineno = 1) ⇒ Object
- #find_catch_entry(frame, type) ⇒ Object
- #frame_at(level) ⇒ Object
- #frame_svar ⇒ Object
- #frame_yield ⇒ Object
- #frozen_core ⇒ Object
-
#initialize(events = NullEvents.new) ⇒ VM
constructor
A new instance of VM.
- #jump(label) ⇒ Object
- #leave ⇒ Object
- #load(filepath) ⇒ Object
- #local_get(index, level) ⇒ Object
- #local_set(index, level, value) ⇒ Object
- #require(filepath) ⇒ Object
- #require_internal(filepath, loading: false) ⇒ Object
- #require_relative(filepath) ⇒ Object
- #require_resolved(filepath) ⇒ Object
- #run_block_frame(iseq, frame, *args, **kwargs, &block) ⇒ Object
- #run_class_frame(iseq, clazz) ⇒ Object
-
#run_frame(frame) ⇒ Object
Helper methods for frames.
- #run_method_frame(name, nesting, iseq, _self, *args, **kwargs, &block) ⇒ Object
- #run_rescue_frame(iseq, frame, error) ⇒ Object
- #run_top_frame(iseq) ⇒ Object
- #setup_arguments(iseq, args, kwargs, block) ⇒ Object
- #throw(tag, value = nil) ⇒ Object
Constructor Details
#initialize(events = NullEvents.new) ⇒ VM
Returns a new instance of VM.
216 217 218 219 220 |
# File 'lib/syntax_tree/yarv/vm.rb', line 216 def initialize(events = NullEvents.new) @events = events @stack = Stack.new(events) @frame = nil end |
Instance Attribute Details
#events ⇒ Object (readonly)
Returns the value of attribute events.
209 210 211 |
# File 'lib/syntax_tree/yarv/vm.rb', line 209 def events @events end |
#frame ⇒ Object (readonly)
Returns the value of attribute frame.
214 215 216 |
# File 'lib/syntax_tree/yarv/vm.rb', line 214 def frame @frame end |
#stack ⇒ Object (readonly)
Returns the value of attribute stack.
211 212 213 |
# File 'lib/syntax_tree/yarv/vm.rb', line 211 def stack @stack end |
Instance Method Details
#catch(tag, &block) ⇒ Object
619 620 621 |
# File 'lib/syntax_tree/yarv/vm.rb', line 619 def catch(tag, &block) Kernel.catch(tag, &block) end |
#const_base ⇒ Object
Helper methods for instructions
490 491 492 |
# File 'lib/syntax_tree/yarv/vm.rb', line 490 def const_base frame.nesting.last end |
#eval(source, binding = TOPLEVEL_BINDING, filename = "(eval)", lineno = 1) ⇒ Object
606 607 608 609 610 611 612 613 |
# File 'lib/syntax_tree/yarv/vm.rb', line 606 def eval( source, binding = TOPLEVEL_BINDING, filename = "(eval)", lineno = 1 ) Kernel.yarv_eval(source, binding, filename, lineno) end |
#find_catch_entry(frame, type) ⇒ Object
340 341 342 343 344 345 346 347 348 349 350 |
# File 'lib/syntax_tree/yarv/vm.rb', line 340 def find_catch_entry(frame, type) iseq = frame.iseq iseq.catch_table.find do |catch_entry| next unless catch_entry.is_a?(type) begin_pc = iseq.insns.index(catch_entry.begin_label) end_pc = iseq.insns.index(catch_entry.end_label) (begin_pc...end_pc).cover?(frame.pc) end end |
#frame_at(level) ⇒ Object
494 495 496 497 498 |
# File 'lib/syntax_tree/yarv/vm.rb', line 494 def frame_at(level) current = frame level.times { current = current.parent } current end |
#frame_svar ⇒ Object
500 501 502 503 504 |
# File 'lib/syntax_tree/yarv/vm.rb', line 500 def frame_svar current = frame current = current.parent while current.is_a?(BlockFrame) current end |
#frame_yield ⇒ Object
506 507 508 509 510 |
# File 'lib/syntax_tree/yarv/vm.rb', line 506 def frame_yield current = frame current = current.parent until current.is_a?(MethodFrame) current end |
#frozen_core ⇒ Object
512 513 514 |
# File 'lib/syntax_tree/yarv/vm.rb', line 512 def frozen_core FROZEN_CORE end |
#jump(label) ⇒ Object
516 517 518 |
# File 'lib/syntax_tree/yarv/vm.rb', line 516 def jump(label) Jump.new(label) end |
#leave ⇒ Object
520 521 522 |
# File 'lib/syntax_tree/yarv/vm.rb', line 520 def leave Leave.new(pop) end |
#load(filepath) ⇒ Object
602 603 604 |
# File 'lib/syntax_tree/yarv/vm.rb', line 602 def load(filepath) require_internal(filepath, loading: true) end |
#local_get(index, level) ⇒ Object
524 525 526 |
# File 'lib/syntax_tree/yarv/vm.rb', line 524 def local_get(index, level) stack[frame_at(level).stack_index + index] end |
#local_set(index, level, value) ⇒ Object
528 529 530 |
# File 'lib/syntax_tree/yarv/vm.rb', line 528 def local_set(index, level, value) stack[frame_at(level).stack_index + index] = value end |
#require(filepath) ⇒ Object
594 595 596 |
# File 'lib/syntax_tree/yarv/vm.rb', line 594 def require(filepath) require_internal(filepath, loading: false) end |
#require_internal(filepath, loading: false) ⇒ Object
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 |
# File 'lib/syntax_tree/yarv/vm.rb', line 545 def require_internal(filepath, loading: false) case (extname = File.extname(filepath)) when "" # search for all the extensions searching = filepath extensions = ["", ".rb", DLEXT, SOEXT] when ".rb", DLEXT, SOEXT # search only for the given extension name searching = File.basename(filepath, extname) extensions = [extname] else # we don't handle these extensions, raise a load error raise LoadError, "cannot load such file -- #{filepath}" end if filepath.start_with?("/") # absolute path, search only in the given directory directories = [File.dirname(searching)] searching = File.basename(searching) else # relative path, search in the load path directories = $LOAD_PATH end directories.each do |directory| extensions.each do |extension| absolute_path = File.join(directory, "#{searching}#{extension}") next unless File.exist?(absolute_path) if !loading && $LOADED_FEATURES.include?(absolute_path) return false elsif extension == ".rb" require_resolved(absolute_path) return true elsif loading return Kernel.send(:yarv_load, filepath) else return Kernel.send(:yarv_require, filepath) end end end if loading Kernel.send(:yarv_load, filepath) else Kernel.send(:yarv_require, filepath) end end |
#require_relative(filepath) ⇒ Object
598 599 600 |
# File 'lib/syntax_tree/yarv/vm.rb', line 598 def require_relative(filepath) Kernel.yarv_require_relative(filepath) end |
#require_resolved(filepath) ⇒ Object
539 540 541 542 543 |
# File 'lib/syntax_tree/yarv/vm.rb', line 539 def require_resolved(filepath) $LOADED_FEATURES << filepath iseq = RubyVM::InstructionSequence.compile_file(filepath) run_top_frame(InstructionSequence.from(iseq.to_a)) end |
#run_block_frame(iseq, frame, *args, **kwargs, &block) ⇒ Object
356 357 358 359 360 |
# File 'lib/syntax_tree/yarv/vm.rb', line 356 def run_block_frame(iseq, frame, *args, **kwargs, &block) run_frame(BlockFrame.new(iseq, frame, stack.length)) do setup_arguments(iseq, args, kwargs, block) end end |
#run_class_frame(iseq, clazz) ⇒ Object
362 363 364 |
# File 'lib/syntax_tree/yarv/vm.rb', line 362 def run_class_frame(iseq, clazz) run_frame(ClassFrame.new(iseq, frame, stack.length, clazz)) end |
#run_frame(frame) ⇒ Object
Helper methods for frames
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/syntax_tree/yarv/vm.rb', line 226 def run_frame(frame) # First, set the current frame to the given value. previous = @frame @frame = frame events.publish_frame_change(@frame) # Next, set up the local table for the frame. This is actually incorrect # as it could use the values already on the stack, but for now we're # just doing this for simplicity. stack.concat(Array.new(frame.iseq.local_table.size)) # Yield so that some frame-specific setup can be done. start_label = yield if block_given? frame.pc = frame.iseq.insns.index(start_label) if start_label # Finally we can execute the instructions one at a time. If they return # jumps or leaves we will handle those appropriately. loop do case (insn = frame.iseq.insns[frame.pc]) when Integer frame.line = insn frame.pc += 1 when Symbol events.publish_tracepoint(insn) frame.pc += 1 when InstructionSequence::Label # skip labels frame.pc += 1 else begin events.publish_instruction(frame.iseq, insn) result = insn.call(self) rescue ReturnError => error raise if frame.iseq.type != :method stack.slice!(frame.stack_index..) @frame = frame.parent events.publish_frame_change(@frame) return error.value rescue BreakError => error raise if frame.iseq.type != :block catch_entry = find_catch_entry(frame, InstructionSequence::CatchBreak) raise unless catch_entry stack.slice!( ( frame.stack_index + frame.iseq.local_table.size + catch_entry.restore_sp ).. ) @frame = frame events.publish_frame_change(@frame) frame.pc = frame.iseq.insns.index(catch_entry.exit_label) push(result = error.value) rescue NextError => error raise if frame.iseq.type != :block catch_entry = find_catch_entry(frame, InstructionSequence::CatchNext) raise unless catch_entry stack.slice!( ( frame.stack_index + frame.iseq.local_table.size + catch_entry.restore_sp ).. ) @frame = frame events.publish_frame_change(@frame) frame.pc = frame.iseq.insns.index(catch_entry.exit_label) push(result = error.value) rescue Exception => error catch_entry = find_catch_entry(frame, InstructionSequence::CatchRescue) raise unless catch_entry stack.slice!( ( frame.stack_index + frame.iseq.local_table.size + catch_entry.restore_sp ).. ) @frame = frame events.publish_frame_change(@frame) frame.pc = frame.iseq.insns.index(catch_entry.exit_label) push(result = run_rescue_frame(catch_entry.iseq, frame, error)) end case result when Jump frame.pc = frame.iseq.insns.index(result.label) + 1 when Leave # this shouldn't be necessary, but is because we're not handling # the stack correctly at the moment stack.slice!(frame.stack_index..) # restore the previous frame @frame = previous || frame.parent events.publish_frame_change(@frame) if @frame return result.value else frame.pc += 1 end end end end |
#run_method_frame(name, nesting, iseq, _self, *args, **kwargs, &block) ⇒ Object
366 367 368 369 370 371 372 373 374 375 376 377 378 |
# File 'lib/syntax_tree/yarv/vm.rb', line 366 def run_method_frame(name, nesting, iseq, _self, *args, **kwargs, &block) run_frame( MethodFrame.new( iseq, nesting, frame, stack.length, _self, name, block ) ) { setup_arguments(iseq, args, kwargs, block) } end |
#run_rescue_frame(iseq, frame, error) ⇒ Object
380 381 382 383 384 385 |
# File 'lib/syntax_tree/yarv/vm.rb', line 380 def run_rescue_frame(iseq, frame, error) run_frame(RescueFrame.new(iseq, frame, stack.length)) do local_set(0, 0, error) nil end end |
#run_top_frame(iseq) ⇒ Object
352 353 354 |
# File 'lib/syntax_tree/yarv/vm.rb', line 352 def run_top_frame(iseq) run_frame(TopFrame.new(iseq)) end |
#setup_arguments(iseq, args, kwargs, block) ⇒ Object
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 |
# File 'lib/syntax_tree/yarv/vm.rb', line 387 def setup_arguments(iseq, args, kwargs, block) locals = [*args] local_index = 0 start_label = nil # First, set up all of the leading arguments. These are positional and # required arguments at the start of the argument list. if (lead_num = iseq.[:lead_num]) lead_num.times do local_set(local_index, 0, locals.shift) local_index += 1 end end # Next, set up all of the optional arguments. The opt array contains # the labels that the frame should start at if the optional is # present. The last element of the array is the label that the frame # should start at if all of the optional arguments are present. if (opt = iseq.[:opt]) opt[0...-1].each do |label| if locals.empty? start_label = label break else local_set(local_index, 0, locals.shift) local_index += 1 end start_label = opt.last if start_label.nil? end end # If there is a splat argument, then we'll set that up here. It will # grab up all of the remaining positional arguments. if (rest_start = iseq.[:rest_start]) if (post_start = iseq.[:post_start]) length = post_start - rest_start local_set(local_index, 0, locals[0...length]) locals = locals[length..] else local_set(local_index, 0, locals.dup) locals.clear end local_index += 1 end # Next, set up any post arguments. These are positional arguments that # come after the splat argument. if (post_num = iseq.[:post_num]) post_num.times do local_set(local_index, 0, locals.shift) local_index += 1 end end if (keyword_option = iseq.[:keyword]) # First, set up the keyword bits array. keyword_bits = keyword_option.map do |config| kwargs.key?(config.is_a?(Array) ? config[0] : config) end iseq.local_table.locals.each_with_index do |local, index| # If this is the keyword bits local, then set it appropriately. if local.name.is_a?(Integer) local_set(index, 0, keyword_bits) next end # First, find the configuration for this local in the keywords # list if it exists. name = local.name config = keyword_option.find do |keyword| keyword.is_a?(Array) ? keyword[0] == name : keyword == name end # If the configuration doesn't exist, then the local is not a # keyword local. next unless config if !config.is_a?(Array) # required keyword local_set(index, 0, kwargs.fetch(name)) elsif !config[1].nil? # optional keyword with embedded default value local_set(index, 0, kwargs.fetch(name, config[1])) else # optional keyword with expression default value local_set(index, 0, kwargs[name]) end end end local_set(local_index, 0, block) if iseq.[:block_start] start_label end |
#throw(tag, value = nil) ⇒ Object
615 616 617 |
# File 'lib/syntax_tree/yarv/vm.rb', line 615 def throw(tag, value = nil) Kernel.throw(tag, value) end |