mirror of
https://github.com/rails/execjs
synced 2023-03-27 23:21:20 -04:00
Instantiate with options instead of subclassing
This commit is contained in:
parent
378a9236be
commit
954db082da
6 changed files with 31 additions and 49 deletions
|
@ -3,6 +3,11 @@ require "tempfile"
|
|||
|
||||
module ExecJS
|
||||
class Runtime
|
||||
def initialize(options)
|
||||
@command = options[:command]
|
||||
@runner_path = options[:runner_path]
|
||||
end
|
||||
|
||||
def exec(source)
|
||||
compile_to_tempfile(source) do |file|
|
||||
extract_result(exec_runtime(file.path))
|
||||
|
@ -21,7 +26,7 @@ module ExecJS
|
|||
end
|
||||
|
||||
def runner_source
|
||||
@runner_source ||= IO.read(runner_path)
|
||||
@runner_source ||= IO.read(@runner_path)
|
||||
end
|
||||
|
||||
def compile_to_tempfile(source)
|
||||
|
@ -34,7 +39,7 @@ module ExecJS
|
|||
end
|
||||
|
||||
def exec_runtime(filename)
|
||||
output = `#{command(filename)} 2>&1`
|
||||
output = `#{@command} #{filename} 2>&1`
|
||||
if $?.success?
|
||||
output
|
||||
else
|
||||
|
|
|
@ -1,11 +1,27 @@
|
|||
module ExecJS
|
||||
module Runtimes
|
||||
autoload :JSC, "execjs/runtimes/jsc"
|
||||
autoload :Node, "execjs/runtimes/node"
|
||||
autoload :V8, "execjs/runtimes/v8"
|
||||
|
||||
def self.runtime
|
||||
V8.new
|
||||
V8
|
||||
end
|
||||
|
||||
def self.runner_path(path)
|
||||
File.expand_path("../runtimes/#{path}", __FILE__)
|
||||
end
|
||||
|
||||
JSC = Runtime.new(
|
||||
:command => "/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc",
|
||||
:runner_path => runner_path("jsc.js")
|
||||
)
|
||||
|
||||
Node = Runtime.new(
|
||||
:command => "node",
|
||||
:runner_path => runner_path("node.js")
|
||||
)
|
||||
)
|
||||
|
||||
V8 = Runtime.new(
|
||||
:command => "v8",
|
||||
:runner_path => runner_path("v8.js")
|
||||
)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,13 +0,0 @@
|
|||
module ExecJS
|
||||
module Runtimes
|
||||
class JSC < Runtime
|
||||
def command(filename)
|
||||
"/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc #{filename}"
|
||||
end
|
||||
|
||||
def runner_path
|
||||
File.expand_path('../v8.js', __FILE__)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,13 +0,0 @@
|
|||
module ExecJS
|
||||
module Runtimes
|
||||
class Node < Runtime
|
||||
def command(filename)
|
||||
"node #{filename}"
|
||||
end
|
||||
|
||||
def runner_path
|
||||
File.expand_path('../node.js', __FILE__)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,13 +0,0 @@
|
|||
module ExecJS
|
||||
module Runtimes
|
||||
class V8 < Runtime
|
||||
def command(filename)
|
||||
"v8 #{filename}"
|
||||
end
|
||||
|
||||
def runner_path
|
||||
File.expand_path('../v8.js', __FILE__)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -43,7 +43,7 @@ class TestJSCRuntime < Test::Unit::TestCase
|
|||
include TestRuntime
|
||||
|
||||
def setup
|
||||
@runtime = ExecJS::Runtimes::JSC.new
|
||||
@runtime = ExecJS::Runtimes::JSC
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -51,7 +51,7 @@ class TestNodeRuntime < Test::Unit::TestCase
|
|||
include TestRuntime
|
||||
|
||||
def setup
|
||||
@runtime = ExecJS::Runtimes::Node.new
|
||||
@runtime = ExecJS::Runtimes::Node
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -59,6 +59,6 @@ class TestV8Runtime < Test::Unit::TestCase
|
|||
include TestRuntime
|
||||
|
||||
def setup
|
||||
@runtime = ExecJS::Runtimes::V8.new
|
||||
@runtime = ExecJS::Runtimes::V8
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue