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.41 lib/flok/interactive.rb
flok-0.0.40 lib/flok/interactive.rb
flok-0.0.39 lib/flok/interactive.rb
flok-0.0.38 lib/flok/interactive.rb
flok-0.0.36 lib/flok/interactive.rb
flok-0.0.35 lib/flok/interactive.rb
flok-0.0.34 lib/flok/interactive.rb
flok-0.0.33 lib/flok/interactive.rb
flok-0.0.32 lib/flok/interactive.rb
flok-0.0.31 lib/flok/interactive.rb
flok-0.0.30 lib/flok/interactive.rb
flok-0.0.29 lib/flok/interactive.rb
flok-0.0.28 lib/flok/interactive.rb
flok-0.0.27 lib/flok/interactive.rb
flok-0.0.26 lib/flok/interactive.rb
flok-0.0.25 lib/flok/interactive.rb
flok-0.0.24 lib/flok/interactive.rb
flok-0.0.23 lib/flok/interactive.rb
flok-0.0.21 lib/flok/interactive.rb
flok-0.0.20 lib/flok/interactive.rb