Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix this.memory(key, falsy) not working in JavaScriptAgent #1551

Merged
merged 2 commits into from
Jun 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if how much more efficient it would be, but would it make sense to keep getMemory with one argument to not always pass the whole memory through the javascript bridge?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree, but there's currently no one-argument use for the function and this is meant to be a minimal fix. We can make further improvements later.

Copy link
Member Author

@knu knu Jun 17, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As handling of variable length arguments is awkward [*] in V8 (and JavaScript itself), I'd rather have a separate functions for getting the whole memory and an individual key value.

[*] Ruby's lambda has a stricter argument length check compared to proc, but V8::Context seems to adjust arguments on the caller side, which gives me a confused feeling.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, that's not really the scope of this PR.

I was wondering how that even works, so V8 has to check the arity of the lambda and fill the missing arguments with nil?

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