mirror of
https://github.com/rails/execjs
synced 2023-03-27 23:21:20 -04:00
Active runtime can be overridden with ExecJS.runtime=
This commit is contained in:
parent
8d3e05f331
commit
71354867bc
2 changed files with 49 additions and 32 deletions
|
@ -1,9 +1,10 @@
|
||||||
require "rbconfig"
|
require "rbconfig"
|
||||||
|
|
||||||
module ExecJS
|
module ExecJS
|
||||||
class Error < ::StandardError; end
|
class Error < ::StandardError; end
|
||||||
class RuntimeError < Error; end
|
class RuntimeError < Error; end
|
||||||
class ProgramError < Error; end
|
class ProgramError < Error; end
|
||||||
|
class RuntimeUnavailable < RuntimeError; end
|
||||||
|
|
||||||
autoload :ExternalRuntime, "execjs/external_runtime"
|
autoload :ExternalRuntime, "execjs/external_runtime"
|
||||||
autoload :MustangRuntime, "execjs/mustang_runtime"
|
autoload :MustangRuntime, "execjs/mustang_runtime"
|
||||||
|
@ -11,35 +12,40 @@ module ExecJS
|
||||||
autoload :RubyRhinoRuntime, "execjs/ruby_rhino_runtime"
|
autoload :RubyRhinoRuntime, "execjs/ruby_rhino_runtime"
|
||||||
autoload :Runtimes, "execjs/runtimes"
|
autoload :Runtimes, "execjs/runtimes"
|
||||||
|
|
||||||
def self.exec(source)
|
class << self
|
||||||
runtime.exec(source)
|
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
|
end
|
||||||
|
|
||||||
def self.eval(source)
|
# Eagerly detect runtime
|
||||||
runtime.eval(source)
|
self.runtime ||= Runtimes.best_available ||
|
||||||
end
|
raise(RuntimeUnavailable, "Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes.")
|
||||||
|
|
||||||
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
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -18,6 +18,17 @@ class TestExecJS < Test::Unit::TestCase
|
||||||
assert runtime.available?
|
assert runtime.available?
|
||||||
end
|
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
|
def test_compile
|
||||||
context = ExecJS.compile("foo = function() { return \"bar\"; }")
|
context = ExecJS.compile("foo = function() { return \"bar\"; }")
|
||||||
assert_equal "bar", context.exec("return foo()")
|
assert_equal "bar", context.exec("return foo()")
|
||||||
|
|
Loading…
Reference in a new issue