예제 #1
0
        public static T TryObjectToDelegate <T>(object obj, out ConversionStrictness strictness)
            where T : class
        {
            T      result   = null;
            object bare_obj = PhpVariable.Unwrap(obj);

            if (bare_obj == null || (result = bare_obj as T) != null)
            {
                strictness = ConversionStrictness.ImplExactMatch;
                return(result);
            }

            // try to convert the object to PhpCallback
            PhpCallback callback = Convert.ObjectToCallback(obj, true);

            if (callback != null && callback.Bind(true))
            {
                // generate a conversion stub
                result = EventClass <T> .GetStub(
                    callback.TargetInstance,
                    callback.TargetRoutine,
                    callback.IsBoundToCaller?callback.RoutineName : null);

                if (result != null)
                {
                    strictness = ConversionStrictness.ImplExactMatch;
                    return(result);
                }
            }

            strictness = ConversionStrictness.Failed;
            return(default(T));
        }
예제 #2
0
        /// <summary>
        /// Checks whether a target of callback argument can be invoked.
        /// </summary>
        /// <param name="callback">The callback to check.</param>
        /// <param name="caller">The class context used to bind the callback function.</param>
        /// <param name="argIndex">The index of argument starting from 1 or 0 if not applicable.</param>
        /// <param name="argName">A name of the argument or <B>null</B> reference if not applicable.</param>
        /// <param name="emptyAllowed">Whether an empty callback is allowed.</param>
        /// <returns>Whether the callback can be bound to its target or it is empty and that is allowed.</returns>
        /// <remarks>The callback is bound if it can be.</remarks>
        public static bool CheckCallback(PhpCallback callback, DTypeDesc caller, string argName, int argIndex, bool emptyAllowed)
        {
            // error has already been reported by Convert.ObjectToCallback:
            if (callback == PhpCallback.Invalid)
            {
                return(false);
            }

            // callback is empty:
            if (callback == null)
            {
                return(emptyAllowed);
            }

            // callback is bindable:
            if (callback.Bind(true, caller, null))
            {
                return(true);
            }

            if (argName != null)
            {
                argName = String.Concat('\'', argName, '\'');
            }
            else
            {
                argName = "#" + argIndex;
            }

            PhpException.Throw(PhpError.Warning, CoreResources.GetString("noncallable_callback",
                                                                         ((IPhpConvertible)callback).ToString(), argName));

            return(false);
        }
예제 #3
0
파일: CLR.cs 프로젝트: dw4dev/Phalanger
		public static bool CreateClrThread(ScriptContext/*!*/context, PhpCallback/*!*/ callback, params object[] args)
		{
			if (callback == null)
				PhpException.ArgumentNull("callback");

			if (!callback.Bind())
				return false;

			object[] copies = (args != null) ? new object[args.Length] : ArrayUtils.EmptyObjects;

			for (int i = 0; i < copies.Length; i++)
				copies[i] = PhpVariable.DeepCopy(args[i]);

            return ThreadPool.QueueUserWorkItem(new Worker(context, copies).Run, callback);
		}
예제 #4
0
		private object InvokeFunctionCore(string name, params object[] args)
		{
			// check whether this function is allowed to be called
			PhpCallback callback;
			if (allFunctionsRegistered)
			{
				registeredFunctions.TryGetValue(name, out callback);
			}
			else
			{
				if (registeredFunctions.TryGetValue(name, out callback))
				{
					PhpException.Throw(PhpError.Warning, String.Format(Resources.HandlerNotAllowed, name));
					return null;
				}
			}

			// if the callback does not already exists, create it
			if (callback == null)
			{
				// parse name
				int index = name.IndexOf("::");
				switch (index)
				{
					case -1: callback = new PhpCallback(name); break;
					case 0: callback = new PhpCallback(name.Substring(2)); break;
					default: callback = new PhpCallback(name.Substring(0, index), name.Substring(index + 2)); break;
				}

				if (!callback.Bind()) return null;

				registeredFunctions[name] = callback;
			}

			// convert arguments
			for (int i = 0; i < args.Length; i++) args[i] = XsltConvertor.DotNetToPhp(args[i]);

			// invoke!
			return callback.Invoke(args);
		}
예제 #5
0
		public static bool Start(PhpCallback filter, int chunkSize, bool erase)
		{
			if (chunkSize != 0)
				PhpException.ArgumentValueNotSupported("chunkSize", "!= 0");
			if (!erase)
				PhpException.ArgumentValueNotSupported("erase", erase);

			ScriptContext context = ScriptContext.CurrentContext;

			context.BufferedOutput.IncreaseLevel();

			bool result = true;

			// skips filter setting if filter is not specified or valid:
			if (filter != null && (result = filter.Bind()))
				context.BufferedOutput.SetFilter(filter);

			context.IsOutputBuffered = true;
			return result;
		}
예제 #6
0
        public static bool Register(NamingContext namingContext, DTypeDesc caller, PhpCallback autoloadFunction, bool throwError, bool prepend)
        {
            if (autoloadFunction == null)
            {
                PhpException.ArgumentNull("autoloadFunction");
                return false;
            }

            if (autoloadFunction.Bind(!throwError, caller, namingContext))
            {
                var context = ScriptContext.CurrentContext;
                if (FindAutoloadFunction(context, autoloadFunction.ToPhpRepresentation()) != null)
                    return false;
                
                if (prepend)
                    context.SplAutoloadFunctions.AddFirst(autoloadFunction);
                else
                    context.SplAutoloadFunctions.AddLast(autoloadFunction);

                return true;
            }
            else
            {
                return false;
            }
        }
예제 #7
0
        ///// <summary>
        ///// Invoked from <c>ExtManager</c> in order to call a (user or system) function. See <c>call_user_function</c>,
        ///// <c>call_user_function_ex</c>, <c>zend_call_function</c> Zend API.
        ///// </summary>
        ///// <param name="target">Designation of the function/method to call.</param>
        ///// <param name="args">Arguments to be passed to the function.</param>
        ///// <returns>Return value of the function.</returns>
        ///// <remarks>
        ///// <p>
        ///// Obsolete: The reason why self is <c>ref</c> is that we need Remoting to
        ///// serialize and transfer the new state of the instance back to the caller.
        ///// </p>
        ///// <p>
        ///// The state is not transferred at all for user-classes (that are usually used for callbacks).
        ///// </p>
        ///// </remarks>
        //public object CallFunction(PhpCallback target, ref object[] args)
        //{
        //    this.callbackTarget = target;
        //    this.callbackArgs = args;

        //    callbackInvoked.Set();
        //    callbackHandled.WaitOne();

        //    return this.callbackRetValue;
        //}

		/// <summary>
		/// Checks whether the function/method designated by <paramref name="callback"/> is callable.
		/// </summary>
		/// <param name="callback">The callback.</param>
		/// <returns><B>true</B> if the <paramref name="callback"/> is callable, <B>false</B> otherwise.
		/// </returns>
		public static bool CheckCallbackDirect(PhpCallback callback)
		{
			return callback.Bind(true);
		}