Permalink
Browse files

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".
  • Loading branch information...
1 parent de9d10f commit 0893e12fe90818b7b1a83575fa10c90e21a37f39 @sstephenson committed Jun 1, 2011
Showing with 75 additions and 50 deletions.
  1. +2 −50 lib/execjs.rb
  2. +39 −0 lib/execjs/module.rb
  3. +34 −0 lib/execjs/runtimes.rb
View
@@ -1,53 +1,5 @@
-require "rbconfig"
+require "execjs/runtimes"
module ExecJS
- VERSION = "1.0.0"
-
- 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.")
+ self.runtime ||= Runtimes.autodetect
end
View
@@ -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
View
@@ -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 Runtimes
RubyRacer = RubyRacerRuntime.new
@@ -32,10 +38,34 @@ module Runtimes
)
+ 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
runtimes.find(&:available?)
end
+ def self.from_environment
+ if name = ENV["EXECJS_RUNTIME"]
+ if runtime = const_get(name)
+ if runtime.available?
+ runtime if runtime.available?
@KostyaSha

KostyaSha Nov 19, 2012

Why so many ifs?

+ 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
@runtimes ||= [
RubyRacer,
@@ -48,4 +78,8 @@ def self.runtimes
]
end
end
+
+ def self.runtimes
+ Runtimes.runtimes
+ end
end

4 comments on commit 0893e12

Contributor

josh commented on 0893e12 Jun 1, 2011

Why do we need a separate file for lib/execjs/module.rb? We need to defer it?

Contributor

josh replied Jun 1, 2011

Ah nevermind. Can you add an explicit require for execjs/module to lib/execjs.rb.

Owner

sstephenson replied Jun 1, 2011

Why's that? execjs/runtimes requires execjs/module. Just thought that stuff was nicer outside the runtimes file.

Contributor

josh replied Jun 1, 2011

I think it should be both for clarity.

require "execjs/module"
require "execjs/runtimes"
Please sign in to comment.