Throw() public static method

public static Throw ( PhpError error, string formatString ) : void
error PhpError
formatString string
return void
Esempio n. 1
0
        public static int Compare(object x, PhpValue y)
        {
            Debug.Assert(x != null);

            if (x.Equals(y.Object))
            {
                return(0);
            }
            if (x is IPhpComparable)
            {
                return(((IPhpComparable)x).Compare(y));
            }
            if (y.Object is IPhpComparable)
            {
                return(-((IPhpComparable)y.Object).Compare(PhpValue.FromClass(x)));
            }

            switch (y.TypeCode)
            {
            case PhpTypeCode.Null: return(1);

            case PhpTypeCode.Boolean: return(y.Boolean ? 0 : 1);

            case PhpTypeCode.Alias: return(Compare(x, y.Alias.Value));

            case PhpTypeCode.String: return(-CompareStringToObject(y.String, x));

            case PhpTypeCode.Object:
                Debug.Assert(y.Object != null);
                var result = CompareObjects(x, y.Object, PhpComparer.Default, out var incomparable);
                if (incomparable)
                {
                    PhpException.Throw(PhpError.Warning,
                                       Resources.ErrResources.incomparable_objects_compared_exception,
                                       x.GetPhpTypeInfo().Name,
                                       y.Object.GetPhpTypeInfo().Name);
                    return(1);
                }
                return(result);

            default: return(1);
            }
        }
