Browse files

Less magic

  • Loading branch information...
1 parent 4746921 commit d19c7601863464233f5c18702ff01cb1740c1ce3 @sstephenson committed Feb 10, 2011
View
4 lib/execjs.rb
@@ -18,6 +18,10 @@ def self.eval(source)
runtime.eval(source)
end
+ def self.runtimes
+ Runtimes.runtimes
+ end
+
def self.runtime
@runtime ||= Runtimes.best_available
end
View
3 lib/execjs/external_runtime.rb
@@ -3,7 +3,10 @@
module ExecJS
class ExternalRuntime
+ attr_reader :name
+
def initialize(options)
+ @name = options[:name]
@command = options[:command]
@runner_path = options[:runner_path]
@test_args = options[:test_args]
View
3 lib/execjs/ruby_racer_runtime.rb
@@ -1,6 +1,7 @@
module ExecJS
class RubyRacerRuntime
- def initialize(options)
+ def name
+ "therubyracer (V8)"
end
def exec(source)
View
3 lib/execjs/ruby_rhino_runtime.rb
@@ -1,6 +1,7 @@
module ExecJS
class RubyRhinoRuntime
- def initialize(options)
+ def name
+ "therubyrhino (Rhino)"
end
def exec(source)
View
71 lib/execjs/runtimes.rb
@@ -1,45 +1,56 @@
module ExecJS
module Runtimes
- def self.best_available
- runtimes.find(&:available?)
- end
-
- def self.runtimes
- @runtimes ||= []
- end
-
- def self.define_runtime(name, options)
- klass = options[:as] || ExternalRuntime
- runtimes.push runtime = klass.new(options)
- const_set(name, runtime)
- end
-
- define_runtime :RubyRacer,
- :as => RubyRacerRuntime
+ RubyRacer = RubyRacerRuntime.new
- define_runtime :RubyRhino,
- :as => RubyRhinoRuntime
+ RubyRhino = RubyRhinoRuntime.new
- define_runtime :V8,
- :command => "v8",
- :test_args => "--help",
- :test_match => /--crankshaft/,
+ V8 = ExternalRuntime.new(
+ :name => "V8",
+ :command => "v8",
+ :test_args => "--help",
+ :test_match => /--crankshaft/,
:runner_path => ExecJS.root + "/support/basic_runner.js"
+ )
- define_runtime :Node,
- :command => ["nodejs", "node"],
+ Node = ExternalRuntime.new(
+ :name => "Node.js (V8)",
+ :command => ["nodejs", "node"],
:runner_path => ExecJS.root + "/support/node_runner.js"
+ )
- define_runtime :JavaScriptCore,
- :command => "/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc",
+ JavaScriptCore = ExternalRuntime.new(
+ :name => "JavaScriptCore",
+ :command => "/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc",
:runner_path => ExecJS.root + "/support/basic_runner.js"
+ )
- define_runtime :Spidermonkey,
- :command => "js",
+ Spidermonkey = ExternalRuntime.new(
+ :name => "Spidermonkey",
+ :command => "js",
:runner_path => ExecJS.root + "/support/basic_runner.js"
+ )
- define_runtime :JScript,
- :command => "cscript //E:jscript //Nologo",
+ JScript = ExternalRuntime.new(
+ :name => "JScript",
+ :command => "cscript //E:jscript //Nologo",
:runner_path => ExecJS.root + "/support/jscript_runner.js"
+ )
+
+
+ def self.best_available
+ runtimes.find(&:available?)
+ end
+
+ def self.runtimes
+ @runtimes ||= [
+ RubyRacer,
+ RubyRhino,
+ V8,
+ Node,
+ JavaScriptCore,
+ Spidermonkey,
+ JScript
+ ]
+ end
end
end
View
30 test/test_runtimes.rb
@@ -45,39 +45,31 @@ def test_thrown_exception
end
end
-def test_runtime(name, description = nil)
- runtime = ExecJS::Runtimes.const_get(name)
- title = [name, description].join(" ")
+warn "Runtime Support:"
+ExecJS.runtimes.each do |runtime|
ok = runtime.available?
- warn "%s %-16s %s" %
+ warn " %s %-21s %s" %
if ok
- ["", title, "Found"]
+ ["", runtime.name, "Found"]
else
- [" ", title, "Not found"]
+ [" ", runtime.name, "Not found"]
end
if ok
Class.new(Test::Unit::TestCase) do
- (class << self; self end).send(:define_method, :name) do
- "#{name}Test"
- end
-
include TestRuntime
+ instance_exec do
+ define_method(:name) do
+ runtime.name
+ end
+ end
+
define_method(:setup) do
instance_variable_set(:@runtime, runtime)
end
end
end
end
-
-warn "Runtime support:"
-test_runtime :RubyRacer, "(V8)"
-test_runtime :RubyRhino
-test_runtime :V8
-test_runtime :Node, "(V8)"
-test_runtime :JavaScriptCore
-test_runtime :Spidermonkey
-test_runtime :JScript
warn ""

0 comments on commit d19c760

Please sign in to comment.