I'm creating a GWT version of a Java library which has support for the javax.script.ScriptEngine to evaluate functions dynamically via Javascript, e.g.,
o => o % 2 == 0
where at runtime, the value of "o" is defined via the javax.script.Bindings (the o =>
part is stripped of course).
The problem is, how can I get the same effect from within GWT? I use a native function
native Object nativ开发者_StackOverflow中文版eEval(String script) /*-{
return $wnd.eval(script);
}-*/
nativeEval("o % 2 == 0");
But how can I bind a value to the identifier "o"?
new Function("o", "return (" + expressionThatUsesO + ")")(o)
If expressionThatUsesO
is "o % 2"
then this is equivalent to a global function that is immediately called
(function (o) { return o % 2; })(o)
For reference, https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function :
new Function ([arg1[, arg2[, ... argN]],] functionBody)
Parameters
arg1, arg2, ... argN
Names to be used by the function as formal argument names. Each must be a string that corresponds to a valid JavaScript identifier or a list of such strings separated with a comma; for example
"x"
,"theValue"
, or"a,b"
.
functionBody
A string containing the JavaScript statements comprising the function definition.
To pass values to the identifier "o" :
public native void define(String handle, Object o) /*-{
eval("var " + handle+ "="+ o);
}-*/;
public native boolean nativeEval(String script) /*-{
return eval(script);
}-*/;
And then calling it :
String script = "o % 2 == 0";
define("o",2);
Window.alert(nativeEval(script)+"");
define("o",3);
Window.alert(nativeEval(script)+"");
I guess I found a solution:
import javax.script.bindings.Bindings;
import javax.script.bindings.SimpleBindings;
int bindSequence;
native void prepareOnWindow(int index) /*-{
$wnd["mylib_bindings_" + index] = new Array();
}-*/;
native void setOnWindow(int index, String name, Object value) /*-{
$wnd["mylib_bindings_" + index][name] = value;
}-*/;
native void clearOnWindow(int index) /*-{
$wnd["mylib_bindings_" + index] = null;
}-*/;
native Object invoke(String script) /*-{
var result = $wnd.eval(script);
if (typeof(result) == "boolean") {
return result ? @java.lang.Boolean::TRUE : @java.lang.Boolean::FALSE;
} else
if (typeof(result) == "number") {
return @java.lang.Double::valueOf(D)(result);
}
return result;
}-*/;
public Object invoke(String script, Bindings bindings) {
int seq = bindSequence++;
try {
StringBuilder script2 = new StringBuilder();
prepareOnWindow(seq);
for (Map.Entry<String, Object> e : bindings.entrySet()) {
setOnWindow(seq, e.getKey(), e.getValue());
script2.append("var ").append(e.getKey()).append(" = ")
.append("window[\"mylib_bindings_\" + ").append(seq)
.append("][\"").append(e.getKey()).append("\"];\r\n");
}
script2.append("\r\n").append(script);
return invoke(script);
} finally {
clearOnWindow(seq);
}
}
void testing() {
Bindings b = new SimpleBindings();
b.put("o", 1);
Window.alert(invoke("o", b).toString());
b.put("o", "Hello world");
Window.alert(invoke("o", b).toString());
b.put("o", 2);
Window.alert(invoke("o % 2 == 0", b).toString());
}
The idea is to set the name-value pairs on a commonly accessible object, such as window and alter the script to get the variables from it. In order to allow a re-entrant capable call, the bindings are stored under a constantly increasing sequence number.
精彩评论