简体   繁体   English

gwt jsni变量名混淆

[英]gwt jsni variable name obfuscated

I am having trouble trying to use javascript eval() with gwt. 我在尝试将gwt使用javascript eval()遇到麻烦。

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. myVar应该是date类型的javascript变量。

This variable is passed via GWT JSNI: 该变量通过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. 但是找不到脚本字符串中的“ myVar”变量。 I found this: https://support.google.com/code/answer/55205?hl=en 我发现了这一点: https : //support.google.com/code/answer/55205?hl=zh_CN

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? 这不可能吗?

Use a function instead: 改用一个函数:

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

That's still as bad as eval security-wise but much cleaner anyway. 这仍然和eval安全性一样糟糕,但是无论如何eval干净得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM