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 Jan 24, 2018
1 parent fc1daf8 commit d49f939
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 29 deletions.
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ gem 'rufus-scheduler', '~> 3.3.2', require: false
gem 'sass-rails', '~> 5.0'
gem 'select2-rails', '~> 3.5.4'
gem 'spectrum-rails'
gem 'therubyracer', '~> 0.12.3'
gem 'mini_racer', '~> 0.1.15'
gem 'execjs', '~> 2.7.0'
gem 'typhoeus', '~> 0.6.3'
gem 'uglifier', '~> 2.7.2'

Expand Down
13 changes: 6 additions & 7 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ GEM
evernote_oauth (0.2.3)
evernote-thrift
oauth (>= 0.4.1)
execjs (2.6.0)
execjs (2.7.0)
faraday (0.12.1)
multipart-post (>= 1.2, < 3)
feedjira (2.1.2)
Expand Down Expand Up @@ -364,7 +364,7 @@ GEM
actionmailer (>= 3.2)
letter_opener (~> 1.0)
railties (>= 3.2)
libv8 (3.16.14.19)
libv8 (6.3.292.48.1)
liquid (4.0.0)
listen (3.0.8)
rb-fsevent (~> 0.9, >= 0.9.4)
Expand All @@ -390,6 +390,8 @@ GEM
mimemagic (0.3.1)
mini_magick (4.2.3)
mini_portile2 (2.3.0)
mini_racer (0.1.15)
libv8 (~> 6.3)
minitest (5.10.2)
mqtt (0.3.1)
multi_json (1.12.1)
Expand Down Expand Up @@ -498,7 +500,6 @@ GEM
ffi (>= 0.5.0, < 2)
rb-kqueue (0.2.4)
ffi (>= 0.5.0)
ref (2.0.0)
representable (3.0.4)
declarative (< 0.1.0)
declarative-option (< 0.2.0)
Expand Down Expand Up @@ -595,9 +596,6 @@ GEM
systemu (2.6.4)
term-ansicolor (1.6.0)
tins (~> 1.0)
therubyracer (0.12.3)
libv8 (~> 3.16.14.15)
ref
thor (0.19.4)
thread_safe (0.3.6)
tilt (2.0.7)
Expand Down Expand Up @@ -675,6 +673,7 @@ DEPENDENCIES
dropbox-api!
em-http-request (~> 1.1.2)
evernote_oauth
execjs (~> 2.7.0)
faraday (~> 0.9)
faraday_middleware!
feedjira (~> 2.1)
Expand Down Expand Up @@ -705,6 +704,7 @@ DEPENDENCIES
listen (~> 3.0.5)
loofah (~> 2.0)
mini_magick
mini_racer (~> 0.1.15)
mqtt
multi_xml
mysql2 (>= 0.3.18, < 0.5)
Expand Down Expand Up @@ -741,7 +741,6 @@ DEPENDENCIES
spring (~> 2.0.2)
spring-commands-rspec (~> 1.0.4)
spring-watcher-listen (~> 2.0.1)
therubyracer (~> 0.12.3)
tumblr_client!
twilio-ruby (~> 3.11.5)
twitter!
Expand Down
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 d49f939

Please sign in to comment.