use of org.mozilla.javascript.NativeJavaObject in project gdmatrix by gdmatrix.
the class ScriptClient method executeScript.
public Object executeScript(String scriptName, Scriptable scope) throws Exception {
LOGGER.log(Level.INFO, "Executing {0} script.", new Object[] { scriptName });
Script script;
long now = System.currentTimeMillis();
if (lastCacheRefresh + REFRESH_TIME < now)
clearCache(now);
script = getScript(scriptName);
Object result = null;
try {
result = script.exec(context, scope);
if (result instanceof NativeJavaObject) {
NativeJavaObject nat = (NativeJavaObject) result;
result = nat.unwrap();
}
if (result instanceof Undefined)
result = null;
} catch (JavaScriptException ex) {
throw new Exception(ex.getMessage());
} finally {
Context.exit();
}
return result;
}
use of org.mozilla.javascript.NativeJavaObject in project knetbuilder by Rothamsted.
the class JSInterpreter method process.
public String process(String command, OutputPrinter out) {
Object result = null;
if (pc != null)
pc.processingStarted();
cx.getFactory().enterContext();
try {
result = cx.evaluateString(scope, command, "<cmd>", 1, null);
} catch (Exception e) {
// e.printStackTrace();
if (e instanceof WrappedException && ((WrappedException) e).getWrappedException() instanceof FunctionException) {
e.printStackTrace();
out.printAndPrompt(((WrappedException) e).getWrappedException().getMessage());
return null;
} else if (e instanceof EcmaError) {
e.printStackTrace();
out.printAndPrompt(e.getMessage().substring(0, e.getMessage().length() - 10));
return null;
} else if (e instanceof EvaluatorException) {
e.printStackTrace();
// out.feedOutput("Unknow command or syntax error.");
out.printAndPrompt(e.getMessage());
return null;
}
out.printAndPrompt(e.getMessage());
Context.exit();
return null;
} finally {
try {
if (pc != null)
pc.processingFinished();
} catch (Exception y) {
y.printStackTrace();
}
}
try {
if (result == null) {
out.printAndPrompt("null");
} else if (result.equals(Context.getUndefinedValue())) {
out.printAndPrompt("Undefined");
} else {
Class<?> wrp = clsToWrapper.get(result.getClass());
if (wrp != null)
out.printAndPrompt("[Object " + wrp.getSimpleName() + "]");
else if (result instanceof NativeJavaObject) {
Object d = ((NativeJavaObject) result).unwrap();
if (Number.class.isAssignableFrom(d.getClass()) || d instanceof String) {
out.printAndPrompt(Context.toString(result));
} else {
out.printAndPrompt("[Object " + d.getClass().getSimpleName() + "]");
}
} else {
out.printAndPrompt(Context.toString(result));
}
}
} catch (Exception e) {
out.printAndPrompt(e.getMessage());
e.printStackTrace();
}
try {
Context.exit();
} catch (Exception e) {
out.printAndPrompt(e.getMessage());
e.printStackTrace();
}
return null;
}
use of org.mozilla.javascript.NativeJavaObject in project veraPDF-library by veraPDF.
the class JavaScriptEvaluator method evalVariableResult.
public static synchronized java.lang.Object evalVariableResult(Variable variable, Object object, ScriptableObject scope) {
Script script;
if (!variableScripts.containsKey(variable.getName())) {
String source = getStringScript(object, variable.getValue());
script = JavaScriptEvaluator.compileString(source);
variableScripts.put(variable.getName(), script);
} else {
script = variableScripts.get(variable.getName());
}
scope.put("obj", scope, object);
java.lang.Object res = script.exec(context, scope);
if (res instanceof NativeJavaObject) {
res = ((NativeJavaObject) res).unwrap();
}
return res;
}
use of org.mozilla.javascript.NativeJavaObject in project veraPDF-library by veraPDF.
the class BaseValidator method initializeAllVariables.
private void initializeAllVariables() {
for (Variable var : this.profile.getVariables()) {
if (var == null)
continue;
java.lang.Object res = JavaScriptEvaluator.evaluateString(var.getDefaultValue(), this.scope);
if (res instanceof NativeJavaObject) {
res = ((NativeJavaObject) res).unwrap();
}
this.scope.put(var.getName(), this.scope, res);
}
}
use of org.mozilla.javascript.NativeJavaObject in project echosvg by css4j.
the class GlobalWrapper method startMouseCapture.
/**
* Wraps the 'startMouseCapture' method of the SVGGlobal interface.
*/
public static void startMouseCapture(Context cx, Scriptable thisObj, Object[] args, Function funObj) {
int len = args.length;
GlobalWrapper gw = (GlobalWrapper) thisObj;
SVGGlobal global = (SVGGlobal) gw.window;
if (len >= 3) {
EventTarget et = null;
if (args[0] instanceof NativeJavaObject) {
Object o = ((NativeJavaObject) args[0]).unwrap();
if (o instanceof EventTarget) {
et = (EventTarget) o;
}
}
if (et == null) {
throw Context.reportRuntimeError("First argument to startMouseCapture must be an EventTarget");
}
boolean sendAll = Context.toBoolean(args[1]);
boolean autoRelease = Context.toBoolean(args[2]);
global.startMouseCapture(et, sendAll, autoRelease);
}
}
Aggregations