Exemple #1
0
 extern public static void NewClosure(IntPtr v, SqFunction func, int nfreevars);
        /// <summary>
        /// Registers a new function inside the squirrel VM.
        /// </summary>
        /// <param name="squirrelApi">The used instance of the <see cref="ISquirrelApi"/>.</param>
        /// <param name="functionName">The name of the new function</param>
        /// <param name="function">The delegate for the new function.</param>
        public static void RegisterFunction(this ISquirrelApi squirrelApi, string functionName, SqFunction function)
        {
            if (squirrelApi == null)
            {
                throw new ArgumentNullException(nameof(squirrelApi));
            }

            if (function == null)
            {
                throw new ArgumentNullException(nameof(function));
            }

            if (string.IsNullOrEmpty(functionName))
            {
                throw new ArgumentException("Value cannot be null or empty.", nameof(functionName));
            }

            squirrelApi.SqPushRootTable();
            squirrelApi.SqPushString(functionName);
            squirrelApi.SqNewClosure(function, 0);
            squirrelApi.SqNewSlot(-3, false);
            squirrelApi.SqPop(1);
        }