Permalink
Browse files

Add Runtime base class

  • Loading branch information...
1 parent 4cd4e45 commit e2680928654bec34a4a701d9b6ab74d7cae56b37 @josh josh committed May 20, 2012
@@ -1,5 +1,7 @@
+require "execjs/runtime"
+
module ExecJS
- class DisabledRuntime
+ class DisabledRuntime < Runtime
def name
"Disabled"
end
View
@@ -0,0 +1,33 @@
+module ExecJS
+ # Encodes strings as UTF-8
+ module Encoding
+ if defined? "".respond_to?(:encode)
+ if defined?(RUBY_ENGINE) && (RUBY_ENGINE == 'jruby' || RUBY_ENGINE == 'rbx')
+ # workaround for jruby bug http://jira.codehaus.org/browse/JRUBY-6588
+ # workaround for rbx bug https://github.com/rubinius/rubinius/issues/1729
+ def encode(string)
+ if string.encoding.name == 'ASCII-8BIT'
+ data = string.dup
+ data.force_encoding('UTF-8')
+
+ unless data.valid_encoding?
+ raise ::Encoding::UndefinedConversionError, "Could not encode ASCII-8BIT data #{string.dump} as UTF-8"
+ end
+ else
+ data = string.encode('UTF-8')
+ end
+ data
+ end
+ else
+ def encode(string)
+ string.encode('UTF-8')
+ end
+ end
+ else
+ # Define no-op on 1.8
+ def encode(string)
+ string
+ end
+ end
+ end
+end
@@ -1,27 +1,28 @@
require "multi_json"
require "shellwords"
require "tempfile"
+require "execjs/runtime"
module ExecJS
- class ExternalRuntime
- class Context
+ class ExternalRuntime < Runtime
+ class Context < Runtime::Context
def initialize(runtime, source = "")
- source = ExecJS.encode(source)
+ source = encode(source)
@runtime = runtime
@source = source
end
def eval(source, options = {})
- source = ExecJS.encode(source)
+ source = encode(source)
if /\S/ =~ source
exec("return eval(#{json_encode("(#{source})")})")
end
end
def exec(source, options = {})
- source = ExecJS.encode(source)
+ source = encode(source)
source = "#{@source}\n#{source}" if @source
compile_to_tempfile(source) do |file|
@@ -116,20 +117,6 @@ def initialize(options)
@binary = nil
end
- def exec(source)
- context = Context.new(self)
- context.exec(source)
- end
-
- def eval(source)
- context = Context.new(self)
- context.eval(source)
- end
-
- def compile(source)
- Context.new(self, source)
- end
-
def available?
binary ? true : false
end
@@ -195,7 +182,7 @@ def which(command)
def sh(command)
output, options = nil, {}
options[:external_encoding] = @encoding if @encoding
- options[:internal_encoding] = Encoding.default_internal || 'UTF-8'
+ options[:internal_encoding] = ::Encoding.default_internal || 'UTF-8'
IO.popen(command, options) { |f| output = f.read }
output
end
@@ -1,21 +1,25 @@
+require "execjs/runtime"
+
module ExecJS
- class JohnsonRuntime
- class Context
- def initialize(source = "")
+ class JohnsonRuntime < Runtime
+ class Context < Runtime::Context
+ def initialize(runtime, source = "")
+ source = encode(source)
+
@runtime = Johnson::Runtime.new
@runtime.evaluate(source)
end
def exec(source, options = {})
- source = source.encode('UTF-8') if source.respond_to?(:encode)
+ source = encode(source)
if /\S/ =~ source
eval "(function(){#{source}})()", options
end
end
def eval(source, options = {})
- source = source.encode('UTF-8') if source.respond_to?(:encode)
+ source = encode(source)
if /\S/ =~ source
unbox @runtime.evaluate("(#{source})")
@@ -88,20 +92,6 @@ def name
"Johnson (SpiderMonkey)"
end
- def exec(source)
- context = Context.new
- context.exec(source)
- end
-
- def eval(source)
- context = Context.new
- context.eval(source)
- end
-
- def compile(source)
- Context.new(source)
- end
-
def available?
require "johnson"
true
View
@@ -34,34 +34,5 @@ def root
def windows?
@windows ||= RbConfig::CONFIG["host_os"] =~ /mswin|mingw/
end
-
- if defined? Encoding
- if (!defined?(RUBY_ENGINE) || (RUBY_ENGINE != "jruby" && RUBY_ENGINE != "rbx"))
- def encode(string)
- string.encode('UTF-8')
- end
- else
- # workaround for jruby bug http://jira.codehaus.org/browse/JRUBY-6588
- # workaround for rbx bug https://github.com/rubinius/rubinius/issues/1729
- def encode(string)
- if string.encoding.name == 'ASCII-8BIT'
- data = string.dup
- data.force_encoding('utf-8')
-
- unless data.valid_encoding?
- raise Encoding::UndefinedConversionError, "Could not encode ASCII-8BIT data #{string.dump} as UTF-8"
- end
- else
- data = string.encode('utf-8')
- end
- data
- end
- end
- else
- # Define no-op on 1.8
- def encode(string)
- string
- end
- end
end
end
@@ -1,23 +1,25 @@
+require "execjs/runtime"
+
module ExecJS
- class MustangRuntime
- class Context
- def initialize(source = "")
- source = ExecJS.encode(source)
+ class MustangRuntime < Runtime
+ class Context < Runtime::Context
+ def initialize(runtime, source = "")
+ source = encode(source)
@v8_context = ::Mustang::Context.new
@v8_context.eval(source)
end
def exec(source, options = {})
- source = ExecJS.encode(source)
+ source = encode(source)
if /\S/ =~ source
eval "(function(){#{source}})()", options
end
end
def eval(source, options = {})
- source = ExecJS.encode(source)
+ source = encode(source)
if /\S/ =~ source
unbox @v8_context.eval("(#{source})")
@@ -60,20 +62,6 @@ def name
"Mustang (V8)"
end
- def exec(source)
- context = Context.new
- context.exec(source)
- end
-
- def eval(source)
- context = Context.new
- context.eval(source)
- end
-
- def compile(source)
- Context.new(source)
- end
-
def available?
require "mustang"
true
@@ -1,8 +1,10 @@
+require "execjs/runtime"
+
module ExecJS
- class RubyRacerRuntime
- class Context
- def initialize(source = "")
- source = source.encode('UTF-8') if source.respond_to?(:encode)
+ class RubyRacerRuntime < Runtime
+ class Context < Runtime::Context
+ def initialize(runtime, source = "")
+ source = encode(source)
lock do
@v8_context = ::V8::Context.new
@@ -11,15 +13,15 @@ def initialize(source = "")
end
def exec(source, options = {})
- source = source.encode('UTF-8') if source.respond_to?(:encode)
+ source = encode(source)
if /\S/ =~ source
eval "(function(){#{source}})()", options
end
end
def eval(source, options = {})
- source = source.encode('UTF-8') if source.respond_to?(:encode)
+ source = encode(source)
if /\S/ =~ source
lock do
@@ -92,21 +94,7 @@ def lock
def name
"therubyracer (V8)"
end
-
- def exec(source)
- context = Context.new
- context.exec(source)
- end
-
- def eval(source)
- context = Context.new
- context.eval(source)
- end
-
- def compile(source)
- Context.new(source)
- end
-
+
def available?
require "v8"
true
@@ -1,24 +1,26 @@
+require "execjs/runtime"
+
module ExecJS
- class RubyRhinoRuntime
- class Context
- def initialize(source = "")
- source = ExecJS.encode(source)
+ class RubyRhinoRuntime < Runtime
+ class Context < Runtime::Context
+ def initialize(runtime, source = "")
+ source = encode(source)
@rhino_context = ::Rhino::Context.new
fix_memory_limit! @rhino_context
@rhino_context.eval(source)
end
def exec(source, options = {})
- source = ExecJS.encode(source)
+ source = encode(source)
if /\S/ =~ source
eval "(function(){#{source}})()", options
end
end
def eval(source, options = {})
- source = ExecJS.encode(source)
+ source = encode(source)
if /\S/ =~ source
unbox @rhino_context.eval("(#{source})")
@@ -76,21 +78,7 @@ def fix_memory_limit!(context)
def name
"therubyrhino (Rhino)"
end
-
- def exec(source)
- context = Context.new
- context.exec(source)
- end
-
- def eval(source)
- context = Context.new
- context.eval(source)
- end
-
- def compile(source)
- Context.new(source)
- end
-
+
def available?
require "rhino"
true
Oops, something went wrong.

0 comments on commit e268092

Please sign in to comment.