Search in sources :

Example 46 with ObjectMemory

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

the class WrapTime method replace.

@Signature({ @Arg(value = "args", type = HintType.ARRAY) })
public Memory replace(Environment env, Memory... args) {
    ArrayMemory arg = args[0].toValue(ArrayMemory.class);
    Memory year = arg.getByScalar("year");
    Memory month = arg.getByScalar("month");
    Memory day = arg.getByScalar("day");
    Memory hour = arg.getByScalar("hour");
    Memory min = arg.getByScalar("min");
    Memory sec = arg.getByScalar("sec");
    Memory millis = arg.getByScalar("millis");
    Calendar calendar1 = (Calendar) calendar.clone();
    if (year != null)
        calendar1.set(Calendar.YEAR, year.toInteger());
    if (month != null)
        calendar1.set(Calendar.MONTH, month.toInteger() - 1);
    if (day != null)
        calendar1.set(Calendar.DATE, day.toInteger());
    if (hour != null)
        calendar1.set(Calendar.HOUR_OF_DAY, hour.toInteger());
    if (min != null)
        calendar1.set(Calendar.MINUTE, min.toInteger());
    if (sec != null)
        calendar1.set(Calendar.SECOND, sec.toInteger());
    if (millis != null)
        calendar1.set(Calendar.MILLISECOND, millis.toInteger());
    return new ObjectMemory(new WrapTime(env, calendar1.getTime(), calendar1.getTimeZone()));
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) ObjectMemory(php.runtime.memory.ObjectMemory) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) LongMemory(php.runtime.memory.LongMemory) ObjectMemory(php.runtime.memory.ObjectMemory) StringMemory(php.runtime.memory.StringMemory) Calendar(java.util.Calendar)

Example 47 with ObjectMemory

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

the class InvokeHelper method callStatic.

public static Memory callStatic(Environment env, TraceInfo trace, String className, String methodName, String originClassName, String originMethodName, Memory[] args, MethodCallCache callCache, int cacheIndex) throws Throwable {
    if (callCache != null) {
        MethodEntity entity = callCache.get(env, cacheIndex);
        if (entity != null) {
            return callStatic(env, trace, entity, originClassName, args, false);
        }
    }
    ClassEntity classEntity = env.fetchClass(originClassName, className, true);
    MethodEntity method = classEntity == null ? null : classEntity.findMethod(methodName);
    Memory[] passed = null;
    boolean isMagic = false;
    if (method == null) {
        IObject maybeObject = env.getLateObject();
        if (maybeObject != null && maybeObject.getReflection().isInstanceOf(classEntity))
            return ObjectInvokeHelper.invokeMethod(new ObjectMemory(maybeObject), originMethodName, methodName, env, trace, args);
        if (classEntity != null && classEntity.methodMagicCallStatic != null) {
            method = classEntity.methodMagicCallStatic;
            isMagic = true;
            passed = new Memory[] { new StringMemory(originMethodName), ArrayMemory.of(args) };
        } else {
            if (classEntity == null) {
                env.error(trace, Messages.ERR_CLASS_NOT_FOUND.fetch(originClassName));
                return Memory.NULL;
            }
        }
    }
    if (method == null) {
        env.error(trace, Messages.ERR_CALL_TO_UNDEFINED_METHOD.fetch(originClassName + "::" + originMethodName));
        return Memory.NULL;
    }
    if (!method.isStatic()) {
        IObject maybeObject = env.getLateObject();
        if (maybeObject != null && maybeObject.getReflection().isInstanceOf(classEntity))
            return ObjectInvokeHelper.invokeMethod(maybeObject, method, env, trace, args, true);
        env.error(trace, ErrorType.E_STRICT, Messages.ERR_NON_STATIC_METHOD_CALLED_DYNAMICALLY, originClassName, originMethodName);
    }
    if (callCache != null && !isMagic) {
        callCache.put(env, cacheIndex, method);
    }
    checkAccess(env, trace, method);
    if (passed == null)
        passed = makeArguments(env, args, method.getParameters(), originClassName, originMethodName, trace);
    Memory result = method.getImmutableResult();
    if (result != null)
        return result;
    try {
        if (trace != null)
            env.pushCall(trace, null, args, originMethodName, method.getClazz().getName(), originClassName);
        return method.invokeStatic(env, trace, passed);
    } finally {
        if (trace != null)
            env.popCall();
    }
}
Also used : IObject(php.runtime.lang.IObject) ObjectMemory(php.runtime.memory.ObjectMemory) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) ObjectMemory(php.runtime.memory.ObjectMemory) StringMemory(php.runtime.memory.StringMemory) StringMemory(php.runtime.memory.StringMemory)

