mirror of
https://github.com/rails/execjs
synced 2023-03-27 23:21:20 -04:00
Add the ability to specify ExecJS.runtime via ENV["EXECJS_RUNTIME"].
You can now load ExecJS and bypass engine autodetection by requiring "execjs/runtimes" instead of "execjs".
This commit is contained in:
parent
de9d10fa91
commit
0893e12fe9
3 changed files with 75 additions and 50 deletions
|
@ -1,53 +1,5 @@
|
||||||
require "rbconfig"
|
require "execjs/runtimes"
|
||||||
|
|
||||||
module ExecJS
|
module ExecJS
|
||||||
VERSION = "1.0.0"
|
self.runtime ||= Runtimes.autodetect
|
||||||
|
|
||||||
class Error < ::StandardError; end
|
|
||||||
class RuntimeError < Error; end
|
|
||||||
class ProgramError < Error; end
|
|
||||||
class RuntimeUnavailable < RuntimeError; end
|
|
||||||
|
|
||||||
autoload :ExternalRuntime, "execjs/external_runtime"
|
|
||||||
autoload :MustangRuntime, "execjs/mustang_runtime"
|
|
||||||
autoload :RubyRacerRuntime, "execjs/ruby_racer_runtime"
|
|
||||||
autoload :RubyRhinoRuntime, "execjs/ruby_rhino_runtime"
|
|
||||||
autoload :Runtimes, "execjs/runtimes"
|
|
||||||
|
|
||||||
class << self
|
|
||||||
attr_reader :runtime
|
|
||||||
|
|
||||||
def exec(source)
|
|
||||||
runtime.exec(source)
|
|
||||||
end
|
|
||||||
|
|
||||||
def eval(source)
|
|
||||||
runtime.eval(source)
|
|
||||||
end
|
|
||||||
|
|
||||||
def compile(source)
|
|
||||||
runtime.compile(source)
|
|
||||||
end
|
|
||||||
|
|
||||||
def runtimes
|
|
||||||
Runtimes.runtimes
|
|
||||||
end
|
|
||||||
|
|
||||||
def runtime=(runtime)
|
|
||||||
raise RuntimeUnavailable, "#{runtime.name} is unavailable on this system" unless runtime.available?
|
|
||||||
@runtime = runtime
|
|
||||||
end
|
|
||||||
|
|
||||||
def root
|
|
||||||
@root ||= File.expand_path("../execjs", __FILE__)
|
|
||||||
end
|
|
||||||
|
|
||||||
def windows?
|
|
||||||
@windows ||= RbConfig::CONFIG["host_os"] =~ /mswin|mingw/
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Eagerly detect runtime
|
|
||||||
self.runtime ||= Runtimes.best_available ||
|
|
||||||
raise(RuntimeUnavailable, "Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes.")
|
|
||||||
end
|
end
|
||||||
|
|
39
lib/execjs/module.rb
Normal file
39
lib/execjs/module.rb
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
require "rbconfig"
|
||||||
|
|
||||||
|
module ExecJS
|
||||||
|
VERSION = "1.0.0"
|
||||||
|
|
||||||
|
class Error < ::StandardError; end
|
||||||
|
class RuntimeError < Error; end
|
||||||
|
class ProgramError < Error; end
|
||||||
|
class RuntimeUnavailable < RuntimeError; end
|
||||||
|
|
||||||
|
class << self
|
||||||
|
attr_reader :runtime
|
||||||
|
|
||||||
|
def runtime=(runtime)
|
||||||
|
raise RuntimeUnavailable, "#{runtime.name} is unavailable on this system" unless runtime.available?
|
||||||
|
@runtime = runtime
|
||||||
|
end
|
||||||
|
|
||||||
|
def exec(source)
|
||||||
|
runtime.exec(source)
|
||||||
|
end
|
||||||
|
|
||||||
|
def eval(source)
|
||||||
|
runtime.eval(source)
|
||||||
|
end
|
||||||
|
|
||||||
|
def compile(source)
|
||||||
|
runtime.compile(source)
|
||||||
|
end
|
||||||
|
|
||||||
|
def root
|
||||||
|
@root ||= File.expand_path("..", __FILE__)
|
||||||
|
end
|
||||||
|
|
||||||
|
def windows?
|
||||||
|
@windows ||= RbConfig::CONFIG["host_os"] =~ /mswin|mingw/
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,3 +1,9 @@
|
||||||
|
require "execjs/module"
|
||||||
|
require "execjs/external_runtime"
|
||||||
|
require "execjs/mustang_runtime"
|
||||||
|
require "execjs/ruby_racer_runtime"
|
||||||
|
require "execjs/ruby_rhino_runtime"
|
||||||
|
|
||||||
module ExecJS
|
module ExecJS
|
||||||
module Runtimes
|
module Runtimes
|
||||||
RubyRacer = RubyRacerRuntime.new
|
RubyRacer = RubyRacerRuntime.new
|
||||||
|
@ -32,10 +38,34 @@ module ExecJS
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def self.autodetect
|
||||||
|
from_environment || best_available ||
|
||||||
|
raise(RuntimeUnavailable, "Could not find a JavaScript runtime. " +
|
||||||
|
"See https://github.com/sstephenson/execjs for a list of available runtimes.")
|
||||||
|
end
|
||||||
|
|
||||||
def self.best_available
|
def self.best_available
|
||||||
runtimes.find(&:available?)
|
runtimes.find(&:available?)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.from_environment
|
||||||
|
if name = ENV["EXECJS_RUNTIME"]
|
||||||
|
if runtime = const_get(name)
|
||||||
|
if runtime.available?
|
||||||
|
runtime if runtime.available?
|
||||||
|
else
|
||||||
|
raise RuntimeUnavailable, "#{name} runtime is not available on this system"
|
||||||
|
end
|
||||||
|
elsif !name.empty?
|
||||||
|
raise RuntimeUnavailable, "#{name} runtime is not defined"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.names
|
||||||
|
constants
|
||||||
|
end
|
||||||
|
|
||||||
def self.runtimes
|
def self.runtimes
|
||||||
@runtimes ||= [
|
@runtimes ||= [
|
||||||
RubyRacer,
|
RubyRacer,
|
||||||
|
@ -48,4 +78,8 @@ module ExecJS
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.runtimes
|
||||||
|
Runtimes.runtimes
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue