Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 107 lines (80 sloc) 2.071 kb
f90a59e @jbarnette Merged in wycats' interactive shell.
authored
1 #!/usr/bin/env ruby
2c8606c @jbarnette Add a bunch of improvements to bin/johnson. All of these can be specifie...
authored
2
f90a59e @jbarnette Merged in wycats' interactive shell.
authored
3 require "readline"
c601585 @jbarnette Localize the johnson/cli requires.
authored
4 require "johnson/cli"
f90a59e @jbarnette Merged in wycats' interactive shell.
authored
5
931eea8 @jbarnette Make sure bin/johnson doesn't talk about context at all.
authored
6 RUNTIME = js = Johnson::Runtime.new
26d4d61 @jbarnette Make bin/johnson act a bit more like Rhino for easy framework-hoppin'.
authored
7 RUNTIME.evaluate(Johnson::CLI::JS)
f90a59e @jbarnette Merged in wycats' interactive shell.
authored
8
00eb07a @matthewd Exit on EOF, too.
matthewd authored
9 EXIT_VERBS = [nil] + %w(exit quit)
cb1e2c5 @jbarnette Clean up inline commands in the interactive loop.
authored
10
f90a59e @jbarnette Merged in wycats' interactive shell.
authored
11 local_binding = binding
12 ruby_readline = []
13
14 def copy_history
15 new_history = []
16 until Readline::HISTORY.empty?
17 new_history.push(Readline::HISTORY.pop)
18 end
19 new_history
20 end
21
22 def paste_history(old_history)
23 until old_history.empty?
24 Readline::HISTORY << old_history.pop
25 end
26 end
27
36533ab @jbarnette Exiting bin/johnson with ^D adds a trailing newline.
authored
28 def handle_exit(input)
29 if EXIT_VERBS.include?(input)
30 puts if input.nil?
31 exit
32 end
33 end
34
f90a59e @jbarnette Merged in wycats' interactive shell.
authored
35 def rescued(&block)
36 yield
36533ab @jbarnette Exiting bin/johnson with ^D adds a trailing newline.
authored
37 rescue Exception => e
26d4d61 @jbarnette Make bin/johnson act a bit more like Rhino for easy framework-hoppin'.
authored
38 exit if SystemExit === e
39
f90a59e @jbarnette Merged in wycats' interactive shell.
authored
40 puts e.message
9ab93fb @jbarnette Scrub 'bin/johnson' lines from the backtrace in interactive mode.
authored
41 puts e.backtrace.reject { |l| l =~ /bin\/johnson/ }
f90a59e @jbarnette Merged in wycats' interactive shell.
authored
42 end
43
2c8606c @jbarnette Add a bunch of improvements to bin/johnson. All of these can be specifie...
authored
44 def eval_in_js(expression)
931eea8 @jbarnette Make sure bin/johnson doesn't talk about context at all.
authored
45 rescued { puts "=> " + RUNTIME.evaluate(expression, "(console)").inspect }
2c8606c @jbarnette Add a bunch of improvements to bin/johnson. All of these can be specifie...
authored
46 end
47
48 def eval_in_ruby(expression, bind_to)
49 rescued { puts "=> " + eval(expression, bind_to).inspect }
50 end
51
52 options = Johnson::CLI::Options.parse!(ARGV)
2e05a22 @jbarnette Make bin/johnson take a -r.
authored
53
2c8606c @jbarnette Add a bunch of improvements to bin/johnson. All of these can be specifie...
authored
54 options.load_paths.each { |d| $LOAD_PATH << d }
7881560 @jbarnette Fix -r.
authored
55 options.paths_to_require.each { |p| RUNTIME.evaluate("Johnson.require('#{p}')") }
931eea8 @jbarnette Make sure bin/johnson doesn't talk about context at all.
authored
56 options.files_to_preload.each { |f| RUNTIME.load(f) }
2c8606c @jbarnette Add a bunch of improvements to bin/johnson. All of these can be specifie...
authored
57
58 unless options.expressions.empty?
931eea8 @jbarnette Make sure bin/johnson doesn't talk about context at all.
authored
59 options.expressions.each { |e| RUNTIME.evaluate(e, '-e') }
b38a1ab @jbarnette It's now possible to execute multiple JS files on the command-line.
authored
60 exit if options.files_to_evaluate.empty?
95f111c @jbarnette Make sure -e still works when JS files are specified on the command line...
authored
61 end
62
b38a1ab @jbarnette It's now possible to execute multiple JS files on the command-line.
authored
63 unless options.files_to_evaluate.empty?
26d4d61 @jbarnette Make bin/johnson act a bit more like Rhino for easy framework-hoppin'.
authored
64 RUNTIME[:arguments] = options.arguments
b38a1ab @jbarnette It's now possible to execute multiple JS files on the command-line.
authored
65
66 options.files_to_evaluate.each do |file|
67 RUNTIME.load(file)
68 end
69
2c8606c @jbarnette Add a bunch of improvements to bin/johnson. All of these can be specifie...
authored
70 exit
71 end
72
f90a59e @jbarnette Merged in wycats' interactive shell.
authored
73 loop do
74 input = Readline.readline("js> ", true)
36533ab @jbarnette Exiting bin/johnson with ^D adds a trailing newline.
authored
75 handle_exit(input)
f90a59e @jbarnette Merged in wycats' interactive shell.
authored
76
2c8606c @jbarnette Add a bunch of improvements to bin/johnson. All of these can be specifie...
authored
77 if input =~ /^rb\s+(.+)$/
78 eval_in_ruby($1, local_binding)
79 next
80 end
81
cb1e2c5 @jbarnette Clean up inline commands in the interactive loop.
authored
82 if input == "rb"
f90a59e @jbarnette Merged in wycats' interactive shell.
authored
83 js_readline = copy_history
84 paste_history(ruby_readline)
85
86 loop do
87 input = Readline.readline("rb> ", true)
36533ab @jbarnette Exiting bin/johnson with ^D adds a trailing newline.
authored
88 handle_exit(input)
89
cb1e2c5 @jbarnette Clean up inline commands in the interactive loop.
authored
90 break if input == "js"
2c8606c @jbarnette Add a bunch of improvements to bin/johnson. All of these can be specifie...
authored
91
92 if input =~ /^js\s+(.+)$/
93 eval_in_js($1)
94 next
95 end
96
97 eval_in_ruby(input, local_binding)
f90a59e @jbarnette Merged in wycats' interactive shell.
authored
98 end
99
100 ruby_readline = copy_history
101 paste_history(js_readline)
102 next
103 end
104
2c8606c @jbarnette Add a bunch of improvements to bin/johnson. All of these can be specifie...
authored
105 eval_in_js(input)
f90a59e @jbarnette Merged in wycats' interactive shell.
authored
106 end
Something went wrong with that request. Please try again.