From 71354867bc71bb547fd7ed26afe072b5cd8827b7 Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Tue, 17 May 2011 10:08:41 -0500 Subject: [PATCH] Active runtime can be overridden with `ExecJS.runtime=` --- lib/execjs.rb | 70 ++++++++++++++++++++++++--------------------- test/test_execjs.rb | 11 +++++++ 2 files changed, 49 insertions(+), 32 deletions(-) diff --git a/lib/execjs.rb b/lib/execjs.rb index 50c620d..ca95d87 100644 --- a/lib/execjs.rb +++ b/lib/execjs.rb @@ -1,9 +1,10 @@ require "rbconfig" module ExecJS - class Error < ::StandardError; end - class RuntimeError < Error; end - class ProgramError < Error; end + 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" @@ -11,35 +12,40 @@ module ExecJS autoload :RubyRhinoRuntime, "execjs/ruby_rhino_runtime" autoload :Runtimes, "execjs/runtimes" - def self.exec(source) - runtime.exec(source) + 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 - def self.eval(source) - runtime.eval(source) - end - - def self.compile(source) - runtime.compile(source) - end - - def self.runtimes - Runtimes.runtimes - end - - def self.runtime - @runtime ||= Runtimes.best_available || - raise(ExecJS::RuntimeError, "Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes.") - end - - def self.root - @root ||= File.expand_path("../execjs", __FILE__) - end - - def self.windows? - @windows ||= RbConfig::CONFIG["host_os"] =~ /mswin|mingw/ - end - - # Eager detect runtime - self.runtime + # 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 diff --git a/test/test_execjs.rb b/test/test_execjs.rb index 24dc676..0a67ff8 100644 --- a/test/test_execjs.rb +++ b/test/test_execjs.rb @@ -18,6 +18,17 @@ class TestExecJS < Test::Unit::TestCase assert runtime.available? end + def test_runtime_assignment + original_runtime = ExecJS.runtime + runtime = ExecJS::ExternalRuntime.new(:command => "nonexistent") + assert_raises(ExecJS::RuntimeUnavailable) { ExecJS.runtime = runtime } + assert_equal original_runtime, ExecJS.runtime + + runtime = ExecJS::ExternalRuntime.new(:command => "ruby") + ExecJS.runtime = runtime + assert_equal runtime, ExecJS.runtime + end + def test_compile context = ExecJS.compile("foo = function() { return \"bar\"; }") assert_equal "bar", context.exec("return foo()")