use of org.mozilla.javascript.Function in project servoy-client by Servoy.
the class ScriptVariableScope method put.
/*
* @see Scriptable#put(String, Scriptable, Object)
*/
@Override
public void put(String name, Scriptable arg1, Object value) {
if (value instanceof Function) {
super.put(name, arg1, value);
} else {
try {
Context currentContext = Context.getCurrentContext();
if (currentContext != null) {
Debugger debugger = currentContext.getDebugger();
if (debugger != null) {
if (debugger instanceof IDebuggerWithWatchPoints) {
IDebuggerWithWatchPoints wp = (IDebuggerWithWatchPoints) debugger;
wp.modification(name, this);
}
}
}
put(name, value);
} catch (RuntimeException re) {
throw new WrappedException(re);
}
}
}
use of org.mozilla.javascript.Function in project servoy-client by Servoy.
the class FunctionDefinition method exists.
/**
* Test if the given methodName or formName do exist. Will return one of the {@link Exist} enums.
* @since 5.2
*/
public Exist exists(IClientPluginAccess access) {
final Exist[] retVal = new Exist[] { Exist.METHOD_NOT_FOUND };
if (access instanceof ClientPluginAccessProvider) {
final IApplication application = ((ClientPluginAccessProvider) access).getApplication();
application.invokeAndWait(new Runnable() {
public void run() {
if (application.getSolution() != null) {
if (contextName.startsWith(ScriptVariable.SCOPES_DOT_PREFIX)) {
GlobalScope gs = application.getScriptEngine().getScopesScope().getGlobalScope(contextName.substring(ScriptVariable.SCOPES_DOT_PREFIX.length()));
if (gs != null && gs.get(methodName) instanceof Function) {
retVal[0] = Exist.METHOD_FOUND;
}
} else {
IFormController fp = application.getFormManager().leaseFormPanel(contextName);
if (fp == null) {
retVal[0] = Exist.FORM_NOT_FOUND;
} else if (fp.getFormScope().get(methodName, fp.getFormScope()) instanceof Function) {
retVal[0] = Exist.METHOD_FOUND;
}
}
} else {
retVal[0] = Exist.NO_SOLUTION;
}
}
});
}
return retVal[0];
}
use of org.mozilla.javascript.Function in project servoy-client by Servoy.
the class MapSerializer method convertToMap.
public static Map<String, Object> convertToMap(Object jsobj) {
Map<String, Object> retval = new HashMap<String, Object>();
if (jsobj == null || jsobj == Undefined.instance || jsobj instanceof IFoundSet || jsobj instanceof IRecord || !(jsobj instanceof NativeObject)) {
return retval;
}
IdScriptableObject no = (IdScriptableObject) jsobj;
Object[] noIDs = no.getIds();
String propertyKey;
Object propertyValue;
for (Object element : noIDs) {
// id can be Integer or String
if (element instanceof Integer) {
propertyKey = ((Integer) element).toString();
propertyValue = no.get(((Integer) element).intValue(), no);
} else if (element instanceof String) {
propertyKey = (String) element;
propertyValue = no.get((String) element, no);
} else {
// should not happen
continue;
}
if (// allow but ignore functions nested in objects
propertyValue instanceof Function) {
continue;
}
if (propertyValue instanceof NativeObject) {
propertyValue = convertToMap(propertyValue);
}
if (propertyValue instanceof Wrapper) {
propertyValue = ((Wrapper) propertyValue).unwrap();
}
retval.put(propertyKey, propertyValue);
}
return retval;
}
use of org.mozilla.javascript.Function in project servoy-client by Servoy.
the class RuntimeWebComponent method put.
@Override
public void put(String name, Scriptable start, Object value) {
if (isInvalidValue(name, value))
return;
List<Pair<String, String>> oldVisibleForms = getVisibleForms();
if (specProperties != null && specProperties.contains(name)) {
Object previousVal = null;
PropertyDescription pd = webComponentSpec.getProperties().get(name);
if (pd.getType() instanceof ISabloComponentToRhino && !(pd.getType() instanceof IRhinoToSabloComponent)) {
// the it has sablo to rhino conversion but not the other way around then we should just use the
// value from the conversion so call get(String,Scriptable)
previousVal = get(name, start);
} else
previousVal = component.getProperty(name);
Object val = NGConversions.INSTANCE.convertRhinoToSabloComponentValue(value, previousVal, pd, component);
if (val != previousVal)
component.setProperty(name, val);
if (pd != null && pd.getType() instanceof VisiblePropertyType) {
// search all labelfor elements
for (WebComponent siblingComponent : component.getParent().getComponents()) {
Collection<PropertyDescription> labelFors = siblingComponent.getSpecification().getProperties(LabelForPropertyType.INSTANCE);
if (labelFors != null) {
for (PropertyDescription labelForProperty : labelFors) {
if (Utils.equalObjects(component.getName(), siblingComponent.getProperty(labelForProperty.getName()))) {
// sibling component is labelfor, so set value to all its visible properties
Collection<PropertyDescription> visibleProperties = siblingComponent.getSpecification().getProperties(VisiblePropertyType.INSTANCE);
if (visibleProperties != null) {
for (PropertyDescription visibleProperty : visibleProperties) {
previousVal = siblingComponent.getProperty(visibleProperty.getName());
val = NGConversions.INSTANCE.convertRhinoToSabloComponentValue(value, previousVal, visibleProperty, siblingComponent);
if (val != previousVal)
siblingComponent.setProperty(name, val);
}
}
break;
}
}
}
}
}
} else if (prototypeScope != null) {
if (!apiFunctions.containsKey(name)) {
// check if we have a setter for this property
if (name != null && name.length() > 0) {
String uName = new StringBuffer(name.substring(0, 1).toUpperCase()).append(name.substring(1)).toString();
if (apiFunctions.containsKey("set" + uName) && apiFunctions.containsKey("get" + uName)) {
// call setter
Function propertySetter = apiFunctions.get("set" + uName);
propertySetter.call(Context.getCurrentContext(), start, start, new Object[] { value });
} else {
prototypeScope.put(name, start, value);
}
}
}
}
updateVisibleContainers(oldVisibleForms);
}
use of org.mozilla.javascript.Function in project servoy-client by Servoy.
the class RuntimeWebComponent method get.
@Override
public Object get(final String name, final Scriptable start) {
if (specProperties != null && specProperties.contains(name)) {
PropertyDescription pd = webComponentSpec.getProperties().get(name);
if (WebFormComponent.isDesignOnlyProperty(pd))
return Scriptable.NOT_FOUND;
return NGConversions.INSTANCE.convertSabloComponentToRhinoValue(component.getProperty(name), pd, component, start);
}
if ("getFormName".equals(name)) {
return new Callable() {
@Override
public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
IWebFormUI parent = component.findParent(IWebFormUI.class);
if (parent != null) {
return parent.getController().getName();
}
return null;
}
};
}
if ("getDesignTimeProperty".equals(name) && component.getFormElement().getPersistIfAvailable() instanceof AbstractBase) {
return new Callable() {
@Override
public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
return Utils.parseJSExpression(((AbstractBase) component.getFormElement().getPersistIfAvailable()).getCustomDesignTimeProperty((String) args[0]));
}
};
}
if ("getDesignProperties".equals(name) && component.getFormElement().getPersistIfAvailable() instanceof AbstractBase) {
return new Callable() {
@Override
public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
Map<String, Object> designProperties = ((AbstractBase) component.getFormElement().getPersistIfAvailable()).getMergedCustomDesignTimeProperties();
Map<String, Object> parsedMap = new HashMap<String, Object>();
designProperties.entrySet().forEach(entry -> {
parsedMap.put(entry.getKey(), Utils.parseJSExpression(entry.getValue()));
});
return parsedMap;
}
};
}
final Function func = apiFunctions.get(name);
if (func != null && isApiFunctionEnabled(name)) {
final List<Pair<String, String>> oldVisibleForms = getVisibleForms();
return new BaseFunction() {
@Override
public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
Object retValue;
cx.putThreadLocal(SERVER_SIDE_SCRIPT_EXECUTE, Boolean.TRUE);
try {
retValue = func.call(cx, scope, thisObj, args);
} finally {
cx.removeThreadLocal(SERVER_SIDE_SCRIPT_EXECUTE);
}
if (!(func instanceof WebComponentFunction)) {
WebObjectFunctionDefinition def = webComponentSpec.getApiFunctions().get(name);
retValue = NGConversions.INSTANCE.convertServerSideRhinoToRhinoValue(retValue, def.getReturnType(), component, null);
}
updateVisibleContainers(oldVisibleForms);
return retValue;
}
};
}
// check if we have a setter/getter for this property
if (name != null && name.length() > 0) {
String uName = new StringBuffer(name.substring(0, 1).toUpperCase()).append(name.substring(1)).toString();
if (apiFunctions.containsKey("set" + uName) && apiFunctions.containsKey("get" + uName)) {
// call getter
Function propertyGetter = apiFunctions.get("get" + uName);
return propertyGetter.call(Context.getCurrentContext(), start, start, new Object[] {});
}
}
if ("svyMarkupId".equals(name)) {
String formName = null;
IWebFormUI parent = component.findParent(IWebFormUI.class);
if (parent != null) {
formName = parent.getController().getName();
} else {
formName = component.getFormElement().getForm().getName();
}
return ComponentFactory.getMarkupId(formName, component.getName());
}
// is this really needed? will not prototype be looked at automatically by Rhino code?
if (prototypeScope != null) {
return prototypeScope.get(name, start);
}
return Scriptable.NOT_FOUND;
}
Aggregations