Esempio n. 1
0
        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)));
        }
Esempio n. 2
0
        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)));
            }
        }
Esempio n. 3
0
 /// <summary>
 /// Specifies dynamic behavior for invoke operation for global function
 /// </summary>
 public bool TryInvokeMember(
     string memberName,
     Object[] args,
     out Object result
     )
 {
     result = PhpVariable.Unwrap(PhpVariable.Dereference(Context.Call(memberName, null, null, wrapArgs(args))));
     return(true);
 }
Esempio n. 4
0
 /// <summary>
 /// Specifies dynamic behavior for invoke operation for static method
 /// </summary>
 public override bool TryInvokeMember(
     InvokeMemberBinder binder,
     Object[] args,
     out Object result
     )
 {
     Context.Stack.AddFrame(wrapArgs(args));
     result = PhpVariable.Unwrap(PhpVariable.Dereference(Operators.InvokeStaticMethod(type, binder.Name, null, null, Context)));
     return(true);
 }
Esempio n. 5
0
        /// <summary>
        /// Specifies dynamic behavior for get operation for static variable
        /// </summary>
        public override bool TryGetMember(
            GetMemberBinder binder,
            out Object result
            )
        {
            if (binder.Name == ConstID)
            {
                result = new ConstsScope(Context, type);
                return(true);
            }

            result = PhpVariable.Unwrap(PhpVariable.Dereference(Operators.GetStaticProperty(type, binder.Name, null, Context, false)));
            return(true);
        }
Esempio n. 6
0
        public static object CallUserFunction(DTypeDesc caller, PhpCallback function, params object[] args)
        {
            if (function == null)
            {
                PhpException.ArgumentNull("function");
                return(null);
            }
            if (function.IsInvalid)
            {
                return(null);
            }

            // invoke the callback:
            return(PhpVariable.Dereference(function.Invoke(caller, args)));
        }
Esempio n. 7
0
        public virtual object /*mixed*/ getValue(ScriptContext context, object @object)
        {
            var dobj = @object as DObject;

            if (property != null)
            {
                if (!property.IsStatic && dobj == null)
                {
                    PhpException.ArgumentNull("object");
                    return(false);
                }

                return(PhpVariable.Dereference(property.Get(dobj)));
            }

            return(false);
        }
Esempio n. 8
0
        public static string GetEnvironmentVariable(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                return(null);
            }

            var servervar = ScriptContext.CurrentContext.AutoGlobals.Server.Value as PhpArray;

            if (servervar != null)
            {
                object value;
                if (servervar.TryGetValue(name, out value))
                {
                    return(PHP.Core.Convert.ObjectToString(PhpVariable.Dereference(value)));
                }
            }

            return(Environment.GetEnvironmentVariable(name));
        }
Esempio n. 9
0
        /// <summary>
        /// Calls the method referred by <paramref name="methodName"/> from the user defined
        /// object <paramref name="classNameOrObject"/> with parameters <paramref name="args"/>.
        /// </summary>
        /// <param name="caller">DTypeDesc of the caller's class context. Can be UnknownTypeDesc.</param>
        /// <param name="methodName">The name of the method.</param>
        /// <param name="classNameOrObject">An instance to invoke the method on or a class name.</param>
        /// <param name="args">Parameters to invoke the method with.</param>
        /// <returns>The method's return value (always dereferenced).</returns>
        internal static object CallUserMethodInternal(DTypeDesc caller, string methodName, object classNameOrObject, ICollection args)
        {
            PhpException.Throw(PhpError.Notice, LibResources.GetString("call_user_method_deprecated"));

            object  ret_val = false;
            DObject obj;
            string  class_name;

            ScriptContext context = ScriptContext.CurrentContext;

            //DTypeDesc classContext = PhpStackTrace.GetClassContext();  // TODO: GetClassContext only if needed by context.ResolveType
            if (caller != null && caller.IsUnknown)
            {
                caller = PhpStackTrace.GetClassContext();
            }

            if ((obj = classNameOrObject as DObject) != null)
            {
                // push arguments on stack
                context.Stack.AddFrame(args);
                ret_val = obj.InvokeMethod(methodName, caller, context);
            }
            else if ((class_name = PhpVariable.AsString(classNameOrObject)) != null)
            {
                // push arguments on stack
                context.Stack.AddFrame(args);

                ResolveTypeFlags flags = ResolveTypeFlags.UseAutoload | ResolveTypeFlags.ThrowErrors;
                DTypeDesc        type  = PHP.Core.Convert.ObjectToTypeDesc(class_name, flags, caller, context, null, null);

                ret_val = Operators.InvokeStaticMethod(type, methodName, null, caller, context);
            }
            else
            {
                PhpException.InvalidArgument("classNameOrObject", LibResources.GetString("arg:not_object_or_class_name"));
            }

            return(PhpVariable.Dereference(ret_val));
        }
