class ExecJS::ExternalRuntime

Attributes

name[R]

Public Class Methods

new(options) click to toggle source
# File lib/execjs/external_runtime.rb, line 71
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 generate_compile_method(@runner_path)
  end
end

Public Instance Methods

available?() click to toggle source
# File lib/execjs/external_runtime.rb, line 88
def available?
  require 'json'
  binary ? true : false
end
deprecated?() click to toggle source
# File lib/execjs/external_runtime.rb, line 93
def deprecated?
  @deprecated
end
exec_runtime(filename) click to toggle source
# File lib/execjs/external_runtime.rb, line 144
def exec_runtime(filename)
  io = IO.popen(binary.split(' ') + [filename, {err: [:child, :out]}], @popen_options)
  output = io.read
  io.close

  if $?.success?
    output
  else
    raise RuntimeError, output
  end
end

Protected Instance Methods

encode_source(source) click to toggle source
# File lib/execjs/external_runtime.rb, line 133
def encode_source(source)
  encoded_source = encode_unicode_codepoints(source)
  ::JSON.generate("(function(){ #{encoded_source} })()", quirks_mode: true)
end
encode_unicode_codepoints(str) click to toggle source
# File lib/execjs/external_runtime.rb, line 138
def encode_unicode_codepoints(str)
  str.gsub(/[\u0080-\uffff]/) do |ch|
    "\\u%04x" % ch.codepoints.to_a
  end
end
generate_compile_method(path) click to toggle source
# File lib/execjs/external_runtime.rb, line 119
def generate_compile_method(path)
  <<-RUBY
  def compile_source(source)
    <<-RUNNER
    #{IO.read(path)}
    RUNNER
  end
  RUBY
end
json2_source() click to toggle source
# File lib/execjs/external_runtime.rb, line 129
def json2_source
  @json2_source ||= IO.read(ExecJS.root + "/support/json2.js")
end
which(command) click to toggle source

Internally exposed for Context.

# File lib/execjs/external_runtime.rb, line 158
def which(command)
  Array(command).find do |name|
    name, args = name.split(/\s+/, 2)
    path = locate_executable(name)

    next unless path

    args ? "#{path} #{args}" : path
  end
end

Private Instance Methods

binary() click to toggle source
# File lib/execjs/external_runtime.rb, line 98
def binary
  @binary ||= which(@command)
end
locate_executable(cmd) click to toggle source
# File lib/execjs/external_runtime.rb, line 102
def locate_executable(cmd)
  if ExecJS.windows? && File.extname(cmd) == ""
    cmd << ".exe"
  end

  if File.executable? cmd
    cmd
  else
    path = ENV['PATH'].split(File::PATH_SEPARATOR).find { |p|
      full_path = File.join(p, cmd)
      File.executable?(full_path) && File.file?(full_path)
    }
    path && File.expand_path(cmd, path)
  end
end