Example #1
0
        /// <summary>
        /// Performs bitwise negation.
        /// </summary>
        internal static PhpValue BitNot(ref PhpValue x)
        {
            switch (x.TypeCode)
            {
                case PhpTypeCode.Long: return PhpValue.Create(~x.Long);

                case PhpTypeCode.Int32: return PhpValue.Create(~x.ToLong());

                case PhpTypeCode.Alias: return BitNot(ref x.Alias.Value);

                case PhpTypeCode.String:
                case PhpTypeCode.WritableString:
                    throw new NotImplementedException();    // bitwise negation of each character in string

                case PhpTypeCode.Object:
                    if (x.Object == null)
                    {
                        return PhpValue.Null;
                    }
                    goto default;

                default:
                    // TODO: Err UnsupportedOperandTypes
                    return PhpValue.Null;
            }
        }
Example #2
0
 /// <summary>
 /// Constructs the object with single runtime field <c>scalar</c>.
 /// </summary>
 /// <param name="scalar">Value of <c>scalar</c> field.</param>
 internal stdClass(PhpValue scalar)
 {
     __peach__runtimeFields = new PhpArray(1)
     {
         { new IntStringKey("scalar"), scalar }
     };
 }
Example #3
0
        /// <summary>
        /// Creates an aliased value.
        /// </summary>
        public PhpAlias(PhpValue value, int refcount = 1)
        {
            Debug.Assert(refcount >= 1);
            Debug.Assert(value.TypeCode != PhpTypeCode.Alias);

            Value = value;
            _refcount = refcount;
        }
Example #4
0
        /// <summary>
        /// Performs bitwise or operation.
        /// </summary>
        internal static PhpValue BitOr(ref PhpValue x, ref PhpValue y)
        {
            var xtype = x.TypeCode;
            if (xtype == PhpTypeCode.String || xtype == PhpTypeCode.WritableString)
            {
                var ytype = y.TypeCode;
                if (ytype == PhpTypeCode.String || ytype == PhpTypeCode.WritableString)
                {
                    throw new NotImplementedException();
                }
            }

            //
            return PhpValue.Create(x.ToLong() | y.ToLong());
        }
Example #5
0
        /// <summary>
        /// Calls a function or a method defined by callback with arguments stored in an array.
        /// </summary>
        /// <param name="ctx">Current runtime context.</param>
        /// <param name="function">Target callback.</param>
        /// <param name="args">Arguments. Can be null.</param>
        /// <returns>The returned value.</returns>
        public static PhpValue call_user_func_array(Context ctx, IPhpCallable function, PhpArray args)
        {
            PhpValue[] args_array;

            if (args != null && args.Count != 0)
            {
                args_array = new PhpValue[args.Count];
                args.CopyValuesTo(args_array, 0);
            }
            else
            {
                args_array = Core.Utilities.ArrayUtils.EmptyValues;
            }

            return call_user_func(ctx, function, args_array);
        }
Example #6
0
        public static TextElement FromValue(Context ctx, PhpValue value)
        {
            switch (value.TypeCode)
            {
                case PhpTypeCode.Object:
                    if (value.Object is byte[])
                    {
                        return new TextElement((byte[])value.Object);
                    }
                    goto default;

                case PhpTypeCode.WritableString:
                    return new TextElement(value.WritableString, ctx.StringEncoding);

                default:
                    return new TextElement(value.ToStringOrThrow(ctx));
            }
        }
Example #7
0
        public static int Compare(string sx, PhpValue y)
        {
            switch (y.TypeCode)
            {
                case PhpTypeCode.Double: return Compare(sx, y.Double);
                case PhpTypeCode.Long: return Compare(sx, y.Long);
                case PhpTypeCode.Boolean: return Compare(Convert.ToBoolean(sx), y.Boolean);
                case PhpTypeCode.String: return Compare(sx, y.String);
                case PhpTypeCode.WritableString: return Compare(sx, y.WritableString.ToString());
                case PhpTypeCode.Alias: return Compare(sx, y.Alias.Value);
                case PhpTypeCode.Undefined: return (sx.Length == 0) ? 0 : 1;
                case PhpTypeCode.Object:
                    if (y.Object == null) return (sx.Length == 0) ? 0 : 1;
                    break;
            }

            throw new NotImplementedException($"compare(String, {y.TypeCode})");
        }
