Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Instantiate with options instead of subclassing

  • Loading branch information...
commit 954db082da03227e02e9444967f9b1825718f410 1 parent 378a923
@sstephenson authored
View
9 lib/execjs/runtime.rb
@@ -3,6 +3,11 @@
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 @@ def compile(source)
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 @@ def compile_to_tempfile(source)
end
def exec_runtime(filename)
- output = `#{command(filename)} 2>&1`
+ output = `#{@command} #{filename} 2>&1`
if $?.success?
output
else
View
26 lib/execjs/runtimes.rb
@@ -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
View
13 lib/execjs/runtimes/jsc.rb
@@ -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
View
13 lib/execjs/runtimes/node.rb
@@ -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
View
13 lib/execjs/runtimes/v8.rb
@@ -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
View
6 test/test_runtime.rb → test/test_runtimes.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.