mirror of
https://github.com/rails/execjs
synced 2023-03-27 23:21:20 -04:00
Merge branch 'elcuervo-mustang_runtime'
This commit is contained in:
commit
4ccf35465a
5 changed files with 79 additions and 0 deletions
|
@ -11,6 +11,8 @@ ExecJS supports these runtimes:
|
|||
embedded within MRI Ruby
|
||||
* [therubyrhino](https://github.com/cowboyd/therubyrhino) - Mozilla
|
||||
Rhino embedded within JRuby
|
||||
* [Mustang](https://github.com/nu7hatch/mustang) - Mustang V8
|
||||
embedded within Ruby
|
||||
* [Node.js](http://nodejs.org/)
|
||||
* Apple JavaScriptCore - Included with Mac OS X
|
||||
* [Mozilla Spidermonkey](http://www.mozilla.org/js/spidermonkey/)
|
||||
|
|
|
@ -6,6 +6,7 @@ module ExecJS
|
|||
class ProgramError < Error; end
|
||||
|
||||
autoload :ExternalRuntime, "execjs/external_runtime"
|
||||
autoload :MustangRuntime, "execjs/mustang_runtime"
|
||||
autoload :RubyRacerRuntime, "execjs/ruby_racer_runtime"
|
||||
autoload :RubyRhinoRuntime, "execjs/ruby_rhino_runtime"
|
||||
autoload :Runtimes, "execjs/runtimes"
|
||||
|
|
72
lib/execjs/mustang_runtime.rb
Normal file
72
lib/execjs/mustang_runtime.rb
Normal file
|
@ -0,0 +1,72 @@
|
|||
module ExecJS
|
||||
class MustangRuntime
|
||||
class 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 => e
|
||||
raise ProgramError, e.message
|
||||
end
|
||||
|
||||
def unbox(value)
|
||||
case value
|
||||
when Mustang::V8::Array
|
||||
value.map { |v| unbox(v) }
|
||||
when Mustang::V8::Boolean
|
||||
value.to_bool
|
||||
when Mustang::V8::NullClass, Mustang::V8::UndefinedClass
|
||||
nil
|
||||
when Mustang::V8::Function
|
||||
nil
|
||||
when Mustang::V8::SyntaxError
|
||||
raise RuntimeError, value.message
|
||||
when Mustang::V8::Error
|
||||
raise ProgramError, value.message
|
||||
else
|
||||
value.respond_to?(:delegate) ? value.delegate : value
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
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
|
||||
rescue LoadError
|
||||
false
|
||||
end
|
||||
end
|
||||
end
|
|
@ -4,6 +4,8 @@ module ExecJS
|
|||
|
||||
RubyRhino = RubyRhinoRuntime.new
|
||||
|
||||
Mustang = MustangRuntime.new
|
||||
|
||||
Node = ExternalRuntime.new(
|
||||
:name => "Node.js (V8)",
|
||||
:command => ["nodejs", "node"],
|
||||
|
@ -38,6 +40,7 @@ module ExecJS
|
|||
@runtimes ||= [
|
||||
RubyRacer,
|
||||
RubyRhino,
|
||||
Mustang,
|
||||
Node,
|
||||
JavaScriptCore,
|
||||
Spidermonkey,
|
||||
|
|
|
@ -59,6 +59,7 @@ end
|
|||
runtimes = [
|
||||
"RubyRacer",
|
||||
"RubyRhino",
|
||||
"Mustang",
|
||||
"Node",
|
||||
"JavaScriptCore",
|
||||
"Spidermonkey",
|
||||
|
|
Loading…
Reference in a new issue