runtimes.rb 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. require "execjs/module"
  2. require "execjs/disabled_runtime"
  3. require "execjs/external_runtime"
  4. require "execjs/johnson_runtime"
  5. require "execjs/mustang_runtime"
  6. require "execjs/ruby_racer_runtime"
  7. require "execjs/ruby_rhino_runtime"
  8. module ExecJS
  9. module Runtimes
  10. Disabled = DisabledRuntime.new
  11. RubyRacer = RubyRacerRuntime.new
  12. RubyRhino = RubyRhinoRuntime.new
  13. Johnson = JohnsonRuntime.new
  14. Mustang = MustangRuntime.new
  15. Node = ExternalRuntime.new(
  16. :name => "Node.js (V8)",
  17. :command => ["nodejs", "node"],
  18. :runner_path => ExecJS.root + "/support/node_runner.js",
  19. :encoding => 'UTF-8'
  20. )
  21. JavaScriptCore = ExternalRuntime.new(
  22. :name => "JavaScriptCore",
  23. :command => "/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc",
  24. :runner_path => ExecJS.root + "/support/jsc_runner.js"
  25. )
  26. SpiderMonkey = Spidermonkey = ExternalRuntime.new(
  27. :name => "SpiderMonkey",
  28. :command => "js",
  29. :runner_path => ExecJS.root + "/support/spidermonkey_runner.js",
  30. :deprecated => true
  31. )
  32. JScript = ExternalRuntime.new(
  33. :name => "JScript",
  34. :command => "cscript //E:jscript //Nologo //U",
  35. :runner_path => ExecJS.root + "/support/jscript_runner.js",
  36. :encoding => 'UTF-16LE' # CScript with //U returns UTF-16LE
  37. )
  38. def self.autodetect
  39. from_environment || best_available ||
  40. raise(RuntimeUnavailable, "Could not find a JavaScript runtime. " +
  41. "See https://github.com/sstephenson/execjs for a list of available runtimes.")
  42. end
  43. def self.best_available
  44. runtimes.reject(&:deprecated?).find(&:available?)
  45. end
  46. def self.from_environment
  47. if name = ENV["EXECJS_RUNTIME"]
  48. if runtime = const_get(name)
  49. if runtime.available?
  50. runtime if runtime.available?
  51. else
  52. raise RuntimeUnavailable, "#{runtime.name} runtime is not available on this system"
  53. end
  54. elsif !name.empty?
  55. raise RuntimeUnavailable, "#{name} runtime is not defined"
  56. end
  57. end
  58. end
  59. def self.names
  60. @names ||= constants.inject({}) { |h, name| h.merge(const_get(name) => name) }.values
  61. end
  62. def self.runtimes
  63. @runtimes ||= [
  64. RubyRacer,
  65. RubyRhino,
  66. Johnson,
  67. Mustang,
  68. Node,
  69. JavaScriptCore,
  70. SpiderMonkey,
  71. JScript
  72. ]
  73. end
  74. end
  75. def self.runtimes
  76. Runtimes.runtimes
  77. end
  78. end