Skip to content

Commit

Permalink
Merge pull request #1551 from cantino/jsagent_fix_memory
Browse files Browse the repository at this point in the history
Fix `this.memory(key, falsy)` not working in JavaScriptAgent
  • Loading branch information
knu committed Jun 20, 2016
2 parents 3b4b4f3 + 806722b commit 371bfa3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
11 changes: 4 additions & 7 deletions app/models/agents/java_script_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,9 @@ def execute_js(js_function, incoming_events = [])
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) }
Expand Down Expand Up @@ -168,7 +165,7 @@ def setup_javascript
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 {
Expand Down
24 changes: 24 additions & 0 deletions spec/models/agents/java_script_agent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,30 @@
@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
Expand Down

0 comments on commit 371bfa3

Please sign in to comment.