public ILMethod(MethodDefinition def, ILType type, ILRuntime.Runtime.Enviorment.AppDomain domain, int flags) { this.def = def; declaringType = type; this.jitFlags = flags; if (def.ReturnType.IsGenericParameter) { ReturnType = FindGenericArgument(def.ReturnType.Name); } else { ReturnType = domain.GetType(def.ReturnType, type, this); } if (type.IsDelegate && def.Name == "Invoke") { isDelegateInvoke = true; } this.appdomain = domain; paramCnt = def.HasParameters ? def.Parameters.Count : 0; if (def.HasCustomAttributes) { for (int i = 0; i < def.CustomAttributes.Count; i++) { int f; if (def.CustomAttributes[i].GetJITFlags(domain, out f)) { this.jitFlags = f; break; } } } jitImmediately = (jitFlags & ILRuntimeJITFlags.JITImmediately) == ILRuntimeJITFlags.JITImmediately; jitOnDemand = (jitFlags & ILRuntimeJITFlags.JITOnDemand) == ILRuntimeJITFlags.JITOnDemand; #if DEBUG && !DISABLE_ILRUNTIME_DEBUG if (def.HasBody) { var sp = GetValidSequence(0, 1); if (sp != null) { StartLine = sp.StartLine; sp = GetValidSequence(def.Body.Instructions.Count - 1, -1); if (sp != null) { EndLine = sp.EndLine; } } } #endif }
public ILRuntimeFieldInfo(FieldDefinition def, ILRuntimeType declaredType, bool isStatic, int fieldIdx) { definition = def; this.name = def.Name; dType = declaredType; ilType = dType.ILType; appdomain = ilType.AppDomain; this.isStatic = isStatic; this.fieldIdx = fieldIdx; if (isStatic) { attr |= System.Reflection.FieldAttributes.Static; } fieldType = isStatic ? ilType.StaticFieldTypes[fieldIdx] : ilType.FieldTypes[fieldIdx]; }
private void InitBaseType() { lock (Environment) { if (isBaseTypeInit) { return; } if (typeForCLR.BaseType != null) { baseType = Environment.GetType(typeForCLR.BaseType); } isBaseTypeInit = true; } }
public ILRuntimeFieldInfo(FieldDefinition def, ILRuntimeType declaredType, int fieldIdx, IType fieldType) { definition = def; this.name = def.Name; dType = declaredType; ilType = dType.ILType; appdomain = ilType.AppDomain; this.isStatic = false; this.fieldIdx = fieldIdx; if (isStatic) { attr |= System.Reflection.FieldAttributes.Static; } this.fieldType = fieldType; }
/// <summary> /// Constructor /// </summary> /// <param name="method">Method</param> public NeoMethod(ILMethod method) { this.method = method; this.type = method.type; foreach (var attr in method.method.CustomAttributes) { ProcessAttribute(attr); } _namespace = method.method.DeclaringType.FullName; name = method.method.FullName; displayName = method.method.Name; inSmartContract = method.method.DeclaringType.BaseType.Name == "SmartContract"; isPublic = method.method.IsPublic; }
public static void EmitExplicit(ILGenerator generator, ILType onStackIL, ILType otherIL) { if (otherIL != onStackIL) { switch (otherIL) { case ILType.I1: generator.Emit(OpCodes.Conv_I1); break; case ILType.I2: generator.Emit(OpCodes.Conv_I2); break; case ILType.I4: generator.Emit(OpCodes.Conv_I4); break; case ILType.I8: generator.Emit(OpCodes.Conv_I8); break; case ILType.U1: generator.Emit(OpCodes.Conv_U1); break; case ILType.U2: generator.Emit(OpCodes.Conv_U2); break; case ILType.U4: generator.Emit(OpCodes.Conv_U4); break; case ILType.U8: generator.Emit(OpCodes.Conv_U8); break; case ILType.R4: generator.Emit(OpCodes.Conv_R4); break; case ILType.R8: generator.Emit(OpCodes.Conv_R8); break; } } }
/// <summary> /// 从流加载Assembly,以及symbol符号文件(pdb) /// </summary> /// <param name="stream">Assembly Stream</param> /// <param name="symbol">symbol Stream</param> /// <param name="symbolReader">symbol 读取器</param> public void LoadAssembly(System.IO.Stream stream, System.IO.Stream symbol, ISymbolReaderProvider symbolReader) { var module = ModuleDefinition.ReadModule(stream); //从MONO中加载模块 if (symbolReader != null && symbol != null) { module.ReadSymbols(symbolReader.GetSymbolReader(module, symbol)); //加载符号表 } if (module.HasAssemblyReferences) //如果此模块引用了其他模块 { foreach (var ar in module.AssemblyReferences) { /*if (moduleref.Contains(ar.Name) == false) * moduleref.Add(ar.Name); * if (moduleref.Contains(ar.FullName) == false) * moduleref.Add(ar.FullName);*/ } } if (module.HasTypes) { List <ILType> types = new List <ILType>(); foreach (var t in module.GetTypes()) //获取所有此模块定义的类型 { ILType type = new ILType(t, this); mapType[t.FullName] = type; types.Add(type); } } if (voidType == null) { voidType = GetType("System.Void"); intType = GetType("System.Int32"); longType = GetType("System.Int64"); boolType = GetType("System.Boolean"); floatType = GetType("System.Single"); doubleType = GetType("System.Double"); objectType = GetType("System.Object"); } module.AssemblyResolver.ResolveFailure += AssemblyResolver_ResolveFailure; #if DEBUG debugService.NotifyModuleLoaded(module.Name); #endif }
void TryBindBreakpoint(CSBindBreakpoint msg) { var domain = ds.AppDomain; SCBindBreakpointResult res = new Protocol.SCBindBreakpointResult(); res.BreakpointHashCode = msg.BreakpointHashCode; IType type; if (domain.LoadedTypes.TryGetValue(msg.TypeName, out type)) { if (type is ILType) { ILType it = (ILType)type; ILMethod found = null; foreach (var i in it.GetMethods()) { if (i.Name == msg.MethodName) { ILMethod ilm = (ILMethod)i; if (ilm.StartLine <= (msg.StartLine + 1) && ilm.EndLine >= (msg.StartLine + 1)) { found = ilm; break; } } } if (found != null) { ds.SetBreakPoint(found.GetHashCode(), msg.BreakpointHashCode, msg.StartLine); res.Result = BindBreakpointResults.OK; } else { res.Result = BindBreakpointResults.CodeNotFound; } } else { res.Result = BindBreakpointResults.TypeNotFound; } } else { res.Result = BindBreakpointResults.TypeNotFound; } SendSCBindBreakpointResult(res); }
static void InitByProperty(System.Type attType, string name, List <IType> types) { string autoAttName = attType.FullName; List <IMethod> calls = new List <IMethod>(); foreach (var itor in types) { ILType ilType = itor as ILType; if (ilType == null) { continue; } TypeDefinition td = ilType.TypeDefinition; foreach (var att in td.CustomAttributes) { if (att.AttributeType.FullName == autoAttName) { var type = ilType.TypeForCLR; var method = ilType.GetMethod(name, 0); if (method == null) { continue; } if (!method.IsStatic) { wxb.L.LogErrorFormat("hot type:{0} method:{1} not static!", ilType.FullName, name); continue; } calls.Add(method); break; } } } for (int i = 0; i < calls.Count; ++i) { try { appdomain.Invoke(calls[i], null); } catch (System.Exception ex) { wxb.L.LogException(ex); } } }
void InitializeFields(ILType type) { for (int i = 0; i < type.FieldTypes.Length; i++) { var idx = type.FieldStartIndex + i; var ft = type.FieldTypes[i]; if (ft.IsValueType && idx < 64) { valueTypeMask |= (ulong)1 << idx; } StackObject.Initialized(ref fields[idx], idx, ft, managedObjs); } if (type.BaseType != null && type.BaseType is ILType) { InitializeFields((ILType)type.BaseType); } }
public ILMethod(MethodDefinition def, ILType type, ILRuntime.Runtime.Enviorment.AppDomain domain) { this.def = def; declaringType = type; if (def.ReturnType.IsGenericParameter) { ReturnType = FindGenericArgument(def.ReturnType.Name); } else { ReturnType = domain.GetType(def.ReturnType, type); } if (type.IsDelegate && def.Name == "Invoke") { isDelegateInvoke = true; } this.appdomain = domain; }
public override bool IsAssignableFrom(Type c) { IType type; if (c is ILRuntimeWrapperType) { type = ((ILRuntimeWrapperType)c).CLRType; } else if (c is ILRuntimeType) { type = ((ILRuntimeType)c).ILType; } else { type = ILType.AppDomain.GetType(c); } return(type.CanAssignTo(ILType)); }
public static bool GetTypeWeakMatchScore(ILType type, ITypeSymbol symbol, out int score) { var genericSymbol = symbol as GenericSymbol; if (genericSymbol != null) { int totalScore; if (!GetTypeWeakMatchScore(type.GenericTypeDefinition, genericSymbol.Element, out totalScore)) { return(Result(out score, -1)); } var genericArguments = type.GenericArguments; for (var i = 0; i < genericArguments.Count; i++) { int genericScore; if (!GetTypeWeakMatchScore(genericArguments[i], genericSymbol.GenericParameters[i], out genericScore)) { return(Result(out score, -1)); } totalScore += genericScore; } return(Result(out score, totalScore)); } var componentSymbol = symbol as ComponentSymbol; if (componentSymbol != null) { return(GetTypeWeakMatchScore(type.ElementType, componentSymbol.Element, out score)); } var nameSymbol = symbol as NameSymbol; if (nameSymbol != null) { if (type.FullName != nameSymbol.Name) { return(Result(out score, -1)); } score = GetAssemblyWeakMatchScore(type.AssemblyName, symbol.AssemblyName); return(true); } return(Result(out score, -1)); }
static FieldDefinition FindStaticField(ILType type, string name) { var StaticFieldDefinitions = type.StaticFieldDefinitions; if (StaticFieldDefinitions == null) { return(null); } foreach (var ator in StaticFieldDefinitions) { if (ator.Name == name) { return(ator); } } return(null); }
private ILType CacheTypeInternal(ILType type) { var clrType = type as CLRType; if (clrType != null) { //todo 这里还要区分泛型参数是RuntimeType的情况 TypeToILType[type.TypeForCLR] = type; } var runtimeType = type as RuntimeType; if (runtimeType != null) { TypeReferenceToTypes[runtimeType.TypeReference.GetHashCode()] = type; } IdToType[type.Id] = type; NameToTypes.Add(type.FullName, type); return(type); }
public ILTypeInstance(ILType type) { this.type = type; fields = new StackObject[type.TotalFieldCount]; managedObjs = new List <object>(fields.Length); for (int i = 0; i < fields.Length; i++) { managedObjs.Add(null); } InitializeFields(type); if (type.BaseType is Enviorment.CrossBindingAdaptor) { clrInstance = ((Enviorment.CrossBindingAdaptor)type.BaseType).CreateCLRInstance(type.AppDomain, this); } else { clrInstance = this; } }
public ILTypeInstance(ILType type, bool initializeCLRInstance = true) { this.type = type; fields = new StackObject[type.TotalFieldCount]; managedObjs = new List <object>(fields.Length); for (int i = 0; i < fields.Length; i++) { managedObjs.Add(null); } InitializeFields(type); if (initializeCLRInstance) { if (type.FirstCLRBaseType is Enviorment.CrossBindingAdaptor) { clrInstance = ((Enviorment.CrossBindingAdaptor)type.FirstCLRBaseType).CreateCLRInstance(type.AppDomain, this); } else { clrInstance = this; } if (type.Implements != null) { foreach (var i in type.Implements) { if (i is Enviorment.CrossBindingAdaptor) { if (clrInstance != this)//Only one CLRInstance is allowed atm, so implementing multiple interfaces is not supported { throw new NotSupportedException("Inheriting and implementing interface at the same time is not supported yet"); } clrInstance = ((Enviorment.CrossBindingAdaptor)i).CreateCLRInstance(type.AppDomain, this); break; } } } } else { clrInstance = this; } }
override public object GetCSLEObjectParmasByType(IType _type, params object[] _parmas) { if (_type == null) { throw new NullReferenceException("LS GetCSLEObjectParmasByType _type = null"); } ILType ilType = _type as ILType; if (ilType != null) { bool hasConstructor = _parmas != null && _parmas.Length != 0; ILTypeInstance res = ilType.Instantiate(!hasConstructor); if (hasConstructor) { IMethod ilm = ilType.GetConstructor(_parmas.Length); mApp.Invoke(ilm, res, _parmas); } return(res); } return(null); }
internal void InitializeField(int fieldIdx) { int curStart = type.FieldStartIndex; ILType curType = type; while (curType != null) { int maxIdx = curType.FieldStartIndex + curType.FieldTypes.Length; if (fieldIdx < maxIdx && fieldIdx >= curType.FieldStartIndex) { var ft = curType.FieldTypes[fieldIdx - curType.FieldStartIndex]; StackObject.Initialized(ref fields[fieldIdx], fieldIdx, ft.TypeForCLR, ft, managedObjs); return; } else { curType = curType.BaseType as ILType; } } throw new NotImplementedException(); }
public static StackObject *EnumToObject(ILIntepreter intp, StackObject *esp, IList <object> mStack, CLRMethod method, bool isNewObj) { var ret = esp - 1 - 1; AppDomain domain = intp.AppDomain; var p = esp - 1; int val = p->Value; intp.Free(p); p = esp - 1 - 1; Type t = (Type)StackObject.ToObject(p, domain, mStack); intp.Free(p); if (t is ILRuntimeType) { ILType it = ((ILRuntimeType)t).ILType; List <string> res = new List <string>(); if (it.IsEnum) { ILEnumTypeInstance ins = new ILEnumTypeInstance(it); ins[0] = val; return(ILIntepreter.PushObject(ret, mStack, ins.ToString(), true)); } else { throw new Exception(string.Format("{0} is not Enum", t.FullName)); } } else if (t is ILRuntimeWrapperType) { return(ILIntepreter.PushObject(ret, mStack, Enum.GetName(((ILRuntimeWrapperType)t).RealType, val), true)); } else { return(ILIntepreter.PushObject(ret, mStack, Enum.GetName(t, val), true)); } }
/// <summary> /// Create a instance of the specified type /// </summary> /// <param name="type">Full Name of the type</param> /// <param name="args">Arguments for the constructor</param> /// <returns></returns> public ILTypeInstance Instantiate(string type, object[] args = null) { IType t; if (mapType.TryGetValue(type, out t)) { ILType ilType = t as ILType; if (ilType != null) { bool hasConstructor = args != null && args.Length != 0; var res = ilType.Instantiate(!hasConstructor); if (hasConstructor) { var ilm = ilType.GetConstructor(args.Length); Invoke(ilm, res, args); } return(res); } } return(null); }
public static StackObject *EnumGetNames(ILIntepreter intp, StackObject *esp, IList <object> mStack, CLRMethod method, bool isNewObj) { var ret = esp - 1; AppDomain domain = intp.AppDomain; var p = esp - 1; Type t = (Type)StackObject.ToObject(p, domain, mStack); intp.Free(p); if (t is ILRuntimeType) { ILType it = ((ILRuntimeType)t).ILType; List <string> res = new List <string>(); if (it.IsEnum) { var fields = it.TypeDefinition.Fields; for (int i = 0; i < fields.Count; i++) { var f = fields[i]; if (f.IsStatic) { res.Add(f.Name); } } return(ILIntepreter.PushObject(ret, mStack, res.ToArray(), true)); } else { throw new Exception(string.Format("{0} is not Enum", t.FullName)); } } else if (t is ILRuntimeWrapperType) { return(ILIntepreter.PushObject(ret, mStack, Enum.GetNames(((ILRuntimeWrapperType)t).RealType), true)); } else { return(ILIntepreter.PushObject(ret, mStack, Enum.GetNames(t), true)); } }
public override Delegate CreateDelegate(Type t) { if (t is ILRuntimeType) { ILType it = ((ILRuntimeType)t).ILType; if (it.IsDelegate) { var ilMethod = ILMethod; if (ilMethod.DelegateAdapter == null) { var m = it.GetMethod("Invoke") as ILMethod; ilMethod.DelegateAdapter = appdomain.DelegateManager.FindDelegateAdapter(null, ilMethod, m); } return(ilMethod.DelegateAdapter.Delegate); } else { throw new NotSupportedException(string.Format("{0} is not Delegate", t.FullName)); } } else if (t is ILRuntimeWrapperType) { ILRuntimeWrapperType iwt = (ILRuntimeWrapperType)t; return(appdomain.DelegateManager.FindDelegateAdapter(iwt.CLRType, null, ILMethod).Delegate); } else { CLRType clrType = appdomain.GetType(t) as CLRType; if (clrType != null) { return(appdomain.DelegateManager.FindDelegateAdapter(clrType, null, ILMethod).Delegate); } else { throw new NotSupportedException(); } } }
public void LoadAssembly(System.IO.Stream stream, System.IO.Stream symbol, ISymbolReaderProvider symbolReader) { var module = ModuleDefinition.ReadModule(stream); if (symbolReader != null && symbol != null) { module.ReadSymbols(symbolReader.GetSymbolReader(module, symbol)); } if (module.HasAssemblyReferences) { foreach (var ar in module.AssemblyReferences) { /*if (moduleref.Contains(ar.Name) == false) * moduleref.Add(ar.Name); * if (moduleref.Contains(ar.FullName) == false) * moduleref.Add(ar.FullName);*/ } } if (module.HasTypes) { List <ILType> types = new List <ILType>(); foreach (var t in module.GetTypes()) { ILType type = new ILType(t, this); mapType[t.FullName] = type; types.Add(type); } } voidType = GetType("System.Void"); intType = GetType("System.Int32"); longType = GetType("System.Int64"); boolType = GetType("System.Boolean"); floatType = GetType("System.Single"); doubleType = GetType("System.Double"); objectType = GetType("System.Object"); module.AssemblyResolver.ResolveFailure += AssemblyResolver_ResolveFailure; }
//IL2CPP can't process esp->Initialized() properly, so I can only use static function for this public unsafe static void Initialized(StackObject *esp, IType type) { if (type.IsPrimitive) { *esp = type.DefaultObject; } else if (type.IsEnum) { ILType ilType = type as ILType; if (ilType != null) { Initialized(esp, ilType.FieldTypes[0]); } else { Initialized(esp, ((CLRType)type).OrderedFieldTypes[0]); } } else { *esp = Null; } }
public CSHotFixFieldInfo(FieldDefinition def, CSHotFixType declaredType, int fieldIdx, IType fieldType) { definition = def; this.name = def.Name; dType = declaredType; ilType = dType.ILType; appdomain = ilType.AppDomain; this.isStatic = false; this.fieldIdx = fieldIdx; if (isStatic) { attr |= System.Reflection.FieldAttributes.Static; } if (def.IsPublic) { attr |= System.Reflection.FieldAttributes.Public; } else { attr |= System.Reflection.FieldAttributes.Private; } this.fieldType = fieldType; }
public static void FindAttribute(System.Type attType, System.Action <System.Type> on) { string autoAttName = attType.FullName; List <IMethod> calls = new List <IMethod>(); foreach (var itor in AllTypes) { ILType ilType = itor as ILType; if (ilType == null) { continue; } TypeDefinition td = ilType.TypeDefinition; foreach (var att in td.CustomAttributes) { if (att.AttributeType.FullName == autoAttName) { on(ilType.ReflectionType); } } } }
private ILType GetTypeInternal(ITypeSymbol symbol) { var fullname = symbol.FullName; IListView <ILType> list; if (!NameToTypes.TryGetValues(fullname, out list)) { return(CreateTypeInternal(symbol)); } int currentScore = int.MinValue; ILType currentType = null; foreach (var type in list) { int score; if (Score.GetTypeWeakMatchScore(type, symbol, out score)) { if (score > currentScore) { currentScore = score; currentType = type; } } } if (currentType == null) { return(CreateTypeInternal(symbol)); } if (currentScore >= 0) { return(currentType); } var typeInternal = CreateTypeInternal(symbol); return(typeInternal ?? currentType); }
public static Type GetPrimitiveType(ILType iLType) { switch (iLType) { case ILType.R8: return(typeof(double)); case ILType.R4: return(typeof(float)); case ILType.U8: return(typeof(ulong)); case ILType.I8: return(typeof(long)); case ILType.U4: return(typeof(uint)); case ILType.I4: return(typeof(int)); case ILType.U2: return(typeof(short)); case ILType.I2: return(typeof(ushort)); case ILType.U1: return(typeof(byte)); case ILType.I1: return(typeof(sbyte)); } throw new ArgumentOutOfRangeException("iLType"); }
public ILObjectBase(ILGenerator il, ILType ilType, object obj) { this.obj = obj; this.ilType = ilType; this.il = il; if ((int)ilType <= 4) { this.valueType = obj.GetType(); } else if (ilType == ILType.LocalBuilder) { this.valueType = ((LocalBuilder)obj).LocalType; } else if (ilType == ILType.Null) { this.valueType = typeof(void); } else { throw new ArgumentException("ILParameterBase类无法构造该类型的IL参数实例"); } this.InitLdManner(); }