Search in sources :

Example 1 with DoubleMemory

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

the class DateFunctions method microtime.

public static Memory microtime(boolean getAsFloat) {
    double now = System.currentTimeMillis() / 1000.0;
    int s = (int) now;
    return getAsFloat ? new DoubleMemory(now) : new StringMemory((Math.round((now - s) * 1000) / 1000) + " " + s);
}
Also used : StringMemory(php.runtime.memory.StringMemory) DoubleMemory(php.runtime.memory.DoubleMemory)

Example 2 with DoubleMemory

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

the class MathFunctions method pow.

@Immutable
public static Memory pow(Memory base, Memory exp) {
    Memory realBase = base.toNumeric();
    Memory realExp = exp.toNumeric();
    if (realBase.type == Memory.Type.INT && realExp.type == Memory.Type.INT) {
        double result = Math.pow(realBase.toLong(), realExp.toLong());
        if (result > Long.MAX_VALUE) {
            return DoubleMemory.valueOf(result);
        }
        return LongMemory.valueOf((long) result);
    } else {
        return new DoubleMemory(Math.pow(base.toDouble(), exp.toDouble()));
    }
}
Also used : DoubleMemory(php.runtime.memory.DoubleMemory) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) LongMemory(php.runtime.memory.LongMemory) DoubleMemory(php.runtime.memory.DoubleMemory) Immutable(php.runtime.annotation.Runtime.Immutable)

Example 3 with DoubleMemory

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

the class MemoryDeserializer method convert.

protected Memory convert(JsonElement json, int depth) {
    if (depth > maxDepth)
        throw new MaxDepthException();
    if (json.isJsonNull())
        return Memory.NULL;
    if (json.isJsonPrimitive()) {
        JsonPrimitive jsonPrimitive = json.getAsJsonPrimitive();
        if (jsonPrimitive.isString())
            return StringMemory.valueOf(jsonPrimitive.getAsString());
        else if (jsonPrimitive.isBoolean())
            return jsonPrimitive.getAsBoolean() ? Memory.TRUE : Memory.FALSE;
        else if (jsonPrimitive.isNumber()) {
            Memory l = StringMemory.toLong(jsonPrimitive.getAsString());
            if (l != null)
                return l;
            else
                return new DoubleMemory(json.getAsDouble());
        }
        return Memory.NULL;
    } else if (json.isJsonArray()) {
        ArrayMemory array = new ArrayMemory();
        for (JsonElement el : json.getAsJsonArray()) array.add(convert(el, depth + 1).toImmutable());
        return array.toConstant();
    } else if (json.isJsonObject()) {
        JsonObject jsonObject = json.getAsJsonObject();
        StdClass stdClass = assoc ? null : new StdClass(env.get());
        ArrayMemory array = assoc ? new ArrayMemory() : stdClass.getProperties();
        for (Map.Entry<String, JsonElement> el : jsonObject.entrySet()) {
            String key = el.getKey();
            if (!key.startsWith("\0"))
                array.put(key, convert(el.getValue(), depth + 1).toImmutable());
        }
        return assoc ? array : new ObjectMemory(stdClass);
    } else
        return Memory.NULL;
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) ObjectMemory(php.runtime.memory.ObjectMemory) DoubleMemory(php.runtime.memory.DoubleMemory) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) StringMemory(php.runtime.memory.StringMemory) ObjectMemory(php.runtime.memory.ObjectMemory) DoubleMemory(php.runtime.memory.DoubleMemory) Map(java.util.Map) StdClass(php.runtime.lang.StdClass)

Example 4 with DoubleMemory

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

the class DateFunctions method gettimeofday.

public static Memory gettimeofday(boolean getAsFloat) {
    long msec_time = System.currentTimeMillis();
    if (getAsFloat) {
        double now = msec_time / 1000.0;
        return new DoubleMemory(now);
    } else {
        ArrayMemory result = new ArrayMemory();
        TimeZone timeZone = TimeZone.getDefault();
        long sec = msec_time / 1000;
        long usec = (msec_time % 1000) * 1000;
        long minuteswest = -timeZone.getOffset(msec_time) / MSEC_IN_MIN;
        boolean is_dst = timeZone.inDaylightTime(new Date(msec_time));
        result.refOfIndex("sec").assign(sec);
        result.refOfIndex("usec").assign(usec);
        result.refOfIndex("minuteswest").assign(minuteswest);
        result.refOfIndex("dsttime").assign(is_dst ? 1 : 0);
        return result.toConstant();
    }
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) TimeZone(java.util.TimeZone) DoubleMemory(php.runtime.memory.DoubleMemory) Date(java.util.Date)

Aggregations

DoubleMemory (php.runtime.memory.DoubleMemory)4 ArrayMemory (php.runtime.memory.ArrayMemory)3 Memory (php.runtime.Memory)2 StringMemory (php.runtime.memory.StringMemory)2 Date (java.util.Date)1 Map (java.util.Map)1 TimeZone (java.util.TimeZone)1 Immutable (php.runtime.annotation.Runtime.Immutable)1 StdClass (php.runtime.lang.StdClass)1 LongMemory (php.runtime.memory.LongMemory)1 ObjectMemory (php.runtime.memory.ObjectMemory)1