Example #1
0
        /// <summary>
        /// Deserializes a graph of connected object from a byte array using a given formatter.
        /// </summary>
        /// <param name="bytes">The byte array to deserialize the graph from.</param>
        /// <param name="caller">DTypeDesc of the caller's class context if it is known or UnknownTypeDesc if it should be determined lazily.</param>
        /// <returns>
        /// The deserialized object graph or an instance of <see cref="PhpReference"/> containing <B>false</B> on error.
        /// </returns>
        /// <exception cref="PhpException">Deserialization failed (Notice).</exception>
        public PhpReference Deserialize(PhpBytes bytes, DTypeDesc caller)
        {
            MemoryStream stream = new MemoryStream(bytes.ReadonlyData);
            object       result = null;

            try
            {
                try
                {
                    // deserialize the data
                    result = GetFormatter(caller).Deserialize(stream);
                }
                catch (System.Reflection.TargetInvocationException e)
                {
                    throw e.InnerException;
                }
            }
            catch (SerializationException e)
            {
                PhpException.Throw(PhpError.Notice, LibResources.GetString("deserialization_failed",
                                                                           e.Message, stream.Position, stream.Length));
                return(new PhpReference(false));
            }

            return(PhpVariable.MakeReference(result));
        }
Example #2
0
        public static int Extract(Dictionary <string, object> localVariables, PhpArray /*!*/ vars, ExtractType type,
                                  string prefix)
        {
            if (vars == null)
            {
                PhpException.ArgumentNull("vars");
                return(0);
            }

            if (vars.Count == 0)
            {
                return(0);
            }

            // unfortunately, type contains flags are combined with enumeration:
            bool refs = (type & ExtractType.Refs) != 0;

            type &= ExtractType.NonFlags;

            //
            // construct the action used to set the variable into the locals/globals
            //
            Action <string /*name*/, object /*value*/> updateVariableFn; // function that writes the value to locals/globals
            Predicate <string /*name*/> containsFn;                      // function that checks if variable exists
            PhpArray globals = (localVariables != null) ? null : ScriptContext.CurrentContext.GlobalVariables;

            #region select function that writes the variable

            if (refs)
            {
                // makes a reference and writes it back (deep copy is not necessary, "no duplicate pointers" rule preserved):

                if (localVariables != null)
                {
                    updateVariableFn = (name, value) =>
                    {
                        localVariables[name] = vars[name] = PhpVariable.MakeReference(value);
                    };
                }
                else
                {
                    updateVariableFn = (name, value) =>
                    {
                        globals[name] = vars[name] = PhpVariable.MakeReference(value);
                    };
                }
            }
            else
            {
                if (localVariables != null)
                {
                    updateVariableFn = (name, value) =>
                    {
                        // deep copy the value
                        value = PhpVariable.DeepCopy(PhpVariable.Dereference(value));

                        // put into locals
                        object       item;
                        PhpReference ref_item;
                        if (localVariables.TryGetValue(name, out item) && (ref_item = item as PhpReference) != null)
                        {
                            ref_item.Value = value;
                        }
                        else
                        {
                            localVariables[name] = value;
                        }
                    };
                }
                else
                {
                    updateVariableFn = (name, value) =>
                    {
                        // deep copy the value
                        value = PhpVariable.DeepCopy(PhpVariable.Dereference(value));

                        // set the value to globals
                        object       item;
                        PhpReference ref_item;
                        if (globals.TryGetValue(name, out item) && (ref_item = item as PhpReference) != null)
                        {
                            ref_item.Value = value;
                        }
                        else
                        {
                            globals[name] = value;
                        }
                    };
                }
            }

            #endregion

            Debug.Assert(updateVariableFn != null);

            #region select function that checks if variable exists

            if (localVariables != null)
            {
                containsFn = (name) => localVariables.ContainsKey(name);
            }
            else
            {
                containsFn = (name) => globals.ContainsKey(name);
            }

            #endregion

            Debug.Assert(containsFn != null);

            //
            //
            //
            int extracted_count = 0;
            foreach (KeyValuePair <IntStringKey, object> entry in vars)
            {
                string name = entry.Key.ToString();
                if (String.IsNullOrEmpty(name) && type != ExtractType.PrefixInvalid)
                {
                    continue;
                }

                switch (type)
                {
                case ExtractType.Overwrite:

                    // anything is overwritten:

                    break;

                case ExtractType.Skip:

                    // skips existing name:
                    if (containsFn(name))
                    {
                        continue;
                    }

                    break;

                case ExtractType.IfExists:

                    // skips nonexistent name:
                    if (!containsFn(name))
                    {
                        continue;
                    }

                    break;

                case ExtractType.PrefixAll:

                    // prefix anything:
                    name = String.Concat(prefix, "_", name);

                    break;

                case ExtractType.PrefixInvalid:

                    // prefixes invalid, others are overwritten:
                    if (!PhpVariable.IsValidName(name))
                    {
                        name = String.Concat(prefix, "_", name);
                    }

                    break;

                case ExtractType.PrefixSame:

                    // prefixes existing, others are overwritten:
                    if (containsFn(name))
                    {
                        name = String.Concat(prefix, "_", name);
                    }

                    break;

                case ExtractType.PrefixIfExists:

                    // prefixes existing, others are skipped:
                    if (containsFn(name))
                    {
                        name = String.Concat(prefix, "_", name);
                    }
                    else
                    {
                        continue;
                    }

                    break;

                default:
                    PhpException.InvalidArgument("type", LibResources.GetString("arg:invalid_value"));
                    return(0);
                }

                // invalid names are skipped:
                if (PhpVariable.IsValidName(name))
                {
                    // write the value to locals or globals:
                    updateVariableFn(name, entry.Value);

                    extracted_count++;
                }
            }

            return(extracted_count);
        }