Skip to content

Commit

Permalink
Replace therubyracer with mini_racer
Browse files Browse the repository at this point in the history
 #1959

Might help with #1919
  • Loading branch information
dsander committed Apr 9, 2017
1 parent f21d6ab commit e394f48
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ gem 'rufus-scheduler', '~> 3.3.2', require: false
gem 'sass-rails', '~> 5.0.6'
gem 'select2-rails', '~> 3.5.4'
gem 'spectrum-rails'
gem 'therubyracer', '~> 0.12.2'
gem 'mini_racer', '~> 0.1.9'
gem 'typhoeus', '~> 0.6.3'
gem 'uglifier', '~> 2.7.2'

Expand Down
12 changes: 5 additions & 7 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ GEM
actionmailer (>= 3.2)
letter_opener (~> 1.0)
railties (>= 3.2)
libv8 (3.16.14.15)
libv8 (5.3.332.38.5)
liquid (3.0.6)
listen (3.0.5)
rb-fsevent (>= 0.9.3)
Expand All @@ -329,6 +329,8 @@ GEM
mimemagic (0.3.1)
mini_magick (4.2.3)
mini_portile2 (2.1.0)
mini_racer (0.1.9)
libv8 (~> 5.3)
minitest (5.10.1)
mqtt (0.3.1)
multi_json (1.12.1)
Expand Down Expand Up @@ -438,7 +440,6 @@ GEM
ffi (>= 0.5.0)
rb-kqueue (0.2.4)
ffi (>= 0.5.0)
ref (2.0.0)
responders (2.3.0)
railties (>= 4.2.0, < 5.1)
rest-client (1.8.0)
Expand Down Expand Up @@ -530,9 +531,6 @@ GEM
systemu (2.6.4)
term-ansicolor (1.3.2)
tins (~> 1.0)
therubyracer (0.12.2)
libv8 (~> 3.16.14.0)
ref
thor (0.19.4)
thread_safe (0.3.6)
tilt (2.0.5)
Expand Down Expand Up @@ -648,6 +646,7 @@ DEPENDENCIES
listen (~> 3.0.5)
loofah (~> 2.0)
mini_magick
mini_racer (~> 0.1.9)
mqtt
multi_xml
mysql2 (~> 0.3.20)
Expand Down Expand Up @@ -684,7 +683,6 @@ DEPENDENCIES
spring (~> 1.7.2)
spring-commands-rspec (~> 1.0.4)
spring-watcher-listen (~> 2.0.0)
therubyracer (~> 0.12.2)
tumblr_client!
twilio-ruby (~> 3.11.5)
twitter (~> 5.14.0)
Expand All @@ -702,7 +700,7 @@ DEPENDENCIES
xmpp4r (~> 0.5.6)

RUBY VERSION
ruby 2.3.1p112
ruby 2.3.3p222

BUNDLED WITH
1.14.6
38 changes: 17 additions & 21 deletions app/models/agents/java_script_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,26 +107,22 @@ def default_options

def execute_js(js_function, incoming_events = [])
js_function = js_function == "check" ? "check" : "receive"
context = V8::Context.new
context = MiniRacer::Context.new
context.eval(setup_javascript)

context["doCreateEvent"] = lambda { |a, y| create_event(payload: clean_nans(JSON.parse(y))).payload.to_json }
context["getIncomingEvents"] = lambda { |a| incoming_events.to_json }
context["getOptions"] = lambda { |a, x| interpolated.to_json }
context["doLog"] = lambda { |a, x| log x }
context["doError"] = lambda { |a, x| error x }
context["getMemory"] = lambda { |a| memory.to_json }
context["setMemoryKey"] = lambda do |a, x, y|
memory[x] = clean_nans(y)
end
context["setMemory"] = lambda do |a, x|
memory.replace(clean_nans(x))
end
context["deleteKey"] = lambda { |a, x| memory.delete(x).to_json }
context["escapeHtml"] = lambda { |a, x| CGI.escapeHTML(x) }
context["unescapeHtml"] = lambda { |a, x| CGI.unescapeHTML(x) }
context['getCredential'] = lambda { |a, k| credential(k); }
context['setCredential'] = lambda { |a, k, v| set_credential(k, v) }
context.attach("doCreateEvent", -> (y) { create_event(payload: clean_nans(JSON.parse(y))).payload.to_json })
context.attach("getIncomingEvents", -> { incoming_events.to_json })
context.attach("getOptions", -> { interpolated.to_json })
context.attach("doLog", -> (x) { log x })
context.attach("doError", -> (x) { error x })
context.attach("getMemory", -> { memory.to_json })
context.attach("setMemoryKey", -> (x, y) { memory[x] = clean_nans(y) })
context.attach("setMemory", -> (x) { memory.replace(clean_nans(x)) })
context.attach("deleteKey", -> (x) { memory.delete(x).to_json })
context.attach("escapeHtml", -> (x) { CGI.escapeHTML(x) })
context.attach("unescapeHtml", -> (x) { CGI.unescapeHTML(x) })
context.attach('getCredential', -> (k) { credential(k); })
context.attach('setCredential', -> (k, v) { set_credential(k, v) })

if (options['language'] || '').downcase == 'coffeescript'
context.eval(CoffeeScript.compile code)
Expand Down Expand Up @@ -225,15 +221,15 @@ def setup_javascript
def log_errors
begin
yield
rescue V8::Error => e
rescue MiniRacer::EvalError, MiniRacer::SnapshotError, MiniRacer::PlatformAlreadyInitialized => e
error "JavaScript error: #{e.message}"
end
end

def clean_nans(input)
if input.is_a?(V8::Array)
if input.is_a?(Array)
input.map {|v| clean_nans(v) }
elsif input.is_a?(V8::Object)
elsif input.is_a?(Hash)
input.inject({}) { |m, (k, v)| m[k] = clean_nans(v); m }
elsif input.is_a?(Float) && input.nan?
'NaN'
Expand Down

0 comments on commit e394f48

Please sign in to comment.