bin/envjsrb in smparkes-envjs-0.0.9 vs bin/envjsrb in smparkes-envjs-0.0.10

- old
+ new

@@ -12,10 +12,115 @@ RUNTIME = js = Johnson::Runtime.new RUNTIME.extend Envjs::Runtime RUNTIME.load Envjs::ENVJS +EXIT_VERBS = [nil] + %w(exit quit) + +prepped = false +ruby_readline = [] +js_readline = [] +local_binding = binding +mode = nil + +(class<<self; self; end).send :define_method, :prep do + + return if prepped + prepped = true + + require "readline" + require "johnson/cli" + + RUNTIME.evaluate(Johnson::CLI::JS) + + 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) + return EXIT_VERBS.include?(input) + 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,stream) + rescued do + v = RUNTIME.evaluate(expression, "(console)") + stream.result { "=> " + v.inspect } + end + end + + def eval_in_ruby(expression, bind_to, stream) + rescued do + v = eval(expression, bind_to) + stream.result { "=> " + v.inspect } + end + end + + (class<<self; self; end).send :define_method, :do_loop do |stream| + # def foo + + catch "Breakout" do + loop do + mode = :js + input = stream.next("js> ") + input and input.chomp! + begin stream.exit(input); throw "Breakout" end if handle_exit(input) + + if input =~ /^rb\s+(.+)$/ + eval_in_ruby($1, local_binding, stream) + next + end + + if input == "rb" + js_readline = copy_history + paste_history(ruby_readline) + + loop do + mode = :rb + # input = Readline.readline("rb> ", true) + input = stream.next("rb> ") + begin stream.exit(input); throw "Breakout" end if handle_exit(input) + + break if input == "js" + + if input =~ /^js\s+(.+)$/ + eval_in_js($1) + next + end + + eval_in_ruby(input, local_binding, stream) + end + + ruby_readline = copy_history + paste_history(js_readline) + next + end + + eval_in_js(input, stream) + end + end + end +end + if ARGV.length > 0 begin about_blank = true ARGV.each do |file| @@ -33,10 +138,11 @@ RUNTIME.wait RUNTIME.evaluate("window.location = 'file://#{Pathname.new(file).realpath}'", file, 1) about_blank = false else RUNTIME.become_first_script_window + RUNTIME.top_level_load file RUNTIME.load file end end rescue Johnson::Error => je if je.message.match /<SystemExit: exit>/ @@ -47,58 +153,28 @@ RUNTIME.wait else - require "readline" - require "johnson/cli" + prep - RUNTIME.evaluate(Johnson::CLI::JS) + if !(files = Dir[".envjsrbrc",File.join(ENV["HOME"],".envjsrbrc")]).empty? + file = Class.new do + def initialize f + @file = open f + end + def next prompt + @file.gets + end + def result &block + end + def exit v; end + end.new files[0] - 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 + do_loop file 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) } @@ -116,40 +192,48 @@ end exit end - loop do - input = Readline.readline("js> ", true) - handle_exit(input) - - if input =~ /^rb\s+(.+)$/ - eval_in_ruby($1, local_binding) - next + reader = Class.new do + def next prompt + Readline.readline(prompt, true) end - - if input == "rb" - js_readline = copy_history - paste_history(ruby_readline) - - loop do - input = Readline.readline("rb> ", true) - handle_exit(input) + def result + puts yield + end + def exit v; puts if v.nil?; end + end.new - break if input == "js" + begin + open(File.join(ENV["HOME"],".envjsrb.js")) do |f| + js_readline = f.read.split("\n") + end + rescue; end - if input =~ /^js\s+(.+)$/ - eval_in_js($1) - next - end + begin + open(File.join(ENV["HOME"],".envjsrb.ruby")) do |f| + ruby_readline = f.read.split("\n") + end + rescue; end - eval_in_ruby(input, local_binding) - end - - ruby_readline = copy_history - paste_history(js_readline) - next - end - - eval_in_js(input) + case mode + when :js; paste_history js_readline + when :rb; paste_history ruby_readline end + + do_loop reader + + case mode + when :js; js_readline = copy_history + when :rb; ruby_readline = copy_history + end + + open(File.join(ENV["HOME"],".envjsrb.js"),"w") do |f| + f.write js_readline[0..100].join("\n")+"\n" + end + + open(File.join(ENV["HOME"],".envjsrb.ruby"),"w") do |f| + f.write ruby_readline[0..100].join("\n")+"\n" + end + end