Edu
Edu

Reputation: 160

gwt jsni variable name obfuscated

I am having trouble trying to use javascript eval() with gwt.

Basically, I have in my DB a dynamic string, for example:

"'31.07.'  + (myVar.getMonth() <= 7 ? myVar.getFullYear() + 3: myVar.getFullYear() + 4 )"

myVar is supposed to be a javascript variable of type date.

This variable is passed via GWT JSNI:

private native String eval(Date dateFieldValue, String scriptlet) /*-{
  var myVar = dateFieldValue;
  return $wnd.eval(scriptlet);
}-*/; 

But the "myVar" variable in the scriptlet string is not being found. I found this: https://support.google.com/code/answer/55205?hl=en

Which explains why this happens. I would have to separate my scriptlet in such a way:

"'31.07.'  + (" + myVar + ".getMonth() <= 7 ? " + myVar + ".getFullYear() + 3: " + myVar + ".getFullYear() + 4 )"

The problem is that this would not be flexible, since the scriptlet is administered in the database, not in the code. So what to do in this case? Is this impossible?

Upvotes: 0

Views: 227

Answers (1)

Thomas Broyer
Thomas Broyer

Reputation: 64551

Use a function instead:

return (new Function('myVar', 'return ' + scriptlet))(dateFieldValue);

That's still as bad as eval security-wise but much cleaner anyway.

Upvotes: 1

Related Questions