From 954db082da03227e02e9444967f9b1825718f410 Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Sun, 6 Feb 2011 20:06:36 -0600 Subject: [PATCH] Instantiate with options instead of subclassing --- lib/execjs/runtime.rb | 9 ++++++-- lib/execjs/runtimes.rb | 26 +++++++++++++++++----- lib/execjs/runtimes/jsc.rb | 13 ----------- lib/execjs/runtimes/node.rb | 13 ----------- lib/execjs/runtimes/v8.rb | 13 ----------- test/{test_runtime.rb => test_runtimes.rb} | 6 ++--- 6 files changed, 31 insertions(+), 49 deletions(-) delete mode 100644 lib/execjs/runtimes/jsc.rb delete mode 100644 lib/execjs/runtimes/node.rb delete mode 100644 lib/execjs/runtimes/v8.rb rename test/{test_runtime.rb => test_runtimes.rb} (92%) diff --git a/lib/execjs/runtime.rb b/lib/execjs/runtime.rb index 951fd7d..9e432c0 100644 --- a/lib/execjs/runtime.rb +++ b/lib/execjs/runtime.rb @@ -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 diff --git a/lib/execjs/runtimes.rb b/lib/execjs/runtimes.rb index 1bdda96..4bc2489 100644 --- a/lib/execjs/runtimes.rb +++ b/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 diff --git a/lib/execjs/runtimes/jsc.rb b/lib/execjs/runtimes/jsc.rb deleted file mode 100644 index 9d36cb1..0000000 --- a/lib/execjs/runtimes/jsc.rb +++ /dev/null @@ -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 diff --git a/lib/execjs/runtimes/node.rb b/lib/execjs/runtimes/node.rb deleted file mode 100644 index 677fb4b..0000000 --- a/lib/execjs/runtimes/node.rb +++ /dev/null @@ -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 diff --git a/lib/execjs/runtimes/v8.rb b/lib/execjs/runtimes/v8.rb deleted file mode 100644 index aec0030..0000000 --- a/lib/execjs/runtimes/v8.rb +++ /dev/null @@ -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 diff --git a/test/test_runtime.rb b/test/test_runtimes.rb similarity index 92% rename from test/test_runtime.rb rename to test/test_runtimes.rb index c06509c..7c3d656 100644 --- a/test/test_runtime.rb +++ b/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