Esempio n. 1
0
        /// <summary>
        /// A callback used by <see cref="GetExtensionFunctions"/> method. Adds a function to the resulting array as a key.
        /// </summary>
        private static bool AddFunctionToHashtable(MethodInfo info, ImplementsFunctionAttribute ifa, object result)
        {
            if ((ifa.Options & FunctionImplOptions.NotSupported) == 0)
            {
                ((Hashtable)result)[ifa.Name] = null;
            }

            return(true);
        }
Esempio n. 2
0
 private static bool FunctionsCallback(MethodInfo method, ImplementsFunctionAttribute ifa, object result)
 {
     if ((ifa.Options & FunctionImplOptions.Internal) == 0 && (ifa.Options & FunctionImplOptions.NotSupported) == 0)
     {
         PhpArray array = (PhpArray)result;
         ((PhpArray)array["name"]).Add(ifa.Name);
         ((PhpArray)array["type"]).Add(method.DeclaringType.FullName);
         ((PhpArray)array["method"]).Add(method.Name);
     }
     return(true);
 }
Esempio n. 3
0
        /// <summary>
        /// Reflect argfull stubs from the list of given methods.
        /// </summary>
        /// <param name="realMethods">List of MethodInfos to reflect.</param>
        /// <param name="functions">Dictionary of reflected functions.</param>
        /// <param name="lookForArgless">True to look for argless stub in <c>realMethods</c> if argless stub was not found in <c>functions</c>.</param>
        private void FullReflectFunctions(MethodInfo[] /*!!*/ realMethods, Dictionary <string, DRoutineDesc> /*!*/ functions, bool lookForArgless)
        {
            int i = 0;

            while (i < realMethods.Length)
            {
                MethodInfo method = realMethods[i];

                if (!method.IsGenericMethodDefinition)
                {
                    ImplementsFunctionAttribute impl_func = ImplementsFunctionAttribute.Reflect(method);
                    if (impl_func != null)
                    {
                        DRoutineDesc desc;

                        // argless is reflected already
                        // or argless was not reflected yet and can be found in realMethods list,
                        // otherwise an exception is thrown:
                        if (functions.TryGetValue(impl_func.Name, out desc) ||
                            (lookForArgless && (desc = FindArglessStub(realMethods, functions, method, impl_func)) != null) ||
                            (!Configuration.IsLoaded && !Configuration.IsBeingLoaded && (desc = AddEmptyArglessStub(functions, impl_func.Name)) != null))
                        {
                            if (desc.Member == null)
                            {
                                // first argfull overload occurrence //

                                // estimate overload count by counting subsequent methods with the same CLR name (heuristics):
                                int j = i + 1;
                                while (j < realMethods.Length && realMethods[j].Name == method.Name)
                                {
                                    j++;
                                }
                                int estimated_overload_count = j - i;

                                new PhpLibraryFunction((PhpLibraryFunctionDesc)desc, new Name(impl_func.Name), impl_func.Options,
                                                       estimated_overload_count);
                            }

                            PhpLibraryFunction.Overload overload;
                            desc.PhpLibraryFunction.AddOverload(method, out overload);

                            //if (NeedsArglessAttribute.IsSet(method))
                            //{/* function should be called via argless stub, occurs only if library function is defined in PHP library and calls arg-aware function inside */}
                        }
                        else
                        {
                            throw new ReflectionException(CoreResources.GetString("invalid_class_library_wrapper", dynamicWrapper.CodeBase));
                        }
                    }
                }

                i++;
            }
        }
Esempio n. 4
0
        ///// <summary>
        ///// Reflect <c>Module</c> with global declarations.
        ///// </summary>
        ///// <param name="functions">Dictionary of reflected functions.</param>
        ///// <param name="constants">Dictionary of reflected constants.</param>
        ///// <param name="module">The module to reflect functions and constants from.</param>
        //private void ReflectGlobals(
        //    //Dictionary<string, DTypeDesc>/*!*/ types,
        //    Dictionary<string, DRoutineDesc>/*!*/ functions,
        //    DualDictionary<string, DConstantDesc>/*!*/ constants,
        //    Module/*!*/module)
        //{
        //    // functions (arglesses & argfulls):
        //    FullReflectFunctions(module.GetMethods(BindingFlags.Public | BindingFlags.Static), functions, true);

        //    // constants:
        //    ReflectConstants(module.GetFields(BindingFlags.Public | BindingFlags.Static), constants);
        //}

        /// <summary>
        /// Find the MethodInfo representing argless stub for the specified method. If the method
        /// is found, it will be added using <c>AddArglessStub</c>.
        /// </summary>
        /// <param name="realMethods">List of MethodInfos to search from.</param>
        /// <param name="functions">Dictionary of reflected functions.</param>
        /// <param name="method">Argfull overload which argless stub is searched.</param>
        /// <param name="impl_func">ImplementsFunction attribute of tjhe <c>method</c>.</param>
        /// <returns>DRoutineDesc of argless stub or null if it was not found.</returns>
        private DRoutineDesc FindArglessStub(
            MethodInfo[] /*!!*/ realMethods, Dictionary <string, DRoutineDesc> /*!*/ functions,
            MethodInfo /*!*/ method, ImplementsFunctionAttribute /*!*/ impl_func)
        {
            foreach (var argless in realMethods)
            {
                if (argless.Name == method.Name && argless != method &&
                    PhpFunctionUtils.IsArglessStub(argless, null))
                {
                    // we have found the argless stub for the impl_func in the realMethods list
                    return(AddArglessStub(functions, argless, impl_func.Name));
                }
            }

            return(null);
        }