Example #8
0
        public static int Compare(double dx, PhpValue y)
        {
            switch (y.TypeCode)
            {
                case PhpTypeCode.Double: return Compare(dx, y.Double);
                case PhpTypeCode.Long: return Compare(dx, (double)y.Long);
                case PhpTypeCode.Boolean: return Compare(dx != 0.0, y.Boolean);
                case PhpTypeCode.String: return -Compare(y.String, dx);
                case PhpTypeCode.WritableString: return -Compare(y.WritableString.ToString(), dx);
                case PhpTypeCode.Alias: return Compare(dx, y.Alias.Value);
                case PhpTypeCode.Undefined: return (dx == 0.0) ? 0 : 1;
                case PhpTypeCode.Object:
                    if (y.Object == null) return (dx == 0.0) ? 0 : 1;
                    break;
            }

            throw new NotImplementedException($"compare(Double, {y.TypeCode})");
        }
Example #9
0
        int ProcessStatus(Context ctx, ref PhpValue status)
        {
            switch (status.TypeCode)
            {
                case PhpTypeCode.Alias:
                    return ProcessStatus(ctx, ref status.Alias.Value);

                case PhpTypeCode.Long:
                case PhpTypeCode.Int32:
                    return (int)status.ToLong();

                default:
                    if (ctx != null)
                    {
                        ctx.Echo(status);
                    }
                    return 0;
            }
        }
Example #10
0
 /// <summary>
 /// Tries to get a global constant from current context.
 /// </summary>
 internal bool TryGetConstant(string name, out PhpValue value, ref int idx)
 {
     value = _constants.GetConstant(name, ref idx);
     return(value.IsSet);
 }
Example #11
0
 /// <summary>
 /// Debug textual representation of the value.
 /// </summary>
 public abstract string DisplayString(ref PhpValue me);
Example #12
0
 /// <summary>
 /// Gets value as a callable object that can be invoked dynamically.
 /// </summary>
 public static IPhpCallable AsCallable(PhpValue value, RuntimeTypeHandle callerCtx, object callerObj) => value.AsCallable(callerCtx, callerObj);
Example #13
0
        static PhpValue preg_replace(Context ctx, string pattern, string replacement, PhpCallable callback, PhpValue subject, int limit, ref long count)
        {
            var regex = new PerlRegex.Regex(pattern);

            // TODO: count
            // TODO: callback

            var subject_array = subject.AsArray();
            if (subject_array == null)
            {
                return PhpValue.Create(regex.Replace(subject.ToStringOrThrow(ctx), replacement, limit));
            }
            else
            {
                var arr = new PhpArray(subject_array, false);
                var enumerator = arr.GetFastEnumerator();
                while (enumerator.MoveNext())
                {
                    var newvalue = regex.Replace(enumerator.CurrentValue.ToStringOrThrow(ctx), replacement, limit);
                    enumerator.CurrentValue = PhpValue.Create(newvalue);
                }

                return PhpValue.Create(arr);
            }
        }
Example #14
0
        //class HtmlDumpFormatter : DumpFormatter
        //{

        //}

        #endregion

        /// <summary>
        /// Outputs or returns human-readable information about a variable. 
        /// </summary>
        /// <param name="ctx">Current runtime context.</param>
        /// <param name="value">The variable.</param>
        /// <param name="returnString">Whether to return a string representation.</param>
        /// <returns>A string representation or <c>true</c> if <paramref name="returnString"/> is <c>false</c>.</returns>
        public static PhpValue print_r(Context ctx, PhpValue value, bool returnString = false)
        {
            var output = (new PrintFormatter(ctx, "\n")).Serialize(value);

            if (returnString)
            {
                // output to a string:
                return PhpValue.Create(output);
            }
            else
            {
                // output to script context:
                ctx.Echo(output);
                return PhpValue.True;
            }
        }
