private void defineMethod(final String methodName, final Scriptable scope) { for (Method method : getClass().getMethods()) { if (method.getName().equals(methodName)) { final FunctionObject functionObject = new FunctionObject(methodName, method, scope); ((ScriptableObject) scope).defineProperty(methodName, functionObject, ScriptableObject.EMPTY); } } }
/** * @deprecated Use {@link #getTypeTag(Class)} * and {@link #convertArg(Context, Scriptable, Object, int)} * for type conversion. */ public static Object convertArg(Context cx, Scriptable scope, Object arg, Class<?> desired) { int tag = getTypeTag(desired); if (tag == JAVA_UNSUPPORTED_TYPE) { throw Context.reportRuntimeError1 ("msg.cant.convert", desired.getName()); } return convertArg(cx, scope, arg, tag); }
void initAsConstructor(Scriptable scope, Scriptable prototype) { ScriptRuntime.setFunctionProtoAndParent(this, scope); setImmunePrototypeProperty(prototype); prototype.setParentScope(this); defineProperty(prototype, "constructor", this, ScriptableObject.DONTENUM | ScriptableObject.PERMANENT | ScriptableObject.READONLY); setParentScope(scope); }
public static void finishInit(Scriptable scope, FunctionObject ctor, Scriptable proto) { Context cx = Context.getCurrentContext(); Scriptable myObjectInstance = ctor.construct(cx, scope, new Object[] { /* args */ }); ctor.defineProperty("PROP", myObjectInstance, READONLY); .... }
final FunctionObject jsCustomEval = new FunctionObject("eval", evalFn, window); window.associateValue("custom_eval", jsCustomEval); final Scriptable prototype = prototypesPerJSName.get(jsClassName); if (prototype != null) { final FunctionObject jsCtor = new FunctionObject(jsClassName, jsConstructor, window); jsCtor.addAsConstructor(window, prototype);
/** * Wraps a method as a JavaScript function. * @param methodName the name of the method to wrap * @param clazz the class declaring the method * @param parameterTypes the types of the method's parameter * @throws NoSuchMethodException if the method is no found */ MethodWrapper(final String methodName, final Class<?> clazz, final Class<?>[] parameterTypes) throws NoSuchMethodException { clazz_ = clazz; method_ = clazz.getMethod(methodName, parameterTypes); jsTypeTags_ = new int[parameterTypes.length]; int i = 0; for (final Class<?> klass : parameterTypes) { jsTypeTags_[i++] = FunctionObject.getTypeTag(klass); } }
Method[] methods = FunctionObject.getMethodList(clazz); for (int i=0; i < methods.length; i++) { Method method = methods[i]; Member ctorMember = FunctionObject.findSingleMethod(methods, ctorName); FunctionObject ctor = new FunctionObject(className, ctorMember, scope); if (ctor.isVarArgsMethod()) { throw Context.reportRuntimeError1 ("msg.varargs.ctor", ctorMember.getName()); ctor.initAsConstructor(scope, proto); Method setter = FunctionObject.findSingleMethod( methods, setterPrefix + name); FunctionObject f = new FunctionObject(name, methods[i], proto); if (f.isVarArgsConstructor()) { throw Context.reportRuntimeError1 ("msg.varargs.fun", ctorMember.getName()); defineProperty(dest, name, f, DONTENUM); if (sealed) { f.sealObject(); ctor.sealObject(); if (proto instanceof ScriptableObject) { ((ScriptableObject) proto).sealObject();
/** * Converts JavaScript arguments to Java arguments. * @param context the current context * @param scope the current scope * @param jsArgs the JavaScript arguments * @return the java arguments */ Object[] convertJSArgsToJavaArgs(final Context context, final Scriptable scope, final Object[] jsArgs) { if (jsArgs.length != jsTypeTags_.length) { throw Context.reportRuntimeError("Bad number of parameters for function " + method_.getName() + ": expected " + jsTypeTags_.length + " got " + jsArgs.length); } final Object[] javaArgs = new Object[jsArgs.length]; int i = 0; for (final Object object : jsArgs) { javaArgs[i] = FunctionObject.convertArg(context, scope, object, jsTypeTags_[i++]); } return javaArgs; }
/** * Search for names in a class, adding the resulting methods * as properties. * * <p> Uses reflection to find the methods of the given names. Then * FunctionObjects are constructed from the methods found, and * are added to this object as properties with the given names. * * @param names the names of the Methods to add as function properties * @param clazz the class to search for the Methods * @param attributes the attributes of the new properties * @see net.sourceforge.htmlunit.corejs.javascript.FunctionObject */ public void defineFunctionProperties(String[] names, Class<?> clazz, int attributes) { Method[] methods = FunctionObject.getMethodList(clazz); for (int i=0; i < names.length; i++) { String name = names[i]; Method m = FunctionObject.findSingleMethod(methods, name); if (m == null) { throw Context.reportRuntimeError2( "msg.method.not.found", name, clazz.getName()); } FunctionObject f = new FunctionObject(name, m, this); defineProperty(name, f, attributes); } }
private void define(final Class<? extends SimpleScriptable> c, final BrowserVersion browserVersion) { try { final ClassConfiguration config = AbstractJavaScriptConfiguration.getClassConfiguration(c, browserVersion); final HtmlUnitScriptable prototype = JavaScriptEngine.configureClass(config, this, browserVersion); final FunctionObject functionObject = new RecursiveFunctionObject(c.getSimpleName(), config.getJsConstructor(), this); if (c == V8BreakIterator.class) { prototype.setClassName("v8BreakIterator"); } functionObject.addAsConstructor(this, prototype); } catch (final Exception e) { throw Context.throwAsScriptRuntimeEx(e); } } }
boolean compatible = false; if (thisObj == scope) { Scriptable parentScope = getParentScope(); if (scope != parentScope) { for (int i = 0; i != parmsLength; ++i) { Object arg = args[i]; Object converted = convertArg(cx, scope, arg, typeTags[i]); if (arg != converted) { if (invokeArgs == args) { ? args[i] : Undefined.instance; invokeArgs[i] = convertArg(cx, scope, arg, typeTags[i]);
/** * Define this function as a JavaScript constructor. * <p> * Sets up the "prototype" and "constructor" properties. Also * calls setParent and setPrototype with appropriate values. * Then adds the function object as a property of the given scope, using * <code>prototype.getClassName()</code> * as the name of the property. * * @param scope the scope in which to define the constructor (typically * the global object) * @param prototype the prototype object * @see net.sourceforge.htmlunit.corejs.javascript.Scriptable#setParentScope * @see net.sourceforge.htmlunit.corejs.javascript.Scriptable#setPrototype * @see net.sourceforge.htmlunit.corejs.javascript.Scriptable#getClassName */ public void addAsConstructor(Scriptable scope, Scriptable prototype) { initAsConstructor(scope, prototype); defineProperty(scope, prototype.getClassName(), this, ScriptableObject.DONTENUM); }
public class MyObject extends ScriptableObject { @Override public String getClassName() { return "MyObject"; } // define static properties public static void finishInit(Scriptable scope, FunctionObject ctor, Scriptable proto) { ctor.defineProperty("PROP_ONE", 1, READONLY); ctor.defineProperty("PROP_TWO", 2, READONLY); ctor.defineProperty("PROP_THREE", 3, READONLY); }
/** * {@inheritDoc} */ @Override public Object call(final Context cx, final Scriptable scope, final Scriptable thisObj, final Object[] args) { final Object object = super.call(cx, scope, thisObj, args); if (object instanceof Scriptable) { final Scriptable result = (Scriptable) object; if (result.getPrototype() == null) { final Scriptable proto = getClassPrototype(); if (result != proto) { result.setPrototype(proto); } } if (result.getParentScope() == null) { final Scriptable parent = getParentScope(); if (result != parent) { result.setParentScope(parent); } } } return object; } }
final FunctionObject jsCustomEval = new FunctionObject("eval", evalFn, window); window.associateValue("custom_eval", jsCustomEval); final Scriptable prototype = prototypesPerJSName.get(jsClassName); if (prototype != null) { final FunctionObject jsCtor = new FunctionObject(jsClassName, jsConstructor, window); jsCtor.addAsConstructor(window, prototype);
/** * Wraps a method as a JavaScript function. * @param methodName the name of the method to wrap * @param clazz the class declaring the method * @param parameterTypes the types of the method's parameter * @throws NoSuchMethodException if the method is no found */ MethodWrapper(final String methodName, final Class< ? > clazz, final Class< ? >[] parameterTypes) throws NoSuchMethodException { clazz_ = clazz; method_ = clazz.getMethod(methodName, parameterTypes); jsTypeTags_ = new int[parameterTypes.length]; int i = 0; for (final Class< ? > klass : parameterTypes) { jsTypeTags_[i++] = FunctionObject.getTypeTag(klass); } }
/** * Converts js arguments to java arguments * @param context the current context * @param scope the current scope * @param jsArgs the JavaScript arguments * @return the java arguments */ Object[] convertJSArgsToJavaArgs(final Context context, final Scriptable scope, final Object[] jsArgs) { if (jsArgs.length != jsTypeTags_.length) { throw Context.reportRuntimeError("Bad number of parameters for function " + method_.getName() + ": expected " + jsTypeTags_.length + " got " + jsArgs.length); } final Object[] javaArgs = new Object[jsArgs.length]; int i = 0; for (final Object object : jsArgs) { javaArgs[i] = FunctionObject.convertArg(context, scope, object, jsTypeTags_[i++]); } return javaArgs; }
private void define(final Class<? extends SimpleScriptable> c, final BrowserVersion browserVersion) { try { final ClassConfiguration config = AbstractJavaScriptConfiguration.getClassConfiguration(c, browserVersion); final HtmlUnitScriptable prototype = JavaScriptEngine.configureClass(config, this, browserVersion); final FunctionObject functionObject = new RecursiveFunctionObject(c.getSimpleName(), config.getJsConstructor(), this); if (c == V8BreakIterator.class) { prototype.setClassName("v8BreakIterator"); } functionObject.addAsConstructor(this, prototype); } catch (final Exception e) { throw Context.throwAsScriptRuntimeEx(e); } } }
/** * {@inheritDoc} */ @Override public Object call(final Context cx, final Scriptable scope, final Scriptable thisObj, final Object[] args) { final Object object = super.call(cx, scope, thisObj, args); if (object instanceof Scriptable) { final Scriptable result = (Scriptable) object; if (result.getPrototype() == null) { final Scriptable proto = getClassPrototype(); if (result != proto) { result.setPrototype(proto); } } if (result.getParentScope() == null) { final Scriptable parent = getParentScope(); if (result != parent) { result.setParentScope(parent); } } } return object; } }
private void defineMethod(final String methodName, final Scriptable scope) { for (Method method : getClass().getMethods()) { if (method.getName().equals(methodName)) { final FunctionObject functionObject = new FunctionObject(methodName, method, scope); ((ScriptableObject) scope).defineProperty(methodName, functionObject, ScriptableObject.EMPTY); } } }