mirror of
https://github.com/rails/execjs
synced 2023-03-27 23:21:20 -04:00
Less magic
This commit is contained in:
parent
4746921afd
commit
d19c760186
6 changed files with 68 additions and 56 deletions
|
@ -18,6 +18,10 @@ module ExecJS
|
|||
runtime.eval(source)
|
||||
end
|
||||
|
||||
def self.runtimes
|
||||
Runtimes.runtimes
|
||||
end
|
||||
|
||||
def self.runtime
|
||||
@runtime ||= Runtimes.best_available
|
||||
end
|
||||
|
|
|
@ -3,7 +3,10 @@ require "tempfile"
|
|||
|
||||
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]
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
module ExecJS
|
||||
class RubyRacerRuntime
|
||||
def initialize(options)
|
||||
def name
|
||||
"therubyracer (V8)"
|
||||
end
|
||||
|
||||
def exec(source)
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
module ExecJS
|
||||
class RubyRhinoRuntime
|
||||
def initialize(options)
|
||||
def name
|
||||
"therubyrhino (Rhino)"
|
||||
end
|
||||
|
||||
def exec(source)
|
||||
|
|
|
@ -1,45 +1,56 @@
|
|||
module ExecJS
|
||||
module Runtimes
|
||||
RubyRacer = RubyRacerRuntime.new
|
||||
|
||||
RubyRhino = RubyRhinoRuntime.new
|
||||
|
||||
V8 = ExternalRuntime.new(
|
||||
:name => "V8",
|
||||
:command => "v8",
|
||||
:test_args => "--help",
|
||||
:test_match => /--crankshaft/,
|
||||
:runner_path => ExecJS.root + "/support/basic_runner.js"
|
||||
)
|
||||
|
||||
Node = ExternalRuntime.new(
|
||||
:name => "Node.js (V8)",
|
||||
:command => ["nodejs", "node"],
|
||||
:runner_path => ExecJS.root + "/support/node_runner.js"
|
||||
)
|
||||
|
||||
JavaScriptCore = ExternalRuntime.new(
|
||||
:name => "JavaScriptCore",
|
||||
:command => "/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc",
|
||||
:runner_path => ExecJS.root + "/support/basic_runner.js"
|
||||
)
|
||||
|
||||
Spidermonkey = ExternalRuntime.new(
|
||||
:name => "Spidermonkey",
|
||||
:command => "js",
|
||||
:runner_path => ExecJS.root + "/support/basic_runner.js"
|
||||
)
|
||||
|
||||
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 ||= []
|
||||
@runtimes ||= [
|
||||
RubyRacer,
|
||||
RubyRhino,
|
||||
V8,
|
||||
Node,
|
||||
JavaScriptCore,
|
||||
Spidermonkey,
|
||||
JScript
|
||||
]
|
||||
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
|
||||
|
||||
define_runtime :RubyRhino,
|
||||
:as => RubyRhinoRuntime
|
||||
|
||||
define_runtime :V8,
|
||||
:command => "v8",
|
||||
:test_args => "--help",
|
||||
:test_match => /--crankshaft/,
|
||||
:runner_path => ExecJS.root + "/support/basic_runner.js"
|
||||
|
||||
define_runtime :Node,
|
||||
:command => ["nodejs", "node"],
|
||||
:runner_path => ExecJS.root + "/support/node_runner.js"
|
||||
|
||||
define_runtime :JavaScriptCore,
|
||||
:command => "/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc",
|
||||
:runner_path => ExecJS.root + "/support/basic_runner.js"
|
||||
|
||||
define_runtime :Spidermonkey,
|
||||
:command => "js",
|
||||
:runner_path => ExecJS.root + "/support/basic_runner.js"
|
||||
|
||||
define_runtime :JScript,
|
||||
:command => "cscript //E:jscript //Nologo",
|
||||
:runner_path => ExecJS.root + "/support/jscript_runner.js"
|
||||
end
|
||||
end
|
||||
|
|
|
@ -45,39 +45,31 @@ module TestRuntime
|
|||
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 ""
|
||||
|
|
Loading…
Reference in a new issue