diff --git a/lib/execjs.rb b/lib/execjs.rb index 1a47f4d..4b5de07 100644 --- a/lib/execjs.rb +++ b/lib/execjs.rb @@ -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 diff --git a/lib/execjs/module.rb b/lib/execjs/module.rb new file mode 100644 index 0000000..f1b4d4d --- /dev/null +++ b/lib/execjs/module.rb @@ -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 diff --git a/lib/execjs/runtimes.rb b/lib/execjs/runtimes.rb index f99777b..80e11cc 100644 --- a/lib/execjs/runtimes.rb +++ b/lib/execjs/runtimes.rb @@ -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 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 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? + 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 @@ module ExecJS ] end end + + def self.runtimes + Runtimes.runtimes + end end