Search in sources :

Example 36 with StringMemory

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

the class BCMathFunctions method bcsqrt.

public static Memory bcsqrt(Environment env, Memory operand, int scale) {
    BigDecimal value = toBigDecimal(operand);
    int compareToZero = value.compareTo(BigDecimal.ZERO);
    if (compareToZero < 0) {
        return Memory.NULL;
    } else if (compareToZero == 0) {
        return new StringMemory("0");
    }
    int compareToOne = value.compareTo(BigDecimal.ONE);
    if (compareToOne == 0)
        return new StringMemory("1");
    int cscale;
    BigDecimal initialGuess;
    if (compareToOne < 1) {
        initialGuess = BigDecimal.ONE;
        cscale = value.scale();
    } else {
        BigInteger integerPart = value.toBigInteger();
        int length = integerPart.toString().length();
        if ((length % 2) == 0)
            length--;
        length /= 2;
        initialGuess = BigDecimal.ONE.movePointRight(length);
        cscale = Math.max(scale, value.scale()) + 2;
    }
    BigDecimal guess = initialGuess;
    BigDecimal lastGuess;
    for (int iteration = 0; iteration < 50; iteration++) {
        lastGuess = guess;
        guess = value.divide(guess, cscale, RoundingMode.DOWN);
        guess = guess.add(lastGuess);
        guess = guess.divide(DECIMAL_TWO, cscale, RoundingMode.DOWN);
        if (lastGuess.equals(guess)) {
            break;
        }
    }
    value = guess;
    value = value.setScale(scale, RoundingMode.DOWN);
    return new StringMemory(value.toPlainString());
}
Also used : StringMemory(php.runtime.memory.StringMemory) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal)

Example 37 with StringMemory

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

the class WrapUIReader method onTranslate.

@Signature(@Arg(value = "handler", type = HintType.CALLABLE, optional = @Optional("NULL")))
public Memory onTranslate(final Environment env, Memory... args) {
    if (args[0].isNull())
        reader.setTranslateHandler(null);
    else {
        final Invoker invoker = Invoker.valueOf(env, null, args[0]);
        UIReader.TranslateHandler handler = new UIReader.TranslateHandler() {

            @Override
            public Value onTranslate(Component component, Value var) {
                return new Value(invoker.callNoThrow(new ObjectMemory(UIElement.of(env, component)), var == null ? Memory.NULL : new StringMemory(var.asString())).toString());
            }
        };
        reader.setTranslateHandler(handler);
    }
    return Memory.NULL;
}
Also used : UIReader(org.develnext.jphp.swing.loader.UIReader) Invoker(php.runtime.invoke.Invoker) ObjectMemory(php.runtime.memory.ObjectMemory) Value(org.develnext.jphp.swing.loader.support.Value) StringMemory(php.runtime.memory.StringMemory)

Example 38 with StringMemory

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

the class WrapUIReader method onRead.

@Signature(@Arg(value = "handler", type = HintType.CALLABLE, optional = @Optional("NULL")))
public Memory onRead(final Environment env, Memory... args) {
    if (args[0].isNull())
        reader.setReadHandler(null);
    else {
        final Invoker invoker = Invoker.valueOf(env, null, args[0]);
        UIReader.ReadHandler handler = new UIReader.ReadHandler() {

            @Override
            public void onRead(Component component, String var) {
                invoker.callNoThrow(new ObjectMemory(UIElement.of(env, component)), var == null ? Memory.NULL : new StringMemory(var));
            }
        };
        reader.setReadHandler(handler);
    }
    return Memory.NULL;
}
Also used : UIReader(org.develnext.jphp.swing.loader.UIReader) Invoker(php.runtime.invoke.Invoker) ObjectMemory(php.runtime.memory.ObjectMemory) StringMemory(php.runtime.memory.StringMemory)

Example 39 with StringMemory

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

the class WrapTime method __toString.

@Signature
public Memory __toString(Environment env, Memory... args) {
    SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    isoFormat.setTimeZone(timeZone);
    return new StringMemory(isoFormat.format(date));
}
Also used : StringMemory(php.runtime.memory.StringMemory) SimpleDateFormat(java.text.SimpleDateFormat)

Example 40 with StringMemory

use of php.runtime.memory.StringMemory 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)

Aggregations

StringMemory (php.runtime.memory.StringMemory)47 ArrayMemory (php.runtime.memory.ArrayMemory)14 Memory (php.runtime.Memory)11 ObjectMemory (php.runtime.memory.ObjectMemory)8 ReferenceMemory (php.runtime.memory.ReferenceMemory)7 BigDecimal (java.math.BigDecimal)5 Invoker (php.runtime.invoke.Invoker)4 ClassEntity (php.runtime.reflection.ClassEntity)4 TraceInfo (php.runtime.env.TraceInfo)3 IObject (php.runtime.lang.IObject)3 LongMemory (php.runtime.memory.LongMemory)3 StringWriter (java.io.StringWriter)2 ComponentProperties (org.develnext.jphp.swing.ComponentProperties)2 UIReader (org.develnext.jphp.swing.loader.UIReader)2 Signature (php.runtime.annotation.Reflection.Signature)2 CallStackItem (php.runtime.env.CallStackItem)2 ConcurrentEnvironment (php.runtime.env.ConcurrentEnvironment)2 CriticalException (php.runtime.exceptions.CriticalException)2 MethodEntity (php.runtime.reflection.MethodEntity)2 BigInteger (java.math.BigInteger)1