This repository has been archived by the owner on Dec 4, 2023. It is now read-only.
/
cli.rb
131 lines (119 loc) · 3.34 KB
/
cli.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
require 'optparse'
require 'ostruct'
module V8
module CLI
def self.run(exename = 'v8', args = [])
options = OpenStruct.new
options.libs = []
options.libdirs = []
parser = OptionParser.new
parser.banner = "Usage: #{exename} [options]"
parser.on("-r", "--require FILE", "load and execute FILE as JavaScript source") {|r| options.libs << r}
parser.on("-i", "--interactive", "interactive mode") {options.interactive = true}
parser.on("-e", "--execute JAVASCRIPT", String, "evaluate JAVASCRIPT and exit") {|src| options.execute = src}
parser.on("--selftest", "check that therubyracer is functioning properly") {options.selftest = true}
parser.on_tail("-v", "--version", "Show version and exit") {options.version_info = true}
parser.on_tail("-h", "--help", "Show this message") do
puts parser
exit
end
begin
parser.parse!(args.dup)
rescue OptionParser::InvalidOption => e
puts "#{exename}: #{e.message}"
exit(1)
end
if options.version_info
puts "The Ruby Racer #{V8::VERSION}"
puts "V8 Version 2.3.3"
exit
elsif options.selftest
self.test
end
Context.new(:with => Shell.new) do |cxt|
for libfile in options.libs do
load(cxt,libfile)
end
if options.interactive
repl(cxt, exename)
elsif options.execute
cxt.eval(options.execute, '<execute>')
else
begin
cxt.eval($stdin, '<stdin>')
rescue Interrupt => e
puts; exit
end
end
end
end
def self.load(cxt, libfile)
begin
cxt.load(libfile)
rescue V8::JSError => e
puts e.message
puts e.backtrace(:javascript)
rescue StandardError => e
puts e
end
end
def self.test
begin
require 'rubygems'
require 'rspec'
ARGV.clear
ARGV << File.dirname(__FILE__) + '/../../spec/'
::RSpec::Core::Runner.autorun
exit(0)
rescue LoadError => e
puts "selftest requires rspec to be installed (gem install rspec)"
exit(1)
end
end
def self.repl(cxt, exename)
require 'readline'
puts "help() for help. quit() to quit."
puts "The Ruby Racer #{V8::VERSION}"
puts "Vroom Vroom!"
trap("SIGINT") do
puts "^C"
end
loop do
line = Readline.readline("#{exename}> ", true)
begin
result = cxt.eval(line, '<shell>')
puts(result) unless result.nil?
rescue V8::JSError => e
puts e.message
puts e.backtrace(:javascript)
rescue StandardError => e
puts e
puts e.backtrace.join("\n")
end
end
end
class Shell
def to_s
"[object Shell]"
end
def print(string)
puts string
end
def exit(status = 0)
Kernel.exit(status)
end
alias_method :quit, :exit
def help(*args)
<<-HELP
print(msg)
print msg to STDOUT
exit(status = 0)
exit the shell
also: quit()
evalrb(source)
evaluate some ruby source
HELP
end
end
end
end