Permalink
Browse files

Extract ExecJS::Runtime

  • Loading branch information...
1 parent 6a1bf0c commit 0be476df211e8fea2ef2f8a88c155ab9bc5f4395 @sstephenson committed Feb 7, 2011
Showing with 62 additions and 52 deletions.
  1. +2 −1 lib/execjs.rb
  2. +54 −0 lib/execjs/runtime.rb
  3. +5 −50 lib/execjs/runtimes/node.rb
  4. +1 −1 test/test_node_runtime.rb
View
3 lib/execjs.rb
@@ -3,6 +3,7 @@ class Error < ::StandardError; end
class RuntimeError < Error; end
class ProgramError < Error; end
+ autoload :Runtime, "execjs/runtime"
autoload :Runtimes, "execjs/runtimes"
def self.exec(source)
@@ -14,6 +15,6 @@ def self.eval(source)
end
def self.runtime
- @runtime ||= Runtimes::Node
+ @runtime ||= Runtimes::Node.new
end
end
View
54 lib/execjs/runtime.rb
@@ -0,0 +1,54 @@
+require "json"
+require "tempfile"
+
+module ExecJS
+ class Runtime
+ def exec(source)
+ compile_to_tempfile(source) do |file|
+ extract_result(exec_runtime(file.path))
+ end
+ end
+
+ def eval(source)
+ if /\S/ =~ source
+ exec("return eval(#{"(#{source})".to_json})")
+ end
+ end
+
+ protected
+ def compile(source)
+ runner_source.sub('#{source}', source)
+ end
+
+ def runner_source
+ @runner_source ||= IO.read(runner_path)
+ end
+
+ def compile_to_tempfile(source)
+ tempfile = Tempfile.open("execjs")
+ tempfile.write compile(source)
+ tempfile.close
+ yield tempfile
+ ensure
+ tempfile.close!
+ end
+
+ def exec_runtime(filename)
+ output = `#{command(filename)} 2>&1`
+ if $?.success?
+ output
+ else
+ raise RuntimeError, output
+ end
+ end
+
+ def extract_result(output)
+ status, value = output.empty? ? [] : JSON.parse(output)
+ if status == "ok"
+ value
+ else
+ raise ProgramError, value
+ end
+ end
+ end
+end
View
55 lib/execjs/runtimes/node.rb
@@ -1,58 +1,13 @@
-require "json"
-require "tempfile"
-
module ExecJS
module Runtimes
- module Node
- extend self
-
- def exec(source)
- compile_to_tempfile(source) do |file|
- extract_result(exec_runtime("node #{file.path}"))
- end
+ class Node < Runtime
+ def command(filename)
+ "node #{filename}"
end
- def eval(source)
- if /\S/ =~ source
- exec("return eval(#{"(#{source})".to_json})")
- end
+ def runner_path
+ File.expand_path('../node.js', __FILE__)
end
-
- protected
- def compile_to_tempfile(source)
- tempfile = Tempfile.open("execjs")
- tempfile.write compile(source)
- tempfile.close
- yield tempfile
- ensure
- tempfile.close!
- end
-
- def compile(source)
- wrapper.sub('#{source}', source)
- end
-
- def wrapper
- @wrapper ||= IO.read(File.expand_path('../node.js', __FILE__))
- end
-
- def exec_runtime(command)
- output = `#{command} 2>&1`
- if $?.success?
- output
- else
- raise RuntimeError, output
- end
- end
-
- def extract_result(output)
- status, value = output.empty? ? [] : JSON.parse(output)
- if status == "ok"
- value
- else
- raise ProgramError, value
- end
- end
end
end
end
View
2 test/test_node_runtime.rb
@@ -3,7 +3,7 @@
class TestNodeRuntime < Test::Unit::TestCase
def setup
- @runtime = ExecJS::Runtimes::Node
+ @runtime = ExecJS::Runtimes::Node.new
end
def test_exec

0 comments on commit 0be476d

Please sign in to comment.