use of php.runtime.env.TraceInfo in project jphp by jphp-compiler.
the class TokenizerTest method testParseError.
@Test
public void testParseError() throws IOException {
Throwable ex = null;
Tokenizer tokenizer = new Tokenizer(new Context(" 'foobar \n "));
try {
tokenizer.nextToken();
} catch (Throwable e) {
ex = e;
}
assertTrue(ex instanceof ParseException);
TraceInfo traceInfo = ((ParseException) ex).getTraceInfo();
assertNotNull(traceInfo);
assertNull(traceInfo.getContext().getFileName());
assertEquals(1, traceInfo.getStartLine());
assertEquals(1, traceInfo.getEndLine());
assertEquals(1, traceInfo.getStartPosition());
assertEquals(1, traceInfo.getEndPosition());
}
use of php.runtime.env.TraceInfo in project jphp by jphp-compiler.
the class StackGetCommand method run.
@Override
public void run(Debugger context, CommandArguments args, Document result) {
DebugTick tick = context.getRegisteredTick();
Element response = createResponse(args, result);
CallStack callStack = tick.getCallStack();
List<CallStackItem> list = new ArrayList<>();
Collections.addAll(list, callStack.getSnapshot());
CallStackItem last = list.get(list.size() - 1);
if (list.size() > 1) {
CallStackItem prevLast = list.get(list.size() - 2);
TraceInfo trace = prevLast.getTrace();
prevLast.setTrace(new TraceInfo(trace.getContext(), last.getTrace().getStartLine(), trace.getEndLine(), last.getTrace().getStartPosition(), trace.getEndLine()));
}
last.setTrace(tick.getTrace());
Collections.reverse(list);
int depth = args.containsKey("d") ? Integer.parseInt(args.get("d")) : -1;
if (depth > -1) {
list = list.subList(0, depth + 1);
}
int i = 0;
for (CallStackItem stackItem : list) {
Element stack = result.createElement("stack");
stack.setAttribute("level", String.valueOf(i));
stack.setAttribute("type", "file");
stack.setAttribute("filename", context.getFileName(stackItem.trace.getFileName()));
stack.setAttribute("lineno", String.valueOf(stackItem.trace.getStartLine() + 1));
stack.setAttribute("where", stackItem.getWhere());
response.appendChild(stack);
i++;
}
}
use of php.runtime.env.TraceInfo in project jphp by jphp-compiler.
the class FileObject method find.
@Signature(@Arg(value = "filter", optional = @Optional("NULL")))
public Memory find(final Environment env, Memory... args) {
if (args[0].isNull()) {
return ArrayMemory.ofStrings(file.list()).toConstant();
} else {
final Invoker invoker = Invoker.valueOf(env, null, args[0]);
if (invoker == null) {
exception(env, "Invalid filter value, must be callable");
return Memory.NULL;
}
final TraceInfo trace = env.trace();
invoker.setTrace(trace);
String[] result = file.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
FileObject o = new FileObject(env, __class__, dir);
Memory[] args = new Memory[] { new ObjectMemory(o), new StringMemory(name) };
return invoker.callNoThrow(args).toBoolean();
}
});
return ArrayMemory.ofStrings(result);
}
}
use of php.runtime.env.TraceInfo 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.TraceInfo in project jphp by jphp-compiler.
the class ClassStmtCompiler method writeInitStatic.
@SuppressWarnings("unchecked")
protected void writeInitStatic() {
MethodNode node = new MethodNodeImpl();
node.access = ACC_STATIC;
node.name = Constants.STATIC_INIT_METHOD;
node.desc = Type.getMethodDescriptor(Type.getType(void.class));
MethodStmtCompiler methodCompiler = new MethodStmtCompiler(this, node);
ExpressionStmtCompiler expressionCompiler = new ExpressionStmtCompiler(methodCompiler, null);
methodCompiler.writeHeader();
// trace list
expressionCompiler.writePushSmallInt(traceList.size());
node.instructions.add(new TypeInsnNode(ANEWARRAY, Type.getInternalName(TraceInfo.class)));
expressionCompiler.stackPush(Memory.Type.REFERENCE);
int i = 0;
for (TraceInfo traceInfo : traceList) {
expressionCompiler.writePushDup();
expressionCompiler.writePushSmallInt(i);
expressionCompiler.writePushCreateTraceInfo(traceInfo.getStartLine(), traceInfo.getStartPosition());
node.instructions.add(new InsnNode(AASTORE));
expressionCompiler.stackPop();
expressionCompiler.stackPop();
i++;
}
expressionCompiler.writePutStatic("$TRC", TraceInfo[].class);
// memory constants
expressionCompiler.writePushSmallInt(memoryConstants.size());
node.instructions.add(new TypeInsnNode(ANEWARRAY, Type.getInternalName(Memory.class)));
expressionCompiler.stackPush(Memory.Type.REFERENCE);
i = 0;
for (Memory memory : memoryConstants) {
expressionCompiler.writePushDup();
expressionCompiler.writePushSmallInt(i);
expressionCompiler.writePushMemory(memory);
expressionCompiler.writePopBoxing(true, false);
node.instructions.add(new InsnNode(AASTORE));
expressionCompiler.stackPop();
expressionCompiler.stackPop();
i++;
}
expressionCompiler.writePutStatic("$MEM", Memory[].class);
// memory array constants
expressionCompiler.writePushSmallInt(memoryArrayConstants.size());
node.instructions.add(new TypeInsnNode(ANEWARRAY, Type.getInternalName(Memory[].class)));
expressionCompiler.stackPush(Memory.Type.REFERENCE);
i = 0;
for (Collection<Memory> memories : memoryArrayConstants) {
expressionCompiler.writePushDup();
expressionCompiler.writePushSmallInt(i);
expressionCompiler.writePushParameters(memories);
node.instructions.add(new InsnNode(AASTORE));
expressionCompiler.stackPop();
expressionCompiler.stackPop();
i++;
}
expressionCompiler.writePutStatic("$AMEM", Memory[][].class);
// cached calls
expressionCompiler.writePushNewObject(FunctionCallCache.class);
expressionCompiler.writePutStatic("$CALL_FUNC_CACHE", FunctionCallCache.class);
expressionCompiler.writePushNewObject(MethodCallCache.class);
expressionCompiler.writePutStatic("$CALL_METH_CACHE", MethodCallCache.class);
expressionCompiler.writePushNewObject(ConstantCallCache.class);
expressionCompiler.writePutStatic("$CALL_CONST_CACHE", ConstantCallCache.class);
expressionCompiler.writePushNewObject(PropertyCallCache.class);
expressionCompiler.writePutStatic("$CALL_PROP_CACHE", PropertyCallCache.class);
expressionCompiler.writePushNewObject(ClassCallCache.class);
expressionCompiler.writePutStatic("$CALL_CLASS_CACHE", ClassCallCache.class);
node.instructions.add(new InsnNode(RETURN));
methodCompiler.writeFooter();
this.node.methods.add(node);
}
Aggregations