Class: IO

Inherits:
Object show all
Defined in:
opal/opal/corelib/io.rb

Overview

backtick_javascript: true

Direct Known Subclasses

File

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fd, flags = 'r') ⇒ IO

Returns a new instance of IO.

[View source]

17
18
19
20
21
22
23
24
25
26
27
# File 'opal/opal/corelib/io.rb', line 17

def initialize(fd, flags = 'r')
  @fd = fd
  @flags = flags
  @eof = false

  if flags.include?('r') && !flags.match?(/[wa+]/)
    @closed = :write
  elsif flags.match?(/[wa]/) && !flags.match?(/[r+]/)
    @closed = :read
  end
end

Instance Attribute Details

#eofObject (readonly) Also known as: eof?

Returns the value of attribute eof.


13
14
15
# File 'opal/opal/corelib/io.rb', line 13

def eof
  @eof
end

#read_procObject

Returns the value of attribute read_proc.


15
16
17
# File 'opal/opal/corelib/io.rb', line 15

def read_proc
  @read_proc
end

#syncObject

Returns the value of attribute sync.


15
16
17
# File 'opal/opal/corelib/io.rb', line 15

def sync
  @sync
end

#ttyObject

Returns the value of attribute tty.


15
16
17
# File 'opal/opal/corelib/io.rb', line 15

def tty
  @tty
end

#write_procObject

Returns the value of attribute write_proc.


15
16
17
# File 'opal/opal/corelib/io.rb', line 15

def write_proc
  @write_proc
end

Instance Method Details

#<<(string) ⇒ Object

[View source]

46
47
48
49
# File 'opal/opal/corelib/io.rb', line 46

def <<(string)
  write(string)
  self
end

#check_readableObject

[View source]

295
296
297
298
299
# File 'opal/opal/corelib/io.rb', line 295

def check_readable
  if closed_read?
    ::Kernel.raise ::IOError, 'not opened for reading'
  end
end

#check_writableObject

[View source]

288
289
290
291
292
# File 'opal/opal/corelib/io.rb', line 288

def check_writable
  if closed_write?
    ::Kernel.raise ::IOError, 'not opened for writing'
  end
end

#closeObject

Closedness

[View source]

255
256
257
# File 'opal/opal/corelib/io.rb', line 255

def close
  @closed = :both
end

#close_readObject

[View source]

259
260
261
262
263
264
265
# File 'opal/opal/corelib/io.rb', line 259

def close_read
  if @closed == :write
    @closed = :both
  else
    @closed = :read
  end
end

#close_writeObject

[View source]

267
268
269
270
271
272
273
# File 'opal/opal/corelib/io.rb', line 267

def close_write
  if @closed == :read
    @closed = :both
  else
    @closed = :write
  end
end

#closed?Boolean

Returns:

[View source]

275
276
277
# File 'opal/opal/corelib/io.rb', line 275

def closed?
  @closed == :both
end

#closed_read?Boolean

Returns:

[View source]

279
280
281
# File 'opal/opal/corelib/io.rb', line 279

def closed_read?
  @closed == :read || @closed == :both
end

#closed_write?Boolean

Returns:

[View source]

283
284
285
# File 'opal/opal/corelib/io.rb', line 283

def closed_write?
  @closed == :write || @closed == :both
end

#each(sep = $/, *args, &block) ⇒ Object Also known as: each_line

[View source]

223
224
225
226
227
228
229
230
231
# File 'opal/opal/corelib/io.rb', line 223

def each(sep = $/, *args, &block)
  return enum_for :each, sep, *args unless block_given?

  while (s = gets(sep, *args))
    yield(s)
  end

  self
end

#each_byte(&block) ⇒ Object

[View source]

233
234
235
236
237
238
239
240
241
# File 'opal/opal/corelib/io.rb', line 233

def each_byte(&block)
  return enum_for :each_byte unless block_given?

  while (s = getbyte)
    yield(s)
  end

  self
end

#each_char(&block) ⇒ Object

[View source]

243
244
245
246
247
248
249
250
251
# File 'opal/opal/corelib/io.rb', line 243

def each_char(&block)
  return enum_for :each_char unless block_given?

  while (s = getc)
    yield(s)
  end

  self
end

#filenoObject

[View source]

29
30
31
# File 'opal/opal/corelib/io.rb', line 29

def fileno
  @fd
end

#flushObject

[View source]

42
43
44
# File 'opal/opal/corelib/io.rb', line 42

def flush
  # noop
end

#getbyteObject

[View source]

107
108
109
# File 'opal/opal/corelib/io.rb', line 107

def getbyte
  getc&.ord
end

#getcObject

Reading

[View source]

89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'opal/opal/corelib/io.rb', line 89

def getc
  @read_buffer ||= ''
  parts = ''

  # Will execure at most twice - one time reading from a buffer
  # second time executing read proc
  begin
    @read_buffer += parts
    if @read_buffer != ''
      ret = @read_buffer[0]
      @read_buffer = @read_buffer[1..-1]
      return ret
    end
  end while parts = sysread_noraise(1)

  nil
end

#gets(sep = false, limit = nil, opts = {}) ⇒ Object

[View source]

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
# File 'opal/opal/corelib/io.rb', line 123

