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
|
module ExecJS
|
||||||
class Runtime
|
class Runtime
|
||||||
|
def initialize(options)
|
||||||
|
@command = options[:command]
|
||||||
|
@runner_path = options[:runner_path]
|
||||||
|
end
|
||||||
|
|
||||||
def exec(source)
|
def exec(source)
|
||||||
compile_to_tempfile(source) do |file|
|
compile_to_tempfile(source) do |file|
|
||||||
extract_result(exec_runtime(file.path))
|
extract_result(exec_runtime(file.path))
|
||||||
|
@ -21,7 +26,7 @@ module ExecJS
|
||||||
end
|
end
|
||||||
|
|
||||||
def runner_source
|
def runner_source
|
||||||
@runner_source ||= IO.read(runner_path)
|
@runner_source ||= IO.read(@runner_path)
|
||||||
end
|
end
|
||||||
|
|
||||||
def compile_to_tempfile(source)
|
def compile_to_tempfile(source)
|
||||||
|
@ -34,7 +39,7 @@ module ExecJS
|
||||||
end
|
end
|
||||||
|
|
||||||
def exec_runtime(filename)
|
def exec_runtime(filename)
|
||||||
output = `#{command(filename)} 2>&1`
|
output = `#{@command} #{filename} 2>&1`
|
||||||
if $?.success?
|
if $?.success?
|
||||||
output
|
output
|
||||||
else
|
else
|
||||||
|
|
|
@ -1,11 +1,27 @@
|
||||||
module ExecJS
|
module ExecJS
|
||||||
module Runtimes
|
module Runtimes
|
||||||
autoload :JSC, "execjs/runtimes/jsc"
|
|
||||||
autoload :Node, "execjs/runtimes/node"
|
|
||||||
autoload :V8, "execjs/runtimes/v8"
|
|
||||||
|
|
||||||
def self.runtime
|
def self.runtime
|
||||||
V8.new
|
V8
|
||||||
end
|
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
|
||||||
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
|
include TestRuntime
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
@runtime = ExecJS::Runtimes::JSC.new
|
@runtime = ExecJS::Runtimes::JSC
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -51,7 +51,7 @@ class TestNodeRuntime < Test::Unit::TestCase
|
||||||
include TestRuntime
|
include TestRuntime
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
@runtime = ExecJS::Runtimes::Node.new
|
@runtime = ExecJS::Runtimes::Node
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -59,6 +59,6 @@ class TestV8Runtime < Test::Unit::TestCase
|
||||||
include TestRuntime
|
include TestRuntime
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
@runtime = ExecJS::Runtimes::V8.new
|
@runtime = ExecJS::Runtimes::V8
|
||||||
end
|
end
|
||||||
end
|
end
|
Loading…
Add table
Reference in a new issue