internal void Append(CustomAttribute elem){ this.list.Add(elem); this.context.UpdateWith(elem.context); }
internal override Object Invoke(Object obj, Object thisob, BindingFlags options, Binder binder, Object[] parameters, CultureInfo culture) { int n = this.formalParams.Length; int pn = (parameters != null ? parameters.Length : 0); if (!this.hasThis && !this.hasVarargs && n == pn) { if (binder != null) { return(TypeReferences.ToExecutionContext(this.method).Invoke(this.obj, BindingFlags.SuppressChangeType, null, this.ConvertParams(0, parameters, binder, culture), null)); } else { return(TypeReferences.ToExecutionContext(this.method).Invoke(this.obj, options, binder, parameters, culture)); } } int offset = (this.hasThis ? 1 : 0) + (this.hasEngine ? 1 : 0); Object[] arguments = new Object[n]; if (this.hasThis) { arguments[0] = thisob; if (this.hasEngine) { arguments[1] = this.engine; } } else if (this.hasEngine) { arguments[0] = this.engine; } if (this.hasVarargs) { if (n == offset + 1) //No params other than the vararg array { arguments[offset] = parameters; } else { //Some of the values in parameters must be passed separately from the vararg array int argsToCopy = n - 1 - offset; //The number of separate arguments if (pn > argsToCopy) { ArrayObject.Copy(parameters, 0, arguments, offset, argsToCopy); int vn = pn - argsToCopy; Object[] varargs = new Object[vn]; ArrayObject.Copy(parameters, argsToCopy, varargs, 0, vn); arguments[n - 1] = varargs; } else { ArrayObject.Copy(parameters, 0, arguments, offset, pn); for (int i = pn; i < argsToCopy; i++) { arguments[i + offset] = Missing.Value; } arguments[n - 1] = new Object[0]; } } } else { if (parameters != null) { if (n - offset < pn) { ArrayObject.Copy(parameters, 0, arguments, offset, n - offset); } else { ArrayObject.Copy(parameters, 0, arguments, offset, pn); } } if (n - offset > pn) { for (int i = pn + offset; i < n; i++) { if (i == n - 1 && this.formalParams[i].ParameterType.IsArray && CustomAttribute.IsDefined(this.formalParams[i], typeof(ParamArrayAttribute), true)) { arguments[i] = System.Array.CreateInstance(this.formalParams[i].ParameterType.GetElementType(), 0); } else { arguments[i] = Missing.Value; } } } } if (binder != null) { return(TypeReferences.ToExecutionContext(this.method).Invoke(this.obj, BindingFlags.SuppressChangeType, null, this.ConvertParams(offset, arguments, binder, culture), null)); } else { return(TypeReferences.ToExecutionContext(this.method).Invoke(this.obj, options, binder, arguments, culture)); } }
internal void Remove(CustomAttribute elem){ this.list.Remove(elem); }
internal static bool TypeIsCLSCompliant(Object type) { if (type is ClassScope) { return(((ClassScope)type).IsCLSCompliant()); } else if (type is TypedArray) { Object et = ((TypedArray)type).elementType; if (et is TypedArray || (et is Type && ((Type)et).IsArray)) { return(false); } return(TypeExpression.TypeIsCLSCompliant(et)); } else { Type t = (Type)type; if (t.IsPrimitive) { if (t == Typeob.Boolean || t == Typeob.Byte || t == Typeob.Char || t == Typeob.Double || t == Typeob.Int16 || t == Typeob.Int32 || t == Typeob.Int64 || t == Typeob.Single) { return(true); } else { return(false); } } else { if (t.IsArray) { Type et = t.GetElementType(); if (et.IsArray) { return(false); } return(TypeExpression.TypeIsCLSCompliant(t)); } Object[] attr = CustomAttribute.GetCustomAttributes(t, typeof(CLSCompliantAttribute), false); if (attr.Length > 0) { return(((CLSCompliantAttribute)attr[0]).IsCompliant); } else { Module m = t.Module; attr = CustomAttribute.GetCustomAttributes(m, typeof(CLSCompliantAttribute), false); if (attr.Length > 0) { return(((CLSCompliantAttribute)attr[0]).IsCompliant); } else { Assembly a = m.Assembly; attr = CustomAttribute.GetCustomAttributes(a, typeof(CLSCompliantAttribute), false); if (attr.Length > 0) { return(((CLSCompliantAttribute)attr[0]).IsCompliant); } else { return(false); } } } } } }
//Check that custom attribute is applicable to its target private bool CheckIfTargetOK(Object caType) { if (caType == null) { return(false); } AttributeTargets validOn = (AttributeTargets)0; Type caTypeT = caType as Type; if (caTypeT != null) { Object[] usage = caTypeT.GetCustomAttributes(typeof(AttributeUsageAttribute), true); validOn = ((AttributeUsageAttribute)usage[0]).ValidOn; } else { validOn = ((ClassScope)caType).owner.validOn; } Object target = this.target; Class c = target as Class; if (c != null) { if (c.isInterface) { if ((validOn & AttributeTargets.Interface) != 0) { return(true); } } else if (c is EnumDeclaration) { if ((validOn & AttributeTargets.Enum) != 0) { return(true); } } else if ((validOn & AttributeTargets.Class) != 0) { if (caTypeT == typeof(AttributeUsageAttribute)) { //This is an attribute that describes a new attribute class //Set the validOn field of Class so that it becomes easier to check the usage of the new attribute if (this.positionalArgValues.Count > 0) { Object par0 = this.positionalArgValues[0]; if (par0 is AttributeTargets) { c.validOn = (AttributeTargets)par0; } } for (int i = 0, n = this.namedArgProperties.Count; i < n; i++) { PropertyInfo prop = this.namedArgProperties[i] as PropertyInfo; if (prop.Name == "AllowMultiple") { c.allowMultiple = (bool)this.namedArgPropertyValues[i]; } } } return(true); } else if (caTypeT.FullName == "System.NonSerializedAttribute") { c.attributes &= ~TypeAttributes.Serializable; return(false); } this.context.HandleError(JSError.InvalidCustomAttributeTarget, CustomAttribute.GetTypeName(caType)); return(false); } FunctionDeclaration fDecl = target as FunctionDeclaration; if (fDecl != null) { if ((validOn & AttributeTargets.Property) != 0 && fDecl.enclosingProperty != null) { if (fDecl.enclosingProperty.getter == null || ((JSFieldMethod)fDecl.enclosingProperty.getter).func == fDecl.func) { this.raiseToPropertyLevel = true; return(true); } else { this.context.HandleError(JSError.PropertyLevelAttributesMustBeOnGetter); return(false); } } if ((validOn & AttributeTargets.Method) != 0 && fDecl.isMethod) { return(true); } if ((validOn & AttributeTargets.Constructor) != 0 && fDecl.func.isConstructor) { return(true); } this.context.HandleError(JSError.InvalidCustomAttributeTarget, CustomAttribute.GetTypeName(caType)); return(false); } if (target is VariableDeclaration || target is Constant) { if ((validOn & AttributeTargets.Field) != 0) { return(true); } this.context.HandleError(JSError.InvalidCustomAttributeTarget, CustomAttribute.GetTypeName(caType)); return(false); } if (target is AssemblyCustomAttributeList && (validOn & AttributeTargets.Assembly) != 0) { return(true); } if (target == null && (validOn & AttributeTargets.Parameter) != 0) { return(true); } this.context.HandleError(JSError.InvalidCustomAttributeTarget, CustomAttribute.GetTypeName(caType)); return(false); }
internal override AST PartiallyEvaluate() { this.ctor = this.ctor.PartiallyEvaluateAsCallable(); //first weed out assignment expressions and use them as property initializers ASTList positionalArgs = new ASTList(this.args.context); ASTList namedArgs = new ASTList(this.args.context); for (int i = 0, m = this.args.count; i < m; i++) { AST arg = this.args[i]; Assign assign = arg as Assign; if (assign != null) { assign.rhside = assign.rhside.PartiallyEvaluate(); namedArgs.Append(assign); } else { positionalArgs.Append(arg.PartiallyEvaluate()); } } int n = positionalArgs.count; IReflect[] argIRs = new IReflect[n]; for (int i = 0; i < n; i++) { AST arg = positionalArgs[i]; // only accept ConstantWrappers if (arg is ConstantWrapper) { Object argument = arg.Evaluate(); if ((argIRs[i] = CustomAttribute.TypeOfArgument(argument)) != null) { this.positionalArgValues.Add(argument); continue; } } else if (arg is ArrayLiteral && ((ArrayLiteral)arg).IsOkToUseInCustomAttribute()) { argIRs[i] = Typeob.ArrayObject; this.positionalArgValues.Add(arg.Evaluate()); continue; } arg.context.HandleError(JSError.InvalidCustomAttributeArgument); return(null); // the custom attribute is not good and it will be ignored } //Get the custom attribute and the appropriate constructor (under the covers) this.type = this.ctor.ResolveCustomAttribute(positionalArgs, argIRs, this.target); if (this.type == null) { return(null); } if (Convert.IsPromotableTo((IReflect)this.type, typeof(CodeAccessSecurityAttribute))) { this.context.HandleError(JSError.CannotUseStaticSecurityAttribute); return(null); } //Coerce the positional arguments to the right type and supply default values for optional parameters ConstructorInfo c = (ConstructorInfo)((Binding)this.ctor).member; ParameterInfo[] parameters = c.GetParameters(); int j = 0; int len = this.positionalArgValues.Count; foreach (ParameterInfo p in parameters) { IReflect ir = p is ParameterDeclaration ? ((ParameterDeclaration)p).ParameterIReflect : p.ParameterType; if (j < len) { Object value = this.positionalArgValues[j]; this.positionalArgValues[j] = Convert.Coerce(value, ir, value is ArrayObject); j++; } else { Object value; if (p.DefaultValue == System.Convert.DBNull) { value = Convert.Coerce(null, ir); } else { value = p.DefaultValue; } this.positionalArgValues.Add(value); } } //Check validity of property/field initializers for (int i = 0, m = namedArgs.count; i < m; i++) { Assign assign = (Assign)namedArgs[i]; if (assign.lhside is Lookup && (assign.rhside is ConstantWrapper || (assign.rhside is ArrayLiteral && ((ArrayLiteral)assign.rhside).IsOkToUseInCustomAttribute()))) { Object value = assign.rhside.Evaluate(); IReflect argType = null; if (value is ArrayObject || ((argType = CustomAttribute.TypeOfArgument(value)) != null && argType != Typeob.Object)) { String name = ((Lookup)assign.lhside).Name; MemberInfo [] members = ((IReflect)this.type).GetMember(name, BindingFlags.Public | BindingFlags.Instance); if (members == null || members.Length == 0) { assign.context.HandleError(JSError.NoSuchMember); return(null); } if (members.Length == 1) { MemberInfo member = members[0]; if (member is FieldInfo) { FieldInfo fieldInfo = (FieldInfo)member; if (!fieldInfo.IsLiteral && !fieldInfo.IsInitOnly) { try{ IReflect ir = fieldInfo is JSVariableField ? ((JSVariableField)fieldInfo).GetInferredType(null) : fieldInfo.FieldType; value = Convert.Coerce(value, ir, value is ArrayObject); this.namedArgFields.Add(member); this.namedArgFieldValues.Add(value); continue; }catch (JScriptException) { assign.rhside.context.HandleError(JSError.TypeMismatch); return(null); // the custom attribute is not good and it will be ignored } } } else if (member is PropertyInfo) { PropertyInfo propertyInfo = (PropertyInfo)member; MethodInfo setMethodInfo = JSProperty.GetSetMethod(propertyInfo, false); if (setMethodInfo != null) { ParameterInfo [] paramInfo = setMethodInfo.GetParameters(); if (paramInfo != null && paramInfo.Length == 1) { try{ IReflect ir = paramInfo[0] is ParameterDeclaration ? ((ParameterDeclaration)paramInfo[0]).ParameterIReflect : paramInfo[0].ParameterType; value = Convert.Coerce(value, ir, value is ArrayObject); this.namedArgProperties.Add(member); this.namedArgPropertyValues.Add(value); }catch (JScriptException) { assign.rhside.context.HandleError(JSError.TypeMismatch); return(null); // the custom attribute is not good and it will be ignored } continue; } } } } } } assign.context.HandleError(JSError.InvalidCustomAttributeArgument); return(null); } if (!this.CheckIfTargetOK(this.type)) { return(null); //Ignore attribute } //Consume and discard assembly name attributes try{ Type ty = this.type as Type; if (ty != null && this.target is AssemblyCustomAttributeList) { if (ty.FullName == "System.Reflection.AssemblyAlgorithmIdAttribute") { if (this.positionalArgValues.Count > 0) { this.Engine.Globals.assemblyHashAlgorithm = (AssemblyHashAlgorithm)Convert.CoerceT(this.positionalArgValues[0], typeof(AssemblyHashAlgorithm)); } return(null); } if (ty.FullName == "System.Reflection.AssemblyCultureAttribute") { if (this.positionalArgValues.Count > 0) { String cultureId = Convert.ToString(this.positionalArgValues[0]); if (this.Engine.PEFileKind != PEFileKinds.Dll && cultureId.Length > 0) { this.context.HandleError(JSError.ExecutablesCannotBeLocalized); return(null); } this.Engine.Globals.assemblyCulture = new CultureInfo(cultureId); } return(null); } if (ty.FullName == "System.Reflection.AssemblyDelaySignAttribute") { if (this.positionalArgValues.Count > 0) { this.Engine.Globals.assemblyDelaySign = Convert.ToBoolean(this.positionalArgValues[0], false); } return(null); } if (ty.FullName == "System.Reflection.AssemblyFlagsAttribute") { if (this.positionalArgValues.Count > 0) { this.Engine.Globals.assemblyFlags = (AssemblyFlags)(uint)Convert.CoerceT(this.positionalArgValues[0], typeof(uint)); } return(null); } if (ty.FullName == "System.Reflection.AssemblyKeyFileAttribute") { if (this.positionalArgValues.Count > 0) { this.Engine.Globals.assemblyKeyFileName = Convert.ToString(this.positionalArgValues[0]); if (this.Engine.Globals.assemblyKeyFileName != null && this.Engine.Globals.assemblyKeyFileName.Length == 0) { this.Engine.Globals.assemblyKeyFileName = null; } } return(null); } if (ty.FullName == "System.Reflection.AssemblyKeyNameAttribute") { if (this.positionalArgValues.Count > 0) { this.Engine.Globals.assemblyKeyName = Convert.ToString(this.positionalArgValues[0]); if (this.Engine.Globals.assemblyKeyName != null && this.Engine.Globals.assemblyKeyName.Length == 0) { this.Engine.Globals.assemblyKeyName = null; } } return(null); } if (ty.FullName == "System.Reflection.AssemblyVersionAttribute") { if (this.positionalArgValues.Count > 0) { this.Engine.Globals.assemblyVersion = this.ParseVersion(Convert.ToString(this.positionalArgValues[0])); } return(null); } if (ty.FullName == "System.CLSCompliantAttribute") { this.Engine.isCLSCompliant = this.args == null || this.args.count == 0 || Convert.ToBoolean(this.positionalArgValues[0], false); return(this); } } }catch (ArgumentException) { this.context.HandleError(JSError.InvalidCall); } return(this); }
internal void Append(CustomAttribute elem) { this.list.Add(elem); base.context.UpdateWith(elem.context); }
internal void Remove(CustomAttribute elem) { this.list.Remove(elem); }