1
0
Fork 0
mirror of https://github.com/rails/execjs synced 2023-03-27 23:21:20 -04:00

add MiniRacer runtime support

This commit is contained in:
Sam 2016-05-11 18:45:27 +10:00 committed by Guillermo Iguaran
parent 8f22894ca7
commit f587a97475
3 changed files with 107 additions and 0 deletions

View file

@ -7,4 +7,5 @@ group :test do
gem 'therubyracer', platform: :mri gem 'therubyracer', platform: :mri
gem 'therubyrhino', ">=1.73.3", platform: :jruby gem 'therubyrhino', ">=1.73.3", platform: :jruby
gem 'minitest', require: false gem 'minitest', require: false
gem 'mini_racer', platform: :mri
end end

View file

@ -0,0 +1,102 @@
require "execjs/runtime"
module ExecJS
class MiniRacerRuntime < Runtime
class Context < Runtime::Context
def initialize(runtime, source = "", options={})
source = encode(source)
@context = ::MiniRacer::Context.new
translate do
@context.eval(source)
end
end
def exec(source, options = {})
source = encode(source)
if /\S/ =~ source
eval "(function(){#{source}})()"
end
end
def eval(source, options = {})
source = encode(source)
if /\S/ =~ source
translate do
@context.eval("(#{source})")
end
end
end
def call(identifier, *args)
# TODO optimise generate
eval "#{identifier}.apply(this, #{::JSON.generate(args)})"
end
private
def strip_functions!(value)
if Array === value
value.map! do |v|
if MiniRacer::JavaScriptFunction === value
nil
else
strip_functions!(v)
end
end
elsif Hash === value
value.each do |k,v|
if MiniRacer::JavaScriptFunction === v
value.delete k
else
value[k] = strip_functions!(v)
end
end
value
elsif MiniRacer::JavaScriptFunction === value
nil
else
value
end
end
def translate
begin
strip_functions! yield
rescue MiniRacer::RuntimeError => e
ex = ProgramError.new e.message
if backtrace = e.backtrace
backtrace = backtrace.map { |line|
if line =~ /JavaScript at/
line.sub("JavaScript at ", "")
.sub("<anonymous>", "(execjs)")
.strip
else
line
end
}
ex.set_backtrace backtrace
end
raise ex
rescue MiniRacer::ParseError => e
ex = RuntimeError.new e.message
ex.set_backtrace(["(execjs):1"] + e.backtrace)
raise ex
end
end
end
def name
"mini_racer (V8)"
end
def available?
require "mini_racer"
true
rescue LoadError
false
end
end
end

View file

@ -4,6 +4,7 @@ require "execjs/duktape_runtime"
require "execjs/external_runtime" require "execjs/external_runtime"
require "execjs/ruby_racer_runtime" require "execjs/ruby_racer_runtime"
require "execjs/ruby_rhino_runtime" require "execjs/ruby_rhino_runtime"
require "execjs/mini_racer_runtime"
module ExecJS module ExecJS
module Runtimes module Runtimes
@ -15,6 +16,8 @@ module ExecJS
RubyRhino = RubyRhinoRuntime.new RubyRhino = RubyRhinoRuntime.new
MiniRacer = MiniRacerRuntime.new
Node = ExternalRuntime.new( Node = ExternalRuntime.new(
name: "Node.js (V8)", name: "Node.js (V8)",
command: ["nodejs", "node"], command: ["nodejs", "node"],
@ -79,6 +82,7 @@ module ExecJS
RubyRacer, RubyRacer,
RubyRhino, RubyRhino,
Duktape, Duktape,
MiniRacer,
Node, Node,
JavaScriptCore, JavaScriptCore,
SpiderMonkey, SpiderMonkey,