public override void SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture) { if (this.ILField != null) { this.ILField.SetValue(null, value, invokeAttr, binder, culture); return; } if ((this.IsLiteral || this.IsInitOnly) && !(this.value is Missing)) { if (this.value is FunctionObject && value is FunctionObject && this.Name.Equals(((FunctionObject)value).name)) { this.value = value; return; } throw new JScriptException(JSError.AssignmentToReadOnly); } if (this.type != null) { this.value = Convert.Coerce(value, this.type); } else { this.value = value; } }
public override void SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, CultureInfo locale) { if (this.type != null) { value = Convert.Coerce(value, this.type); } while (obj is BlockScope) { obj = ((BlockScope)obj).GetParent(); } StackFrame sf = (StackFrame)obj; JSLocalField f = this.outerField; int slot = this.slotNumber; while (f != null) { slot = f.slotNumber; sf = (StackFrame)sf.GetParent(); f = f.outerField; } if (sf.localVars == null) { return; //happens when assigning to a constant from latebound code } sf.localVars[slot] = value; }
internal override AST PartiallyEvaluate() { this.field.attributeFlags &= ~FieldAttributes.InitOnly; this.identifier.PartiallyEvaluateAsReference(); if (this.field.type != null) { this.field.type.PartiallyEvaluate(); } ScriptObject scope = (ScriptObject)Globals.ScopeStack.Peek(); if (this.value != null) { this.value = this.value.PartiallyEvaluate(); this.identifier.SetPartialValue(this.value); if (this.value is ConstantWrapper) { Object val = this.field.value = this.value.Evaluate(); if (this.field.type != null) { this.field.value = Convert.Coerce(val, this.field.type, true); } if (this.field.IsStatic && (val is Type || val is ClassScope || val is TypedArray || Convert.GetTypeCode(val) != TypeCode.Object)) { this.field.attributeFlags |= FieldAttributes.Literal; goto set_field_type; } } this.field.attributeFlags |= FieldAttributes.InitOnly; set_field_type: if (this.field.type == null) { this.field.type = new TypeExpression(new ConstantWrapper(this.value.InferType(null), null)); } } else { this.value = new ConstantWrapper(null, this.context); this.field.attributeFlags |= FieldAttributes.InitOnly; } // deal with custom attributes if (this.field != null && this.field.customAttributes != null) { this.field.customAttributes.PartiallyEvaluate(); } return(this); }
private void SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, CultureInfo locale, ScriptObject scope) { if (this.IsStatic || this.IsLiteral) { if ((this.IsLiteral || this.IsInitOnly) && !(this.value is Missing)) { throw new JScriptException(JSError.AssignmentToReadOnly); } goto setValue; } if (this.obj != obj) { if (obj is JSObject) { FieldInfo field = ((JSObject)obj).GetField(this.Name, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); if (field != null) { field.SetValue(obj, value, invokeAttr, binder, locale); return; } } throw new TargetException(); } if (!this.IsPublic && (scope == null || !this.IsAccessibleFrom(scope))) { if (((JSObject)this.obj).noExpando) { throw new JScriptException(JSError.NotAccessible, new Context(new DocumentContext("", null), this.Name)); } else { this.expandoValue = value; } return; } setValue: if (this.type != null) { this.value = Convert.Coerce(value, this.type); } else { this.value = value; } }
internal override Object Evaluate() { ConstructorInfo c = (ConstructorInfo)((Binding)this.ctor).member; ParameterInfo[] pars = c.GetParameters(); int pn = pars.Length; for (int i = positionalArgValues.Count; i < pn; i++) { positionalArgValues.Add(Convert.CoerceT(null, pars[i].ParameterType)); } Object[] pArgVals = new Object[pn]; positionalArgValues.CopyTo(0, pArgVals, 0, pn); Object ca = c.Invoke(BindingFlags.ExactBinding, null, pArgVals, null); for (int i = 0, n = this.namedArgProperties.Count; i < n; i++) { JSProperty prop = this.namedArgProperties[i] as JSProperty; if (prop != null) { prop.SetValue(ca, Convert.Coerce(this.namedArgPropertyValues[i], prop.PropertyIR()), null); } else { ((PropertyInfo)this.namedArgProperties[i]).SetValue(ca, this.namedArgPropertyValues[i], null); } } for (int i = 0, n = this.namedArgFields.Count; i < n; i++) { JSVariableField field = this.namedArgFields[i] as JSVariableField; if (field != null) { field.SetValue(ca, Convert.Coerce(this.namedArgFieldValues[i], field.GetInferredType(null))); } else { ((FieldInfo)this.namedArgFields[i]).SetValue(ca, this.namedArgFieldValues[i]); } } return(ca); }
internal override Object Evaluate() { ScriptObject scope = Globals.ScopeStack.Peek(); Object value = null; if (this.initializer != null) { value = this.initializer.Evaluate(); } if (this.type != null) { value = Convert.Coerce(value, this.type); } else { while (scope is BlockScope) { scope = scope.GetParent(); } if (scope is WithObject) { this.identifier.SetWithValue((WithObject)scope, value); } while (scope is WithObject || scope is BlockScope) { scope = scope.GetParent(); } if (this.initializer == null && !(this.field.value is Missing)) { this.completion.value = this.field.value; return(this.completion); } } this.field.SetValue(scope, this.completion.value = value); return(this.completion); }
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); }