Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 738 lines (643 sloc) 17.461 kb
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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 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 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 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 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 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 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
# -*- encoding: us-ascii -*-

#
# This is the beginning of loading Ruby code. At this point, the VM
# is bootstrapped and the fundamental data structures, primitive
# functions and the basic classes and objects are available.
#
# The classes, modules, and methods defined here provide basic
# functionality needed to load the bootstrap directory. By the
# end of this file, the following methods are available:
#
# attr_reader :sym
# attr_writer :sym
# attr_accessor :sym
#
# private :sym
# protected :sym
#
# module_function :sym
#
# These forms should be used in the rest of the kernel. In delta,
# more complete forms of these methods are provided for user code.
#
# NOTE: The order of these definitions is important. Do not
# change it without consultation.


# This class encapsulates primitives that involve the VM
# itself rather than something in Ruby-land.
#
# See kernel/bootstrap/vm.rb
#
class Rubinius::VM

  # Write given string to stderr.
  #
  # Used to support error reporting where IO is not reliable yet.
  #
  def self.write_error(str)
    Rubinius.primitive :vm_write_error
    raise PrimitiveFailure, "Rubinius::VM.write_error primitive failed"
  end

  # Prints Ruby backtrace at point of call.
  #
  def self.show_backtrace
    Rubinius.primitive :vm_show_backtrace
    raise PrimitiveFailure, "Rubinius::VM.show_backtrace primitive failed"
  end

  # Reset the method cache globally for given method name.
  #
  def self.reset_method_cache(sym)
    Rubinius.primitive :vm_reset_method_cache
    raise PrimitiveFailure, "Rubinius::VM.reset_method_cache primitive failed"
  end
end


class Object
  # Prints basic information about the object to stdout.
  #
  def __show__
    Rubinius.primitive :object_show
    "<unknown>"
  end
end


class Class

  # Allocate memory for an instance of the class without initialization.
  #
  # The object returned is valid to use, but its #initialize
  # method has not been called. In almost all cases, .new is
  # the correct method to use instead.
  #
  # See .new
  #
  def allocate
    Rubinius.primitive :class_allocate
    raise RuntimeError, "Class#allocate primitive failed on #{self.inspect}"
  end

  # Allocate and initialize an instance of the class.
  #
  # Default implementation: merely allocates the instance, and
  # then calls the #initialize method on the object with the
  # given arguments and block, if provided.
  #
  # See .allocate
  #
  def new(*args)
    obj = allocate()

    Rubinius.asm(args, obj) do |args, obj|
      run obj
      run args
      push_block
      send_with_splat :initialize, 0, true
      # no pop here, as .asm blocks imply a pop as they're not
      # allowed to leak a stack value
    end

    return obj
  end
end