Example 48 with ObjectMemory

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

the class WrapThreadPool method schedule.

@Signature({ @Arg(value = "runnable", type = HintType.CALLABLE), @Arg("delay"), @Arg(value = "env", typeClass = "php\\lang\\Environment", optional = @Optional("NULL")) })
public Memory schedule(final Environment env, Memory... args) {
    final Environment _env = args[2].isNull() ? env : args[2].toObject(WrapEnvironment.class).getWrapEnvironment();
    final Invoker invoker = Invoker.valueOf(_env, null, args[0]);
    ScheduledFuture<Memory> future = getScheduledExecutorService(env).schedule(new Callable<Memory>() {

        @Override
        public Memory call() throws Exception {
            Environment.addThreadSupport(_env);
            return invoker.callNoThrow();
        }
    }, args[1].toLong(), TimeUnit.MILLISECONDS);
    return new ObjectMemory(new WrapFuture(env, future));
}
Also used : Invoker(php.runtime.invoke.Invoker) ObjectMemory(php.runtime.memory.ObjectMemory) Memory(php.runtime.Memory) ObjectMemory(php.runtime.memory.ObjectMemory) Environment(php.runtime.env.Environment)

Example 49 with ObjectMemory

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

the class LangFunctions method method_exists.

public static boolean method_exists(Environment env, Memory clazz, String method) {
    ClassEntity classEntity;
    if (clazz.isObject()) {
        ObjectMemory tmp = clazz.toValue(ObjectMemory.class);
        classEntity = tmp.getReflection();
    } else {
        String name = clazz.toString();
        String nameL = name.toLowerCase();
        classEntity = env.fetchClass(name, nameL, true);
        if (classEntity == null)
            classEntity = env.fetchMagicClass(name, nameL);
    }
    return classEntity != null && classEntity.findMethod(method.toLowerCase()) != null;
}
Also used : ObjectMemory(php.runtime.memory.ObjectMemory)

Example 50 with ObjectMemory

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

the class LangFunctions method get_object_vars.

public static Memory get_object_vars(Environment env, TraceInfo trace, Memory object) {
    if (expecting(env, trace, 1, object, Memory.Type.OBJECT)) {
        ObjectMemory o = object.toValue(ObjectMemory.class);
        ArrayMemory props = o.value.getProperties();
        ClassEntity entity = o.getReflection();
        ClassEntity context = env.getLastClassOnStack();
        ForeachIterator iterator = props.foreachIterator(false, false);
        ArrayMemory result = new ArrayMemory();
        while (iterator.next()) {
            PropertyEntity prop = entity.findProperty(iterator.getKey().toString());
            if (prop == null || prop.canAccess(env, context) == 0)
                result.refOfIndex(prop == null ? iterator.getKey().toString() : prop.getName()).assign(iterator.getValue());
        }
        return result.toConstant();
    } else
        return Memory.NULL;
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) ForeachIterator(php.runtime.lang.ForeachIterator) ObjectMemory(php.runtime.memory.ObjectMemory)

Aggregations

ObjectMemory (php.runtime.memory.ObjectMemory)55 ArrayMemory (php.runtime.memory.ArrayMemory)24 Memory (php.runtime.Memory)19 ClassEntity (php.runtime.reflection.ClassEntity)17 StringMemory (php.runtime.memory.StringMemory)14 IObject (php.runtime.lang.IObject)11 Invoker (php.runtime.invoke.Invoker)6 LongMemory (php.runtime.memory.LongMemory)6 Environment (php.runtime.env.Environment)5 MethodEntity (php.runtime.reflection.MethodEntity)5 Calendar (java.util.Calendar)4 Closure (php.runtime.lang.Closure)4 Locale (java.util.Locale)3 Test (org.junit.Test)3 WrapLocale (php.runtime.ext.core.classes.util.WrapLocale)3 PropertyEntity (php.runtime.reflection.PropertyEntity)3 AbstractFunctionEntity (php.runtime.reflection.support.AbstractFunctionEntity)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Date (java.util.Date)2 TimeZone (java.util.TimeZone)2