Class: ExecJS::ExternalRuntime::Context

### Home Classes Methods

### In Files

- lib/execjs/external_runtime.rb

### Parent

Runtime::Context

### Methods

- ::new
- #call
- #create_tempfile
- #eval
- #exec
- #extract_result
- #write_to_tempfile

### Class/Module Index

Quicksearch

- ExecJS
- ExecJS::DisabledRuntime
- 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::Context

### Public Class Methods

new(runtime, source = "") click to toggle source

    # File lib/execjs/external_runtime.rb, line 7
    def initialize(runtime, source = "")
      source = encode(source)

      @runtime = runtime
      @source  = source

      # Test compile context source
      exec("")
    end

### Public Instance Methods

call(identifier, *args) click to toggle source

    # File lib/execjs/external_runtime.rb, line 38
    def call(identifier, *args)
      eval "#{identifier}.apply(this, #{::JSON.generate(args)})"
    end

eval(source, options = {}) click to toggle source

    # File lib/execjs/external_runtime.rb, line 17
    def eval(source, options = {})
      source = encode(source)

      if /\S/ =~ source
        exec("return eval(#{::JSON.generate("(#{source})", quirks_mode: true)})")
      end
    end

exec(source, options = {}) click to toggle source

    # File lib/execjs/external_runtime.rb, line 25
    def exec(source, options = {})
      source = encode(source)
      source = "#{@source}\n#{source}" if @source != ""
      source = @runtime.compile_source(source)

      tmpfile = write_to_tempfile(source)
      begin
        extract_result(@runtime.exec_runtime(tmpfile.path), tmpfile.path)
      ensure
        File.unlink(tmpfile)
      end
    end

### Protected Instance Methods

create_tempfile(basename) click to toggle source

See Tempfile.create on Ruby 2.1

    # File lib/execjs/external_runtime.rb, line 44
    def create_tempfile(basename)
      tmpfile = nil
      Dir::Tmpname.create(basename) do |tmpname|
        mode    = File::WRONLY | File::CREAT | File::EXCL
        tmpfile = File.open(tmpname, mode, 0600)
      end
      tmpfile
    end

extract_result(output, filename) click to toggle source

    # File lib/execjs/external_runtime.rb, line 60
    def extract_result(output, filename)
      status, value, stack = output.empty? ? [] : ::JSON.parse(output, create_additions: false)
      if status == "ok"
        value
      else
        stack ||= ""
        real_filename = File.realpath(filename)
        stack = stack.split("\n").map do |line|
          line.sub(" at ", "")
              .sub(real_filename, "(execjs)")
              .sub(filename, "(execjs)")
              .strip
        end
        stack.reject! { |line| ["eval code", "eval@[native code]"].include?(line) }
        stack.shift unless stack[0].to_s.include?("(execjs)")
        error_class = value =~ /SyntaxError:/ ? RuntimeError : ProgramError
        error = error_class.new(value)
        error.set_backtrace(stack + caller)
        raise error
      end
    end

write_to_tempfile(contents) click to toggle source

    # File lib/execjs/external_runtime.rb, line 53
    def write_to_tempfile(contents)
      tmpfile = create_tempfile(['execjs', 'js'])
      tmpfile.write(contents)
      tmpfile.close
      tmpfile
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.