module Kernel

  # Return the Class object this object is an instance of.
  #
  # Note that this method must always be called with an
  # explicit receiver, since class by itself is a keyword.
  #
  def class
    Rubinius.primitive :object_class
    raise PrimitiveFailure, "Kernel#class primitive failed."
  end

  # Return the class object of self.
  #
  # This is the same as #class, but named to always be available.
  def __class__
    Rubinius.primitive :object_class
    raise PrimitiveFailure, "Kernel#class primitive failed."
  end

  # String representation of an object.
  #
  # By default, the representation is the name of the object's
  # class preceded by a # to indicate the object is an instance
  # thereof.
  #
  def to_s
    "#<#{self.class.name}>"
  end

  # :internal:
  #
  # Lowest-level implementation of raise, used internally by
  # kernel code until a more sophisticated version is loaded.
  #
  # Redefined later.
  #
  def raise(cls, str, junk=nil)
    Rubinius::VM.write_error "Fatal error loading runtime kernel:\n "
    Rubinius::VM.write_error str
    Rubinius::VM.write_error "\n"
    Rubinius::VM.show_backtrace
    Process.exit! 1
  end

  # Returns true if the given Class is either the class or superclass of the
  # object or, when given a Module, if the Module has been included in object's
  # class or one of its superclasses. Returns false otherwise.
  #
  # If the argument is not a Class or Module, a TypeError is raised.
  #
  def kind_of?(cls)
    Rubinius.primitive :object_kind_of
    raise TypeError, "Kernel#kind_of? primitive failed"
  end

  # Hook method invoked when object is sent a message it cannot handle.
  #
  # The default implementation will merely raise a NoMethodError with
  # information about the message.
  #
  # This method may be overridden, and is often used to provide dynamic
  # behaviour. An overriding version should call super if it fails to
  # resolve the message. This practice ensures that the default version
  # will be called if all else fails.
  #
  def method_missing(meth, *args)
    raise NoMethodError, "Unable to send '#{meth}' on '#{self}' (#{self.class})"
  end

  # :internal:
  #
  # Backend method for Object#dup and Object#clone.
  #
  # Redefined in kernel/common/kernel.rb
  #
  def initialize_copy(other)
  end

  # Generic shallow copy of object.
  #
  # Copies instance variables, but does not recursively copy the
  # objects they reference. Copies taintedness.
  #
  # In contrast to .clone, .dup can be considered as creating a
  # new object of the same class and populating it with data from
  # the object.
  #
  # If class-specific behaviour is desired, the class should
  # define #initialize_copy and implement the behaviour there.
  # #initialize_copy will automatically be called on the new
  # object - the copy - with the original object as argument
  # if defined.
  #
  def dup
    copy = Rubinius.invoke_primitive(:object_class, self).allocate

    Rubinius.invoke_primitive :object_copy_object, copy, self

    Rubinius.privately do
      copy.initialize_copy self
    end
    copy
  end

  # Direct shallow copy of object.
  #
  # Copies instance variables, but does not recursively copy the
  # objects they reference. Copies taintedness and frozenness.
  #
  # In contrast to .dup, .clone can be considered to actually
  # clone the existing object, including its internal state
  # and any singleton methods.
  #
  # If class-specific behaviour is desired, the class should
  # define #initialize_copy and implement the behaviour there.
  # #initialize_copy will automatically be called on the new
  # object - the copy - with the original object as argument
  # if defined.
  #
  def clone
    # Do not implement in terms of dup. It breaks rails.
    #
    cls = Rubinius.invoke_primitive :object_class, self
    copy = cls.allocate

    Rubinius.invoke_primitive :object_copy_object, copy, self
    Rubinius.invoke_primitive :object_copy_singleton_class, copy, self

    Rubinius.privately do
      copy.initialize_copy self
    end

    copy.freeze if frozen?
    copy
  end
end


# Module for internals.
#
# See kernel/bootstrap/rubinius.rb
#
module Rubinius

  # Executable abstraction for accessors.
  #
  class AccessVariable

    # Specialised allocation.
    #
    def self.allocate
      Rubinius.primitive :accessvariable_allocate
      raise PrimitiveFailure, "AccessVariable.allocate primitive failed"
    end

    # Set up the executable.
    #
    # Name of variable provided without leading @, the
    # second parameter should be true if the attr is
    # writable.
    #
    def initialize(variable, write)
      @primitive = nil
      @serial = 0
      @name = "@#{variable}".to_sym
      @write = write
    end

    # Get the arity for this reader or writr
    #
    def arity
      @write ? 1 : 0
    end

    # Create a getter for named instance var, without leading @.
    #
    def self.get_ivar(name)
      new(name, false)
    end

    # Create a setter for named instance var, without leading @.
    #
    def self.set_ivar(name)
      new(name, true)
    end
  end

  # Simplified lookup table.
  #
  # See kernel/bootstrap/lookuptable.rb.
  #
  class LookupTable

    # Retrieve value for given key.
    #
    def [](key)
      Rubinius.primitive :lookuptable_aref
      raise PrimitiveFailure, "LookupTable#[] primitive failed"
    end

    # Store value under key.
    #
    def []=(key, val)
      Rubinius.primitive :lookuptable_store
      raise PrimitiveFailure, "LookupTable#[]= primitive failed"
    end
  end

  # Lookup table for storing methods.
  #
  # See kernel/bootstrap/methodtable.rb and
  # kernel/common/method_table.rb
  #
  class MethodTable

    # Perform lookup for method name.
    #
    def lookup(name)
      Rubinius.primitive :methodtable_lookup
      raise PrimitiveFailure, "MethodTable#lookup primitive failed"
    end

    # Store Executable under name, with given visibility.
    #
    def store(name, exec, visibility)
      Rubinius.primitive :methodtable_store
      raise PrimitiveFailure, "MethodTable#store primitive failed"
    end

    # Make an alias from +original_name+ in +original_mod+ to +name+
    # with visibility +vis+
    def alias(name, visibility, original_name, original_exec, original_mod)
      Rubinius.primitive :methodtable_alias
      raise PrimitiveFailure, "MethodTable#alias primitive failed"
    end
  end
