Subversion Repositories pub

Compare Revisions

Ignore whitespace Rev 67 → Rev 68

/pluggablejs/trunk/src/net/outlyer/plugins/Internal.java
26,7 → 26,7
 
// $Id$
 
import java.util.Map;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
 
67,14 → 67,18
private static String mapName = "$_";
static String getInternalInitialisation() {
// Commented-out is the the closer-to-Java version
return String.format("= {\n" +
" %2$s: new java.util.HashMap(),\n" +
// " %2$s: new java.util.HashMap(),\n" +
" %2$s: {},\n" +
" set: function(n,v) {\n"+
" %1$s.put(n,v);\n" +
// " %1$s.put(n,v);\n" +
" %1$s[n] = v;\n" +
" return v;\n" +
" },\n" +
" get: function(n) {\n" +
" return %1$s.get(n);\n" +
// " return %1$s.get(n);\n" +
" return %1$s[n];\n" +
" }\n"+
"};",
getInternalObjectFieldName(mapName),
81,16 → 85,38
mapName);
}
 
static void setInternalValue(final ScriptEngine engine,
static void setInternalValue(final ScriptEngine e,
final String name,
final Object value) throws ScriptException {
final Map map = (Map) engine.eval(getInternalObjectFieldName(mapName));
map.put(name, value);
assert null != e;
assert null != name;
// final Map map = (Map) e.eval(getInternalObjectFieldName(mapName));
// map.put(name, value);
try {
assert e instanceof Invocable;
final Invocable ie = (Invocable) e;
ie.invokeMethod(e.eval(getInternalObjectName()), "set", name, value);
}
catch (final NoSuchMethodException x) {
throw new ScriptException("Internal error while storing " +
"internal variable: "+x.getMessage());
}
}
 
static Object getInternalValue(final ScriptEngine e,
final String name) throws ScriptException {
final Map map = (Map) e.eval(getInternalObjectFieldName(mapName));
return map.get(name);
assert null != e;
assert null != name;
// final Map map = (Map) e.eval(getInternalObjectFieldName(mapName));
// return map.get(name);
try {
assert e instanceof Invocable;
final Invocable ie = (Invocable) e;
return ie.invokeMethod(e.eval(getInternalObjectName()), name);
}
catch (final NoSuchMethodException x) {
throw new ScriptException("Internal error while accessing " +
"internal variable: "+x.getMessage());
}
}
}