Esempio n. 5
0
        private static Dictionary <string, List <PhpLibraryFunction.Overload> > /*!*/ GetLibraryFunctions(Type attr, Assembly /*!*/ assembly)
        {
            Dictionary <string, List <PhpLibraryFunction.Overload> > result =
                new Dictionary <string, List <PhpLibraryFunction.Overload> >(500);

            foreach (Type type in assembly.GetTypes())
            {
                if (PhpLibraryModule.IsLibraryType(type))
                {
                    foreach (MethodInfo method in type.GetMethods(BindingFlags.Public | BindingFlags.Static))
                    {
                        if (!method.IsGenericMethodDefinition)
                        {
                            ImplementsFunctionAttribute impl_func =
                                (attr == null)?
                                ImplementsFunctionAttribute.Reflect(method):
                                ImplementsFunctionAttribute.ReflectDynamic(attr, method);

                            if (impl_func != null)
                            {
                                List <PhpLibraryFunction.Overload> overloads;
                                if (!result.TryGetValue(impl_func.Name, out overloads))
                                {
                                    overloads = new List <PhpLibraryFunction.Overload>();
                                    result.Add(impl_func.Name, overloads);
                                }

                                if (PhpLibraryFunction.AddOverload(overloads, method, impl_func.Options) == DRoutine.InvalidOverloadIndex)
                                {
                                    throw new ReflectionException(CoreResources.GetString("invalid_class_library", assembly.CodeBase));
                                }
                            }
                        }
                    }
                }
            }

            return(result);
        }
Esempio n. 6
0
        ///// <summary>
        ///// Reflect <c>Module</c> with global declarations.
        ///// </summary>
        ///// <param name="functions">Dictionary of reflected functions.</param>
        ///// <param name="constants">Dictionary of reflected constants.</param>
        ///// <param name="module">The module to reflect functions and constants from.</param>
        //private void ReflectGlobals(
        //    //Dictionary<string, DTypeDesc>/*!*/ types,
        //    Dictionary<string, DRoutineDesc>/*!*/ functions,
        //    DualDictionary<string, DConstantDesc>/*!*/ constants,
        //    Module/*!*/module)
        //{
        //    // functions (arglesses & argfulls):
        //    FullReflectFunctions(module.GetMethods(BindingFlags.Public | BindingFlags.Static), functions, true);

        //    // constants:
        //    ReflectConstants(module.GetFields(BindingFlags.Public | BindingFlags.Static), constants);
        //}

        /// <summary>
        /// Find the MethodInfo representing argless stub for the specified method. If the method
        /// is found, it will be added using <c>AddArglessStub</c>.
        /// </summary>
        /// <param name="realMethods">List of MethodInfos to search from.</param>
        /// <param name="functions">Dictionary of reflected functions.</param>
        /// <param name="method">Argfull overload which argless stub is searched.</param>
        /// <param name="impl_func">ImplementsFunction attribute of tjhe <c>method</c>.</param>
        /// <returns>DRoutineDesc of argless stub or null if it was not found.</returns>
        private DRoutineDesc FindArglessStub(
            MethodInfo[]/*!!*/ realMethods, Dictionary<string, DRoutineDesc>/*!*/ functions,
            MethodInfo/*!*/method, ImplementsFunctionAttribute/*!*/impl_func)
        {
            foreach (var argless in realMethods)
                if (argless.Name == method.Name && argless != method &&
                    PhpFunctionUtils.IsArglessStub(argless, null))
                {
                    // we have found the argless stub for the impl_func in the realMethods list
                    return AddArglessStub(functions, argless, impl_func.Name);
                }

            return null;
        }