Example #15
0
        /// <summary>
        /// Checks whether a dereferenced variable is numeric.
        /// </summary>
        /// <param name="variable">The variable.</param>
        /// <returns>Whether <paramref name="variable"/> is integer, double or numeric string.
        /// <seealso cref="PHP.Core.Convert.StringToNumber"/></returns>
        public static bool is_numeric(PhpValue variable)
        {
            switch (variable.TypeCode)
            {
                case PhpTypeCode.Int32:
                case PhpTypeCode.Long:
                case PhpTypeCode.Double:
                    return true;

                case PhpTypeCode.String:
                case PhpTypeCode.WritableString:
                    PhpNumber tmp;
                    return (variable.ToNumber(out tmp) & Core.Convert.NumberInfo.IsNumber) != 0;

                default:
                    return false;
            }
        }
Example #16
0
 /// <summary>
 /// Converts given value to a class object.
 /// </summary>
 public static object ToClass(PhpValue value) => value.ToClass();
Example #17
0
 /// <summary>
 /// Casts value to <see cref="PhpArray"/> or <c>null</c>.
 /// </summary>
 public static PhpArray AsArray(PhpValue value) => value.AsArray();
Example #18
0
 /// <summary>
 /// Converts value to an array.
 /// </summary>
 public static PhpArray ToArray(PhpValue value) => value.ToArray();
Example #19
0
 /// <summary>
 /// Gets underlaying class instance or <c>null</c>.
 /// </summary>
 public static object AsObject(PhpValue value) => value.AsObject();
Example #20
0
 /// <summary>
 /// Creates an instance of a type dynamically with constructor overload resolution.
 /// </summary>
 /// <param name="classname">Full name of the class to instantiate. The name uses PHP syntax of name separators (<c>\</c>) and is case insensitive.</param>
 /// <param name="arguments">Arguments to be passed to the constructor.</param>
 /// <returns>Object instance or <c>null</c> if class is not declared.</returns>
 public object Create(string classname, params object[] arguments) => Create(classname, PhpValue.FromClr(arguments));
Example #21
0
 /// <summary>
 /// Call a function by its name dynamically.
 /// </summary>
 /// <param name="function">Function name valid within current runtime context.</param>
 /// <param name="arguments">Arguments to be passed to the function call.</param>
 /// <returns>Returns value given from the function call.</returns>
 public PhpValue Call(string function, params object[] arguments) => PhpCallback.Create(function, default(RuntimeTypeHandle)).Invoke(this, PhpValue.FromClr(arguments));
Example #22
0
 /// <summary>
 /// Gets value as a callable object that can be invoked dynamically.
 /// </summary>
 public static IPhpCallable AsCallable(PhpValue value, RuntimeTypeHandle callerCtx) => value.AsCallable(callerCtx);
Example #23
0
 /// <summary>
 /// Checks whether a dereferenced variable is a <B>null</B> reference.
 /// </summary>
 /// <param name="variable">The variable.</param>
 /// <returns>Whether <paramref name="variable"/> is a <B>null</B> reference.</returns>
 public static bool is_null(PhpValue variable) => variable.IsNull;
Example #24
0
 /// <summary>
 /// Tries conversion to an array key.
 /// </summary>
 public static bool TryToIntStringKey(PhpValue value, out IntStringKey key) => value.TryToIntStringKey(out key);
