[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/zhcom888/ClearScript/master/ClearScript/Windows/VBScriptEngine.cs [Back]  [Original]

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
using System.Collections.Generic;
using Microsoft.ClearScript.Util;

namespace Microsoft.ClearScript.Windows
{
    /// 
    /// Represents an instance of the VBScript engine in a desktop environment.
    /// 
    /// 
    /// Each Windows Script engine instance in a desktop environment has thread affinity and is
    /// bound to a  during instantiation.
    /// Attempting to execute script code on a different thread results in an exception. Script
    /// delegates and event handlers are marshaled synchronously onto the correct thread.
    /// 
    public class VBScriptEngine : WindowsScriptEngine, IVBScriptEngineTag
    {
        #region constructors

        /// 
        /// Initializes a new VBScript engine instance.
        /// 
        public VBScriptEngine()
            : this(null)
        {
        }

        /// 
        /// Initializes a new VBScript engine instance with the specified name.
        /// 
        /// A name to associate with the instance. Currently this name is used only as a label in presentation contexts such as debugger user interfaces.
        public VBScriptEngine(string name)
            : this(name, WindowsScriptEngineFlags.None)
        {
        }

        /// 
        /// Initializes a new VBScript engine instance with the specified options.
        /// 
        /// A value that selects options for the operation.
        public VBScriptEngine(WindowsScriptEngineFlags flags)
            : this(null, flags)
        {
        }

        /// 
        /// Initializes a new VBScript engine instance with the specified name and options.
        /// 
        /// A name to associate with the instance. Currently this name is used only as a label in presentation contexts such as debugger user interfaces.
        /// A value that selects options for the operation.
        public VBScriptEngine(string name, WindowsScriptEngineFlags flags)
            : this("VBScript", name, "vbs", flags)
        {
        }

        /// 
        /// Initializes a new VBScript engine instance with the specified programmatic
        /// identifier, name, list of supported file name extensions, and options.
        /// 
        /// The programmatic identifier (ProgID) of the VBScript engine class.
        /// A name to associate with the instance. Currently this name is used only as a label in presentation contexts such as debugger user interfaces.
        /// A semicolon-delimited list of supported file name extensions.
        /// A value that selects options for the operation.
        /// 
        /// The  argument can be a class identifier (CLSID) in standard
        /// GUID format with braces (e.g., "{F414C260-6AC0-11CF-B6D1-00AA00BBBB58}").
        /// 
        protected VBScriptEngine(string progID, string name, string fileNameExtensions, WindowsScriptEngineFlags flags)
            : base(progID, name, fileNameExtensions, flags)
        {
            Execute(MiscHelpers.FormatInvariant("{0} [internal]", GetType().Name), Core.VBScriptEngine.InitScript);
        }

        #endregion

        #region ScriptEngine overrides

        /// 
        /// Gets the script engine's recommended file name extension for script files.
        /// 
        /// 
        ///  instances return "vbs" for this property.
        /// 
        public override string FileNameExtension => "vbs";

        /// 
        /// Executes script code as a command.
        /// 
        /// The script command to execute.
        /// The command output.
        /// 
        /// 
        /// This method is similar to  but optimized for
        /// command consoles. The specified command must be limited to a single expression or
        /// statement. Script engines can override this method to customize command execution as
        /// well as the process of converting the result to a string for console output.
        /// 
        /// 
        /// The  version of this method supports both expressions and
        /// statements. If the specified command begins with "eval " (not case-sensitive), the
        /// engine executes the remainder as an expression and attempts to use
        /// CStr
        /// to convert the result value. Otherwise, it executes the command as a statement and does
        /// not return a value.
        /// 
        /// 
        public override string ExecuteCommand(string command)
        {
            var trimmedCommand = command.Trim();
            if (trimmedCommand.StartsWith("eval ", StringComparison.OrdinalIgnoreCase))
            {
                var expression = MiscHelpers.FormatInvariant("EngineInternal.getCommandResult({0})", trimmedCommand.Substring(5));
                var documentInfo = new DocumentInfo("Expression") { Flags = DocumentFlags.IsTransient };
                return GetCommandResultString(Evaluate(documentInfo.MakeUnique(this), expression, false));
            }

            Execute("Command", true, trimmedCommand);
            return null;
        }

        internal override IDictionary RuntimeErrorMap => Core.VBScriptEngine.StaticRuntimeErrorMap;

        internal override IDictionary SyntaxErrorMap => Core.VBScriptEngine.StaticSyntaxErrorMap;

        internal override object Execute(UniqueDocumentInfo documentInfo, string code, bool evaluate)
        {
            if (FormatCode)
            {
                code = MiscHelpers.FormatCode(code);
            }

            if (documentInfo.Category != DocumentCategory.Script)
            {
                throw new NotSupportedException("The script engine cannot execute documents of type '" + documentInfo.Category + "'");
            }

            return base.Execute(documentInfo, code, evaluate);
        }

        #endregion
    }
}

Web Proxy Viewer  |  New URL  |  Original Page