protected override TargetObject DoEvaluateObject(ScriptingContext context) { Thread target = context.CurrentThread; TargetObject obj = expr.EvaluateObject (context); // array[int] TargetArrayObject aobj = obj as TargetArrayObject; if (aobj != null) { int[] int_indices = GetIntIndices (target, context); try { return aobj.GetElement (target, int_indices); } catch (ArgumentException) { throw new ScriptingException ( "Index of array expression `{0}' out of bounds.", expr.Name); } } // pointer[int] TargetPointerObject pobj = obj as TargetPointerObject; if (pobj != null) { // single dimensional array only at present int[] int_indices = GetIntIndices (target, context); if (int_indices.Length != 1) throw new ScriptingException ( "Multi-dimensial arrays of type {0} are not yet supported", expr.Name); if (pobj.Type.IsArray) return pobj.GetArrayElement (target, int_indices [0]); throw new ScriptingException ( "Variable {0} is not an array type.", expr.Name); } // indexers TargetClassObject sobj = Convert.ToClassObject (target, obj); if (sobj != null) { ArrayList props = new ArrayList (); foreach (TargetPropertyInfo prop in sobj.Type.Properties) { if (!prop.CanRead) continue; props.Add (prop.Getter); } if (props.Count == 0) throw new ScriptingException ( "Indexer `{0}' doesn't have a getter.", expr.Name); TargetFunctionType[] funcs = new TargetFunctionType [props.Count]; props.CopyTo (funcs, 0); MethodGroupExpression mg = new MethodGroupExpression ( sobj.Type, sobj, expr.Name + ".this", funcs, true, false); InvocationExpression invocation = new InvocationExpression ( mg, indices); invocation.Resolve (context); return invocation.EvaluateObject (context); } throw new ScriptingException ( "{0} is neither an array/pointer type, nor is it " + "an object with a valid indexer.", expr); }
public TargetObject Invoke(ScriptingContext context) { TargetClassType stype = Convert.ToClassType ( type_expr.EvaluateType (context)); TargetMethodInfo[] ctors = stype.Constructors; TargetFunctionType[] funcs = new TargetFunctionType [ctors.Length]; for (int i = 0; i < ctors.Length; i++) funcs [i] = ctors [i].Type; MethodGroupExpression mg = new MethodGroupExpression ( stype, null, ".ctor", funcs, false, true); InvocationExpression invocation = new InvocationExpression (mg, arguments); invocation.Resolve (context); return invocation.EvaluateObject (context); }
protected override MethodExpression DoResolveMethod(ScriptingContext context, LocationType type) { method_expr = (MethodGroupExpression) expr.ResolveMethod (context, type); if (method_expr == null) return null; resolved = true; return this; }
protected override bool DoAssign(ScriptingContext context, TargetObject right) { Thread target = context.CurrentThread; TargetObject obj = expr.EvaluateObject (context); // array[int] TargetArrayObject aobj = obj as TargetArrayObject; if (aobj != null) { int[] int_indices = GetIntIndices (target, context); try { aobj.SetElement (target, int_indices, right); } catch (ArgumentException) { throw new ScriptingException ( "Index of array expression `{0}' out of bounds.", expr.Name); } return true; } // indexers TargetClassObject sobj = Convert.ToClassObject (target, obj); if (sobj != null) { ArrayList props = new ArrayList (); foreach (TargetPropertyInfo prop in sobj.Type.Properties) { if (!prop.CanWrite) continue; props.Add (prop.Setter); } if (props.Count == 0) throw new ScriptingException ( "Indexer `{0}' doesn't have a setter.", expr.Name); TargetFunctionType[] funcs = new TargetFunctionType [props.Count]; props.CopyTo (funcs, 0); MethodGroupExpression mg = new MethodGroupExpression ( sobj.Type, sobj, expr.Name + "[]", funcs, true, false); Expression[] indexargs = new Expression [indices.Length + 1]; indices.CopyTo (indexargs, 0); indexargs [indices.Length] = new ArgumentExpression (right); InvocationExpression invocation = new InvocationExpression ( mg, indexargs); invocation.Resolve (context); invocation.Invoke (context, false); return true; } throw new ScriptingException ( "{0} is neither an array/pointer type, nor is it " + "an object with a valid indexer.", expr); }
protected override Expression DoResolve(ScriptingContext context) { method_expr = (MethodGroupExpression) expr.ResolveMethod ( context, LocationType.Method); if (method_expr == null) return null; argtypes = new TargetType [arguments.Length]; for (int i = 0; i < arguments.Length; i++) { arguments [i] = arguments [i].Resolve (context); if (arguments [i] == null) return null; argtypes [i] = arguments [i].EvaluateType (context); } method = method_expr.OverloadResolve (context, argtypes); resolved = true; return this; }
public static MethodGroupExpression ResolveDelegate(ScriptingContext context, Expression expr) { TargetClassType ctype = Convert.ToClassType (expr.EvaluateType (context)); if (ctype == null) return null; TargetClassObject cobj; try { cobj = Convert.ToClassObject ( context.CurrentThread, expr.EvaluateObject (context)); } catch { cobj = null; } TargetClassType delegate_type = ctype.Language.DelegateType; if (!CastExpression.TryCast (context, ctype, delegate_type)) return null; TargetFunctionType invoke = null; foreach (TargetMethodInfo method in ctype.Methods) { if (method.Name == "Invoke") { invoke = method.Type; break; } } if (invoke == null) return null; TargetFunctionType[] methods = new TargetFunctionType[] { invoke }; MethodGroupExpression mg = new MethodGroupExpression ( ctype, cobj, "Invoke", methods, true, false); return mg; }