diff --git a/app/models/agents/java_script_agent.rb b/app/models/agents/java_script_agent.rb index cfce1843..63fc2896 100644 --- a/app/models/agents/java_script_agent.rb +++ b/app/models/agents/java_script_agent.rb @@ -220,9 +220,9 @@ module Agents end def clean_nans(input) - if input.is_a?(Array) + if input.is_a?(V8::Array) input.map {|v| clean_nans(v) } - elsif input.is_a?(Hash) + elsif input.is_a?(V8::Object) input.inject({}) { |m, (k, v)| m[k] = clean_nans(v); m } elsif input.is_a?(Float) && input.nan? 'NaN' diff --git a/spec/models/agents/java_script_agent_spec.rb b/spec/models/agents/java_script_agent_spec.rb index dbf74a2b..afc3305b 100644 --- a/spec/models/agents/java_script_agent_spec.rb +++ b/spec/models/agents/java_script_agent_spec.rb @@ -132,7 +132,9 @@ describe Agents::JavaScriptAgent do expect(AgentLog.last.message).to match(/oh no/) expect(AgentLog.last.level).to eq(4) end + end + describe "getMemory" do it "won't store NaNs" do @agent.options['code'] = 'Agent.check = function() { this.memory("foo", NaN); };' @agent.save! @@ -141,6 +143,49 @@ describe Agents::JavaScriptAgent do @agent.save! expect { @agent.reload.memory }.not_to raise_error end + + it "it stores an Array" do + @agent.options['code'] = 'Agent.check = function() { + var arr = [1,2]; + this.memory("foo", arr); + };' + @agent.save! + @agent.check + expect(@agent.memory['foo']).to eq([1,2]) + @agent.save! + expect { @agent.reload.memory }.not_to raise_error + end + + it "it stores a Hash" do + @agent.options['code'] = 'Agent.check = function() { + var obj = {}; + obj["one"] = 1; + obj["two"] = [1,2]; + this.memory("foo", obj); + };' + @agent.save! + @agent.check + expect(@agent.memory['foo']).to eq({"one"=>1, "two"=> [1,2]}) + @agent.save! + expect { @agent.reload.memory }.not_to raise_error + end + + it "it stores a nested Hash" do + @agent.options['code'] = 'Agent.check = function() { + var u = {}; + u["one"] = 1; + u["two"] = 2; + var obj = {}; + obj["three"] = 3; + obj["four"] = u; + this.memory("foo", obj); + };' + @agent.save! + @agent.check + expect(@agent.memory['foo']).to eq({"three"=>3, "four"=>{"one"=>1, "two"=>2}}) + @agent.save! + expect { @agent.reload.memory }.not_to raise_error + end end describe "creating events" do