Search in sources :

Example 1 with ReferenceMemory

use of php.runtime.memory.ReferenceMemory in project jphp by jphp-compiler.

the class InvokeArgumentHelper method makeArguments.

public static Memory[] makeArguments(Environment env, Memory[] args, ParameterEntity[] parameters, String originClassName, String originMethodName, TraceInfo trace) {
    if (parameters == null)
        return args;
    args = unpackArgs(env, trace, args, parameters);
    Memory[] passed = args;
    if ((args == null && parameters.length > 0) || (args != null && args.length < parameters.length)) {
        passed = new Memory[parameters.length];
        if (args != null && args.length > 0) {
            System.arraycopy(args, 0, passed, 0, args.length);
        }
    }
    int i = 0;
    if (passed != null) {
        boolean variadicMemoryExists = false;
        for (ParameterEntity param : parameters) {
            Memory arg = passed[i];
            if (param.isVariadic()) {
                ArrayMemory variadicArgs = new ArrayMemory();
                int _i = i;
                while (arg != null) {
                    if (arg instanceof VariadicMemory) {
                        variadicMemoryExists = true;
                        ForeachIterator iterator = arg.getNewIterator(env, param.isReference(), false);
                        if (iterator == null) {
                            env.warning(trace, INVALID_TYPE_MESSAGE);
                        } else {
                            makeVariadic(iterator, variadicArgs, param, env, trace, _i, originClassName, originMethodName);
                        }
                    } else {
                        if (variadicMemoryExists) {
                            env.error(trace, "Cannot use positional argument after argument unpacking");
                        }
                        if (!param.checkTypeHinting(env, arg)) {
                            invalidType(env, trace, param, _i + 1, arg, originClassName, originMethodName);
                        }
                        variadicArgs.add(makeValue(param, arg, env, trace));
                    }
                    i++;
                    if (i < passed.length) {
                        arg = passed[i];
                    } else {
                        break;
                    }
                }
                passed[_i] = variadicArgs;
                break;
            }
            if (arg == null) {
                Memory def = param.getDefaultValue();
                if (def != null) {
                    if (!param.isReference()) {
                        passed[i] = param.isMutable() ? def.toImmutable(env, trace) : def;
                    } else {
                        passed[i] = new ReferenceMemory(param.isMutable() ? def.toImmutable(env, trace) : def);
                    }
                } else {
                    if (param.getTypeClass() != null) {
                        invalidType(env, trace, param, i + 1, null, originClassName, originMethodName);
                    }
                    env.error(trace, ErrorType.E_ERROR, Messages.ERR_MISSING_ARGUMENT, (i + 1) + " ($" + param.getName() + ")", originMethodName == null ? originClassName : originClassName + "::" + originMethodName);
                    passed[i] = param.isReference() ? new ReferenceMemory() : Memory.NULL;
                }
            } else {
                if (param.isReference()) {
                    if (!arg.isReference() && !arg.isObject()) {
                        env.error(trace, ErrorType.E_ERROR, "Only variables can be passed by reference");
                        passed[i] = new ReferenceMemory(arg);
                    }
                } else {
                    passed[i] = param.isMutable() ? arg.toImmutable() : arg.toValue();
                }
            }
            if (!param.checkTypeHinting(env, passed[i])) {
                invalidType(env, trace, param, i + 1, passed[i], originClassName, originMethodName);
            }
            i++;
        }
        if (!variadicMemoryExists) {
            for (int j = parameters.length; j < passed.length; j++) {
                passed[j] = passed[j].toImmutable();
            }
        }
    }
    return passed;
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) ReferenceMemory(php.runtime.memory.ReferenceMemory) ForeachIterator(php.runtime.lang.ForeachIterator) ParameterEntity(php.runtime.reflection.ParameterEntity) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) ReferenceMemory(php.runtime.memory.ReferenceMemory) ObjectMemory(php.runtime.memory.ObjectMemory) VariadicMemory(php.runtime.memory.helper.VariadicMemory) VariadicMemory(php.runtime.memory.helper.VariadicMemory)

Example 2 with ReferenceMemory

use of php.runtime.memory.ReferenceMemory in project jphp by jphp-compiler.

the class InvokeArgumentHelper method makeValue.

public static Memory makeValue(ParameterEntity param, Memory arg, Environment env, TraceInfo trace) {
    if (param.isReference()) {
        if (!arg.isReference() && !arg.isObject()) {
            env.error(trace, ErrorType.E_ERROR, "Only variables can be passed by reference");
            arg = new ReferenceMemory(arg);
        }
    } else {
        arg = param.isMutable() ? arg.toImmutable() : arg.toValue();
    }
    return arg;
}
Also used : ReferenceMemory(php.runtime.memory.ReferenceMemory)

