Sha256: a56f888b9fc87bbc40ee5a8a5adac3846e2bd65793b136f024ed0da650ca203b

Contents?: true

Size: 1.56 KB

Versions: 85

Compression:

Stored size: 1.56 KB

Contents

require 'therubyracer'
require 'v8'

#Interactive pipe for testing. 
#Creates a server that routes $stdin into int_dispatch and $stdout to inf_dispatch.

module Flok
  class InteractiveServer
    def initialize app_js_path
      @app_js_path = app_js_path
      @ctx = V8::Context.new
      @ctx.load app_js_path

      inject_ruby_into_js
      inject_dispatch_shims
    end

    #Will take over any remaining IO
    def begin_pipe
      loop do
        begin
          q = JSON.parse(gets)
          @ctx[:int_dispatch].call(q)
        rescue Errno::EIO
          #Can't say anything here, we don't have a pipe
        rescue V8::Error => e
          $stderr.puts "\n--------------------------------------------------" 
          $stderr.puts "V8 Boo-Boo for #{@app_js_path}"
          $stderr.puts "--------------------------------------------------\n"
          $stderr.puts e.message
          $stderr.puts "--------------------------------------------------\n" 
          exit 1
        end
      end
    end

    #JS Functions#####################################################
    def inject_ruby_into_js 
      #Output to stdout
      @ctx["write"] = lambda do |this, str|
        str.each_line do |line|
          $stdout.puts line
          $stdout.flush
        end
      end
    end

    #Make calls to if_dispatch go to $stdout, make $stdin call int_dispatch
    def inject_dispatch_shims
      @ctx.eval %{
        function if_dispatch(q) {
          write(JSON.stringify(q));
        }
      }
    end
    ##################################################################
  end
end

Version data entries

85 entries across 85 versions & 1 rubygems

Version Path
flok-0.0.105 lib/flok/interactive.rb
flok-0.0.103 lib/flok/interactive.rb
flok-0.0.102 lib/flok/interactive.rb
flok-0.0.101 lib/flok/interactive.rb
flok-0.0.100 lib/flok/interactive.rb
flok-0.0.99 lib/flok/interactive.rb
flok-0.0.98 lib/flok/interactive.rb
flok-0.0.97 lib/flok/interactive.rb
flok-0.0.96 lib/flok/interactive.rb
flok-0.0.95 lib/flok/interactive.rb
flok-0.0.94 lib/flok/interactive.rb
flok-0.0.93 lib/flok/interactive.rb
flok-0.0.92 lib/flok/interactive.rb
flok-0.0.91 lib/flok/interactive.rb
flok-0.0.90 lib/flok/interactive.rb
flok-0.0.89 lib/flok/interactive.rb
flok-0.0.88 lib/flok/interactive.rb
flok-0.0.87 lib/flok/interactive.rb
flok-0.0.86 lib/flok/interactive.rb
flok-0.0.85 lib/flok/interactive.rb