Sha256: d897c659969a8739baa3b1306bd35c3dc878f71367e26d7667eb561edd4639ce

Contents?: true

Size: 1.33 KB

Versions: 5

Compression:

Stored size: 1.33 KB

Contents

%x{
  function executeIOAction(action) {
    try {
      return action();
    } catch (error) {
      if (error.code === 'EACCES' ||
          error.code === 'EISDIR' ||
          error.code === 'EMFILE' ||
          error.code === 'ENOENT' ||
          error.code === 'EPERM') {
        throw Opal.IOError.$new(error.message)
      }
      throw error;
    }
  }
}

`var __fs__ = require('fs')`

class IO
  @__fs__ = `__fs__`

  attr_reader :lineno

  alias initialize_before_node_io initialize

  def initialize(fd, flags = 'r')
    @lineno = 0
    initialize_before_node_io(fd, flags)
  end

  def self.write(path, data)
    File.write(path, data)
  end

  def self.read(path)
    File.read(path)
  end

  def self.binread(path)
    `return executeIOAction(function(){return __fs__.readFileSync(#{path}).toString('binary')})`
  end
end

STDOUT.write_proc = ->(string) { `process.stdout.write(string)` }
STDERR.write_proc = ->(string) { `process.stderr.write(string)` }

STDIN.read_proc = %x{function(_count) {
  // Ignore count, return as much as we can get
  var buf = Buffer.alloc(65536), count;
  try {
    count = __fs__.readSync(this.fd, buf, 0, 65536, null);
  }
  catch (e) { // Windows systems may raise EOF
    return nil;
  }
  if (count == 0) return nil;
  return buf.toString('utf8', 0, count);
}}

STDIN.tty = true
STDOUT.tty = true
STDERR.tty = true

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
opal-1.3.2 stdlib/nodejs/io.rb
opal-1.3.1 stdlib/nodejs/io.rb
opal-1.3.0 stdlib/nodejs/io.rb
opal-1.3.0.rc1 stdlib/nodejs/io.rb
opal-1.3.0.alpha1 stdlib/nodejs/io.rb