// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. using System; using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.ClearScript.JavaScript; using Microsoft.ClearScript.Util; namespace Microsoft.ClearScript.Windows { // ReSharper disable once PartialTypeWithSinglePart /// /// Represents an instance of the JScript 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 partial class JScriptEngine : WindowsScriptEngine, IJavaScriptEngine, Core.IJScriptEngine { #region data private CommonJSManager commonJSManager; #endregion #region constructors /// /// Initializes a new JScript engine instance. /// public JScriptEngine() : this(null) { } /// /// Initializes a new JScript 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 JScriptEngine(string name) : this(name, WindowsScriptEngineFlags.None) { } /// /// Initializes a new JScript engine instance with the specified options. /// /// A value that selects options for the operation. public JScriptEngine(WindowsScriptEngineFlags flags) : this(null, flags) { } /// /// Initializes a new JScript 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 JScriptEngine(string name, WindowsScriptEngineFlags flags) : this("JScript", name, "js", flags) { } /// /// Initializes a new JScript engine instance with the specified programmatic /// identifier, name, list of supported file name extensions, and options. /// /// The programmatic identifier (ProgID) of the JScript 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 JScriptEngine(string progID, string name, string fileNameExtensions, WindowsScriptEngineFlags flags) : base(progID, name, fileNameExtensions, flags) { Execute(MiscHelpers.FormatInvariant("{0} [internal]", GetType().Name), Core.JScriptEngine.InitScript); } #endregion #region internal members private CommonJSManager CommonJSManager => commonJSManager ?? (commonJSManager = new CommonJSManager(this)); #endregion #region ScriptEngine overrides /// /// Gets the script engine's recommended file name extension for script files. /// /// /// instances return "js" for this property. /// public override string FileNameExtension => "js"; /// /// 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 attempts to use /// toString /// to convert the return value. /// /// public override string ExecuteCommand(string command) { Script.EngineInternal.command = command; return base.ExecuteCommand("EngineInternal.getCommandResult(eval(EngineInternal.command))"); } internal override IDictionary RuntimeErrorMap => Core.JScriptEngine.StaticRuntimeErrorMap; internal override IDictionary SyntaxErrorMap => Core.JScriptEngine.StaticSyntaxErrorMap; internal override object Execute(UniqueDocumentInfo documentInfo, string code, bool evaluate) { if (FormatCode) { code = MiscHelpers.FormatCode(code); } if (documentInfo.Category == ModuleCategory.CommonJS) { var module = CommonJSManager.GetOrCreateModule(documentInfo, code); return ScriptInvoke(() => module.Process()); } 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 #region IJavaScriptEngine implementation uint IJavaScriptEngine.BaseLanguageVersion => 3; object IJavaScriptEngine.CreatePromiseForTask(Task task) { throw new NotImplementedException(); } object IJavaScriptEngine.CreatePromiseForTask(Task task) { throw new NotImplementedException(); } Task IJavaScriptEngine.CreateTaskForPromise(ScriptObject promise) { throw new NotImplementedException(); } #endregion } }