def gets(sep = false, limit = nil, opts = {})
  if `sep.$$is_number` && !limit
    sep, limit, opts = false, sep, limit
  end
  if `sep.$$is_hash` && !limit && opts == {}
    sep, limit, opts = false, nil, sep
  elsif `limit.$$is_hash` && opts == {}
    sep, limit, opts = sep, nil, limit
  end

  orig_sep = sep

  sep = $/ if sep == false
  sep = /\r?\n\r?\n/ if sep == ''
  sep ||= ''
  sep = sep.to_str unless orig_sep == ''

  # Try to deduce length of a regexp
  seplen = orig_sep == '' ? 2 : sep.length

  sep = / / if sep == ' ' # WTF is this, String#split(" ") matches all whitespaces???

  @read_buffer ||= ''
  data = ''
  ret = nil

  begin
    @read_buffer += data
    if sep != '' && (`sep.$$is_regexp` ? @read_buffer.match?(sep) : @read_buffer.include?(sep))
      orig_buffer = @read_buffer
      ret, @read_buffer = @read_buffer.split(sep, 2)
      ret += orig_buffer[ret.length, seplen] if ret != orig_buffer
      break
    end
  end while data = sysread_noraise(sep == '' ? 65_536 : 1)

  unless ret
    ret, @read_buffer = (@read_buffer || ''), ''
    ret = nil if ret == ''
  end

  if ret
    if limit
      ret = ret[0...limit]
      @read_buffer = ret[limit..-1] + @read_buffer
    end
    ret = ret.sub(/\r?\n\z/, '') if opts[:chomp]
    ret = ret.sub(/\A[\r\n]+/, '') if orig_sep == ''
  end

  $_ = ret if orig_sep == false
  ret
end
[View source]

51
52
53
54
55
56
57
58
59
# File 'opal/opal/corelib/io.rb', line 51

def print(*args)
  %x{
    for (var i = 0, ii = args.length; i < ii; i++) {
      args[i] = #{::Kernel.String(`args[i]`)}
    }
    self.$write(args.join(#{$,}));
  }
  nil
end

#puts(*args) ⇒ Object

[View source]

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
# File 'opal/opal/corelib/io.rb', line 61

def puts(*args)
  %x{
    var line
    if (args.length === 0) {
      #{write "\n"};
      return nil;
    } else {
      for (var i = 0, ii = args.length; i < ii; i++) {
        if (args[i].$$is_array){
          var ary = #{`args[i]`.flatten}
          if (ary.length > 0) #{puts(*`ary`)}
        } else {
          if (args[i].$$is_string) {
            line = args[i].valueOf();
          } else {
            line = #{::Kernel.String(`args[i]`)};
          }
          if (!line.endsWith("\n")) line += "\n"
          #{write `line`}
        }
      }
    }
  }
  nil
end

#read(integer = nil) ⇒ Object

[View source]

200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'opal/opal/corelib/io.rb', line 200

def read(integer = nil)
  @read_buffer ||= ''
  parts = ''
  ret = nil

  begin
    @read_buffer += parts
    if integer && @read_buffer.length > integer
      ret, @read_buffer = @read_buffer[0...integer], @read_buffer[integer..-1]
      return ret
    end
  end while parts = sysread_noraise(integer || 65_536)

  ret, @read_buffer = @read_buffer, ''
  ret
end

#readbyteObject

[View source]

111
112
113
# File 'opal/opal/corelib/io.rb', line 111

def readbyte
  readchar.ord
end

#readcharObject

[View source]

115
116
117
# File 'opal/opal/corelib/io.rb', line 115

def readchar
  getc || ::Kernel.raise(::EOFError, 'end of file reached')
end

#readline(*args) ⇒ Object

[View source]

119
120
121
# File 'opal/opal/corelib/io.rb', line 119

def readline(*args)
  gets(*args) || ::Kernel.raise(::EOFError, 'end of file reached')
end

#readlines(separator = $/) ⇒ Object

Eaches

[View source]

219
220
221
# File 'opal/opal/corelib/io.rb', line 219

def readlines(separator = $/)
  each_line(separator).to_a
end

#readpartial(integer) ⇒ Object

[View source]

192
193
194
195
196
197
198
# File 'opal/opal/corelib/io.rb', line 192

def readpartial(integer)
  @read_buffer ||= ''
  part = sysread(integer)
  ret, @read_buffer = @read_buffer + (part || ''), ''
  ret = nil if ret == ''
  ret
end

#sysread(integer) ⇒ Object

This method is to be overloaded, or read_proc can be changed

[View source]

178
179
180
181
182
183
# File 'opal/opal/corelib/io.rb', line 178

def sysread(integer)
  `self.read_proc(integer)` || begin
    @eof = true
    ::Kernel.raise ::EOFError, 'end of file reached'
  end
end

#sysread_noraise(integer) ⇒ Object

[View source]

186
187
188
189
190
# File 'opal/opal/corelib/io.rb', line 186

def sysread_noraise(integer)
  sysread(integer)
rescue ::EOFError
  nil
end

#tty?Boolean

Returns:

[View source]

33
34
35
# File 'opal/opal/corelib/io.rb', line 33

def tty?
  `self.tty == true`
end

#write(string) ⇒ Object

[View source]

37
38
39
40
# File 'opal/opal/corelib/io.rb', line 37

def write(string)
  `self.write_proc(string)`
  string.size
end