Esempio n. 10
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);
        }
Esempio n. 11
0
        private DynamicMetaObject /*!*/ FallbackInvokeMember(DynamicMetaObject target /*!*/, DynamicMetaObject /*!*/[] /*!*/ args)
        {
            // determine run time values and additional restrictions:
            DTypeDesc           classContext = this._classContext;
            string              fieldName    = this._fieldName;
            BindingRestrictions restrictions = BindingRestrictions.GetTypeRestriction(target.Expression, target.LimitType); //target.Restrictions;

            int currentArg = 0;

            if (!ClassContextIsKnown)
            {
                Debug.Assert(args.Length > currentArg, "Not enough arguments!");
                Debug.Assert(args[currentArg].Value == null || Types.DTypeDesc[0].IsAssignableFrom(args[currentArg].LimitType), "Wrong class context type!");
                classContext = (DTypeDesc)args[currentArg].Value;
                Debug.Assert(classContext == null || !classContext.IsUnknown, "Class context should be known at run time!");

                restrictions = restrictions.Merge(BindingRestrictions.GetInstanceRestriction(args[currentArg].Expression, classContext));

                currentArg++;
            }
            if (IsIndirect)
            {
                Debug.Assert(args.Length > currentArg, "Not enough arguments!");
                Debug.Assert(Types.String[0].IsAssignableFrom(args[currentArg].LimitType), "Wrong field name type!");
                fieldName = (string)args[currentArg].Value;

                restrictions = restrictions.Merge(
                    BindingRestrictions.GetExpressionRestriction(
                        Expression.Equal(
                            args[currentArg].Expression,
                            Expression.Constant(fieldName, Types.String[0]))));

                currentArg++;
            }

            //
            ////Debug.Assert(!(var is PhpReference) && name != null);
            Debug.Assert(target.HasValue && target.LimitType != Types.PhpReference[0], "Target should not be PhpReference!");

            ////if (ReferenceEquals(obj, ScriptContext.SetterChainSingletonObject))
            ////{
            ////    ScriptContext.CurrentContext.AbortSetterChain(false);
            ////    return new PhpReference();
            ////}
            if (WantReference && ReferenceEquals(target.Value, ScriptContext.SetterChainSingletonObject))
            {
                // GetObjectPropertyRef:
                Func <PhpReference> abortSetterChain = () =>
                {
                    ScriptContext.CurrentContext.AbortSetterChain(false);
                    return(new PhpReference());
                };

                return(new DynamicMetaObject(
                           Expression.Call(abortSetterChain.Method),
                           BindingRestrictions.GetInstanceRestriction(target.Expression, ScriptContext.SetterChainSingletonObject)
                           ));
            }

            DObject obj;

            ////// a property of a DObject:
            if ((obj = target.Value as DObject) != null)
            {
                if (obj is ClrObject /*|| obj is IClrValue // IClrValue -> ClrValue<T> -> already in restriction */)
                {
                    // ((DObject)target).RealType == <obj>.RealType
                    restrictions = restrictions.Merge(
                        BindingRestrictions.GetInstanceRestriction(
                            Expression.Property(Expression.Convert(target.Expression, Types.DObject[0]), Properties.DObject_RealType),
                            obj.RealType));
                }

                ////    return GetObjectProperty(obj, name, caller, quiet);
                DPropertyDesc   property;
                GetMemberResult result = obj.TypeDesc.GetInstanceProperty(new VariableName(fieldName), classContext, out property);

                switch (result)
                {
                case GetMemberResult.OK:
                    ////object value = property.Get(this);
                    ////PhpReference reference = value as PhpReference;

                    if (property.Member is PhpField || property.Member is PhpVisibleProperty)
                    {
                        var          realType     = property.DeclaringType.RealType;
                        FieldInfo    realField    = (property.Member is PhpField) ? property.PhpField.RealField : null;
                        PropertyInfo realProperty = (property.Member is PhpVisibleProperty) ? ((PhpVisibleProperty)property.Member).RealProperty : null;

                        Debug.Assert(realField != null ^ realProperty != null);

                        MemberExpression getter = null;

                        if (realField != null)
                        {
                            getter = Expression.Field(Expression.Convert(target.Expression, realType), realField);
                        }
                        else if (realProperty != null)
                        {
                            getter = Expression.Property(Expression.Convert(target.Expression, realType), realProperty);
                        }


                        if (Types.PhpReference[0].IsAssignableFrom(getter.Type))
                        {
                            var reference  = Expression.Variable(Types.PhpReference[0]);
                            var assignment = Expression.Assign(reference, getter);

                            if (WantReference)
                            {
                                ////value = property.Get(this);
                                ////reference = value as PhpReference;

                                var returnLabel = Expression.Label(this._returnType);

                                ////if (reference != null && reference.IsSet)
                                ////{
                                ////    reference.IsAliased = true;
                                ////    return reference;
                                ////}

                                var isset = Expression.IfThen(
                                    Expression.Property(assignment, Properties.PhpReference_IsSet),
                                    Expression.Block(
                                        Expression.Assign(Expression.Property(reference, Properties.PhpReference_IsAliased), Expression.Constant(true)),
                                        Expression.Return(returnLabel, reference)));

                                ////// the CT property has been unset -> try to invoke __get
                                ////PhpReference get_ref = InvokeGetterRef(name, caller, out getter_exists);
                                ////if (getter_exists) return (get_ref == null ? new PhpReference() : get_ref);

                                ////if (reference == null)
                                ////{
                                ////    reference = new PhpReference(value);
                                ////    property.Set(this, reference);
                                ////}
                                ////else
                                ////{
                                ////    reference.IsAliased = true;
                                ////    reference.IsSet = true;
                                ////}
                                Func <DObject, string, DTypeDesc, PhpReference, PhpReference> notsetOperation = (self, name, caller, refrnc) =>
                                {
                                    bool getter_exists;
                                    // the CT property has been unset -> try to invoke __get
                                    PhpReference get_ref = self.InvokeGetterRef(name, caller, out getter_exists);
                                    if (getter_exists)
                                    {
                                        return(get_ref ?? new PhpReference());
                                    }

                                    Debug.Assert(refrnc != null);

                                    refrnc.IsAliased = true;
                                    refrnc.IsSet     = true;

                                    return(refrnc);
                                };

                                ////return reference;

                                return(new DynamicMetaObject(
                                           Expression.Block(this._returnType,
                                                            new[] { reference },
                                                            new Expression[] {
                                    isset,
                                    Expression.Label(returnLabel,
                                                     Expression.Call(null, notsetOperation.Method, Expression.Convert(target.Expression, Types.DObject[0]), Expression.Constant(fieldName), Expression.Constant(classContext, Types.DTypeDesc[0]), reference))
                                }),
                                           restrictions));
                            }
                            else
                            {
                                ////if (reference != null && !reference.IsSet)
                                ////{
                                ////    // the property is CT but has been unset
                                ////    if (issetSemantics)
                                ////    {
                                ////        bool handled;
                                ////        return PropertyIssetHandler(name, caller, out handled);
                                ////    }
                                ////    else return GetRuntimeField(name, caller);
                                ////}
                                ////else return value;


                                Func <DObject, string, DTypeDesc, object> notsetOperation;
                                if (_issetSemantics)
                                {
                                    notsetOperation = (self, name, caller) =>
                                    {
                                        return(PhpVariable.Dereference(self.GetRuntimeField(name, caller)));
                                    }
                                }
                                ;
                                else
                                {
                                    notsetOperation = (self, name, caller) =>
                                    {
                                        bool handled;
                                        return(PhpVariable.Dereference(self.PropertyIssetHandler(name, caller, out handled)));
                                    }
                                };
                                var value =
                                    Expression.Block(this._returnType,
                                                     new[] { reference },
                                                     Expression.Condition(
                                                         Expression.Property(assignment, Properties.PhpReference_IsSet),
                                                         Expression.Field(reference, Fields.PhpReference_Value),
                                                         Expression.Call(null, notsetOperation.Method, Expression.Convert(target.Expression, Types.DObject[0]), Expression.Constant(fieldName), Expression.Constant(classContext, Types.DTypeDesc[0]))
                                                         ));

                                return(new DynamicMetaObject(value, restrictions));
                            }
                        }
                        else
                        {
                            if (WantReference)
                            {
                                return(new DynamicMetaObject(
                                           Expression.New(Constructors.PhpReference_Object, Expression.Convert(getter, Types.Object[0])),
                                           restrictions));
                            }
                            else
                            {
                                return(new DynamicMetaObject(
                                           Expression.Call(Methods.PhpVariable.Dereference, Expression.Convert(getter, Types.Object[0])),
                                           restrictions));
                            }
                        }
                    }
                    else if (property.Member is ClrProperty)
                    {
                        var realType     = property.DeclaringType.RealType;
                        var realProperty = property.ClrProperty.RealProperty;

                        // (target.{RealObject|realValue}).<realProperty>
                        Expression value = Expression.Convert(
                            BinderHelper.ClrObjectWrapDynamic(
                                Expression.Property(
                                    BinderHelper.ClrRealObject(target, realType),
                                    realProperty)),
                            Types.Object[0]);

                        if (WantReference)
                        {
                            value = BinderHelper.MakePhpReference(value);
                        }

                        return(new DynamicMetaObject(value, restrictions));
                    }
                    else if (property.Member is ClrField)
                    {
                        var realType  = property.DeclaringType.RealType;
                        var realField = property.ClrField.FieldInfo;

                        // (target.{RealObject|realValue}).<realField>
                        Expression value = Expression.Convert(
                            BinderHelper.ClrObjectWrapDynamic(
                                Expression.Field(
                                    BinderHelper.ClrRealObject(target, realType),
                                    realField)),
                            Types.Object[0]);

                        if (WantReference)
                        {
                            value = BinderHelper.MakePhpReference(value);
                        }

                        return(new DynamicMetaObject(value, restrictions));
                    }
                    else if (property.Member is ClrEvent)
                    {
                        var clrEvent = (ClrEvent)property.Member;
                        var realType = property.DeclaringType.RealType;

                        // emit stub that Wraps event as [ ClrEventObject<handlerType>.Wrap(<SC>, <event name>, <addMethod>, <removeMethod>) ]
                        var stub = new System.Reflection.Emit.DynamicMethod(
                            string.Format("event<{0}>", fieldName),
                            Types.DObject[0], new[] { realType }, realType);
                        var il = new ILEmitter(stub);
                        clrEvent.EmitGetEventObject(
                            il,
                            new Place(null, Properties.ScriptContext_CurrentContext),
                            new IndexedPlace(PlaceHolder.Argument, 0),
                            false);
                        il.Emit(System.Reflection.Emit.OpCodes.Ret);

                        Expression value = Expression.Call(stub, BinderHelper.ClrRealObject(target, realType));
                        if (WantReference)
                        {
                            value = BinderHelper.MakePhpReference(value);
                        }
                        return(new DynamicMetaObject(value, restrictions));
                    }
                    else
                    {
                        throw new NotImplementedException();
                    }

                case GetMemberResult.NotFound:
                    if (WantReference)
                    {
                        Func <DObject, string, DTypeDesc, PhpReference> op = (self, name, caller) =>
                        {
                            PhpReference reference;
                            bool         getter_exists;

                            // search in RT fields
                            if (self.RuntimeFields != null && self.RuntimeFields.ContainsKey(name))
                            {
                                var namekey = new IntStringKey(name);
                                return(self.RuntimeFields.table._ensure_item_ref(ref namekey, self.RuntimeFields));
                            }

                            // property is not present -> try to invoke __get
                            reference = self.InvokeGetterRef(name, caller, out getter_exists);
                            if (getter_exists)
                            {
                                return((reference == null) ? new PhpReference() : reference);
                            }

                            // (no notice/warning/error thrown by PHP)

                            // add the field
                            reference = new PhpReference();
                            if (self.RuntimeFields == null)
                            {
                                self.RuntimeFields = new PhpArray();
                            }
                            self.RuntimeFields[name] = reference;

                            return(reference);
                        };

                        return(new DynamicMetaObject(
                                   Expression.Call(null, op.Method, Expression.Convert(target.Expression, Types.DObject[0]), Expression.Constant(fieldName), Expression.Constant(classContext, Types.DTypeDesc[0])),
                                   restrictions));
                    }
                    else
                    {
                        ////if (issetSemantics)
                        ////{
                        ////    OrderedHashtable<string>.Element element;
                        ////    if (RuntimeFields != null && (element = RuntimeFields.GetElement(name)) != null)
                        ////    {
                        ////        return element.Value;
                        ////    }
                        ////    else
                        ////    {
                        ////        bool handled;
                        ////        return PropertyIssetHandler(name, caller, out handled);
                        ////    }
                        ////}
                        ////else return GetRuntimeField(name, caller);

                        if (_issetSemantics)
                        {
                            Func <DObject, string, DTypeDesc, object> notsetOperation = (self, name, caller) =>
                            {
                                if (self.RuntimeFields != null)
                                {
                                    object value;
                                    if (self.RuntimeFields.TryGetValue(name, out value))
                                    {
                                        return(value);
                                    }
                                }

                                bool handled;
                                return(self.PropertyIssetHandler(name, caller, out handled));
                            };

                            return(new DynamicMetaObject(
                                       Expression.Call(Methods.PhpVariable.Dereference,
                                                       Expression.Call(null, notsetOperation.Method, Expression.Convert(target.Expression, Types.DObject[0]), Expression.Constant(fieldName), Expression.Constant(classContext, Types.DTypeDesc[0]))),
                                       restrictions));
                        }
                        else
                        {
                            return(new DynamicMetaObject(
                                       Expression.Call(
                                           Methods.PhpVariable.Dereference,
                                           Expression.Call(
                                               Expression.Convert(target.Expression, Types.DObject[0]),
                                               Methods.DObject_GetRuntimeField, Expression.Constant(fieldName), Expression.Constant(classContext, Types.DTypeDesc[0]))),
                                       restrictions));
                        };
                    }

                case GetMemberResult.BadVisibility:
                {
                    ////PhpException.PropertyNotAccessible(
                    ////    property.DeclaringType.MakeFullName(),
                    ////    name.ToString(),
                    ////    (caller == null ? String.Empty : caller.MakeFullName()),
                    ////    property.IsProtected);

                    string stringResourceKey = property.IsProtected ? "protected_property_accessed" : "private_property_accessed";

                    return(new DynamicMetaObject(
                               Expression.Block(this._returnType,
                                                Expression.Call(null, Methods.PhpException.Throw,
                                                                Expression.Constant(PhpError.Error, Types.PhpError_String[0]),
                                                                Expression.Constant(CoreResources.GetString(stringResourceKey, property.DeclaringType.MakeFullName(), fieldName, (classContext == null ? String.Empty : classContext.MakeFullName())))),
                                                WantReference ? (Expression)Expression.New(Constructors.PhpReference_Void) : Expression.Constant(null)
                                                ),
                               restrictions));
                }
                }
            }

            ////// warnings:
            ////if (!quiet) // not in isset() operator only
            ////{
            if (!_issetSemantics)
            {
                ////    if (PhpVariable.IsEmpty(var))
                ////        // empty:
                ////        PhpException.Throw(PhpError.Notice, CoreResources.GetString("empty_used_as_object"));
                ////    else
                ////        // PhpArray, string, scalar type:
                ////        PhpException.VariableMisusedAsObject(var, false);

                Action <object> error = (var) =>
                {
                    if (PhpVariable.IsEmpty(var))
                    {
                        // empty:
                        PhpException.Throw(PhpError.Notice, CoreResources.GetString("empty_used_as_object"));
                    }
                    else
                    {
                        // PhpArray, string, scalar type:
                        PhpException.VariableMisusedAsObject(var, false);
                    }
                };

                return(new DynamicMetaObject(
                           Expression.Block(this._returnType,
                                            Expression.Call(error.Method, target.Expression),
                                            WantReference ? (Expression)Expression.New(Constructors.PhpReference_Void) : Expression.Constant(null)),
                           (target.HasValue && target.Value == null) ?
                           BindingRestrictions.GetInstanceRestriction(target.Expression, null) :
                           BindingRestrictions.GetTypeRestriction(target.Expression, target.LimitType)));
            }
            ////}

            ////// property does not exist
            ////return null;
            return(new DynamicMetaObject(
                       Expression.Constant(null),
                       (target.HasValue && target.Value == null) ?
                       BindingRestrictions.GetInstanceRestriction(target.Expression, null) :
                       BindingRestrictions.GetTypeRestriction(target.Expression, target.LimitType)));
        }
