public override bool HasMethod(EvaluationContext gctx, object targetType, string methodName, object[] argTypes, BindingFlags flags) { SoftEvaluationContext ctx = (SoftEvaluationContext)gctx; TypeMirror[] types; if (argTypes == null) { types = new TypeMirror [0]; } else { types = new TypeMirror [argTypes.Length]; for (int n = 0; n < argTypes.Length; n++) { if (argTypes [n] is TypeMirror) { types [n] = (TypeMirror)argTypes [n]; } else { types [n] = (TypeMirror)GetType(ctx, ((Type)argTypes[n]).FullName); } } } MethodMirror met = OverloadResolve(ctx, methodName, (TypeMirror)targetType, types, (flags & BindingFlags.Instance) != 0, (flags & BindingFlags.Static) != 0, false); return(met != null); }
public MethodCall(SoftEvaluationContext ctx, MethodMirror function, object obj, Value[] args) { this.ctx = ctx; this.function = function; this.obj = obj; this.args = args; }
static bool IsApplicable(SoftEvaluationContext ctx, MethodMirror method, TypeMirror[] types, out string error, out int matchCount) { ParameterInfoMirror[] mparams = method.GetParameters(); matchCount = 0; for (int i = 0; i < types.Length; i++) { TypeMirror param_type = mparams[i].ParameterType; if (param_type.FullName == types [i].FullName) { matchCount++; continue; } if (param_type.IsAssignableFrom(types [i])) { continue; } error = String.Format( "Argument {0}: Cannot implicitly convert `{1}' to `{2}'", i, types [i].FullName, param_type.FullName); return(false); } error = null; return(true); }
public void SetValue(SoftEvaluationContext ctx, LocalVariable variable, Value value) { var canUpdateCache = values != null && version == ctx.Session.StackVersion; ctx.Frame.SetValue(variable, value); ctx.Session.StackVersion++; if (canUpdateCache) { for (int i = 0; i < variables.Length; i++) { if (variable == variables[i]) { values[i] = value; break; } } version = ctx.Session.StackVersion; } else { values = null; version = 0; } }
public static MethodMirror OverloadResolve(SoftEvaluationContext ctx, string methodName, TypeMirror type, TypeMirror[] argtypes, bool allowInstance, bool allowStatic, bool throwIfNotFound) { List <MethodMirror> candidates = new List <MethodMirror> (); TypeMirror currentType = type; while (currentType != null) { foreach (MethodMirror met in currentType.GetMethods()) { if (met.Name == methodName) { ParameterInfoMirror[] pars = met.GetParameters(); if (pars.Length == argtypes.Length && (met.IsStatic && allowStatic || !met.IsStatic && allowInstance)) { candidates.Add(met); } } } if (methodName == ".ctor") { break; // Can't create objects using constructor from base classes } currentType = currentType.BaseType; } return(OverloadResolve(ctx, type.Name, methodName, argtypes, candidates, throwIfNotFound)); }
public override void CopyFrom(EvaluationContext ctx) { base.CopyFrom(ctx); SoftEvaluationContext other = (SoftEvaluationContext)ctx; frame = other.frame; stackVersion = other.stackVersion; Thread = other.Thread; session = other.session; }
public override string CallToString(EvaluationContext ctx, object obj) { SoftEvaluationContext cx = (SoftEvaluationContext)ctx; if (obj is StringMirror) { return(((StringMirror)obj).Value); } else if (obj is EnumMirror) { EnumMirror eob = (EnumMirror)obj; return(eob.StringValue); } else if (obj is PrimitiveValue) { return(((PrimitiveValue)obj).Value.ToString()); } else if ((obj is StructMirror) && ((StructMirror)obj).Type.IsPrimitive) { // Boxed primitive StructMirror sm = (StructMirror)obj; if (sm.Fields.Length > 0 && (sm.Fields[0] is PrimitiveValue)) { return(((PrimitiveValue)sm.Fields[0]).Value.ToString()); } } else if (obj == null) { return(string.Empty); } else if ((obj is ObjectMirror) && cx.Options.AllowTargetInvoke) { ObjectMirror ob = (ObjectMirror)obj; MethodMirror method = OverloadResolve(cx, "ToString", ob.Type, new TypeMirror[0], true, false, false); if (method != null && method.DeclaringType.FullName != "System.Object") { StringMirror res = cx.RuntimeInvoke(method, obj, new Value[0]) as StringMirror; return(res != null ? res.Value : string.Empty); } } else if ((obj is StructMirror) && cx.Options.AllowTargetInvoke) { StructMirror ob = (StructMirror)obj; MethodMirror method = OverloadResolve(cx, "ToString", ob.Type, new TypeMirror[0], true, false, false); if (method != null && method.DeclaringType.FullName != "System.ValueType") { StringMirror res = cx.RuntimeInvoke(method, obj, new Value[0]) as StringMirror; return(res != null ? res.Value : string.Empty); } } return(GetDisplayTypeName(GetValueTypeName(ctx, obj))); }
public override object CreateValue(EvaluationContext ctx, object value) { SoftEvaluationContext cx = (SoftEvaluationContext)ctx; if (value is string) { return(cx.Thread.Domain.CreateString((string)value)); } else { return(cx.Session.VirtualMachine.CreateValue(value)); } }
public override string[] GetImportedNamespaces(EvaluationContext ctx) { SoftEvaluationContext cx = (SoftEvaluationContext)ctx; HashSet <string> namespaces = new HashSet <string> (); foreach (TypeMirror type in cx.Session.GetAllTypes()) { namespaces.Add(type.Namespace); } string[] nss = new string [namespaces.Count]; namespaces.CopyTo(nss); return(nss); }
public override bool IsExternalType(EvaluationContext gctx, object type) { SoftEvaluationContext ctx = (SoftEvaluationContext)gctx; TypeMirror tm = type as TypeMirror; if (tm != null) { return(ctx.Session.IsExternalCode(tm)); } else { return(true); } }
public override ValueReference GetThisReference(EvaluationContext ctx) { try { SoftEvaluationContext cx = (SoftEvaluationContext)ctx; if (cx.Frame.Method.IsStatic) { return(null); } Value val = cx.Frame.GetThis(); return(LiteralValueReference.CreateTargetObjectLiteral(ctx, "this", val)); } catch (AbsentInformationException) { return(null); } }
void SetValue(SoftEvaluationContext ctx, object value) { if (batch != null) { batch.SetValue(ctx, variable, (Value)value); } else { ctx.Frame.SetValue(variable, (Value)value); ctx.Session.StackVersion++; } version = ctx.Session.StackVersion; this.value = (Value)value; }
object GetValue(SoftEvaluationContext ctx) { try { if (value == null) { value = batch != null?batch.GetValue(variable) : ctx.Frame.GetValue(variable); } return(NormalizeValue(ctx, value)); } catch (AbsentInformationException ex) { throw new EvaluatorException(ex, "Value not available"); } catch (Exception ex) { throw new EvaluatorException(ex.Message); } }
public override ValueReference GetLocalVariable(EvaluationContext ctx, string name) { try { SoftEvaluationContext cx = (SoftEvaluationContext)ctx; LocalVariable local = cx.Frame.Method.GetLocal(name); if (local != null) { return(new VariableValueReference(ctx, name, local)); } else { return(null); } } catch (AbsentInformationException) { return(null); } }
public override ValueReference GetCurrentException(EvaluationContext ctx) { try { SoftEvaluationContext cx = (SoftEvaluationContext)ctx; ObjectMirror exc = cx.Session.GetExceptionObject(cx.Thread); if (exc != null) { return(LiteralValueReference.CreateTargetObjectLiteral(ctx, ctx.Options.CurrentExceptionTag, exc)); } else { return(null); } } catch (AbsentInformationException) { return(null); } }
string EvaluateExpression(ThreadMirror thread, string exp) { try { MDB.StackFrame[] frames = thread.GetFrames(); if (frames.Length == 0) { return(string.Empty); } EvaluationOptions ops = Options.EvaluationOptions; ops.AllowTargetInvoke = true; SoftEvaluationContext ctx = new SoftEvaluationContext(this, frames[0], ops); ValueReference val = ctx.Evaluator.Evaluate(ctx, exp); return(val.CreateObjectValue(false).Value); } catch (Exception ex) { OnDebuggerOutput(true, ex.ToString()); return(string.Empty); } }
public override IEnumerable <ValueReference> GetLocalVariables(EvaluationContext ctx) { SoftEvaluationContext cx = (SoftEvaluationContext)ctx; LocalVariable[] locals; try { locals = cx.Frame.Method.GetLocals(); } catch (AbsentInformationException) { yield break; } foreach (LocalVariable var in locals) { if (!var.IsArg) { yield return(new VariableValueReference(ctx, var.Name, var)); } } }
public override void GetNamespaceContents(EvaluationContext ctx, string namspace, out string[] childNamespaces, out string[] childTypes) { SoftEvaluationContext cx = (SoftEvaluationContext)ctx; HashSet <string> types = new HashSet <string> (); HashSet <string> namespaces = new HashSet <string> (); string namspacePrefix = namspace.Length > 0 ? namspace + "." : ""; foreach (TypeMirror type in cx.Session.GetAllTypes()) { if (type.Namespace == namspace || type.Namespace.StartsWith(namspacePrefix)) { namespaces.Add(type.Namespace); types.Add(type.FullName); } } childNamespaces = new string [namespaces.Count]; namespaces.CopyTo(childNamespaces); childTypes = new string [types.Count]; types.CopyTo(childTypes); }
public Value GetValue(SoftEvaluationContext ctx, LocalVariable variable) { if (variable == null) { throw new ArgumentNullException("variable"); } if (values == null || version != ctx.Session.StackVersion) { values = ctx.Frame.GetValues(variables); version = ctx.Session.StackVersion; } for (int i = 0; i < variables.Length; i++) { if (variable == variables[i]) { return(values[i]); } } throw new ArgumentOutOfRangeException("variable"); }
public override object CreateValue(EvaluationContext ctx, object type, params object[] args) { ctx.AssertTargetInvokeAllowed(); SoftEvaluationContext cx = (SoftEvaluationContext)ctx; TypeMirror t = (TypeMirror)type; TypeMirror[] types = new TypeMirror [args.Length]; for (int n = 0; n < args.Length; n++) { types [n] = ToTypeMirror(ctx, GetValueType(ctx, args [n])); } Value[] values = new Value[args.Length]; for (int n = 0; n < args.Length; n++) { values[n] = (Value)args [n]; } MethodMirror ctor = OverloadResolve(cx, ".ctor", t, types, true, true, true); return(t.NewInstance(cx.Thread, ctor, values)); }
public override object ForceLoadType(EvaluationContext gctx, string typeName) { // Shortcut to avoid a target invoke in case the type is already loaded object t = GetType(gctx, typeName); if (t != null) { return(t); } SoftEvaluationContext ctx = (SoftEvaluationContext)gctx; if (!ctx.Options.AllowTargetInvoke) { return(null); } TypeMirror tm = (TypeMirror)ctx.Thread.Type.GetTypeObject().Type; TypeMirror stype = ctx.Session.GetType("System.String"); if (stype == null) { // If the string type is not loaded, we need to get it in another way StringMirror ss = ctx.Thread.Domain.CreateString(""); stype = ss.Type; } TypeMirror[] ats = new TypeMirror[] { stype }; MethodMirror met = OverloadResolve(ctx, "GetType", tm, ats, false, true, true); try { tm.InvokeMethod(ctx.Thread, met, new Value[] { (Value)CreateValue(ctx, typeName) }); } catch { return(null); } finally { ctx.Session.StackVersion++; } return(GetType(ctx, typeName)); }
public override object RuntimeInvoke(EvaluationContext gctx, object targetType, object target, string methodName, object[] argTypes, object[] argValues) { SoftEvaluationContext ctx = (SoftEvaluationContext)gctx; ctx.AssertTargetInvokeAllowed(); TypeMirror type = target != null ? ((ObjectMirror)target).Type : (TypeMirror)targetType; TypeMirror[] types = new TypeMirror [argTypes.Length]; for (int n = 0; n < argTypes.Length; n++) { types [n] = ToTypeMirror(ctx, argTypes [n]); } Value[] values = new Value[argValues.Length]; for (int n = 0; n < argValues.Length; n++) { values[n] = (Value)argValues [n]; } MethodMirror method = OverloadResolve(ctx, methodName, type, types, target != null, target == null, true); return(ctx.RuntimeInvoke(method, target ?? targetType, values)); }
internal string [] GetTupleElementNames(SoftEvaluationContext ctx) { return(ctx.Session.GetPdbData(variable.Method)?.GetTupleElementNames(variable.Method, variable.Index)); }
static MethodMirror OverloadResolve(SoftEvaluationContext ctx, string typeName, string methodName, TypeMirror[] argtypes, List <MethodMirror> candidates, bool throwIfNotFound) { if (candidates.Count == 1) { string error; int matchCount; if (IsApplicable(ctx, candidates[0], argtypes, out error, out matchCount)) { return(candidates [0]); } if (throwIfNotFound) { throw new EvaluatorException("Invalid arguments for method `{0}': {1}", methodName, error); } else { return(null); } } if (candidates.Count == 0) { if (throwIfNotFound) { throw new EvaluatorException("Method `{0}' not found in type `{1}'.", methodName, typeName); } else { return(null); } } // Ok, now we need to find an exact match. MethodMirror match = null; int bestCount = -1; bool repeatedBestCount = false; foreach (MethodMirror method in candidates) { string error; int matchCount; if (!IsApplicable(ctx, method, argtypes, out error, out matchCount)) { continue; } if (matchCount == bestCount) { repeatedBestCount = true; } else if (matchCount > bestCount) { match = method; bestCount = matchCount; repeatedBestCount = false; } } if (match == null) { if (!throwIfNotFound) { return(null); } if (methodName != null) { throw new EvaluatorException("Invalid arguments for method `{0}'.", methodName); } else { throw new EvaluatorException("Invalid arguments for indexer."); } } if (repeatedBestCount) { // If there is an ambiguous match, just pick the first match. If the user was expecting // something else, he can provide more specific arguments /* if (!throwIfNotFound) * return null; * if (methodName != null) * throw new EvaluatorException ("Ambiguous method `{0}'; need to use full name", methodName); * else * throw new EvaluatorException ("Ambiguous arguments for indexer.", methodName); */ } return(match); }
public override object TryCast(EvaluationContext ctx, object obj, object targetType) { SoftEvaluationContext cx = (SoftEvaluationContext)ctx; if (obj == null) { return(null); } object valueType = GetValueType(ctx, obj); if (valueType is TypeMirror) { if ((targetType is TypeMirror) && ((TypeMirror)targetType).IsAssignableFrom((TypeMirror)valueType)) { return(obj); } // Try casting the primitive type of the enum EnumMirror em = obj as EnumMirror; if (em != null) { return(TryCast(ctx, CreateValue(ctx, em.Value), targetType)); } } else if (valueType is Type) { if (targetType is TypeMirror) { TypeMirror tm = (TypeMirror)targetType; if (tm.IsEnum) { PrimitiveValue casted = TryCast(ctx, obj, tm.EnumUnderlyingType) as PrimitiveValue; if (casted == null) { return(null); } return(cx.Session.VirtualMachine.CreateEnumMirror(tm, casted)); } targetType = Type.GetType(((TypeMirror)targetType).FullName, false); } Type tt = targetType as Type; if (tt != null) { if (tt.IsAssignableFrom((Type)valueType)) { return(obj); } if (obj is PrimitiveValue) { obj = ((PrimitiveValue)obj).Value; } if (tt != typeof(string) && !(obj is string)) { try { if (obj == null) { return(null); } object res = System.Convert.ChangeType(obj, tt); return(CreateValue(ctx, res)); } catch { } } } } return(null); }
string EvaluateExpression (ThreadMirror thread, string exp) { try { MDB.StackFrame[] frames = thread.GetFrames (); if (frames.Length == 0) return string.Empty; EvaluationOptions ops = Options.EvaluationOptions; ops.AllowTargetInvoke = true; SoftEvaluationContext ctx = new SoftEvaluationContext (this, frames[0], ops); ValueReference val = ctx.Evaluator.Evaluate (ctx, exp); return val.CreateObjectValue (false).Value; } catch (Exception ex) { OnDebuggerOutput (true, ex.ToString ()); return string.Empty; } }
public override object GetEnclosingType(EvaluationContext ctx) { SoftEvaluationContext cx = (SoftEvaluationContext)ctx; return(cx.Frame.Method.DeclaringType); }
public ThisValueReference(SoftEvaluationContext ctx) : base(ctx) { this.ctx = ctx; }
public override object GetType(EvaluationContext ctx, string name, object[] typeArgs) { SoftEvaluationContext cx = (SoftEvaluationContext)ctx; int i = name.IndexOf(','); if (i != -1) { // Find first comma outside brackets int nest = 0; for (int n = 0; n < name.Length; n++) { char c = name [n]; if (c == '[') { nest++; } else if (c == ']') { nest--; } else if (c == ',' && nest == 0) { name = name.Substring(0, n).Trim(); break; } } } if (typeArgs != null && typeArgs.Length > 0) { string args = ""; foreach (object t in typeArgs) { if (args.Length > 0) { args += ","; } string tn; if (t is TypeMirror) { TypeMirror atm = (TypeMirror)t; tn = atm.FullName + "," + atm.Assembly.GetName(); } else { Type atm = (Type)t; tn = atm.FullName + "," + atm.Assembly.GetName(); } if (tn.IndexOf(',') != -1) { tn = "[" + tn + "]"; } args += tn; } name += "[" + args + "]"; } TypeMirror tm = cx.Session.GetType(name); if (tm != null) { return(tm); } foreach (AssemblyMirror asm in cx.Thread.Domain.GetAssemblies()) { tm = asm.GetType(name, false, false); if (tm != null) { return(tm); } } return(null); }
protected override TypeDisplayData OnGetTypeDisplayData(EvaluationContext gctx, object type) { SoftEvaluationContext ctx = (SoftEvaluationContext)gctx; TypeDisplayData td = new TypeDisplayData(); try { TypeMirror t = (TypeMirror)type; foreach (CustomAttributeDataMirror attr in t.GetCustomAttributes(true)) { string attName = attr.Constructor.DeclaringType.FullName; if (attName == "System.Diagnostics.DebuggerDisplayAttribute") { DebuggerDisplayAttribute at = BuildAttribute <DebuggerDisplayAttribute> (attr); td.NameDisplayString = at.Name; td.TypeDisplayString = at.Type; td.ValueDisplayString = at.Value; } else if (attName == "System.Diagnostics.DebuggerTypeProxyAttribute") { DebuggerTypeProxyAttribute at = BuildAttribute <DebuggerTypeProxyAttribute> (attr); td.ProxyType = at.ProxyTypeName; if (!string.IsNullOrEmpty(td.ProxyType)) { ForceLoadType(ctx, td.ProxyType); } } } foreach (FieldInfoMirror fi in t.GetFields()) { CustomAttributeDataMirror[] attrs = fi.GetCustomAttributes(true); DebuggerBrowsableAttribute att = GetAttribute <DebuggerBrowsableAttribute> (attrs); if (att == null) { var cga = GetAttribute <System.Runtime.CompilerServices.CompilerGeneratedAttribute> (attrs); if (cga != null) { att = new DebuggerBrowsableAttribute(DebuggerBrowsableState.Never); } } if (att != null) { if (td.MemberData == null) { td.MemberData = new Dictionary <string, DebuggerBrowsableState> (); } td.MemberData [fi.Name] = att.State; } } foreach (PropertyInfoMirror pi in t.GetProperties()) { DebuggerBrowsableAttribute att = GetAttribute <DebuggerBrowsableAttribute> (pi.GetCustomAttributes(true)); if (att != null) { if (td.MemberData == null) { td.MemberData = new Dictionary <string, DebuggerBrowsableState> (); } td.MemberData [pi.Name] = att.State; } } } catch (Exception ex) { ctx.Session.WriteDebuggerOutput(true, ex.ToString()); } return(td); }