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))); } }
protected override object GetArrayItemOverride(object key, bool quiet) { PhpStack stack = ScriptContext.CurrentContext.Stack; stack.AddFrame(key); return(PhpVariable.Dereference(arrayAccess.InvokeMethod(offsetGet, null, stack.Context))); }
public void Test1() { string inFile = "01"; string outFile = inFile + ".a"; string expected = File.ReadAllText(outFile).Trim(); sr = new StringReader(File.ReadAllText(inFile).Trim()); Console.SetIn(sr); prog.InvokeMethod("Main", new object[] { new string[] { } }); string result = sw.ToString().Trim(); Assert.AreEqual(expected, result); }
new public static void GetObjectData(DObject /*!*/ instance, SerializationInfo /*!*/ info, StreamingContext strctx) { info.SetType(typeof(SPLDeserializer)); SerializationContext context = SerializationContext.CreateFromStreamingContext(strctx); object res = PhpVariable.Dereference(instance.InvokeMethod("serialize", null, context.ScriptContext)); if (res == null) { // serialize returned NULL -> this instance will deserialize as NULL info.AddValue(__PHP_Incomplete_Class.ClassNameFieldName, String.Empty); } else { string res_str = PhpVariable.AsString(res); if (res_str == null) { // serialize did not return NULL nor a string -> throw an exception Library.SPL.Exception.ThrowSplException( _ctx => new Library.SPL.Exception(_ctx, true), context.ScriptContext, string.Format(CoreResources.serialize_must_return_null_or_string, instance.TypeName), 0, null); } info.AddValue(SerializedDataFieldName, res_str); info.AddValue(__PHP_Incomplete_Class.ClassNameFieldName, instance.TypeName); } }
public void TestInOut(string input) { string expected = input + "\n" + input + "\n" + input; sr = new StringReader(input); sw = new StringWriter(); Console.SetIn(sr); Console.SetOut(sw); prog.InvokeMethod("Main", new object[] { new string[] { } }); string result = sw.ToString().Trim().Replace("\r", ""); Assert.AreEqual(expected, result); }
/// <summary> /// Gets an item of a user array by invoking <see cref="Library.SPL.ArrayAccess.offsetGet"/>. /// </summary> /// <param name="arrayAccess">User array object.</param> /// <param name="index">An index.</param> /// <param name="context">The current script context.</param> /// <returns>A reference on item returned by the user getter.</returns> internal static PhpReference GetUserArrayItemRef(DObject /*!*/ arrayAccess, object index, ScriptContext /*!*/ context) { Debug.Assert(arrayAccess.RealObject is Library.SPL.ArrayAccess); Debug.Assert(!(index is PhpReference)); context.Stack.AddFrame(index); object result = arrayAccess.InvokeMethod(Library.SPL.PhpArrayObject.offsetGet, null, context); PhpReference ref_result = result as PhpReference; if (ref_result == null) { // obsolete (?): PhpException.Throw(PhpError.Error,CoreResources.GetString("offsetGet_must_return_byref")); ref_result = new PhpReference(result); } return(ref_result); }
/// <summary> /// Serializes <see cref="DObject"/> using PHP serialization. /// </summary> /// <param name="value">The object.</param> private void WritePhpObjectInternal(DObject/*!*/value) { byte[] binaryClassName; // determine class name bool avoid_pic_name = false; string class_name = null; __PHP_Incomplete_Class pic = value as __PHP_Incomplete_Class; if (pic != null) { if (pic.__PHP_Incomplete_Class_Name.IsSet) { avoid_pic_name = true; class_name = pic.__PHP_Incomplete_Class_Name.Value as string; } } if (value is stdClass) class_name = stdClass.ClassName; if (class_name == null) class_name = value.TypeName; // is the instance PHP5.1 Serializable? if (value.RealObject is Library.SPL.Serializable) { context.Stack.AddFrame(); object res = PhpVariable.Dereference(value.InvokeMethod("serialize", null, context)); if (res == null) { // serialize returned NULL -> serialize the instance as NULL WriteNull(); return; } byte[] resdata = null; if (res is PhpString) { res = res.ToString(); } if (res is string) { resdata = writer.Encoding.GetBytes((string)res); } else if (res is PhpBytes) { resdata = ((PhpBytes)res).ReadonlyData; } if (resdata == null) { // serialize did not return NULL nor a string -> throw an exception SPL.Exception.ThrowSplException( _ctx => new SPL.Exception(_ctx, true), context, string.Format(CoreResources.serialize_must_return_null_or_string, value.TypeName), 0, null); } writer.Write(Tokens.ObjectSer); writer.Write(Tokens.Colon); binaryClassName = writer.Encoding.GetBytes(class_name); // write out class name writer.Write(binaryClassName.Length); writer.Write(Tokens.Colon); writer.Write(Tokens.Quote); // flush the StreamWriter before accessing its underlying stream writer.Flush(); writer.BaseStream.Write(binaryClassName, 0, binaryClassName.Length); writer.Write(Tokens.Quote); writer.Write(Tokens.Colon); // write out the result of serialize writer.Write(resdata.Length); writer.Write(Tokens.Colon); writer.Write(Tokens.BraceOpen); // flush the StreamWriter before accessing its underlying stream writer.Flush(); writer.BaseStream.Write(resdata, 0, resdata.Length); writer.Write(Tokens.BraceClose); return; } // try to call the __sleep method bool sleep_called; PhpArray ser_props = value.Sleep(ClassContext, context, out sleep_called); if (sleep_called && ser_props == null) { // __sleep did not return an array -> serialize the instance as NULL WriteNull(); return; } writer.Write(Tokens.Object); writer.Write(Tokens.Colon); // write out class name binaryClassName = writer.Encoding.GetBytes(class_name); // write out class name writer.Write(binaryClassName.Length); writer.Write(Tokens.Colon); writer.Write(Tokens.Quote); // flush the StreamWriter before accessing its underlying stream writer.Flush(); writer.BaseStream.Write(binaryClassName, 0, binaryClassName.Length); writer.Write(Tokens.Quote); writer.Write(Tokens.Colon); // write out property count if (ser_props != null) writer.Write(ser_props.Count); else writer.Write(value.Count - (avoid_pic_name ? 1 : 0)); writer.Write(Tokens.Colon); writer.Write(Tokens.BraceOpen); // write out properties if (ser_props != null) WriteSleepResult(value, ser_props); else WriteAllProperties(value, avoid_pic_name); writer.Write(Tokens.BraceClose); }
/// <summary> /// Builds a <see cref="DObject"/> from atoms (the object itself given as parameter). /// </summary> /// <param name="obj">The instance.</param> private void BuildDObject(DObject obj) { bool serializable = ((bool)atoms[atomCounter++] == true); if (serializable && obj.RealObject is Library.SPL.Serializable) { // pass the serialized data to unserialize context.Stack.AddFrame(BuildObjectGraph()); obj.InvokeMethod("unserialize", null, context); return; } while (atoms[atomCounter] != delimiter) { string property_name = (string)BuildObjectGraph(); object property_value = BuildObjectGraph(); Debug.Assert(property_name != null); Serialization.SetProperty(obj, property_name, property_value, context); } atomCounter++; // for the delimiter // invoke __wakeup on the deserialized instance obj.Wakeup(ClassContext, context); }
new public static void GetObjectData(DObject/*!*/ instance, SerializationInfo/*!*/ info, StreamingContext strctx) { info.SetType(typeof(SPLDeserializer)); SerializationContext context = SerializationContext.CreateFromStreamingContext(strctx); object res = PhpVariable.Dereference(instance.InvokeMethod("serialize", null, context.ScriptContext)); if (res == null) { // serialize returned NULL -> this instance will deserialize as NULL info.AddValue(__PHP_Incomplete_Class.ClassNameFieldName, String.Empty); } else { string res_str = PhpVariable.AsString(res); if (res_str == null) { // serialize did not return NULL nor a string -> throw an exception Library.SPL.Exception.ThrowSplException( _ctx => new Library.SPL.Exception(_ctx, true), context.ScriptContext, string.Format(CoreResources.serialize_must_return_null_or_string, instance.TypeName), 0, null); } info.AddValue(SerializedDataFieldName, res_str); info.AddValue(__PHP_Incomplete_Class.ClassNameFieldName, instance.TypeName); } }
/// <summary> /// Gets an item of a user array by invoking <see cref="Library.SPL.ArrayAccess.offsetGet"/>. /// </summary> /// <param name="arrayAccess">User array object.</param> /// <param name="index">An index.</param> /// <param name="context">The current script context.</param> /// <returns>A reference on item returned by the user getter.</returns> internal static PhpReference GetUserArrayItemRef(DObject/*!*/ arrayAccess, object index, ScriptContext/*!*/ context) { Debug.Assert(arrayAccess.RealObject is Library.SPL.ArrayAccess); Debug.Assert(!(index is PhpReference)); context.Stack.AddFrame(index); object result = arrayAccess.InvokeMethod(Library.SPL.PhpArrayObject.offsetGet, null, context); PhpReference ref_result = result as PhpReference; if (ref_result == null) { // obsolete (?): PhpException.Throw(PhpError.Error,CoreResources.GetString("offsetGet_must_return_byref")); ref_result = new PhpReference(result); } return ref_result; }
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)); } }
public void Test3() { DObject d = new DObject("Calculator.Calc"); Assert.AreEqual((int)d.InvokeMethod("A"), 6); }