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;
}
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;
}
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;
}
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();
}
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;
}
Aggregations