use of org.mozilla.javascript.NativeFunction in project servoy-client by Servoy.
the class ScriptEngine method compileScriptProvider.
/**
* @param sp
* @param scope
* @param cx
* @return
*/
@SuppressWarnings("nls")
protected Function compileScriptProvider(IScriptProvider sp, Scriptable scope, Context cx, String sourceName) {
String declaration = sp.getDeclaration();
// dont return the calc function itself but still the value.
if (sp instanceof ScriptCalculation) {
declaration = extractFunction(declaration, "function $1_");
} else {
declaration = extractFunction(declaration, "function $1");
}
// f below always seems to be NativeFunction instance as both Codegen.createFunctionObject and Interpreter.createFunctionObject return
// a NativeFunction instance; and that is what cx.compileFunction(...) ends up calling
Function f = cx.compileFunction(scope, declaration, sourceName, sp.getLineNumberOffset(), null);
if (!(sp instanceof ScriptCalculation)) {
if (sp.getScopeName() != null) {
// $NON-NLS-1$
f.put("_scopename_", f, sp.getScopeName());
}
// $NON-NLS-1$
f.put("_methodname_", f, sp.getDataProviderID());
f.put("_AllowToRunInFind_", f, Boolean.valueOf(// $NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
sp.getDeclaration().indexOf("@AllowToRunInFind") != -1 || declaration.indexOf(".search") != -1 || declaration.indexOf("controller.loadAllRecords") != -1));
}
String methodName = sp.getName();
PerformanceData performanceData = application instanceof IPerformanceDataProvider ? ((IPerformanceDataProvider) application).getPerformanceData() : null;
if (performanceData != null) {
String scopeName = scope.getClassName();
if (scope instanceof LazyCompilationScope) {
scopeName = ((LazyCompilationScope) scope).getScopeName();
scopeName = lookForSuperCalls(sp, scopeName);
} else if (scope.getParentScope() instanceof LazyCompilationScope) {
scopeName = ((LazyCompilationScope) scope.getParentScope()).getScopeName();
scopeName = lookForSuperCalls(sp, scopeName);
} else if (scope instanceof FoundSet) {
Scriptable parentScope = ((FoundSet) scope).getPrototype();
if (parentScope instanceof LazyCompilationScope) {
scopeName = ((LazyCompilationScope) parentScope).getScopeName();
}
}
methodName = scopeName + "." + methodName;
return new FunctionWrapper(f, methodName, performanceData, application.getClientID());
}
return f;
}
use of org.mozilla.javascript.NativeFunction in project servoy-client by Servoy.
the class ScriptEngine method eval.
public Object eval(Scriptable scope, String eval_string) {
String userUidBefore = null;
if (Context.getCurrentContext() == null) {
userUidBefore = application.getClientInfo().getUserUid();
}
Context cx = Context.enter();
try {
Object o = null;
// $NON-NLS-1$ //$NON-NLS-2$
Function compileFunction = cx.compileFunction(scope, "function evalFunction(){}", "evalFunction", 0, null);
if (compileFunction instanceof FunctionWrapper)
compileFunction = ((FunctionWrapper) compileFunction).getWrappedFunction();
if (compileFunction instanceof NativeFunction) {
o = // $NON-NLS-1$
cx.evaluateString(// $NON-NLS-1$
ScriptRuntime.createFunctionActivation((NativeFunction) compileFunction, scope, null), // $NON-NLS-1$
eval_string, // $NON-NLS-1$
"internal_anon", // $NON-NLS-1$
1, null);
} else {
// $NON-NLS-1$
o = cx.evaluateString(scope, eval_string, "internal_anon", 1, null);
}
if (o instanceof Wrapper) {
o = ((Wrapper) o).unwrap();
}
if (o == Scriptable.NOT_FOUND || o == Undefined.instance) {
o = null;
}
return o;
} catch (Exception ex) {
if (ex instanceof ExitScriptException) {
throw (ExitScriptException) ex;
} else if (ex.getCause() instanceof ExitScriptException) {
throw (ExitScriptException) ex.getCause();
} else if (ex instanceof JavaScriptException && ((JavaScriptException) ex).getValue() instanceof ExitScriptException) {
throw (ExitScriptException) ((JavaScriptException) ex).getValue();
} else if (application.getSolution() != null) {
// $NON-NLS-1$//$NON-NLS-2$
application.reportError(application.getI18NMessage("servoy.formPanel.error.evalString") + eval_string + "'", ex);
} else {
// $NON-NLS-1$
Debug.trace("Solution already closed, ignoring exception", ex);
}
} finally {
Context.exit();
testClientUidChange(scope, userUidBefore);
}
return null;
}
Aggregations