Esempio n. 12
0
 static object GetMemberIsSet(DObject self, string name, DTypeDesc caller)
 {
     return(PhpVariable.Dereference(self.GetRuntimeField(name, caller)));
 }
Esempio n. 13
0
        private static bool RedirectStreams(Process /*!*/ process, PhpArray /*!*/ descriptors, PhpArray /*!*/ pipes)
        {
            using (var descriptors_enum = descriptors.GetFastEnumerator())
                while (descriptors_enum.MoveNext())
                {
                    int desc_no = descriptors_enum.CurrentKey.Integer;

                    StreamAccessOptions access;
                    Stream stream;
                    switch (desc_no)
                    {
                    case 0: stream = process.StandardInput.BaseStream; access = StreamAccessOptions.Write; break;

                    case 1: stream = process.StandardOutput.BaseStream; access = StreamAccessOptions.Read; break;

                    case 2: stream = process.StandardError.BaseStream; access = StreamAccessOptions.Read; break;

                    default: Debug.Fail(); return(false);
                    }

                    object      value = PhpVariable.Dereference(descriptors_enum.CurrentValue);
                    PhpResource resource;
                    PhpArray    array;

                    if ((array = PhpArray.AsPhpArray(value)) != null)
                    {
                        if (!array.Contains(0))
                        {
                            // value must be either a resource or an array:
                            PhpException.Throw(PhpError.Warning, LibResources.GetString("descriptor_item_missing_qualifier", desc_no));
                            return(false);
                        }

                        string qualifier = Core.Convert.ObjectToString(array[0]);

                        switch (qualifier)
                        {
                        case "pipe":
                        {
                            // mode is ignored (it's determined by the stream):
                            PhpStream php_stream = new NativeStream(stream, null, access, String.Empty, StreamContext.Default);
                            pipes.Add(desc_no, php_stream);
                            break;
                        }

                        case "file":
                        {
                            if (!array.Contains(1))
                            {
                                PhpException.Throw(PhpError.Warning, LibResources.GetString("descriptor_item_missing_file_name", desc_no));
                                return(false);
                            }

                            if (!array.Contains(2))
                            {
                                PhpException.Throw(PhpError.Warning, LibResources.GetString("descriptor_item_missing_mode", desc_no));
                                return(false);
                            }

                            string path = Core.Convert.ObjectToString(array[1]);
                            string mode = Core.Convert.ObjectToString(array[2]);

                            PhpStream php_stream = PhpStream.Open(path, mode, StreamOpenOptions.Empty, StreamContext.Default);
                            if (php_stream == null)
                            {
                                return(false);
                            }

                            if (!ActivePipe.BeginIO(stream, php_stream, access, desc_no))
                            {
                                return(false);
                            }
                            break;
                        }

                        default:
                            PhpException.Throw(PhpError.Warning, LibResources.GetString("invalid_handle_qualifier", qualifier));
                            return(false);
                        }
                    }
                    else if ((resource = value as PhpResource) != null)
                    {
                        PhpStream php_stream = PhpStream.GetValid(resource);
                        if (php_stream == null)
                        {
                            return(false);
                        }

                        if (!ActivePipe.BeginIO(stream, php_stream, access, desc_no))
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        // value must be either a resource or an array:
                        PhpException.Throw(PhpError.Warning, LibResources.GetString("descriptor_item_not_array_nor_resource", desc_no));
                        return(false);
                    }
                }

            return(true);
        }