Skip to content

Commit

Permalink
Showing 3 changed files with 89 additions and 9 deletions.
1 change: 1 addition & 0 deletions truffle/src/main/ruby/core.rb
Original file line number Diff line number Diff line change
@@ -121,6 +121,7 @@ def self.omit(reason)
require_relative 'core/rubinius/common/enumerable'
require_relative 'core/rubinius/common/enumerator'
require_relative 'core/rubinius/common/argf'
require_relative 'core/rubinius/api/shims/argf'
#require_relative 'core/rubinius/common/tuple'
require_relative 'core/rubinius/common/exception'
require_relative 'core/rubinius/api/shims/exception'
81 changes: 81 additions & 0 deletions truffle/src/main/ruby/core/rubinius/api/shims/argf.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Copyright (c) 2007-2015, Evan Phoenix and contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of Rubinius nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

module Rubinius
class ARGFClass
attr_reader :argv

# Truffle: adapted to take argv as input
def initialize(argv = ARGV, *others)
@argv = argv.equal?(ARGV) ? ARGV : [argv, *others]
@lineno = 0
@advance = true
@init = false
@use_stdin_only = false
@encoding_args = nil
end

# Truffle: adapted to take argv as input
def advance!
return true unless @advance

unless @init

if @argv.empty?
@advance = false
@stream = STDIN
@filename = "-"
@use_stdin_only = true
return true
end
@init = true
end

File.unlink(@backup_filename) if @backup_filename && $-i == ""

return false if @use_stdin_only || @argv.empty?

@advance = false

file = @argv.shift
@stream = stream(file)
@filename = file

if $-i && @stream != STDIN
backup_extension = $-i == "" ? ".bak" : $-i
@backup_filename = "#{@filename}#{backup_extension}"
File.rename(@filename, @backup_filename)
@stream = File.open(@backup_filename, "r")
$stdout = File.open(@filename, "w")
end

return true
end
private :advance!
end
end

ARGF = Rubinius::ARGFClass.new(ARGV)
16 changes: 7 additions & 9 deletions truffle/src/main/ruby/core/rubinius/common/argf.rb
Original file line number Diff line number Diff line change
@@ -37,7 +37,6 @@ module Rubinius
#
class ARGFClass
include Enumerable
attr_reader :argv # Truffle

# :internal:
#
@@ -47,9 +46,7 @@ class ARGFClass
#
# @see #advance!
#
# Truffle: adapted to take argv as input
def initialize(argv = ARGV, *others)
@argv = argv.equal?(ARGV) ? ARGV : [argv, *others]
def initialize
@lineno = 0
@advance = true
@init = false
@@ -515,13 +512,12 @@ def to_s
# there are further file names in ARGV, tries to open
# the next one as the current stream.
#
# Truffle: adapted to take argv as input
def advance!
return true unless @advance

unless @init

if @argv.empty?
if ARGV.empty?
@advance = false
@stream = STDIN
@filename = "-"
@@ -533,11 +529,11 @@ def advance!

File.unlink(@backup_filename) if @backup_filename && $-i == ""

return false if @use_stdin_only || @argv.empty?
return false if @use_stdin_only || ARGV.empty?

@advance = false

file = @argv.shift
file = ARGV.shift
@stream = stream(file)
@filename = file

@@ -559,4 +555,6 @@ def advance!
# The virtual concatenation file of the files given on command line (or
# from $stdin if no files were given.) Usable like an IO.
#
ARGF = Rubinius::ARGFClass.new(ARGV) # Truffle
Truffle.omit("We define this in api/shims/argf with an additional argument to the constructor") do
ARGF = Rubinius::ARGFClass.new
end

0 comments on commit 9a7c788

Please sign in to comment.