Java Code Examples for net.sourceforge.htmlunit.corejs.javascript.ScriptableObject#defineProperty()

The following examples show how to use net.sourceforge.htmlunit.corejs.javascript.ScriptableObject#defineProperty() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MSXMLJavaScriptEnvironment.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Configures constants, properties and functions on the object.
 * @param config the configuration for the object
 * @param scriptable the object to configure
 */
private static void configureConstantsPropertiesAndFunctions(final ClassConfiguration config,
        final ScriptableObject scriptable) {

    // the constants
    configureConstants(config, scriptable);

    // the properties
    final Map<String, PropertyInfo> propertyMap = config.getPropertyMap();
    for (final Map.Entry<String, PropertyInfo> entry : propertyMap.entrySet()) {
        final PropertyInfo info = entry.getValue();
        final Method readMethod = info.getReadMethod();
        final Method writeMethod = info.getWriteMethod();
        scriptable.defineProperty(entry.getKey(), null, readMethod, writeMethod, ScriptableObject.EMPTY);
    }

    final int attributes = ScriptableObject.DONTENUM;
    // the functions
    for (final Entry<String, Method> functionInfo : config.getFunctionEntries()) {
        final String functionName = functionInfo.getKey();
        final Method method = functionInfo.getValue();
        final FunctionObject functionObject = new FunctionObject(functionName, method, scriptable);
        scriptable.defineProperty(functionName, functionObject, attributes);
    }
}
 
Example 2
Source File: MSXMLJavaScriptEnvironment.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Configures constants, properties and functions on the object.
 * @param config the configuration for the object
 * @param scriptable the object to configure
 */
private static void configureConstantsPropertiesAndFunctions(final ClassConfiguration config,
        final ScriptableObject scriptable) {

    // the constants
    configureConstants(config, scriptable);

    // the properties
    final Map<String, PropertyInfo> propertyMap = config.getPropertyMap();
    for (final String propertyName : propertyMap.keySet()) {
        final PropertyInfo info = propertyMap.get(propertyName);
        final Method readMethod = info.getReadMethod();
        final Method writeMethod = info.getWriteMethod();
        scriptable.defineProperty(propertyName, null, readMethod, writeMethod, ScriptableObject.EMPTY);
    }

    final int attributes = ScriptableObject.DONTENUM;
    // the functions
    for (final Entry<String, Method> functionInfo : config.getFunctionEntries()) {
        final String functionName = functionInfo.getKey();
        final Method method = functionInfo.getValue();
        final FunctionObject functionObject = new FunctionObject(functionName, method, scriptable);
        scriptable.defineProperty(functionName, functionObject, attributes);
    }
}
 
Example 3
Source File: HTMLObjectElement.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
private void createAppletMethodAndProperties() throws Exception {
    final HtmlObject appletNode = (HtmlObject) getDomNodeOrDie();
    final Applet applet = appletNode.getApplet();
    if (applet == null) {
        return;
    }

    // Rhino should provide the possibility to declare delegate for Functions as it does for properties!!!
    for (final Method method : applet.getClass().getMethods()) {
        final Function f = new BaseFunction() {
            @Override
            public Object call(final Context cx, final Scriptable scope,
                    final Scriptable thisObj, final Object[] args) {

                final Object[] realArgs = new Object[method.getParameterTypes().length];
                for (int i = 0; i < realArgs.length; i++) {
                    final Object arg;
                    if (i > args.length) {
                        arg = null;
                    }
                    else {
                        arg = Context.jsToJava(args[i], method.getParameterTypes()[i]);
                    }
                    realArgs[i] = arg;
                }
                try {
                    return method.invoke(applet, realArgs);
                }
                catch (final Exception e) {
                    throw Context.throwAsScriptRuntimeEx(e);
                }
            }
        };
        ScriptableObject.defineProperty(this, method.getName(), f, ScriptableObject.READONLY);
    }
}
 
Example 4
Source File: HTMLAppletElement.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
private void createAppletMethodAndProperties() throws Exception {
    final HtmlApplet appletNode = (HtmlApplet) getDomNodeOrDie();
    final Applet applet = appletNode.getApplet();
    if (applet == null) {
        return;
    }

    // Rhino should provide the possibility to declare delegate for Functions as it does for properties!!!
    for (final Method method : applet.getClass().getMethods()) {
        final Function f = new BaseFunction() {
            @Override
            public Object call(final Context cx, final Scriptable scope,
                    final Scriptable thisObj, final Object[] args) {

                final Object[] realArgs = new Object[method.getParameterTypes().length];
                for (int i = 0; i < realArgs.length; i++) {
                    final Object arg;
                    if (i > args.length) {
                        arg = null;
                    }
                    else {
                        arg = Context.jsToJava(args[i], method.getParameterTypes()[i]);
                    }
                    realArgs[i] = arg;
                }
                try {
                    return method.invoke(applet, realArgs);
                }
                catch (final Exception e) {
                    throw Context.throwAsScriptRuntimeEx(e);
                }
            }
        };
        ScriptableObject.defineProperty(this, method.getName(), f, ScriptableObject.READONLY);
    }
}
 
Example 5
Source File: JavaScriptEngine.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
private static void configureFunctions(final ClassConfiguration config, final ScriptableObject scriptable) {
    final int attributes = ScriptableObject.EMPTY;
    // the functions
    for (final Entry<String, Method> functionInfo : config.getFunctionEntries()) {
        final String functionName = functionInfo.getKey();
        final Method method = functionInfo.getValue();
        final FunctionObject functionObject = new FunctionObject(functionName, method, scriptable);
        scriptable.defineProperty(functionName, functionObject, attributes);
    }
}
 
