Exemple #1
0
        /// <summary>
        /// Resets the current Lua evaluation session with the specified module content and module title.
        /// </summary>
        /// <param name="moduleContent">Lua module content. The return value of the expression will be the return value of the module.</param>
        /// <param name="moduleTitle">Title of the Lua module, or <c>null</c> to use a dummy module name. To properly evaluate Lua modules, the title should start with <c>Module:</c> namespace prefix.</param>
        /// <param name="cancellationToken">A token used to cancel the operation.</param>
        /// <exception cref="ScribuntoConsoleException">There is Scribunto console error evaluating module content.</exception>
        /// <exception cref="UnexpectedDataException">Cannot validate Scribunto console working properly. The sanity test does not pass.</exception>
        /// <exception cref="NotSupportedException">The MediaWiki site does not support Scribunto console.</exception>
        /// <remarks>
        /// <para>Upon reset of the console, this client library will attempt to evaluate <c>=_VERSION</c> and validates
        /// whether server can return any value (even if it's <c>nil</c>). This is the sanity test.</para>
        /// <para>This operation usually does not change <see cref="SessionId" />, if it's already has a valid value.
        /// To create a new Lua evaluation session with a different <seealso cref="SessionId"/>,
        /// create a new <seealso cref="ScribuntoConsole"/> instance.</para>
        /// </remarks>
        public async Task ResetAsync(string moduleContent, string moduleTitle, CancellationToken cancellationToken)
        {
            if (moduleTitle == null)
            {
                moduleTitle = AdhocModuleTitlePrefix;
            }
            ModuleTitle = moduleTitle;
            ScribuntoEvaluationResult result = null;

            try
            {
                result = await InvokeApiAsync(Site, _SessionId, moduleTitle, moduleContent, "=_VERSION", true, cancellationToken);

                if (string.IsNullOrEmpty(result.ReturnValue))
                {
                    throw new UnexpectedDataException(Prompts.ExceptionScribuntoResetCannotValidate);
                }
            }
            catch (ScribuntoConsoleException ex)
            {
                result = ex.EvaluationResult;
                throw;
            }
            finally
            {
                if (result != null)
                {
                    _SessionId     = result.SessionId;
                    SessionSize    = result.SessionSize;
                    SessionMaxSize = result.SessionMaxSize;
                }
            }
        }
Exemple #2
0
 public ScribuntoConsoleException(string errorCode, string errorMessage, ScribuntoEvaluationResult evaluationResult, string message)
     : base(message ?? MakeMessage(errorCode, errorMessage))
 {
     ErrorCode        = errorCode;
     ErrorMessage     = errorMessage;
     EvaluationResult = evaluationResult;
 }
Exemple #3
0
        /// <summary>
        /// Evaluates the specified Lua expression in the console.
        /// </summary>
        /// <param name="expression">The Lua expression to be evaluated.</param>
        /// <param name="cancellationToken">A token used to cancel the operation.</param>
        /// <exception cref="ScribuntoConsoleException">There is Scribunto console error evaluating module content.</exception>
        /// <returns>The console evaluation result.</returns>
        public async Task <ScribuntoEvaluationResult> EvaluateAsync(string expression, CancellationToken cancellationToken)
        {
            ScribuntoEvaluationResult result = null;

            try
            {
                result = await InvokeApiAsync(Site, _SessionId, ModuleTitle, null, expression, false, cancellationToken);

                return(result);
            }
            catch (ScribuntoConsoleException ex)
            {
                result = ex.EvaluationResult;
                throw;
            }
            finally
            {
                if (result != null)
                {
                    _SessionId     = result.SessionId;
                    SessionSize    = result.SessionSize;
                    SessionMaxSize = result.SessionMaxSize;
                }
            }
        }
Exemple #4
0
 public ScribuntoConsoleException(string errorCode, string errorMessage, ScribuntoEvaluationResult evaluationResult)
     : this(errorCode, errorMessage, evaluationResult, null)
 {
 }