Permalink
Browse files

Added Mustang V8 as runtime

  • Loading branch information...
1 parent 8f438d8 commit bffdd50b228bd2cbdd092e04b347659d3febeee8 @elcuervo elcuervo committed Apr 12, 2011
Showing with 78 additions and 0 deletions.
  1. +1 −0 lib/execjs.rb
  2. +73 −0 lib/execjs/mustang_runtime.rb
  3. +3 −0 lib/execjs/runtimes.rb
  4. +1 −0 test/test_runtimes.rb
View
@@ -7,6 +7,7 @@ class ProgramError < Error; end
autoload :ExternalRuntime, "execjs/external_runtime"
autoload :RubyRacerRuntime, "execjs/ruby_racer_runtime"
+ autoload :MustangRuntime, "execjs/mustang_runtime"
autoload :RubyRhinoRuntime, "execjs/ruby_rhino_runtime"
autoload :Runtimes, "execjs/runtimes"
@@ -0,0 +1,73 @@
+module ExecJS
+ class MustangRuntime
+ def name
+ "Mustang (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
+ rescue LoadError
+ false
+ end
+
+ end
+end
+
+module ExecJS
+ class MustangRuntime::Context
+
+ def initialize(source = "")
+ @v8_context = ::Mustang::Context.new
+ @v8_context.eval(source)
+ end
+
+ def exec(source, options = {})
+ if /\S/ =~ source
+ eval "(function(){#{source}})()", options
+ end
+ end
+
+ def eval(source, options = {})
+ if /\S/ =~ source
+ unbox @v8_context.eval("(#{source})")
+ end
+ end
+
+ def call(properties, *args)
+ unbox @v8_context.eval(properties).call(*args)
+ rescue NoMethodError
+ raise ProgramError
+ end
+
+ def unbox(value)
+ case value
+ when Mustang::V8::NullClass, Mustang::V8::UndefinedClass, Mustang::V8::Function
+ nil
+ when Mustang::V8::Array
+ value.map { |v| unbox(v) }
+ when Mustang::V8::SyntaxError
+ raise RuntimeError
+ when Mustang::V8::Error
+ raise ProgramError
+ else
+ value.respond_to?(:delegate) ? value.delegate : value
+ end
+ end
+
+ end
+end
View
@@ -2,6 +2,8 @@ module ExecJS
module Runtimes
RubyRacer = RubyRacerRuntime.new
+ Mustang = MustangRuntime.new
+
RubyRhino = RubyRhinoRuntime.new
Node = ExternalRuntime.new(
@@ -37,6 +39,7 @@ def self.best_available
def self.runtimes
@runtimes ||= [
RubyRacer,
+ Mustang,
RubyRhino,
Node,
JavaScriptCore,
View
@@ -57,6 +57,7 @@ def test_thrown_exception
end
runtimes = [
+ "Mustang",
"RubyRacer",
"RubyRhino",
"Node",

0 comments on commit bffdd50

Please sign in to comment.