//Similar to ArrayAccess.GetUserArrayItem, but getting access to a C# IDictionary internal static object GetListItem(IList arrayAccess, object key, Operators.GetItemKinds kind) { int index = Convert.ObjectToInteger(key); // index used as key in IList switch (kind) { case Operators.GetItemKinds.Isset: // pass isset() ""/null to say true/false depending on the value returned from "offsetExists": return (index >= 0 && index < arrayAccess.Count) ? "" : null; case Operators.GetItemKinds.Empty: // if "offsetExists" returns false, the empty()/isset() returns false (pass null to say true/false): // otherwise, "offsetGet" is called to retrieve the value, which is passed to isset(): if (index < 0 || index >= arrayAccess.Count) return null; else goto default; default: // regular getter: return ClrObject.WrapDynamic(PhpVariable.Dereference(arrayAccess[index])); } }
/// <summary> /// Emits IL instructions that load the value of an item of given array. /// </summary> /// <param name="array"><see cref="Expression"/> determining the array.</param> /// <param name="index"><see cref="Expression"/> determining the index whose value /// should be obtained from the array.</param> /// <param name="kind">A kind of getter.</param> /// <remarks>Nothing is supposed on the evaluation stack. The value of the item is left /// on the evaluation stack.</remarks> public PhpTypeCode EmitGetItem(Expression/*!*/ array, Expression/*!*/ index, Operators.GetItemKinds kind) { ILEmitter il = codeGenerator.IL; // array: var arrayTypeCode = array.Emit(codeGenerator); // ensure the array is writeable is required if (EnsureWritable) codeGenerator.EmitEnsureWritable(arrayTypeCode); // index: PhpTypeCode index_type_code = codeGenerator.EmitArrayKey(this, index); // kind: if (kind == Operators.GetItemKinds.Get && QuietRead) kind = Operators.GetItemKinds.QuietGet; il.LdcI4((int)kind); // CALL Operators.GetItem(<array>, <index>, <kind>) codeGenerator.EmitGetItem(index_type_code, index, false); return PhpTypeCode.Object; }
//Similar to ArrayAccess.GetUserArrayItem, but getting access to a C# IDictionary internal static object GetDictionaryItem(IDictionary arrayAccess, object key, Operators.GetItemKinds kind) { switch (kind) { case Operators.GetItemKinds.Isset: // pass isset() ""/null to say true/false depending on the value returned from "offsetExists": return arrayAccess.Contains(key) ? "" : null; case Operators.GetItemKinds.Empty: // if "offsetExists" returns false, the empty()/isset() returns false (pass null to say true/false): // otherwise, "offsetGet" is called to retrieve the value, which is passed to isset(): if (!arrayAccess.Contains(key)) return null; else goto default; default: // regular getter: return ClrObject.WrapDynamic(PhpVariable.Dereference(arrayAccess[key])); } }
internal static object GetUserArrayItem(DObject/*!*/ arrayAccess, object index, Operators.GetItemKinds kind) { PhpStack stack = ScriptContext.CurrentContext.Stack; switch (kind) { case Operators.GetItemKinds.Isset: // pass isset() ""/null to say true/false depending on the value returned from "offsetExists": stack.AddFrame(index); return Core.Convert.ObjectToBoolean(arrayAccess.InvokeMethod(offsetExists, null, stack.Context)) ? "" : null; case Operators.GetItemKinds.Empty: // if "offsetExists" returns false, the empty()/isset() returns false (pass null to say true/false): // otherwise, "offsetGet" is called to retrieve the value, which is passed to isset(): stack.AddFrame(index); if (!Core.Convert.ObjectToBoolean(arrayAccess.InvokeMethod(offsetExists, null, stack.Context))) return null; else goto default; default: // regular getter: stack.AddFrame(index); return PhpVariable.Dereference(arrayAccess.InvokeMethod(offsetGet, null, stack.Context)); } }