context.setOptimizationLevel(OPTIMIZATION_LEVEL);
context.setGeneratingDebug(true);
context.setCompileFunctionsWithDynamicScope(true);
context.setErrorReporter(errorReporter);
LocationTrackingDebugger locationTracker = new LocationTrackingDebugger();
if (!enableDebugger) {
//FIXME: add a "tee" debugger that allows both to be used simultaneously
context.setDebugger(locationTracker, null);
}
ThreadScope thrScope = getSessionScope();
synchronized (thrScope) {
ClassLoader savedClassLoader =
Thread.currentThread().getContextClassLoader();
FOM_Cocoon cocoon = null;
try {
try {
setupContext(redirector, context, thrScope);
cocoon = (FOM_Cocoon) thrScope.get("cocoon", thrScope);
// Register the current scope for scripts indirectly called from this function
FOM_JavaScriptFlowHelper.setFOM_FlowScope(cocoon.getObjectModel(), thrScope);
if (enableDebugger) {
if (!getDebugger().isVisible()) {
// only raise the debugger window if it isn't already visible
getDebugger().setVisible(true);
}
}
int size = (params != null ? params.size() : 0);
Object[] funArgs = new Object[size];
Scriptable parameters = context.newObject(thrScope);
for (int i = 0; i < size; i++) {
Interpreter.Argument arg = (Interpreter.Argument)params.get(i);
funArgs[i] = arg.value;
if (arg.name == null) {
arg.name = "";
}
parameters.put(arg.name, parameters, arg.value);
}
cocoon.setParameters(parameters);
Object fun = ScriptableObject.getProperty(thrScope, funName);
if (fun == Scriptable.NOT_FOUND) {
throw new ResourceNotFoundException("Function \"javascript:" + funName + "()\" not found");
}
// Check count of arguments
if (fun instanceof BaseFunction) {
if (((BaseFunction)fun).getArity() != 0) {
getLogger().error("Function '" + funName + "' must have no declared arguments! " +
"Use cocoon.parameters to reach parameters passed from the sitemap into the function.");
}
}
thrScope.setLock(true);
ScriptRuntime.call(context, fun, thrScope, funArgs, thrScope);
} catch (JavaScriptException ex) {
throw locationTracker.getException("Error calling flowscript function " + funName, ex);
// EvaluatorException ee = Context.reportRuntimeError(
// ToolErrorReporter.getMessage("msg.uncaughtJSException",
// ex.getMessage()));
// Throwable unwrapped = unwrap(ex);
// if (unwrapped instanceof ProcessingException) {
// throw (ProcessingException) unwrapped;
// }
// throw new CascadingRuntimeException(ee.getMessage(),
// unwrapped);
} catch (EcmaError ee) {
throw locationTracker.getException("Error calling flowscript function " + funName, ee);
// String msg = ToolErrorReporter.getMessage("msg.uncaughtJSException", ee.toString());
// if (ee.getSourceName() != null) {
// Context.reportRuntimeError(msg, ee.getSourceName(),
// ee.getLineNumber(), ee.getLineSource(),
// ee.getColumnNumber());