Ejemplo n.º 1
0
            protected override PhpCallable BindCore(Context ctx)
            {
                PhpTypeInfo tinfo;
                object      target = null;

                if ((target = _item1.AsObject()) != null)
                {
                    tinfo = target.GetPhpTypeInfo();
                }
                else
                {
                    tinfo = ctx.ResolveType(_item1.ToString(ctx), _callerCtx, true);
                }

                if (tinfo != null)
                {
                    var method  = _item2.ToString(ctx);
                    var routine = (PhpMethodInfo)tinfo.RuntimeMethods[method];
                    if (routine != null)
                    {
                        if (target != null)
                        {
                            return(routine.PhpInvokable.Bind(target));
                        }
                        else
                        {
                            // calling the method statically
                            if (routine.Methods.All(TypeMembersUtils.IsStatic))
                            {
                                return(routine.PhpCallable);
                            }
                            else
                            {
                                // consider: compiler (and this binder) creates dummy instance of self;
                                // can we create a special singleton instance marked as "null" so use of $this inside the method will fail ?
                                // TODO: use caller instance or warning (calling instance method statically)
                                return(routine.PhpInvokable.Bind(tinfo.GetUninitializedInstance(ctx)));
                            }
                        }
                    }
                    else
                    {
                        // __call
                        // __callStatic
                        routine = (PhpMethodInfo)tinfo.RuntimeMethods[(target != null)
                            ? TypeMethods.MagicMethods.__call
                            : TypeMethods.MagicMethods.__callstatic];

                        if (routine != null)
                        {
                            return(routine.PhpInvokable.BindMagicCall(target, method));
                        }
                    }
                }

                return(null);
            }
Ejemplo n.º 2
0
        /// <summary>
        /// Invoked by runtime when PHP assertion fails.
        /// </summary>
        protected virtual void AssertFailed(PhpValue action = default)
        {
            // exception to be thrown
            var userexception = action.IsSet ? action.AsObject() as Exception : null;
            var usermessage   = action.IsSet ? action.AsString() : null;

            var exception = userexception ?? PhpException.AssertionErrorException(usermessage ?? string.Empty);

            throw exception;
        }
Ejemplo n.º 3
0
 void ResolveType(Context ctx, out PhpTypeInfo tinfo, out object target)
 {
     if ((target = _item1.AsObject()) != null)
     {
         tinfo = target.GetPhpTypeInfo();
     }
     else
     {
         tinfo = ctx.ResolveType(_item1.ToString(ctx), _callerCtx, true);
     }
 }
Ejemplo n.º 4
0
            static bool EqualsObj(PhpValue a, PhpValue b)
            {
                // avoid incomparable object comparison
                var targetSelf  = a.AsObject();
                var targetOther = b.AsObject();

                if (targetSelf != null)
                {
                    return(ReferenceEquals(targetSelf, targetOther));
                }
                if (targetOther != null)
                {
                    return(false);
                }

                //
                return(a == b);
            }
Ejemplo n.º 5
0
        public static PhpCallback Create(PhpValue item1, PhpValue item2, RuntimeTypeHandle callerCtx = default, object callerObj = null)
        {
            if (item2.IsString(out var method))
            {
                if (item1.AsObject() != null || item1.IsString())
                {
                    // creates callback from an array,
                    // array entries must be dereferenced so they cannot be changed gainst
                    return(new ArrayCallback(item1.GetValue(), method, callerCtx)
                    {
                        Target = callerObj
                    });
                }
            }

            //
            return(CreateInvalid());
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Gets <see cref="PhpTypeInfo"/> from a string or an object instance.
        /// </summary>
        /// <param name="ctx">Current runtime context.</param>
        /// <param name="object">String or object. Other value types cause an exception.</param>
        /// <returns>Corresponding <see cref="PhpTypeInfo"/> descriptor. Cannot be <c>null</c>.</returns>
        public static PhpTypeInfo TypeNameOrObjectToType(Context ctx, PhpValue @object)
        {
            object obj;
            string str;

            if ((obj = (@object.AsObject())) != null)
            {
                return(obj.GetType().GetPhpTypeInfo());
            }
            else if ((str = PhpVariable.AsString(@object)) != null)
            {
                return(ctx.GetDeclaredType(str, true));
            }
            else
            {
                throw new ArgumentException();
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Invoked by runtime when PHP assertion fails.
        /// </summary>
        protected virtual void AssertFailed(PhpValue action = default(PhpValue))
        {
            const string AssertionErrorName = "AssertionError";

            var t_assertex = GetDeclaredType(AssertionErrorName);

            Debug.Assert(t_assertex != null);

            Exception exception; // exception to be thrown

            if (action.IsSet && !action.IsEmpty)
            {
                var description = action.AsString();
                exception = action.AsObject() as Exception ?? (Exception)t_assertex.Creator(this, (PhpValue)description);
            }
            else
            {
                exception = (Exception)t_assertex.Creator(this);
            }

            //
            throw exception;
        }
Ejemplo n.º 8
0
 /// <summary>
 /// In case value is a resource, gets its reference.
 /// </summary>
 public static PhpResource AsResource(this PhpValue value)
 {
     return(value.AsObject() as PhpResource);
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Gets underlaying class instance or <c>null</c>.
 /// </summary>
 public static object AsObject(PhpValue value) => value.AsObject(); // TOOD: ?? throw new InvalidCastException();
Ejemplo n.º 10
0
 /// <summary>
 /// Gets underlaying class instance or <c>null</c>.
 /// </summary>
 public static object AsObject(PhpValue value) => value.AsObject();
Ejemplo n.º 11
0
 public object AsObject() => Value.AsObject();
Ejemplo n.º 12
0
 /// <summary>
 /// Gets underlaying class instance or <c>null</c>.
 /// </summary>
 public static object AsObject(PhpValue value) => value.AsObject();