Class: ExecJS::ExternalRuntime

### Home Classes Methods

### In Files

- lib/execjs/external_runtime.rb

### Parent

Runtime

### Namespace

- CLASS ExecJS::ExternalRuntime::Context

### Methods

- ::new
- #available?
- #deprecated?
- #encode_source
- #encode_unicode_codepoints
- #exec_runtime
- #exec_runtime_error
- #generate_compile_method
- #json2_source
- #shell_escape
- #which

### Class/Module Index

Quicksearch

- ExecJS
- ExecJS::DisabledRuntime
- ExecJS::DuktapeRuntime
- ExecJS::DuktapeRuntime::Context
- ExecJS::Encoding
- ExecJS::Error
- ExecJS::ExternalRuntime
- ExecJS::ExternalRuntime::Context
- ExecJS::ProgramError
- ExecJS::RubyRacerRuntime
- ExecJS::RubyRacerRuntime::Context
- ExecJS::RubyRhinoRuntime
- ExecJS::RubyRhinoRuntime::Context
- ExecJS::Runtime
- ExecJS::Runtime::Context
- ExecJS::RuntimeError
- ExecJS::RuntimeUnavailable
- ExecJS::Runtimes

No matching classes.

# ExecJS::ExternalRuntime

### Attributes

name[R]

### Public Class Methods

new(options) click to toggle source

    # File lib/execjs/external_runtime.rb, line 92
    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 109
    def available?
      require 'json'
      binary ? true : false
    end

deprecated?() click to toggle source

    # File lib/execjs/external_runtime.rb, line 114
    def deprecated?
      @deprecated
    end

exec_runtime(filename) click to toggle source

    # File lib/execjs/external_runtime.rb, line 166
    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

### Protected Instance Methods

encode_source(source) click to toggle source

    # File lib/execjs/external_runtime.rb, line 154
    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 159
    def encode_unicode_codepoints(str)
      str.gsub(/[\u0080-\uffff]/) do |ch|
        "\\u%04x" % ch.codepoints.to_a
      end
    end

exec_runtime_error(output) click to toggle source

Internally exposed for Context.

    # File lib/execjs/external_runtime.rb, line 221
    def exec_runtime_error(output)
      error = RuntimeError.new(output)
      lines = output.split("\n")
      lineno = lines[0][/:(\d+)$/, 1] if lines[0]
      lineno ||= 1
      error.set_backtrace(["(execjs):#{lineno}"] + caller)
      error
    end

generate_compile_method(path) click to toggle source

    # File lib/execjs/external_runtime.rb, line 140
    def generate_compile_method(path)
              def compile_source(source)          <<-RUNNER          #{IO.read(path)}          RUNNER        end
    end

json2_source() click to toggle source

    # File lib/execjs/external_runtime.rb, line 150
    def json2_source
      @json2_source ||= IO.read(ExecJS.root + "/support/json2.js")
    end

shell_escape(*args) click to toggle source

    # File lib/execjs/external_runtime.rb, line 183
    def shell_escape(*args)
      # see http://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection123121120120
      args.map { |arg|
        arg = %("#{arg.gsub('"','""')}") if arg.match(/[&|()<>^ "]/)
        arg
      }.join(" ")
    end

which(command) click to toggle source

    # File lib/execjs/external_runtime.rb, line 230
    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

[Validate]

Generated with the Darkfish Rdoc Generator 2.