mirror of
https://github.com/rubyjs/therubyracer
synced 2023-03-27 23:21:42 -04:00
Add some nice sugary sweetness to the command line.
This commit is contained in:
parent
1bc3d650ee
commit
b67b38042b
4 changed files with 122 additions and 57 deletions
|
@ -9,58 +9,6 @@ rescue LoadError
|
|||
require 'rubygems'
|
||||
require 'v8'
|
||||
end
|
||||
require 'v8/cli'
|
||||
|
||||
trap("SIGINT") do
|
||||
puts "^C"
|
||||
end
|
||||
|
||||
class V8::Shell
|
||||
def exit(status = 0)
|
||||
Kernel.exit(status)
|
||||
end
|
||||
|
||||
def help(*args)
|
||||
<<-HELP
|
||||
exit(status = 0)
|
||||
exit the shell
|
||||
also: quit()
|
||||
|
||||
ruby(source)
|
||||
evaluate some ruby source
|
||||
HELP
|
||||
|
||||
end
|
||||
|
||||
def to_s
|
||||
"[object TheRubyRacer]"
|
||||
end
|
||||
|
||||
def print(string)
|
||||
puts string
|
||||
end
|
||||
|
||||
def ruby(src)
|
||||
eval(src)
|
||||
end
|
||||
|
||||
alias_method :quit, :exit
|
||||
end
|
||||
|
||||
puts "help() for help"
|
||||
puts "The Ruby Racer #{V8::VERSION}"
|
||||
puts "Vroom Vroom!"
|
||||
|
||||
V8::Context.open(:with => V8::Shell.new) do |cxt|
|
||||
loop do
|
||||
line = Readline.readline('therubyracer> ', true)
|
||||
begin
|
||||
result = cxt.eval(line)
|
||||
puts(result) unless result.nil?
|
||||
|
||||
rescue V8::JavascriptError => e
|
||||
puts e.javascript_stacktrace
|
||||
rescue StandardError => e
|
||||
puts e
|
||||
end
|
||||
end
|
||||
end
|
||||
V8::CLI.run(File.basename(__FILE__), ARGV)
|
||||
|
|
14
bin/v8
Executable file
14
bin/v8
Executable file
|
@ -0,0 +1,14 @@
|
|||
#!/usr/bin/env ruby
|
||||
lib = File.expand_path(File.dirname(__FILE__) + '/../lib')
|
||||
$:.unshift(lib) if File.exists?(lib) unless $:.member?(lib)
|
||||
|
||||
require 'readline'
|
||||
begin
|
||||
require 'v8'
|
||||
rescue LoadError
|
||||
require 'rubygems'
|
||||
require 'v8'
|
||||
end
|
||||
require 'v8/cli'
|
||||
|
||||
V8::CLI.run(File.basename(__FILE__), ARGV)
|
104
lib/v8/cli.rb
Normal file
104
lib/v8/cli.rb
Normal file
|
@ -0,0 +1,104 @@
|
|||
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_tail("-v", "--version", "Show version and exit") {options.version_info = true}
|
||||
parser.on_tail("-h", "--help", "Show this message") do
|
||||
puts parser
|
||||
exit
|
||||
end
|
||||
parser.parse!(args.dup)
|
||||
if options.version_info
|
||||
puts "The Ruby Racer #{V8::VERSION}"
|
||||
puts "V8 Version 2.0.6"
|
||||
exit
|
||||
end
|
||||
Context.open(: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
|
||||
cxt.eval($stdin, '<stdin>')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def self.load(cxt, libfile)
|
||||
begin
|
||||
cxt.load(libfile)
|
||||
rescue V8::JavascriptError => e
|
||||
puts e.javascript_stacktrace
|
||||
rescue StandardError => e
|
||||
puts e
|
||||
end
|
||||
end
|
||||
|
||||
def self.repl(cxt, exename)
|
||||
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::JavascriptError => e
|
||||
puts e.javascript_stacktrace
|
||||
rescue StandardError => e
|
||||
puts e
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class Shell
|
||||
def to_s
|
||||
"[object Shell]"
|
||||
end
|
||||
|
||||
def print(string)
|
||||
puts string
|
||||
end
|
||||
|
||||
def evalrb(src)
|
||||
eval(src)
|
||||
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
|
|
@ -9,11 +9,10 @@ Gem::Specification.new do |s|
|
|||
|
||||
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
||||
s.authors = ["Charles Lowell", "Bill Robertson"]
|
||||
s.date = %q{2010-03-15}
|
||||
s.default_executable = %q{therubyracer}
|
||||
s.date = %q{2010-03-17}
|
||||
s.description = %q{Call javascript code and manipulate javascript objects from ruby. Call ruby code and manipulate ruby objects from javascript.}
|
||||
s.email = %q{cowboyd@thefrontside.net}
|
||||
s.executables = ["therubyracer"]
|
||||
s.executables = ["therubyracer", "v8"]
|
||||
s.extensions = ["ext/v8/extconf.rb"]
|
||||
s.extra_rdoc_files = [
|
||||
"README.rdoc"
|
||||
|
|
Loading…
Reference in a new issue