Esempio n. 2
0
        PhpValue HandleMissingScript(string cd, string path, bool throwOnError)
        {
            if (TryIncludeFileContent(path))    // include non-compiled file (we do not allow dynamic compilation yet)
            {
                return(PhpValue.Null);
            }
            else
            {
                var cause = string.Format(Resources.ErrResources.script_not_found, path);

                PhpException.Throw(
                    throwOnError ? PhpError.Error : PhpError.Notice,
                    Resources.ErrResources.script_inclusion_failed, path, cause, string.Join(";", IncludePaths), cd);

                if (throwOnError)
                {
                    throw new ArgumentException(cause);
                }

                return(PhpValue.False);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Gets <see cref="PhpTypeInfo"/> of parent.
        /// Throws in case of parent being used out of class context or within a parentless class.
        /// </summary>
        public static PhpTypeInfo GetParent(RuntimeTypeHandle self)
        {
            if (self.Equals(default(RuntimeTypeHandle)))
            {
                PhpException.Throw(PhpError.Error, Resources.ErrResources.parent_used_out_of_class);
            }
            else
            {
                var t = self.GetPhpTypeInfo().BaseType;
                if (t != null)
                {
                    return(t);
                }
                else
                {
                    PhpException.Throw(PhpError.Error, Resources.ErrResources.parent_accessed_in_parentless_class);
                }
            }

            //
            throw new ArgumentException(nameof(self));
        }
Esempio n. 4
0
        /// <summary>
        /// Gets <see cref="PhpTypeInfo"/> of parent.
        /// Throws in case of parent being used out of class context or within a parentless class.
        /// </summary>
        public static PhpTypeInfo GetParent(PhpTypeInfo self)
        {
            if (self == null)
            {
                PhpException.Throw(PhpError.Error, Resources.ErrResources.parent_used_out_of_class);
            }
            else
            {
                var t = self.BaseType;
                if (t != null)
                {
                    return(t);
                }
                else
                {
                    PhpException.Throw(PhpError.Error, Resources.ErrResources.parent_accessed_in_parentless_class);
                }
            }

            //
            throw new ArgumentException(nameof(self));
        }
Esempio n. 5
0
        /// <summary>
        /// Resolves path according to PHP semantics, lookups the file in runtime tables and calls its Main method.
        /// </summary>
        /// <param name="cd">Current script directory. Used for relative path resolution. Can be <c>null</c> to not resolve against current directory.</param>
        /// <param name="path">The relative or absolute path to resolve and include.</param>
        /// <param name="locals">Variables scope for the included script.</param>
        /// <param name="this">Reference to <c>this</c> variable.</param>
        /// <param name="self">Reference to current class context.</param>
        /// <param name="once">Whether to include according to include once semantics.</param>
        /// <param name="throwOnError">Whether to include according to require semantics.</param>
        /// <returns>Inclusion result value.</returns>
        public PhpValue Include(string cd, string path, PhpArray locals, object @this = null, RuntimeTypeHandle self = default(RuntimeTypeHandle), bool once = false, bool throwOnError = false)
        {
            var script = ScriptsMap.ResolveInclude(path, RootPath, IncludePaths, WorkingDirectory, cd);

            if (script.IsValid)
            {
                if (once && _scripts.IsIncluded(script.Index))
                {
                    return(PhpValue.Create(true));
                }
                else
                {
                    return(script.Evaluate(this, locals, @this, self));
                }
            }
            else
            {
                if (TryIncludeFileContent(path))    // include non-compiled file (we do not allow dynamic compilation yet)
                {
                    return(PhpValue.Null);
                }
                else
                {
                    var cause = string.Format(Resources.ErrResources.script_not_found, path);

                    PhpException.Throw(
                        throwOnError ? PhpError.Error : PhpError.Notice,
                        Resources.ErrResources.script_inclusion_failed, path, cause, string.Join(";", IncludePaths), cd);

                    if (throwOnError)
                    {
                        throw new ArgumentException(cause);
                    }

                    return(PhpValue.False);
                }
            }
        }
Esempio n. 6
0
        public static IPhpArray EnsureArray(object obj)
        {
            // ArrayAccess
            if (obj is ArrayAccess)
            {
                return(EnsureArray((ArrayAccess)obj));
            }

            // IPhpArray
            if (obj is IPhpArray)
            {
                return((IPhpArray)obj);
            }

            // IList
            if (obj is IList)
            {
                return(new ListAsPhpArray((IList)obj));
            }

            // Fatal error: Uncaught Error: Cannot use object of type {0} as array
            PhpException.Throw(PhpError.Error, Resources.ErrResources.object_used_as_array, obj.GetPhpTypeInfo().Name);
            throw new ArgumentException(nameof(obj));
        }
Esempio n. 7
0
        public static int Compare(long lx, PhpValue y)
        {
            switch (y.TypeCode)
            {
            case PhpTypeCode.Null: return((lx == 0) ? 0 : 1);

            case PhpTypeCode.Boolean: return(Compare(lx != 0, y.Boolean));

            case PhpTypeCode.Long: return(Compare(lx, y.Long));

            case PhpTypeCode.Double: return(Compare((double)lx, y.Double));

            case PhpTypeCode.PhpArray: return(-1);

            case PhpTypeCode.String: return(-Compare(y.String, lx));

            case PhpTypeCode.MutableString: return(-Compare(y.MutableString.ToString(), lx));

            case PhpTypeCode.Object:
                Debug.Assert(y.Object != null);

                // we cannot use ToNumber(object) because it treats some builtin classes as numbers
                if (y.Object is decimal d)
                {
                    return(Compare((double)lx, (double)d));
                }

                // Notice: Object of class {0} could not be converted to int
                PhpException.Throw(PhpError.Notice, string.Format(Resources.ErrResources.object_could_not_be_converted, PhpVariable.GetDebugType(y), PhpVariable.TypeNameInt));
                return(Compare(lx, 1L));    // object is treated as '1'

            case PhpTypeCode.Alias: return(Compare(lx, y.Alias.Value));
            }

            throw InvalidTypeCodeException("compare", PhpVariable.TypeNameInt, PhpVariable.GetDebugType(y));
        }
Esempio n. 8
0
 public string ToStringOrThrow(Context ctx)
 {
     PhpException.Throw(PhpError.Notice, ErrResources.array_to_string_conversion);
     return(ToString(ctx));
 }
Esempio n. 9
0
        public static int Compare(object x, PhpValue y)
        {
            Debug.Assert(x != null);

            if (x.Equals(y.Object))
            {
                return(0);
            }
            if (x is IPhpComparable)
            {
                return(((IPhpComparable)x).Compare(y));
            }
            if (y.Object is IPhpComparable)
            {
                return(-((IPhpComparable)y.Object).Compare(PhpValue.FromClass(x)));
            }
            if (x is decimal xd)
            {
                return(Compare((double)xd, y));
            }

            switch (y.TypeCode)
            {
            case PhpTypeCode.Null:
                return(1);

            case PhpTypeCode.Boolean:
                return(y.Boolean ? 0 : 1);

            case PhpTypeCode.Long:
                // Notice: Object of class {0} could not be converted to int
                PhpException.Throw(PhpError.Notice, string.Format(Resources.ErrResources.object_could_not_be_converted, PhpVariable.GetDebugType(y), PhpVariable.TypeNameInt));
                return(Compare(1L, y.Long));

            case PhpTypeCode.Double:
                // Notice: Object of class {0} could not be converted to float
                PhpException.Throw(PhpError.Notice, string.Format(Resources.ErrResources.object_could_not_be_converted, PhpVariable.GetDebugType(y), PhpVariable.TypeNameFloat));
                return(Compare(1L, y.Long));

            case PhpTypeCode.String:
                return(-CompareStringToObject(y.String, x));

            case PhpTypeCode.Object:
                Debug.Assert(y.Object != null);
                var result = CompareObjects(x, y.Object, PhpComparer.Default, out var incomparable);
                if (incomparable)
                {
                    PhpException.Throw(PhpError.Warning,
                                       Resources.ErrResources.incomparable_objects_compared_exception,
                                       x.GetPhpTypeInfo().Name,
                                       y.Object.GetPhpTypeInfo().Name);
                    return(1);
                }
                return(result);

            case PhpTypeCode.Alias:
                return(Compare(x, y.Alias.Value));

            default:
                PhpException.Throw(PhpError.Notice,
                                   string.Format(Resources.ErrResources.incomparable_objects_compared_exception,
                                                 x.GetPhpTypeInfo().Name,
                                                 PhpVariable.GetDebugType(y)));

                return(0);
            }
        }
Esempio n. 10
0
        public void Throw(PhpError error, string formatString, params string[] args)
        {
            // TODO: once this method gets called, pass the error to actual error handler

            PhpException.Throw(error, formatString, args);
        }