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)
|
runtime.eval(source)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.runtimes
|
||||||
|
Runtimes.runtimes
|
||||||
|
end
|
||||||
|
|
||||||
def self.runtime
|
def self.runtime
|
||||||
@runtime ||= Runtimes.best_available
|
@runtime ||= Runtimes.best_available
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,7 +3,10 @@ require "tempfile"
|
||||||
|
|
||||||
module ExecJS
|
module ExecJS
|
||||||
class ExternalRuntime
|
class ExternalRuntime
|
||||||
|
attr_reader :name
|
||||||
|
|
||||||
def initialize(options)
|
def initialize(options)
|
||||||
|
@name = options[:name]
|
||||||
@command = options[:command]
|
@command = options[:command]
|
||||||
@runner_path = options[:runner_path]
|
@runner_path = options[:runner_path]
|
||||||
@test_args = options[:test_args]
|
@test_args = options[:test_args]
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
module ExecJS
|
module ExecJS
|
||||||
class RubyRacerRuntime
|
class RubyRacerRuntime
|
||||||
def initialize(options)
|
def name
|
||||||
|
"therubyracer (V8)"
|
||||||
end
|
end
|
||||||
|
|
||||||
def exec(source)
|
def exec(source)
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
module ExecJS
|
module ExecJS
|
||||||
class RubyRhinoRuntime
|
class RubyRhinoRuntime
|
||||||
def initialize(options)
|
def name
|
||||||
|
"therubyrhino (Rhino)"
|
||||||
end
|
end
|
||||||
|
|
||||||
def exec(source)
|
def exec(source)
|
||||||
|
|
|
@ -1,45 +1,56 @@
|
||||||
module ExecJS
|
module ExecJS
|
||||||
module Runtimes
|
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
|
def self.best_available
|
||||||
runtimes.find(&:available?)
|
runtimes.find(&:available?)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.runtimes
|
def self.runtimes
|
||||||
@runtimes ||= []
|
@runtimes ||= [
|
||||||
end
|
RubyRacer,
|
||||||
|
RubyRhino,
|
||||||
def self.define_runtime(name, options)
|
V8,
|
||||||
klass = options[:as] || ExternalRuntime
|
Node,
|
||||||
runtimes.push runtime = klass.new(options)
|
JavaScriptCore,
|
||||||
const_set(name, runtime)
|
Spidermonkey,
|
||||||
end
|
JScript
|
||||||
|
]
|
||||||
define_runtime :RubyRacer,
|
end
|
||||||
: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
|
||||||
end
|
end
|
||||||
|
|
|
@ -45,39 +45,31 @@ module TestRuntime
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_runtime(name, description = nil)
|
warn "Runtime Support:"
|
||||||
runtime = ExecJS::Runtimes.const_get(name)
|
ExecJS.runtimes.each do |runtime|
|
||||||
title = [name, description].join(" ")
|
|
||||||
ok = runtime.available?
|
ok = runtime.available?
|
||||||
|
|
||||||
warn "%s %-16s %s" %
|
warn " %s %-21s %s" %
|
||||||
if ok
|
if ok
|
||||||
["✓", title, "Found"]
|
["✓", runtime.name, "Found"]
|
||||||
else
|
else
|
||||||
[" ", title, "Not found"]
|
[" ", runtime.name, "Not found"]
|
||||||
end
|
end
|
||||||
|
|
||||||
if ok
|
if ok
|
||||||
Class.new(Test::Unit::TestCase) do
|
Class.new(Test::Unit::TestCase) do
|
||||||
(class << self; self end).send(:define_method, :name) do
|
|
||||||
"#{name}Test"
|
|
||||||
end
|
|
||||||
|
|
||||||
include TestRuntime
|
include TestRuntime
|
||||||
|
|
||||||
|
instance_exec do
|
||||||
|
define_method(:name) do
|
||||||
|
runtime.name
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
define_method(:setup) do
|
define_method(:setup) do
|
||||||
instance_variable_set(:@runtime, runtime)
|
instance_variable_set(:@runtime, runtime)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
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 ""
|
warn ""
|
||||||
|
|
Loading…
Reference in a new issue