Search in sources :

Example 11 with FunctionArgument

use of lucee.runtime.type.FunctionArgument in project Lucee by lucee.

the class Props method callCFCMetaData.

private void callCFCMetaData(PageContext pc, Component cfc, int format) throws IOException, PageException, ConverterException {
    ComponentSpecificAccess cw = new ComponentSpecificAccess(Component.ACCESS_REMOTE, cfc);
    ComponentScope scope = cw.getComponentScope();
    Struct udfs = new StructImpl(), sctUDF, sctArg;
    Array arrArg;
    Iterator<Object> it = scope.valueIterator();
    Object v;
    UDF udf;
    FunctionArgument[] args;
    while (it.hasNext()) {
        v = it.next();
        // UDF
        if (v instanceof UDF) {
            udf = (UDF) v;
            sctUDF = new StructImpl();
            arrArg = new ArrayImpl();
            udfs.setEL(udf.getFunctionName(), sctUDF);
            args = udf.getFunctionArguments();
            for (int i = 0; i < args.length; i++) {
                sctArg = new StructImpl();
                arrArg.appendEL(sctArg);
                sctArg.setEL(KeyConstants._name, args[i].getName().getString());
                sctArg.setEL(KeyConstants._type, args[i].getTypeAsString());
                sctArg.setEL(KeyConstants._required, args[i].isRequired());
                if (!StringUtil.isEmpty(args[i].getHint()))
                    sctArg.setEL(KeyConstants._hint, args[i].getHint());
            }
            sctUDF.set(KeyConstants._arguments, arrArg);
            sctUDF.set(KeyConstants._returntype, udf.getReturnTypeAsString());
        }
    }
    Struct rtn = new StructImpl();
    rtn.set(KeyConstants._functions, udfs);
    rtn.set(ACCEPT_ARG_COLL_FORMATS, "cfml,json");
    InputStream is;
    Charset cs = null;
    // WDDX
    if (UDF.RETURN_FORMAT_WDDX == format) {
        WDDXConverter converter = new WDDXConverter(pc.getTimeZone(), false, false);
        converter.setTimeZone(pc.getTimeZone());
        String str = converter.serialize(rtn);
        cs = getCharset(pc);
        is = new ByteArrayInputStream(str.getBytes(cs));
    } else // JSON
    if (UDF.RETURN_FORMAT_JSON == format) {
        boolean byColumn = false;
        cs = getCharset(pc);
        JSONConverter converter = new JSONConverter(false, cs);
        String str = converter.serialize(pc, rtn, byColumn);
        is = new ByteArrayInputStream(str.getBytes(cs));
    } else // CFML
    if (UDF.RETURN_FORMAT_SERIALIZE == format) {
        ScriptConverter converter = new ScriptConverter(false);
        String str = converter.serialize(rtn);
        cs = getCharset(pc);
        is = new ByteArrayInputStream(str.getBytes(cs));
    } else // XML
    if (UDF.RETURN_FORMAT_XML == format) {
        XMLConverter converter = new XMLConverter(pc.getTimeZone(), false);
        converter.setTimeZone(pc.getTimeZone());
        String str = converter.serialize(rtn);
        cs = getCharset(pc);
        is = new ByteArrayInputStream(str.getBytes(cs));
    } else // Plain
    if (UDF.RETURN_FORMAT_PLAIN == format) {
        String str = Caster.toString(rtn);
        cs = getCharset(pc);
        is = new ByteArrayInputStream(str.getBytes(cs));
    } else // Java
    if (UDF.RETURN_FORMAT_JAVA == format) {
        byte[] bytes = JavaConverter.serializeAsBinary(rtn);
        is = new ByteArrayInputStream(bytes);
    } else
        throw new IOException("invalid format defintion:" + format);
    OutputStream os = null;
    try {
        os = pc.getResponseStream();
        setFormat(pc.getHttpServletResponse(), format, cs);
        IOUtil.copy(is, os, false, false);
    } finally {
        IOUtil.flushEL(os);
        IOUtil.closeEL(os);
        ((PageContextImpl) pc).getRootOut().setClosed(true);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayImpl(lucee.runtime.type.ArrayImpl) OutputStream(java.io.OutputStream) XMLConverter(lucee.runtime.converter.XMLConverter) Charset(java.nio.charset.Charset) IOException(java.io.IOException) StaticStruct(lucee.runtime.component.StaticStruct) Struct(lucee.runtime.type.Struct) Array(lucee.runtime.type.Array) WDDXConverter(lucee.runtime.converter.WDDXConverter) StructImpl(lucee.runtime.type.StructImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) UDF(lucee.runtime.type.UDF) ScriptConverter(lucee.runtime.converter.ScriptConverter) FunctionArgument(lucee.runtime.type.FunctionArgument) JSONConverter(lucee.runtime.converter.JSONConverter)

Example 12 with FunctionArgument

use of lucee.runtime.type.FunctionArgument in project Lucee by lucee.

the class Function method createArguments.

private final void createArguments(BytecodeContext bc) throws TransformerException {
    GeneratorAdapter ga = bc.getAdapter();
    ga.push(arguments.size());
    ga.newArray(FUNCTION_ARGUMENT);
    Argument arg;
    for (int i = 0; i < arguments.size(); i++) {
        arg = arguments.get(i);
        boolean canHaveKey = Factory.canRegisterKey(arg.getName());
        // CHECK if default values
        // type
        ExprString _strType = arg.getType();
        short _type = CFTypes.TYPE_UNKNOW;
        if (_strType instanceof LitString) {
            _type = CFTypes.toShortStrict(((LitString) _strType).getString(), CFTypes.TYPE_UNKNOW);
        }
        boolean useType = !canHaveKey || _type != CFTypes.TYPE_ANY;
        // boolean useStrType=useType && (_type==CFTypes.TYPE_UNDEFINED || _type==CFTypes.TYPE_UNKNOW || CFTypes.toString(_type, null)==null);
        // required
        ExprBoolean _req = arg.getRequired();
        boolean useReq = !canHaveKey || toBoolean(_req, null) != Boolean.FALSE;
        // default-type
        Expression _def = arg.getDefaultValueType(bc.getFactory());
        boolean useDef = !canHaveKey || toInt(_def, -1) != FunctionArgument.DEFAULT_TYPE_NULL;
        // pass by reference
        ExprBoolean _pass = arg.isPassByReference();
        boolean usePass = !canHaveKey || toBoolean(_pass, null) != Boolean.TRUE;
        // display-hint
        ExprString _dsp = arg.getDisplayName();
        boolean useDsp = !canHaveKey || !isLiteralEmptyString(_dsp);
        // hint
        ExprString _hint = arg.getHint();
        boolean useHint = !canHaveKey || !isLiteralEmptyString(_hint);
        // meta
        Map _meta = arg.getMetaData();
        boolean useMeta = !canHaveKey || (_meta != null && !_meta.isEmpty());
        int functionIndex = 7;
        if (!useMeta) {
            functionIndex--;
            if (!useHint) {
                functionIndex--;
                if (!useDsp) {
                    functionIndex--;
                    if (!usePass) {
                        functionIndex--;
                        if (!useDef) {
                            functionIndex--;
                            if (!useReq) {
                                functionIndex--;
                                if (!useType) {
                                    functionIndex--;
                                }
                            }
                        }
                    }
                }
            }
        }
        // write out arguments
        ga.dup();
        ga.push(i);
        // new FunctionArgument(...)
        ga.newInstance(canHaveKey && functionIndex < INIT_FAI_KEY_LIGHT.length ? FUNCTION_ARGUMENT_LIGHT : FUNCTION_ARGUMENT_IMPL);
        ga.dup();
        // name
        bc.getFactory().registerKey(bc, arg.getName(), false);
        // type
        if (functionIndex >= INIT_FAI_KEY.length - 7) {
            _strType.writeOut(bc, Expression.MODE_REF);
            bc.getAdapter().push(_type);
        }
        // required
        if (functionIndex >= INIT_FAI_KEY.length - 6)
            _req.writeOut(bc, Expression.MODE_VALUE);
        // default value
        if (functionIndex >= INIT_FAI_KEY.length - 5)
            _def.writeOut(bc, Expression.MODE_VALUE);
        // pass by reference
        if (functionIndex >= INIT_FAI_KEY.length - 4)
            _pass.writeOut(bc, Expression.MODE_VALUE);
        // display-name
        if (functionIndex >= INIT_FAI_KEY.length - 3)
            _dsp.writeOut(bc, Expression.MODE_REF);
        // hint
        if (functionIndex >= INIT_FAI_KEY.length - 2)
            _hint.writeOut(bc, Expression.MODE_REF);
        // meta
        if (functionIndex == INIT_FAI_KEY.length - 1)
            Page.createMetaDataStruct(bc, _meta, null);
        if (functionIndex < INIT_FAI_KEY_LIGHT.length)
            ga.invokeConstructor(FUNCTION_ARGUMENT_LIGHT, INIT_FAI_KEY[functionIndex]);
        else
            ga.invokeConstructor(FUNCTION_ARGUMENT_IMPL, INIT_FAI_KEY[functionIndex]);
        ga.visitInsn(Opcodes.AASTORE);
    }
}
Also used : LitString(lucee.transformer.expression.literal.LitString) Argument(lucee.transformer.bytecode.statement.Argument) FunctionArgument(lucee.runtime.type.FunctionArgument) ExprString(lucee.transformer.expression.ExprString) Expression(lucee.transformer.expression.Expression) ExprBoolean(lucee.transformer.expression.ExprBoolean) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

FunctionArgument (lucee.runtime.type.FunctionArgument)12 Key (lucee.runtime.type.Collection.Key)6 UDF (lucee.runtime.type.UDF)6 Struct (lucee.runtime.type.Struct)4 StructImpl (lucee.runtime.type.StructImpl)4 Array (lucee.runtime.type.Array)3 ArrayImpl (lucee.runtime.type.ArrayImpl)3 IOException (java.io.IOException)2 Charset (java.nio.charset.Charset)2 Iterator (java.util.Iterator)2 Map (java.util.Map)2 Entry (java.util.Map.Entry)2 StaticStruct (lucee.runtime.component.StaticStruct)2 ApplicationException (lucee.runtime.exp.ApplicationException)2 PageException (lucee.runtime.exp.PageException)2 Collection (lucee.runtime.type.Collection)2 Argument (lucee.runtime.type.scope.Argument)2 ArgumentIntKey (lucee.runtime.type.scope.ArgumentIntKey)2 LitString (lucee.transformer.expression.literal.LitString)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1