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

Merge branch 'SamSaffron/master'

This commit is contained in:
Guillermo Iguaran 2016-05-14 12:28:08 -05:00
commit 975c80bf03
5 changed files with 138 additions and 6 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
Gemfile.lock
pkg/*
.bundle/*

View file

@ -1,11 +1,13 @@
language: ruby
cache: bundler
sudo: false
# we need trust so correct gcc runs for mini_racer, latest v8 requires gcc 4.8+
before_install:
- if [ "$EXECJS_RUNTIME" == "V8" ]; then brew update; fi
- if [ "$EXECJS_RUNTIME" == "V8" ]; then brew install v8; fi
script: bundle exec ruby test/test_execjs.rb
- if [ "$EXECJS_RUNTIME" == "MiniRacer" ]; then gem install bundler -v 1.12.0; fi
script: bundle && bundle exec ruby test/test_execjs.rb
matrix:
include:
@ -15,6 +17,10 @@ matrix:
env: EXECJS_RUNTIME=Duktape
- rvm: 2.0.0
env: EXECJS_RUNTIME=RubyRacer
- rvm: 2.0.0
env: EXECJS_RUNTIME=MiniRacer
dist: trusty
sudo: true
- rvm: 2.1
env: EXECJS_RUNTIME=Node
@ -22,6 +28,10 @@ matrix:
env: EXECJS_RUNTIME=Duktape
- rvm: 2.1
env: EXECJS_RUNTIME=RubyRacer
- rvm: 2.1
env: EXECJS_RUNTIME=MiniRacer
dist: trusty
sudo: true
- rvm: 2.2
env: EXECJS_RUNTIME=Node
@ -29,13 +39,21 @@ matrix:
env: EXECJS_RUNTIME=Duktape
- rvm: 2.2
env: EXECJS_RUNTIME=RubyRacer
- rvm: 2.2
env: EXECJS_RUNTIME=MiniRacer
dist: trusty
sudo: true
- rvm: 2.3.0
- rvm: 2.3.1
env: EXECJS_RUNTIME=Node
- rvm: 2.3.0
- rvm: 2.3.1
env: EXECJS_RUNTIME=Duktape
- rvm: 2.3.0
- rvm: 2.3.1
env: EXECJS_RUNTIME=RubyRacer
- rvm: 2.3.1
env: EXECJS_RUNTIME=MiniRacer
dist: trusty
sudo: true
- rvm: jruby-19mode
env: EXECJS_RUNTIME=Node
@ -52,3 +70,6 @@ matrix:
env: EXECJS_RUNTIME=RubyRacer
- os: osx
env: EXECJS_RUNTIME=V8
- os: osx
env: EXECJS_RUNTIME=MiniRacer
osx_image: xcode7.3

View file

@ -4,7 +4,11 @@ gemspec
group :test do
gem 'duktape', platform: :mri
gem 'therubyracer', platform: :mri
gem 'therubyrhino', ">=1.73.3", platform: :jruby
if ENV['EXECJS_RUNTIME'] == 'MiniRacer'
gem 'mini_racer', '0.1.0.beta.3', platform: :mri
else
gem 'therubyracer', platform: :mri
end
gem 'therubyrhino', '>=1.73.3', platform: :jruby
gem 'minitest', require: false
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/ruby_racer_runtime"
require "execjs/ruby_rhino_runtime"
require "execjs/mini_racer_runtime"
module ExecJS
module Runtimes
@ -15,6 +16,8 @@ module ExecJS
RubyRhino = RubyRhinoRuntime.new
MiniRacer = MiniRacerRuntime.new
Node = ExternalRuntime.new(
name: "Node.js (V8)",
command: ["nodejs", "node"],
@ -79,6 +82,7 @@ module ExecJS
RubyRacer,
RubyRhino,
Duktape,
MiniRacer,
Node,
JavaScriptCore,
SpiderMonkey,