Example 6
Source File: JavaScriptEngine.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
private static void configureProperties(final ClassConfiguration config, final ScriptableObject scriptable) {
    final Map<String, PropertyInfo> propertyMap = config.getPropertyMap();
    for (final Entry<String, PropertyInfo> propertyEntry : propertyMap.entrySet()) {
        final PropertyInfo info = propertyEntry.getValue();
        final Method readMethod = info.getReadMethod();
        final Method writeMethod = info.getWriteMethod();
        scriptable.defineProperty(propertyEntry.getKey(), null, readMethod, writeMethod, ScriptableObject.EMPTY);
    }
}
 
Example 7
Source File: JavaScriptEngine.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
private static void configureStaticProperties(final ClassConfiguration config, final ScriptableObject scriptable) {
    for (final Entry<String, ClassConfiguration.PropertyInfo> propertyEntry
            : config.getStaticPropertyEntries()) {
        final String propertyName = propertyEntry.getKey();
        final Method readMethod = propertyEntry.getValue().getReadMethod();
        final Method writeMethod = propertyEntry.getValue().getWriteMethod();
        final int flag = ScriptableObject.EMPTY;

        scriptable.defineProperty(propertyName, null, readMethod, writeMethod, flag);
    }
}
 
Example 8
Source File: JavaScriptEngine.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
private static void configureStaticFunctions(final ClassConfiguration config,
        final ScriptableObject scriptable) {
    for (final Entry<String, Method> staticfunctionInfo : config.getStaticFunctionEntries()) {
        final String functionName = staticfunctionInfo.getKey();
        final Method method = staticfunctionInfo.getValue();
        final FunctionObject staticFunctionObject = new FunctionObject(functionName, method,
                scriptable);
        scriptable.defineProperty(functionName, staticFunctionObject, ScriptableObject.EMPTY);
    }
}
 
Example 9
Source File: MSXMLJavaScriptEnvironment.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
private static void configureConstants(final ClassConfiguration config,
        final ScriptableObject scriptable) {
    for (final ConstantInfo constantInfo : config.getConstants()) {
        scriptable.defineProperty(constantInfo.getName(), constantInfo.getValue(),
                ScriptableObject.READONLY | ScriptableObject.PERMANENT);
    }
}
 
Example 10
Source File: JavaScriptEngine.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
private static void configureFunctions(final ClassConfiguration config, final ScriptableObject scriptable) {
    final int attributes = ScriptableObject.EMPTY;
    // the functions
    for (final Entry<String, Method> functionInfo : config.getFunctionEntries()) {
        final String functionName = functionInfo.getKey();
        final Method method = functionInfo.getValue();
        final FunctionObject functionObject = new FunctionObject(functionName, method, scriptable);
        scriptable.defineProperty(functionName, functionObject, attributes);
    }
}
 
Example 11
Source File: JavaScriptEngine.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
private static void configureProperties(final ClassConfiguration config, final ScriptableObject scriptable) {
    final Map<String, PropertyInfo> propertyMap = config.getPropertyMap();
    for (final String propertyName : propertyMap.keySet()) {
        final PropertyInfo info = propertyMap.get(propertyName);
        final Method readMethod = info.getReadMethod();
        final Method writeMethod = info.getWriteMethod();
        scriptable.defineProperty(propertyName, null, readMethod, writeMethod, ScriptableObject.EMPTY);
    }
}
 
Example 12
Source File: JavaScriptEngine.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
private static void configureStaticProperties(final ClassConfiguration config, final ScriptableObject scriptable) {
    for (final Entry<String, PropertyInfo> propertyEntry
            : config.getStaticPropertyEntries()) {
        final String propertyName = propertyEntry.getKey();
        final Method readMethod = propertyEntry.getValue().getReadMethod();
        final Method writeMethod = propertyEntry.getValue().getWriteMethod();
        final int flag = ScriptableObject.EMPTY;

        scriptable.defineProperty(propertyName, null, readMethod, writeMethod, flag);
    }
}
 
Example 13
Source File: JavaScriptEngine.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
private static void configureStaticFunctions(final ClassConfiguration config,
        final ScriptableObject scriptable) {
    for (final Entry<String, Method> staticfunctionInfo : config.getStaticFunctionEntries()) {
        final String functionName = staticfunctionInfo.getKey();
        final Method method = staticfunctionInfo.getValue();
        final FunctionObject staticFunctionObject = new FunctionObject(functionName, method,
                scriptable);
        scriptable.defineProperty(functionName, staticFunctionObject, ScriptableObject.EMPTY);
    }
}
 
Example 14
Source File: MSXMLJavaScriptEnvironment.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
private static void configureConstants(final ClassConfiguration config,
        final ScriptableObject scriptable) {
    for (final ConstantInfo constantInfo : config.getConstants()) {
        scriptable.defineProperty(constantInfo.getName(), constantInfo.getValue(),
                ScriptableObject.READONLY | ScriptableObject.PERMANENT);
    }
}
 
Example 15
Source File: JavaScriptEngine.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
private static void configureConstants(final ClassConfiguration config, final ScriptableObject scriptable) {
    for (final ConstantInfo constantInfo : config.getConstants()) {
        scriptable.defineProperty(constantInfo.getName(), constantInfo.getValue(), constantInfo.getFlag());
    }
}
 
Example 16
Source File: JavaScriptEngine.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
private static void configureConstants(final ClassConfiguration config, final ScriptableObject scriptable) {
    for (final ConstantInfo constantInfo : config.getConstants()) {
        scriptable.defineProperty(constantInfo.getName(), constantInfo.getValue(), constantInfo.getFlag());
    }
}