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