end


class Symbol
  # Produce String representation of this Symbol.
  #
  def to_s
    Rubinius.primitive :symbol_to_s
    raise PrimitiveFailure, "Symbol#to_s primitive failed"
  end

  # For completeness, returns self.
  #
  def to_sym
    self
  end
end


class String
  # Returns the <code>Symbol</code> corresponding to <i>self</i>, creating the
  # symbol if it did not previously exist. See <code>Symbol#id2name</code>.
  #
  # "Koala".intern #=> :Koala
  # s = 'cat'.to_sym #=> :cat
  # s == :cat #=> true
  # s = '@cat'.to_sym #=> :@cat
  # s == :@cat #=> true
  #
  # This can also be used to create symbols that cannot be represented using the
  # <code>:xxx</code> notation.
  #
  # 'cat and dog'.to_sym #=> :"cat and dog"
  #--
  # TODO: Add taintedness-check
  #++
  def to_sym
    Rubinius.primitive :symbol_lookup
    raise PrimitiveFailure, "String#to_sym primitive failed: #{self.dump}"
  end

  # For completeness, returns self.
  #
  def to_s
    self
  end
end


module Process
  # Terminate with given status code.
  #
  def self.exit(code=0)
    case code
    when true
      code = 0
    when false
      code = 1
    else
      code = Rubinius::Type.coerce_to code, Integer, :to_int
    end

    raise SystemExit.new(code)
  end

  def self.exit!(code=1)
    Rubinius.primitive :vm_exit

    case code
    when true
      exit! 0
    when false
      exit! 1
    else
      exit! Rubinius::Type.coerce_to(code, Integer, :to_int)
    end
  end
end


class Module
  def method_table ; @method_table ; end
  def constant_table; @constant_table ; end
  def name ; @module_name.to_s ; end

  # Specialised allocator.
  #
  def self.allocate
    Rubinius.primitive :module_allocate
    raise PrimitiveFailure, "Module.allocate primitive failed"
  end

  # :internal:
  #
  # Hook called when a constant cannot be located.
  #
  # Default implementation 'raises', but we don't use #raise
  # to prevent infinite recursion.
  #
  # Redefined in kernel/common/module.rb
  #
  def const_missing(name)
    Rubinius::VM.write_error "Missing or uninitialized constant: \n"
    Rubinius::VM.write_error name.to_s
    Rubinius::VM.write_error "\n"
  end

  # Set Module's direct superclass.
  #
  # The corresponding 'getter' #superclass method is defined
  # in class.rb, because it is more complex than a mere
  # accessor
  #
  def superclass=(other)
    @superclass = other
  end

  # Module's stored superclass.
  #
  # This may be either an included Module or an inherited Class.
  #
  def direct_superclass
    @superclass
  end

  # :internal:
  #
  # Perform actual work for including a Module in this one.
  #
  # Redefined in kernel/delta/module.rb.
  #
  def append_features(mod)
    im = Rubinius::IncludedModule.new(self)
    im.attach_to mod
  end

  # Hook method called on Module when another Module is .include'd into it.
  #
  # Override for module-specific behaviour.
  #
  def included(mod); end

  # :internal:
  #
  # Basic version of .include used in kernel code.
  #
  # Redefined in kernel/common/module.rb.
  #
  def include(mod)
    mod.append_features(self)
    Rubinius.privately do
      mod.included self
    end
    self
  end

  # :internal:
  #
  # Basic version used in kernel code.
  #
  # Redefined in kernel/common/module.rb.
  #
  def attr_reader(name)
    meth = Rubinius::AccessVariable.get_ivar name
    @method_table.store name, meth, :public
    Rubinius::VM.reset_method_cache name
    nil
  end

  def attr_reader_specific(name, method_name)
    meth = Rubinius::AccessVariable.get_ivar name
    @method_table.store method_name, meth, :public
    Rubinius::VM.reset_method_cache method_name
    nil
  end

  # :internal:
  #
  # Basic version used in kernel code.
  #
  # Redefined in kernel/common/module.rb.
  #
  def attr_writer(name)
    meth = Rubinius::AccessVariable.set_ivar name
    @method_table.store "#{name}=".to_sym, meth, :public
    Rubinius::VM.reset_method_cache name
    nil
  end

  # :internal:
  #
  # Basic version used in kernel code.
  #
  # Redefined in kernel/common/module.rb.
  #
  def attr_accessor(name)
    attr_reader(name)
    attr_writer(name)
    nil
  end

  # :internal:
  #
  # Basic version used in kernel code.
  # Cannot be used as a toggle, and only
  # takes a single method name.
  #
  # Redefined in kernel/common/module.rb.
  #
  def private(name)
    if entry = @method_table.lookup(name)
      entry.visibility = :private
    end
  end

  # :internal:
  #
  # Basic version used in kernel code.
  # Cannot be used as a toggle, and only
  # takes a single method name.
  #
  # Redefined in kernel/common/module.rb.
  #
  def protected(name)
    if entry = @method_table.lookup(name)
      entry.visibility = :protected
    end
  end

  # :internal:
  #
  # Basic version used in kernel code. Creates a copy
  # of current method and stores it under the new name.
  # The two are independent.
  #
  # Redefined in kernel/common/module.rb.
  #
  def alias_method(new_name, current_name)
    # If we're aliasing a method we contain, just reference it directly, no
    # need for the alias wrapper
    if entry = @method_table.lookup(current_name)
      @method_table.store new_name, entry.method, entry.visibility
    else
      mod = direct_superclass()
      while mod
        entry = mod.method_table.lookup(current_name)
        break if entry
        mod = mod.direct_superclass
      end

      unless entry
        raise NoMethodError, "No method '#{current_name}' to alias to '#{new_name}'"
      end

      @method_table.alias new_name, entry.visibility, current_name,
                          entry.method, mod
    end

    Rubinius::VM.reset_method_cache(new_name)
  end

  # :internal:
  #
  # Basic version used in kernel code. Only
  # takes a single method name.
  #
  # Redefined in kernel/common/module.rb.
  #
  def module_function(name)
    if entry = @method_table.lookup(name)
      sc = class << self; self; end
      sc.method_table.store name, entry.method, :public
      Rubinius::VM.reset_method_cache name
      private name
    end
  end
