This is on JDK 8u102. I'm seeing strange behavior where certain certain properties on objects get set to zero when optimistic types are set to true. Here's a jjs
script that demonstrates the behavior:
var config = {};
var creativeDetails = {
fanpage_id: config.fanpage_id || "293867224112667",
image_url: config.image_url || "http://some.url",
title: config.title || "Ad Title",
description: config.description || "Ad Description",
link_description: config.link_description || "Link Description",
call_to_action: config.call_to_action || "LEARN_MORE",
destination_url: "http://some.url",
link_body: config.link_body || "Link Body"
};
var linkData = {
call_to_action: {
type: creativeDetails.call_to_action,
value:{
link_caption: creativeDetails.link_description,
link: creativeDetails.destination_url
}
},
link: creativeDetails.destination_url,
picture: creativeDetails.image_url,
message: creativeDetails.description,
name: creativeDetails.title
};
print(JSON.stringify(linkData, null, 4) + "\n");
Running this with jjs
without any arguments gives:
{
"call_to_action": {
"type": "LEARN_MORE",
"value": {
"link_caption": "Link Description",
"link": "http://some.url"
}
},
"link": "http://some.url",
"picture": "http://some.url",
"message": "Ad Description",
"name": "Ad Title"
}
Whereas running it with jjs --optimistic-types=true
gives:
{
"call_to_action": {
"type": "LEARN_MORE",
"value": {
"link_caption": "Link Description",
"link": "http://some.url"
}
},
"link": 0,
"picture": 0,
"message": 0,
"name": 0
}
Changing the ||
expressions to ternaries didn't change the behavior. However, referencing a property of the creativeDetails
object in any manner (e.g., assigning it to another variable or printing it out) before initializing the linkData
object, ends up printing out the expected result.
The strange thing is that the nested object gets the right values, but the outer object does not. This behavior only shows up if you are using the problematic object's properties to initialize the properties of a nested object. For example, the following code does not cause the issue:
var linkData = {
link: creativeDetails.destination_url,
picture: creativeDetails.image_url,
message: creativeDetails.description,
name: creativeDetails.title
};
I know that Nashorn's optimistic types work by assuming the type of something is an int
and then progressively downgrades to Object
. So it might be something related to that, but I am not sure how or why this is happening. Has anyone seen this behavior?
This was a bug in Nashorn and has been resolved.