Class: Opal::CliRunners::Nashorn

Inherits:
Object
  • Object
show all
Defined in:
opal/lib/opal/cli_runners/nashorn.rb

Defined Under Namespace

Classes: MissingNashorn

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Nashorn

Returns a new instance of Nashorn.



8
9
10
# File 'opal/lib/opal/cli_runners/nashorn.rb', line 8

def initialize(options)
  @output = options.fetch(:output, $stdout)
end

Instance Attribute Details

#exit_statusObject (readonly)

Returns the value of attribute exit_status.



11
12
13
# File 'opal/lib/opal/cli_runners/nashorn.rb', line 11

def exit_status
  @exit_status
end

#outputObject (readonly)

Returns the value of attribute output.



11
12
13
# File 'opal/lib/opal/cli_runners/nashorn.rb', line 11

def output
  @output
end

Instance Method Details

#puts(*args) ⇒ Object



13
14
15
# File 'opal/lib/opal/cli_runners/nashorn.rb', line 13

def puts(*args)
  output.puts(*args)
end

#run(code, argv) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'opal/lib/opal/cli_runners/nashorn.rb', line 17

def run(code, argv)
  require 'tempfile'
  tempfile = Tempfile.new('opal-nashorn-runner-')
  tempfile.write code
  tempfile.close
  system_with_output({}, 'jjs', tempfile.path, *argv)
rescue Errno::ENOENT
  raise MissingNashorn, 'Please install JDK to be able to run Opal scripts.'
end

#system_with_output(env, *cmd) ⇒ Object

Let's support fake IO objects like StringIO



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
# File 'opal/lib/opal/cli_runners/nashorn.rb', line 28

def system_with_output(env, *cmd)
  if IO.try_convert(output)
    system(env, *cmd)
    @exit_status = $?.exitstatus
    return
  end

  if RUBY_PLATFORM == 'java'
    # JRuby has issues in dealing with subprocesses (at least up to 1.7.15)
    # @headius told me it's mostly fixed on master, but while we wait for it
    # to ship here's a tempfile workaround.
    require 'tempfile'
    require 'shellwords'
    tempfile = Tempfile.new('opal-nashorn-output')
    system(env, cmd.shelljoin + " > #{tempfile.path}")
    @exit_status = $?.exitstatus
    captured_output = File.read tempfile.path
    tempfile.close
  else
    require 'open3'
    captured_output, status = Open3.capture2(env, *cmd)
    @exit_status = status.exitstatus
  end

  output.write captured_output
end