end


module Rubinius

  class AccessVariable
    attr_reader :name
  end

  # Visibility handling for MethodTables.
  #
  # See kernel/bootstrap/methodtable.rb and
  # kernel/common/method_table.rb
  #
  class MethodTable::Bucket
    attr_accessor :visibility
    attr_accessor :method

    def public?
      @visibility == :public
    end

    def private?
      @visibility == :private
    end

    def protected?
      @visibility == :protected
    end
  end

  # :internal:
  #
  # Internal representation of a Module's inclusion in another.
  #
  # Abstracts the injection of the included Module into the
  # ancestor hierarchy for method- and constant lookup in a
  # roughly transparent fashion.
  #
  # This class is known to the VM.
  #
  class IncludedModule < Module
    attr_reader :superclass
    attr_reader :module

    # :internal:
    #
    # Specialised allocator.
    #
    def self.allocate
      Rubinius.primitive :included_module_allocate
      raise PrimitiveFailure, "IncludedModule.allocate primitive failed"
    end

    # :internal:
    #
    # Created referencing the Module that is being included.
    #
    def initialize(mod)
      @method_table = mod.method_table
      @constant_table = mod.constant_table
      @module = mod
    end

    def module
      @module
    end

    # :internal:
    #
    # Inject self inbetween class and its previous direct
    # superclass.
    #
    def attach_to(cls)
      @superclass = cls.direct_superclass
      cls.superclass = self
    end

    # :internal:
    #
    # Name of the included Module.
    #
    def name
      @module.name
    end

    # :internal:
    #
    # String representation of the included Module.
    #
    def to_s
      @module.to_s
    end

    def inspect
      "#<IncludedModule #{@module.to_s}>"
    end

    # Returns true if +other+ is the same object as self or if +other+
    # is the module this IncludedModule is for.
    #
    def ==(other)
      if other.kind_of? IncludedModule
        super || other.module == @module
      else
        super || other == @module
      end
    end
  end
end

class Object
  include Kernel
end

Something went wrong with that request. Please try again.