Example #25
0
        /// <summary>
        /// Verifies that the contents of a variable can be called as a function.
        /// </summary>
        /// <param name="ctx">Current runtime context.</param>
        /// <param name="variable">The variable.</param>
        /// <param name="syntaxOnly">If <B>true</B>, it is only checked that has <pararef name="variable"/>
        /// a valid structure to be used as a callback. if <B>false</B>, the existence of the function (or
        /// method) is also verified.</param>
        /// <param name="callableName">Receives the name of the function or method (for example
        /// <c>SomeClass::SomeMethod</c>).</param>
        /// <returns><B>true</B> if <paramref name="variable"/> denotes a function, <B>false</B>
        /// otherwise.</returns>
        public static bool is_callable(Context ctx /*, caller*/, PhpValue variable, bool syntaxOnly, out string callableName)
        {
            var callback = variable.AsCallable();
            if (PhpVariable.IsValidCallback(callback))
            {
                callableName = callback.ToString();
                return true;
            }

            callableName = variable.ToString(ctx);
            return false;
        }
Example #26
0
            static void AddScriptReference(Assembly assembly)
            {
                if (assembly == null)
                {
                    throw new ArgumentNullException(nameof(assembly));
                }

                // remember the assembly for class map:
                s_assClassMap.AddPhpAssemblyNoLock(assembly);

                // reflect the module for imported symbols:

                var module = assembly.ManifestModule;

                // PhpPackageReferenceAttribute
                foreach (var r in module.GetCustomAttributes <PhpPackageReferenceAttribute>())
                {
                    Context.AddScriptReference(r.ScriptType);
                }

                // ImportPhpTypeAttribute
                foreach (var t in module.GetCustomAttributes <ImportPhpTypeAttribute>())
                {
                    TypesTable.DeclareAppType(PhpTypeInfoExtension.GetPhpTypeInfo(t.ImportedType));
                }

                // ImportPhpFunctionsAttribute
                foreach (var t in module.GetCustomAttributes <ImportPhpFunctionsAttribute>())
                {
                    // TODO: remember the container, do not reflect repetitiously

                    foreach (var m in t.ContainerType.GetMethods())
                    {
                        if (m.IsPublic && m.IsStatic && !m.IsPhpHidden())
                        {
                            RoutinesTable.DeclareAppRoutine(m.Name, m);
                        }
                    }
                }

                // ImportPhpConstantsAttribute
                foreach (var t in module.GetCustomAttributes <ImportPhpConstantsAttribute>())
                {
                    // TODO: remember the container, do not reflect repetitiously

                    foreach (var m in t.ContainerType.GetMembers(BindingFlags.Static | BindingFlags.Public))
                    {
                        if (m is FieldInfo fi && !fi.IsPhpHidden())
                        {
                            Debug.Assert(fi.IsStatic && fi.IsPublic);

                            if (fi.IsInitOnly || fi.IsLiteral)
                            {
                                ConstsMap.DefineAppConstant(fi.Name, PhpValue.FromClr(fi.GetValue(null)));
                            }
                            else
                            {
                                ConstsMap.DefineAppConstant(fi.Name, new Func <PhpValue>(() => PhpValue.FromClr(fi.GetValue(null))));
                            }
                        }
Example #27
0
 public static PhpValue preg_replace(Context ctx, PhpValue pattern, PhpValue replacement, PhpValue subject, int limit = -1)
 {
     long count;
     return preg_replace(ctx, pattern, replacement, subject, limit, out count);
 }
Example #28
0
 /// <summary>
 /// Defines a runtime constant.
 /// </summary>
 public bool DefineConstant(string name, PhpValue value, bool ignorecase = false) => _constants.DefineConstant(name, value, ignorecase);
Example #29
0
 /// <summary>
 /// Add a value to the end of array.
 /// Value can be an alias.
 /// </summary>
 void IPhpArray.AddValue(PhpValue value) { throw new NotSupportedException(); }
Example #30
0
 void IConstantsComposition.Define(string name, PhpValue value) => DefineConstant(name, value, ignorecase: false);
Example #31
0
 /// <summary>
 /// Outputs current value to the <see cref="Context.Output"/> or <see cref="Context.OutputStream"/>.
 /// </summary>
 public abstract void Output(ref PhpValue me, Context ctx);
Example #32
0
 /// <summary>
 /// Checks whether a dereferenced variable is double.
 /// Alias for is_float().
 /// </summary>
 /// <param name="variable">The variable.</param>
 /// <returns>Whether <paramref name="variable"/> is double.</returns>
 public static bool is_double(PhpValue variable) => variable.TypeCode == PhpTypeCode.Double;
Example #33
0
 /// <summary>
 /// Defines a runtime constant.
 /// </summary>
 internal bool DefineConstant(string name, PhpValue value, ref int idx, bool ignorecase = false) => _constants.DefineConstant(name, value, ref idx, ignorecase);
Example #34
0
 /// <summary>
 /// Checks whether a dereferenced variable is string.
 /// </summary>
 /// <param name="variable">The variable.</param>
 /// <returns>Whether <paramref name="variable"/> is string.</returns>
 public static bool is_string(PhpValue variable) => variable.TypeCode == PhpTypeCode.String || variable.TypeCode == PhpTypeCode.WritableString;
Example #35
0
        // TODO: static AddScript(string path, MainDelegate @delegate)

        #endregion

        #region Constants

        /// <summary>
        /// Tries to get a global constant from current context.
        /// </summary>
        public bool TryGetConstant(string name, out PhpValue value)
        {
            int idx = 0;

            return(TryGetConstant(name, out value, ref idx));
        }
Example #36
0
 /// <summary>
 /// Checks whether a dereferenced variable is <see cref="Core.Reflection.DObject"/>.
 /// </summary>
 /// <param name="variable">The variable.</param>
 /// <returns>Whether <paramref name="variable"/> is <see cref="Core.Reflection.DObject"/>.</returns>
 public static bool is_object(PhpValue variable)
     => variable.IsObject && variable.Object != null && !(variable.Object is __PHP_Incomplete_Class);
Example #37
0
        /// <summary>
        /// Flushes data on current level of buffering to the sinks or to the previous level.
        /// The current level clean up MUST follow this method's call.
        /// </summary>
        internal void InternalFlush()
        {
            Debug.Assert(_level != null);

            if (_level.filter == null)
            {
                if (_level.Index == 0)
                {
                    // TODO: PhpString buffers
                    // NOTE: avoid calling non-async on ASP.NET Core 3.0; consider changing to async

                    // writes top-level data to sinks:
                    for (int i = 0; i < _level.buffers.Count; i++)
                    {
                        var element = _level.buffers[i];
                        if (element.data is char[] chars)
                        {
                            _charSink.Write(chars, 0, element.size);
                        }
                        else
                        {
                            _byteSink
                            .WriteAsync((byte[])element.data, 0, element.size)
                            .GetAwaiter()
                            .GetResult();
                        }
                    }
                }
                else
                {
                    // joins levels (data are not copied => the current level MUST be cleaned up after the return from this method):
                    if (_level.size > 0)
                    {
                        var lower_level = _levels[_level.Index - 1];

                        lower_level.buffers.AddRange(_level.buffers);
                        lower_level.size             += _level.size;
                        lower_level.freeSpace         = _level.freeSpace; // free space in the last buffer of the level
                        lower_level.containsByteData |= _level.containsByteData;
                        lower_level.containsCharData |= _level.containsCharData;
                    }
                }
            }
            else
            {
                // gets data from user's callback:
                var data = _level.filter.Invoke(_ctx, GetContent(), PhpValue.Create((int)(ChunkPosition.First | ChunkPosition.Middle | ChunkPosition.Last)));
                if (!data.IsEmpty)
                {
                    var bytes = data.AsBytesOrNull(_ctx);

                    // writes data to the current level of buffering or to sinks depending on the level count:
                    if (_level.Index == 0)
                    {
                        // checks whether the filtered data are binary at first; if not so, converts them to a string:

                        if (bytes != null)
                        {
                            _byteSink.Write(bytes);
                        }
                        else
                        {
                            // TODO: PhpString containing both string and byte[]
                            _charSink.Write(data.ToString(_ctx));
                        }
                    }
                    else
                    {
                        // temporarily decreases the level of buffering toredirect writes to the lower level:
                        var old_level = _level;
                        _level = _levels[_level.Index - 1];

                        // checks whether the filtered data are binary at first; if not so, converts them to a string:
                        if (bytes != null)
                        {
                            Stream.Write(bytes);
                        }
                        else
                        {
                            // TODO: PhpString containing both string and byte[]
                            this.Write(data.ToString(_ctx));
                        }

                        // restore the level of buffering:
                        _level = old_level;
                    }
                }
            }
        }
Example #38
0
 void IConstantsComposition.Define(string name, PhpValue value, bool ignoreCase) => DefineConstant(name, value, ignoreCase);
Example #39
0
 /// <summary>
 /// Checks whether a dereferenced variable is double.
 /// </summary>
 /// <param name="variable">The variable.</param>
 /// <returns>Whether <paramref name="variable"/> is double.</returns>
 public static bool is_float(PhpValue variable) => is_double(variable);
Example #40
0
 public PhpHashEntryDebugView(IntStringKey key, PhpValue value)
 {
     _key   = key;
     _value = value;
 }
Example #41
0
 /// <summary>
 /// Checks whether a dereferenced variable is double.
 /// Alias for is_float().
 /// </summary>
 /// <param name="variable">The variable.</param>
 /// <returns>Whether <paramref name="variable"/> is double.</returns>
 public static bool is_real(PhpValue variable) => is_double(variable);
Example #42
0
 /// <summary>
 /// Gets <see cref="IPhpArray"/> instance providing access to the value with array operators.
 /// Returns <c>null</c> if underlaying value does provide array access.
 /// </summary>
 public virtual IPhpArray GetArrayAccess(ref PhpValue me) => null;
Example #43
0
 /// <summary>
 /// Checks whether a dereferenced variable is an <see cref="PhpArray"/>.
 /// </summary>
 /// <param name="variable">The variable.</param>
 /// <returns>Whether <paramref name="variable"/> is <see cref="PhpArray"/>.</returns>
 public static bool is_array(PhpValue variable) => variable.IsArray;
Example #44
0
 /// <summary>
 /// Accesses the value as an array and gets item at given index.
 /// Gets <c>void</c> value in case the key is not found.
 /// </summary>
 public virtual PhpValue GetArrayItem(ref PhpValue me, PhpValue index, bool quiet) => PhpValue.Null;
Example #45
0
 /// <summary>
 /// Checks whether a dereferenced variable is a valid <see cref="PhpResource"/>.
 /// </summary>
 /// <param name="variable">The variable.</param>
 /// <returns>Whether <paramref name="variable"/> is a valid <see cref="PhpResource"/>.</returns>
 public static bool is_resource(PhpValue variable)
 {
     //PhpResource res = variable as PhpResource;
     //return res != null && res.IsValid;
     throw new NotImplementedException();
 }
Example #46
0
 /// <summary>
 /// Accesses the value as an array and gets item at given index.
 /// Gets empty value in case the key is not found.
 /// </summary>
 public virtual PhpAlias EnsureItemAlias(ref PhpValue me, PhpValue index, bool quiet) => new PhpAlias(PhpValue.Null);
Example #47
0
 /// <summary>
 /// Checks whether a dereferenced variable is a scalar.
 /// </summary>
 /// <param name="variable">The variable.</param>
 /// <returns>Whether <paramref name="variable"/> is an integer, a double, a bool or a string after dereferencing.</returns>
 public static bool is_scalar(PhpValue variable) => PhpVariable.IsScalar(variable);
Example #48
0
 /// <summary>
 /// Converts value to an array.
 /// </summary>
 public abstract PhpArray ToArray(ref PhpValue me);
Example #49
0
 /// <summary>
 /// Verifies that the contents of a variable can be called as a function.
 /// </summary>
 /// <param name="variable">The variable.</param>
 /// <param name="syntaxOnly">If <B>true</B>, it is only checked that has <pararef name="variable"/>
 /// a valid structure to be used as a callback. if <B>false</B>, the existence of the function (or
 /// method) is also verified.</param>
 /// <returns><B>true</B> if <paramref name="variable"/> denotes a function, <B>false</B>
 /// otherwise.</returns>
 public static bool is_callable(PhpValue variable, bool syntaxOnly = false)
 {
     return PhpVariable.IsValidCallback(variable.AsCallable());  // TODO: check syntaxOnly || can be bound
 }
Example #50
0
 /// <summary>
 /// Gets <see cref="PhpArray"/> or throws an exception.
 /// </summary>
 public virtual PhpArray GetArray(ref PhpValue me) => throw new InvalidCastException();
Example #51
0
            public PhpString Serialize(PhpValue value)
            {
                _output = new PhpString();
                _indent = 0;

                //
                Accept(value);
                _output.Append(_nl);

                return _output;
            }
Example #52
0
 /// <summary>
 /// Gets underlaying class instance or <c>null</c>.
 /// </summary>
 public virtual object AsObject(ref PhpValue me) => null;
Example #53
0
        /// <summary>
        /// Outputs or returns a pars-able string representation of a variable.
        /// </summary>
        /// <param name="ctx">Current runtime context.</param>
        /// <param name="variable">The variable.</param>
        /// <param name="returnString">Whether to return a string representation.</param>
        /// <returns>A string representation or a <c>null</c> reference if <paramref name="returnString"/> is <c>false</c>.</returns>
        public static string var_export(Context ctx, PhpValue variable, bool returnString = false)
        {
            var output = (new ExportFormatter(ctx, "\n")).Serialize(variable);

            if (returnString)
            {
                // output to a string:
                return output.ToString(ctx);
            }
            else
            {
                // output to script context:
                ctx.Echo(output);
                return null;
            }

        }
Example #54
0
 /// <summary>
 /// Gets callable wrapper for dynamic object invocation.
 /// </summary>
 /// <param name="me"></param>
 /// <param name="callerCtx">Current caller type.</param>
 /// <returns>Instance of a callable object, cannot be <c>null</c>, can be invalid.</returns>
 public virtual IPhpCallable AsCallable(ref PhpValue me, RuntimeTypeHandle callerCtx) => PhpCallback.CreateInvalid();
Example #55
0
        /// <summary>
        /// Perform a regular expression search and replace.
        /// </summary>
        /// <param name="ctx">A reference to current context. Cannot be <c>null</c>.</param>
        /// <param name="pattern">The pattern to search for. It can be either a string or an array with strings.</param>
        /// <param name="replacement">The string or an array with strings to replace.
        /// If this parameter is a string and the pattern parameter is an array, all patterns will be
        /// replaced by that string. If both pattern and replacement parameters are arrays, each pattern will be
        /// replaced by the replacement counterpart. If there are fewer elements in the replacement array than
        /// in the pattern array, any extra patterns will be replaced by an empty string.</param>
        /// <param name="subject">The string or an array with strings to search and replace.
        /// If subject is an array, then the search and replace is performed on every entry of subject, and the return value is an array as well.</param>
        /// <param name="limit">The maximum possible replacements for each pattern in each subject string. Defaults to <c>-1</c> (no limit).</param>
        /// <param name="count">This variable will be filled with the number of replacements done.</param>
        /// <returns></returns>
        public static PhpValue preg_replace(Context ctx, PhpValue pattern, PhpValue replacement, PhpValue subject, int limit, out long count)
        {
            count = 0;

            // PHP's behaviour for undocumented limit range
            if (limit < -1)
            {
                limit = 0;
            }

            //
            var replacement_array = replacement.AsArray();
            var pattern_array = pattern.AsArray();

            if (pattern_array == null)
            {
                if (replacement_array == null)
                {
                    // string pattern
                    // string replacement

                    return preg_replace(ctx, pattern.ToStringOrThrow(ctx), replacement.ToStringOrThrow(ctx), null, subject, limit, ref count);
                }
                else
                {
                    // string pattern and array replacement not allowed:
                    throw new ArgumentException("replacement_array_pattern_not", nameof(replacement));
                    // return PhpValue.Null;
                }
            }
            else if (replacement_array == null)
            {
                // array  pattern
                // string replacement
            }
            else
            {
                // array pattern
                // array replacement
            }

            throw new NotImplementedException();
        }
Example #56
0
 /// <summary>
 /// Creates a deep copy of PHP variable.
 /// </summary>
 /// <returns>A deep copy of the value.</returns>
 public virtual PhpValue DeepCopy(ref PhpValue me) => me;
Example #57
0
        /// <summary>
        /// Sets value at specific index. Value must not be an alias.
        /// </summary>
        void IPhpArray.SetItemValue(IntStringKey key, PhpValue value)
        {
            int index = key.IsInteger ? key.Integer : (int)Convert.StringToLongInteger(key.String);

            char ch;

            switch (value.TypeCode)
            {
                case PhpTypeCode.Long:
                    ch = (char)value.Long;
                    break;

                case PhpTypeCode.String:
                    ch = (value.String.Length != 0) ? value.String[0] : '\0';
                    break;

                case PhpTypeCode.WritableString:
                    ch = value.WritableString[0];
                    break;

                // TODO: other types

                default:
                    throw new NotSupportedException(value.TypeCode.ToString());
            }

            this[key.Integer] = ch;
        }
Example #58
0
 /// <summary>
 /// Performs dereferencing and deep copying of the value inplace.
 /// </summary>
 public virtual void PassValue(ref PhpValue me)
 {
 }
Example #59
0
        public virtual bool SetParameter(StreamParameterOptions option, PhpValue value)
        {
            // Do not display error messages here, the caller will.
            // EX: will have to distinguish between failed and unsupported.
            // (use additional message when fails)

            // Descendants may call this default implementation for unhandled options
            switch (option)
            {
                case StreamParameterOptions.BlockingMode:
                    // Unimplemented in Win32 PHP.
                    return false;

                case StreamParameterOptions.ReadBufferSize:
                    // Unused option (only turns buffering off)
                    return false;

                case StreamParameterOptions.WriteBufferSize:
                    if (value.IsInteger())
                    {
                        // Let the write buffer reset on next write operation.
                        FlushWriteBuffer();
                        writeBuffer = null;
                        // Set the new size (0 to disable write buffering).
                        writeBufferSize = (int)value.ToLong();
                        if (writeBufferSize < 0) writeBufferSize = 0;
                        return true;
                    }
                    return false;

                case StreamParameterOptions.ReadTimeout:
                    // Set the read timeout for network-based streams (overrides DefaultTimeout).
                    this.readTimeout = (double)value;
                    return false;

                case StreamParameterOptions.SetChunkSize:
                    if (value.IsInteger())
                    {
                        // This setting will affect reading after the buffers are emptied.
                        readChunkSize = (int)value.ToLong();
                        if (readChunkSize < 1) readChunkSize = 1;
                        return true;
                    }
                    return false;

                case StreamParameterOptions.Locking:
                    return false;

                case StreamParameterOptions.MemoryMap:
                    return false;

                case StreamParameterOptions.Truncate:
                    // EX: [Truncate] Override SetParameter in NativeStream to truncate a local file.
                    return false;

                default:
                    Debug.Assert(false); // invalid option
                    return false;
            }
        }
Example #60
0
 public static stdClass ToObject(PhpString value) => new stdClass(PhpValue.Create(value.DeepCopy()));