Example 3 with ReferenceMemory

use of php.runtime.memory.ReferenceMemory in project jphp by jphp-compiler.

the class Environment method getOrCreateStatic.

public Memory getOrCreateStatic(String name, Memory initValue) {
    ReferenceMemory result = statics.get(name);
    if (result == null) {
        result = new ReferenceMemory(initValue);
        statics.put(name, result);
    }
    // globals.getByScalarOrCreate(name, initValue);
    return result;
}
Also used : ReferenceMemory(php.runtime.memory.ReferenceMemory)

Example 4 with ReferenceMemory

use of php.runtime.memory.ReferenceMemory in project jphp by jphp-compiler.

the class WrapDomNode method toModel.

protected static Memory toModel(Node node) {
    if (node.getNodeType() == Node.TEXT_NODE) {
        return StringMemory.valueOf(node.getTextContent());
    }
    ArrayMemory result = new ArrayMemory(true);
    NamedNodeMap attrs = node.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        Node attr = attrs.item(i);
        result.refOfIndex("@" + attr.getNodeName()).assign(attr.getNodeValue());
    }
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node el = children.item(i);
        if (el.getNodeType() == Node.TEXT_NODE) {
            continue;
        }
        if (el.hasAttributes() || el.hasChildNodes()) {
            if (el.getChildNodes().getLength() == 1 && el.getFirstChild().getNodeType() == Node.TEXT_NODE) {
                ReferenceMemory one = result.getByScalar(el.getNodeName());
                if (one != null) {
                    if (!one.isArray()) {
                        ArrayMemory tmp = new ArrayMemory();
                        tmp.refOfPush().assign(one.toValue());
                        one.assign(tmp);
                    }
                    one.refOfPush().assign(el.getFirstChild().getTextContent());
                    result.refOfIndex(el.getNodeName()).assignRef(one.toValue());
                } else {
                    result.refOfIndex(el.getNodeName()).assign(el.getFirstChild().getTextContent());
                }
            } else {
                result.refOfIndex(el.getNodeName()).assign(toModel(el).toImmutable());
            }
        } else {
            result.refOfIndex(el.getNodeName()).assign(el.getTextContent());
        }
    }
    if (result.size() == 1) {
        Memory tmp = result.getByScalar("item");
        if (tmp != null) {
            if (tmp.isArray()) {
                return tmp.toValue(ArrayMemory.class).toConstant();
            } else {
                ArrayMemory m = new ArrayMemory();
                m.add(tmp.toValue());
                return m.toConstant();
            }
        }
    }
    return result.toConstant();
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) ReferenceMemory(php.runtime.memory.ReferenceMemory) NamedNodeMap(org.w3c.dom.NamedNodeMap) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) ReferenceMemory(php.runtime.memory.ReferenceMemory) StringMemory(php.runtime.memory.StringMemory) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList)

Example 5 with ReferenceMemory

use of php.runtime.memory.ReferenceMemory in project jphp by jphp-compiler.

the class PSqlConnection method query.

@Signature
public PSqlStatement query(Environment env, String sql, @Nullable ArrayMemory args) throws Throwable {
    PreparedStatement statement = connection.prepareStatement(sql, Statement.NO_GENERATED_KEYS);
    PSqlStatement sqlStatement = new PSqlStatement(env, statement);
    if (args != null) {
        int index = 0;
        for (ReferenceMemory arg : args) {
            env.invokeMethod(sqlStatement, "bind", LongMemory.valueOf(index), arg.value);
            index += 1;
        }
    }
    return sqlStatement;
}
Also used : ReferenceMemory(php.runtime.memory.ReferenceMemory)

Aggregations

ReferenceMemory (php.runtime.memory.ReferenceMemory)11 Memory (php.runtime.Memory)4 ArrayMemory (php.runtime.memory.ArrayMemory)4 StringMemory (php.runtime.memory.StringMemory)3 IObject (php.runtime.lang.IObject)2 NamedNodeMap (org.w3c.dom.NamedNodeMap)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1 HashedMap (php.runtime.common.collections.map.HashedMap)1 ConcurrentEnvironment (php.runtime.env.ConcurrentEnvironment)1 ForeachIterator (php.runtime.lang.ForeachIterator)1 KeyValueMemory (php.runtime.memory.KeyValueMemory)1 ObjectMemory (php.runtime.memory.ObjectMemory)1 VariadicMemory (php.runtime.memory.helper.VariadicMemory)1 ParameterEntity (php.runtime.reflection.ParameterEntity)1