From 4edb6e08663035f12c4a40e2543259230d346ee1 Mon Sep 17 00:00:00 2001 From: Akinori MUSHA Date: Thu, 16 Jun 2016 19:57:33 +0900 Subject: [PATCH 1/2] Add specs that `this.memory(key, falsy)` should work This spots a bug in `getMemory()`. --- spec/models/agents/java_script_agent_spec.rb | 24 ++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/spec/models/agents/java_script_agent_spec.rb b/spec/models/agents/java_script_agent_spec.rb index c39afae3..38c0dcb3 100644 --- a/spec/models/agents/java_script_agent_spec.rb +++ b/spec/models/agents/java_script_agent_spec.rb @@ -186,6 +186,30 @@ describe Agents::JavaScriptAgent do @agent.save! expect { @agent.reload.memory }.not_to raise_error end + + it "it stores null" do + @agent.options['code'] = 'Agent.check = function() { + this.memory("foo", "test"); + this.memory("foo", null); + };' + @agent.save! + @agent.check + expect(@agent.memory['foo']).to eq(nil) + @agent.save! + expect { @agent.reload.memory }.not_to raise_error + end + + it "it stores false" do + @agent.options['code'] = 'Agent.check = function() { + this.memory("foo", "test"); + this.memory("foo", false); + };' + @agent.save! + @agent.check + expect(@agent.memory['foo']).to eq(false) + @agent.save! + expect { @agent.reload.memory }.not_to raise_error + end end describe "deleteKey" do From 806722b4f8c47af6eb8b620f1f0f59643a8e50e7 Mon Sep 17 00:00:00 2001 From: Akinori MUSHA Date: Thu, 16 Jun 2016 20:11:15 +0900 Subject: [PATCH 2/2] Fix `this.memory(key, falsy)` --- app/models/agents/java_script_agent.rb | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/app/models/agents/java_script_agent.rb b/app/models/agents/java_script_agent.rb index 07fd2213..4a4812ca 100644 --- a/app/models/agents/java_script_agent.rb +++ b/app/models/agents/java_script_agent.rb @@ -114,12 +114,9 @@ module Agents 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 do |a, x, y| - if x && y - memory[x] = clean_nans(y) - else - memory.to_json - end + context["getMemory"] = lambda { |a| memory.to_json } + context["setMemory"] = lambda do |a, x, y| + memory[x] = clean_nans(y) end context["deleteKey"] = lambda { |a, x| memory.delete(x).to_json } context["escapeHtml"] = lambda { |a, x| CGI.escapeHTML(x) } @@ -168,7 +165,7 @@ module Agents Agent.memory = function(key, value) { if (typeof(key) !== "undefined" && typeof(value) !== "undefined") { - getMemory(key, value); + setMemory(key, value); } else if (typeof(key) !== "undefined") { return JSON.parse(getMemory())[key]; } else {