#!/usr/bin/env ruby require 'rubygems' require 'pathname' require 'johnson/tracemonkey' require 'johnson/cli' require 'envjs/runtime' RUNTIME = js = Johnson::Runtime.new RUNTIME.extend Envjs::Runtime RUNTIME.load Envjs::ENVJS if ARGV.length > 0 begin about_blank = true ARGV.each do |file| if file == "about:blank" RUNTIME.wait RUNTIME.evaluate("window.location = 'about:blank'", "(comand line)", 1) about_blank = true elsif file =~ /\.x?html?$/ || file =~ %r(^https?://) # this might want to go alway; jazrb does it manually now, which I think was the use case # that wanted this if !about_blank RUNTIME.wait RUNTIME.evaluate("window.location = 'about:blank'", "(comand line)", 1) end RUNTIME.wait RUNTIME.evaluate("window.location = 'file://#{Pathname.new(file).realpath}'", file, 1) about_blank = false else RUNTIME.become_first_script_window RUNTIME.load file end end rescue Johnson::Error => je if je.message.match // exit end raise je end RUNTIME.wait else require "readline" require "johnson/cli" RUNTIME.evaluate(Johnson::CLI::JS) EXIT_VERBS = [nil] + %w(exit quit) local_binding = binding ruby_readline = [] def copy_history new_history = [] until Readline::HISTORY.empty? new_history.push(Readline::HISTORY.pop) end new_history end def paste_history(old_history) until old_history.empty? Readline::HISTORY << old_history.pop end end def handle_exit(input) if EXIT_VERBS.include?(input) puts if input.nil? exit end end def rescued(&block) yield rescue Exception => e exit if SystemExit === e puts e.message puts e.backtrace.reject { |l| l =~ /bin\/johnson/ } end def eval_in_js(expression) rescued { puts "=> " + RUNTIME.evaluate(expression, "(console)").inspect } end def eval_in_ruby(expression, bind_to) rescued { puts "=> " + eval(expression, bind_to).inspect } end options = Johnson::CLI::Options.parse!(ARGV) options.load_paths.each { |d| $LOAD_PATH << d } options.paths_to_require.each { |p| RUNTIME.evaluate("Johnson.require('#{p}')") } options.files_to_preload.each { |f| RUNTIME.load(f) } unless options.expressions.empty? options.expressions.each { |e| RUNTIME.evaluate(e, '-e') } exit if options.files_to_evaluate.empty? end unless options.files_to_evaluate.empty? RUNTIME[:arguments] = options.arguments options.files_to_evaluate.each do |file| RUNTIME.load(file) end exit end loop do input = Readline.readline("js> ", true) handle_exit(input) if input =~ /^rb\s+(.+)$/ eval_in_ruby($1, local_binding) next end if input == "rb" js_readline = copy_history paste_history(ruby_readline) loop do input = Readline.readline("rb> ", true) handle_exit(input) break if input == "js" if input =~ /^js\s+(.+)$/ eval_in_js($1) next end eval_in_ruby(input, local_binding) end ruby_readline = copy_history paste_history(js_readline) next end eval_in_js(input) end end