use of php.runtime.env.Environment in project jphp by jphp-compiler.
the class MemoryOperation method get.
@SuppressWarnings("unchecked")
public static MemoryOperation get(final Class<?> type, Type genericTypes, boolean includeParents) {
MemoryOperation operation = null;
if (genericTypes instanceof ParameterizedType) {
operation = genericOperations.get(new ParametrizedClass(type, ((ParameterizedType) genericTypes).getActualTypeArguments()));
}
if (operation == null) {
operation = operations.get(type);
if (operation == null) {
if (type.isArray()) {
MemoryOperation arrayMemoryOperation = new ArrayMemoryOperation(type);
register(arrayMemoryOperation);
return arrayMemoryOperation;
}
if (Enum.class.isAssignableFrom(type)) {
return new MemoryOperation() {
@Override
public Class<?>[] getOperationClasses() {
return new Class<?>[] { Enum.class };
}
@Override
@SuppressWarnings("unchecked")
public Object convert(Environment env, TraceInfo trace, Memory arg) throws Throwable {
return arg.isNull() ? null : Enum.valueOf((Class<? extends Enum>) type, arg.toString());
}
@Override
public Memory unconvert(Environment env, TraceInfo trace, Object arg) throws Throwable {
return arg == null ? Memory.NULL : StringMemory.valueOf(((Enum) arg).name());
}
@Override
public void applyTypeHinting(ParameterEntity parameter) {
parameter.setTypeEnum((Class<? extends Enum>) type);
}
};
}
final Class<? extends BaseWrapper> wrapperClass = wrappers.get(type);
if (wrapperClass != null) {
Constructor<BaseWrapper> constructor;
try {
constructor = (Constructor<BaseWrapper>) wrapperClass.getConstructor(Environment.class, type);
} catch (NoSuchMethodException e) {
try {
constructor = (Constructor<BaseWrapper>) wrapperClass.getConstructor(Environment.class, Object.class);
} catch (NoSuchMethodException e1) {
throw new CriticalException(e);
}
}
final Constructor<BaseWrapper> finalConstructor = constructor;
return new MemoryOperation() {
@Override
public Class<?>[] getOperationClasses() {
return new Class<?>[0];
}
@Override
public Object convert(Environment env, TraceInfo trace, Memory arg) throws Throwable {
if (arg.isNull()) {
return null;
}
return arg.toObject(BaseWrapper.class).getWrappedObject();
}
@Override
public Memory unconvert(Environment env, TraceInfo trace, Object arg) throws Throwable {
if (arg == null) {
return Memory.NULL;
}
Constructor<BaseWrapper> constructorContext = finalConstructor;
Class<? extends BaseWrapper> wrapperClassContext = wrapperClass;
if (arg.getClass() != type) {
wrapperClassContext = wrappers.get(arg.getClass());
}
if (wrapperClassContext != null && wrapperClassContext != wrapperClass) {
constructorContext = (Constructor<BaseWrapper>) wrapperClassContext.getConstructor(Environment.class, arg.getClass());
}
try {
BaseWrapper instance = constructorContext.newInstance(env, arg);
return ObjectMemory.valueOf(instance.__getOriginInstance());
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new CriticalException(e);
}
}
@Override
public void applyTypeHinting(ParameterEntity parameter) {
parameter.setTypeNativeClass(type);
}
};
} else if (IObject.class.isAssignableFrom(type)) {
return new MemoryOperation() {
@Override
public Class<?>[] getOperationClasses() {
return new Class<?>[] { IObject.class };
}
@Override
@SuppressWarnings("unchecked")
public Object convert(Environment env, TraceInfo trace, Memory arg) throws Throwable {
if (arg.isNull()) {
return null;
}
return arg.toObject((Class<? extends IObject>) type);
}
@Override
public Memory unconvert(Environment env, TraceInfo trace, Object arg) throws Throwable {
if (arg == null) {
return Memory.NULL;
}
return ObjectMemory.valueOf((IObject) arg);
}
@Override
public void applyTypeHinting(ParameterEntity parameter) {
parameter.setType(ReflectionUtils.getClassName(type));
}
};
} else {
Class<?> superType = type.getSuperclass();
if (Object.class != superType && (includeParents || type.isAnonymousClass())) {
return get(superType, type.getGenericSuperclass(), includeParents);
}
}
}
}
if (operation == null) {
return null;
}
if (genericTypes instanceof ParameterizedType) {
return operation.instance(((ParameterizedType) genericTypes).getActualTypeArguments());
}
return operation;
}
use of php.runtime.env.Environment in project jphp by jphp-compiler.
the class ObjectMemory method toString.
@Override
public String toString() {
ClassEntity entity = value.getReflection();
if (entity.methodMagicToString != null) {
Environment env = value.getEnvironment();
if (env == null)
return "Object";
// We can't get real trace info from toString method :(
env.pushCall(entity.methodMagicToString.getTrace(), value, null, entity.methodMagicToString.getName(), entity.getName(), null);
try {
Memory result = entity.methodMagicToString.invokeDynamic(value, env, (Memory[]) null);
if (!result.isString()) {
env.error(ErrorType.E_RECOVERABLE_ERROR, "Method %s must return a string value", entity.methodMagicToString.getSignatureString(false));
return "";
}
return result.toString();
} catch (RuntimeException e) {
throw e;
} catch (Throwable e) {
throw new RuntimeException(e);
} finally {
env.popCall();
}
} else
return "Object";
}
use of php.runtime.env.Environment in project jphp by jphp-compiler.
the class ObjectMemory method unsetOfIndex.
@Override
public void unsetOfIndex(TraceInfo trace, Memory index) {
if (value instanceof ArrayAccess) {
Environment env = value.getEnvironment();
if (env != null && trace != null) {
Memory[] args = new Memory[] { index };
env.pushCall(value, "offsetUnset", args);
try {
((ArrayAccess) value).offsetUnset(env, args);
} finally {
env.popCall();
}
} else
invalidUseAsArray(trace);
} else
invalidUseAsArray(trace);
}
use of php.runtime.env.Environment in project jphp by jphp-compiler.
the class DebugExtension method onRegister.
@Override
public void onRegister(final CompileScope scope) {
if (scope.isDebugMode()) {
String debugIdeKey = "JPHP_DEBUGGER";
if (scope.configuration.containsKey("debug.ideKey")) {
debugIdeKey = scope.configuration.get("debug.ideKey").toString();
}
String debugRootPath = "./src/";
if (scope.configuration.containsKey("debug.rootPath")) {
debugRootPath = scope.configuration.get("debug.rootPath").toString();
}
int debugPort = 9000;
if (scope.configuration.containsKey("debug.port")) {
debugPort = scope.configuration.get("debug.port").toInteger();
}
String debugHost = "127.0.0.1";
if (scope.configuration.containsKey("debug.host")) {
debugHost = scope.configuration.get("debug.host").toString();
}
final String finalDebugIdeKey = debugIdeKey;
final String finalDebugRootPath = debugRootPath;
final int finalDebugPort = debugPort;
final DebugTickHandler tickHandler = new DebugTickHandler();
scope.setTickHandler(tickHandler);
final String finalDebugHost = debugHost;
Thread debuggerThread = new Thread(new Runnable() {
@Override
public void run() {
try {
final Debugger debugger = new Debugger(finalDebugPort, finalDebugHost);
scope.registerProgramShutdownHandler(new ProgramShutdownHandler() {
@Override
public void onShutdown(CompileScope scope, Environment env) {
debugger.shutdown();
}
});
if (debugger.isWorking()) {
debugger.setIdeKey(finalDebugIdeKey);
debugger.setRootPath(finalDebugRootPath);
debugger.run();
}
tickHandler.setDebugger(debugger);
} catch (IOException e) {
throw new DebuggerException(e);
}
}
});
debuggerThread.setName("jphpDebuggerThread");
debuggerThread.start();
}
}
use of php.runtime.env.Environment in project jphp by jphp-compiler.
the class WrapThreadPool method submit.
@Signature({ @Arg(value = "runnable", type = HintType.CALLABLE), @Arg(value = "env", typeClass = "php\\lang\\Environment", optional = @Optional("NULL")) })
public Memory submit(Environment env, Memory... args) {
final Environment _env = args[1].isNull() ? env : args[1].toObject(WrapEnvironment.class).getWrapEnvironment();
final Invoker invoker = Invoker.valueOf(_env, null, args[0]);
Future<Memory> future = service.submit(new Callable<Memory>() {
@Override
public Memory call() throws Exception {
return invoker.callNoThrow();
}
});
return new ObjectMemory(new WrapFuture(env, future));
}
Aggregations