Class: ExecJS::ExternalRuntime

Inherits:
Runtime
  • Object
show all
Defined in:
lib/execjs/external_runtime.rb

Defined Under Namespace

Classes: Context

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Runtime

#compile, #context_class, #eval, #exec

Constructor Details

#initialize(options) ⇒ ExternalRuntime

Returns a new instance of ExternalRuntime.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/execjs/external_runtime.rb', line 93

def initialize(options)
  @name        = options[:name]
  @command     = options[:command]
  @runner_path = options[:runner_path]
  @encoding    = options[:encoding]
  @deprecated  = !!options[:deprecated]
  @binary      = nil

  @popen_options = {}
  @popen_options[:external_encoding] = @encoding if @encoding
  @popen_options[:internal_encoding] = ::Encoding.default_internal || 'UTF-8'

  if @runner_path
    instance_eval <<~RUBY, __FILE__, __LINE__
      def compile_source(source)
        <<-RUNNER
        #{IO.read(@runner_path)}
        RUNNER
      end
    RUBY
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



91
92
93
# File 'lib/execjs/external_runtime.rb', line 91

def name
  @name
end

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


116
117
118
119
# File 'lib/execjs/external_runtime.rb', line 116

def available?
  require 'json'
  binary ? true : false
end

#deprecated?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/execjs/external_runtime.rb', line 121

def deprecated?
  @deprecated
end

#exec_runtime(filename) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/execjs/external_runtime.rb', line 169

def exec_runtime(filename)
  path = Dir::Tmpname.create(['execjs', 'json']) {}
  begin
    command = binary.split(" ") << filename
    `#{shell_escape(*command)} 2>&1 > #{path}`
    output = File.open(path, 'rb', **@popen_options) { |f| f.read }
  ensure
    File.unlink(path) if path
  end

  if $?.success?
    output
  else
    raise exec_runtime_error(output)
  end
end