示例#1
0
        private void Parse(DocumentInfo documentInfo, string code, ScriptTextFlags flags, IntPtr pVarResult, out EXCEPINFO excepInfo)
        {
            var formattedCode = FormatCode ? MiscHelpers.FormatCode(code) : code;

            DebugDocument debugDocument;
            var           sourceContext = CreateDebugDocument(documentInfo, formattedCode, out debugDocument);

            if (sourceContext != UIntPtr.Zero)
            {
                flags |= ScriptTextFlags.HostManagesSource;
            }

            try
            {
                activeScript.ParseScriptText(formattedCode, null, null, null, sourceContext, 0, flags, pVarResult, out excepInfo);
            }
            finally
            {
                if (documentInfo.Flags.GetValueOrDefault().HasFlag(DocumentFlags.IsTransient) && (sourceContext != UIntPtr.Zero))
                {
                    debugDocumentMap.Remove(sourceContext);
                    debugDocument.Close();
                }
            }
        }
        internal override object Execute(string documentName, string code, bool evaluate, bool discard)
        {
            VerifyNotDisposed();

            return(ScriptInvoke(() =>
            {
                var uniqueName = documentNameManager.GetUniqueName(documentName, "Script Document");
                if (discard)
                {
                    uniqueName += " [temp]";
                }
                else
                {
                    documentNames.Add(uniqueName);
                }

                var stateObjects = new object[2];
                using (var timer = new Timer(OnContinuationTimer, stateObjects, Timeout.Infinite, Timeout.Infinite))
                {
                    stateObjects[0] = new WeakReference(this);
                    stateObjects[1] = timer;
                    timer.Change(continuationInterval, Timeout.Infinite);
                    return proxy.Execute(uniqueName, FormatCode ? MiscHelpers.FormatCode(code) : code, discard);
                }
            }));
        }
示例#3
0
        private void Parse(string documentName, string code, ScriptTextFlags flags, IntPtr pVarResult, out EXCEPINFO excepInfo, bool discard)
        {
            var formattedCode = FormatCode ? MiscHelpers.FormatCode(code) : code;

            DebugDocument debugDocument;
            var           sourceContext = CreateDebugDocument(documentName, formattedCode, discard, out debugDocument);

            if (sourceContext != UIntPtr.Zero)
            {
                flags |= ScriptTextFlags.HostManagesSource;
            }

            try
            {
                activeScript.ParseScriptText(formattedCode, null, null, null, sourceContext, 0, flags, pVarResult, out excepInfo);
            }
            finally
            {
                if (discard && (sourceContext != UIntPtr.Zero))
                {
                    debugDocumentMap.Remove(sourceContext);
                    debugDocument.Close();
                }
            }
        }
示例#4
0
        /// <summary>
        /// Creates a compiled script with an associated document name.
        /// </summary>
        /// <param name="documentName">A document name for the compiled script. Currently this name is used only as a label in presentation contexts such as debugger user interfaces.</param>
        /// <param name="code">The script code to compile.</param>
        /// <returns>A compiled script that can be executed by multiple V8 script engine instances.</returns>
        public V8Script Compile(string documentName, string code)
        {
            VerifyNotDisposed();
            var uniqueName = name + ":" + documentNameManager.GetUniqueName(documentName, "Script Document");

            return(proxy.Compile(uniqueName, FormatCode ? MiscHelpers.FormatCode(code) : code));
        }
示例#5
0
        /// <summary>
        /// Creates a compiled script with an associated document name, consuming previously generated cache data.
        /// </summary>
        /// <param name="documentName">A document name for the compiled script. Currently this name is used only as a label in presentation contexts such as debugger user interfaces.</param>
        /// <param name="code">The script code to compile.</param>
        /// <param name="cacheKind">The kind of cache data to be consumed.</param>
        /// <param name="cacheBytes">Cache data for accelerated compilation.</param>
        /// <param name="cacheAccepted"><c>True</c> if <paramref name="cacheBytes"/> was accepted, <c>false</c> otherwise.</param>
        /// <returns>A compiled script that can be executed by multiple V8 script engine instances.</returns>
        /// <remarks>
        /// To be accepted, the cache data must have been generated for identical script code by
        /// the same V8 build. V8 runtimes with debugging enabled cannot consume cache data.
        /// </remarks>
        /// <seealso cref="Compile(string, string, V8CacheKind, out byte[])"/>
        public V8Script Compile(string documentName, string code, V8CacheKind cacheKind, byte[] cacheBytes, out bool cacheAccepted)
        {
            VerifyNotDisposed();
            var uniqueName = name + ":" + documentNameManager.GetUniqueName(documentName, "Script Document");

            return(proxy.Compile(uniqueName, FormatCode ? MiscHelpers.FormatCode(code) : code, cacheKind, cacheBytes, out cacheAccepted));
        }
示例#6
0
        /// <summary>
        /// Creates a compiled script with the specified document information.
        /// </summary>
        /// <param name="documentInfo">A structure containing information about the script document.</param>
        /// <param name="code">The script code to compile.</param>
        /// <returns>A compiled script that can be executed multiple times without recompilation.</returns>
        public V8Script Compile(DocumentInfo documentInfo, string code)
        {
            VerifyNotDisposed();

            return(ScriptInvoke(() =>
            {
                documentInfo.UniqueName = documentNameManager.GetUniqueName(documentInfo.Name, DocumentInfo.DefaultName);
                return proxy.Compile(documentInfo, FormatCode ? MiscHelpers.FormatCode(code) : code);
            }));
        }
        /// <summary>
        /// Creates a compiled script with an associated document name.
        /// </summary>
        /// <param name="documentName">A document name for the compiled script. Currently this name is used only as a label in presentation contexts such as debugger user interfaces.</param>
        /// <param name="code">The script code to compile.</param>
        /// <returns>A compiled script that can be executed multiple times without recompilation.</returns>
        public V8Script Compile(string documentName, string code)
        {
            VerifyNotDisposed();

            return(ScriptInvoke(() =>
            {
                var uniqueName = documentNameManager.GetUniqueName(documentName, "Script Document");
                return proxy.Compile(uniqueName, FormatCode ? MiscHelpers.FormatCode(code) : code);
            }));
        }
示例#8
0
        public void VBScriptEngine_General()
        {
            using (var console = new StringWriter())
            {
                var clr = new HostTypeCollection(type => type != typeof(Console), "mscorlib", "System", "System.Core");
                clr.GetNamespaceNode("System").SetPropertyNoCheck("Console", console);

                engine.AddHostObject("host", new ExtendedHostFunctions());
                engine.AddHostObject("clr", clr);

                engine.Execute(generalScript);
                Assert.AreEqual(MiscHelpers.FormatCode(generalScriptOutput), console.ToString().Replace("\r\n", "\n"));
            }
        }
示例#9
0
        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));
        }
示例#10
0
        private V8Script CompileInternal(UniqueDocumentInfo documentInfo, string code, V8CacheKind cacheKind, byte[] cacheBytes, out bool cacheAccepted)
        {
            if (FormatCode)
            {
                code = MiscHelpers.FormatCode(code);
            }

            if (documentInfo.Category == ModuleCategory.CommonJS)
            {
                code = CommonJSManager.Module.GetAugmentedCode(code);
            }

            return(proxy.Compile(documentInfo, code, cacheKind, cacheBytes, out cacheAccepted));
        }
示例#11
0
        private V8Script CompileInternal(UniqueDocumentInfo documentInfo, string code)
        {
            if (FormatCode)
            {
                code = MiscHelpers.FormatCode(code);
            }

            if (documentInfo.Category == ModuleCategory.CommonJS)
            {
                code = CommonJSManager.Module.GetAugmentedCode(code);
            }

            return(proxy.Compile(documentInfo, code));
        }
        internal override object Execute(string documentName, string code, bool evaluate, bool discard)
        {
            VerifyNotDisposed();

            return(ScriptInvoke(() =>
            {
                var uniqueName = documentNameManager.GetUniqueName(documentName, "Script Document");
                if (discard)
                {
                    uniqueName += " [temp]";
                }
                else if (documentNames != null)
                {
                    documentNames.Add(uniqueName);
                }

                if (inContinuationTimerScope || (ContinuationCallback == null))
                {
                    if (MiscHelpers.Exchange(ref awaitDebuggerAndPause, false))
                    {
                        proxy.AwaitDebuggerAndPause();
                    }

                    return proxy.Execute(uniqueName, FormatCode ? MiscHelpers.FormatCode(code) : code, evaluate, discard);
                }

                var state = new Timer[] { null };
                using (state[0] = new Timer(unused => OnContinuationTimer(state[0]), null, Timeout.Infinite, Timeout.Infinite))
                {
                    inContinuationTimerScope = true;
                    try
                    {
                        state[0].Change(continuationInterval, Timeout.Infinite);

                        if (MiscHelpers.Exchange(ref awaitDebuggerAndPause, false))
                        {
                            proxy.AwaitDebuggerAndPause();
                        }

                        return proxy.Execute(uniqueName, FormatCode ? MiscHelpers.FormatCode(code) : code, evaluate, discard);
                    }
                    finally
                    {
                        inContinuationTimerScope = false;
                    }
                }
            }));
        }
        /// <summary>
        /// Creates a compiled script with an associated document name, consuming previously generated cache data.
        /// </summary>
        /// <param name="documentName">A document name for the compiled script. Currently this name is used only as a label in presentation contexts such as debugger user interfaces.</param>
        /// <param name="code">The script code to compile.</param>
        /// <param name="cacheKind">The kind of cache data to be consumed.</param>
        /// <param name="cacheBytes">Cache data for accelerated compilation.</param>
        /// <param name="cacheAccepted"><c>True</c> if <paramref name="cacheBytes"/> was accepted, <c>false</c> otherwise.</param>
        /// <returns>A compiled script that can be executed multiple times without recompilation.</returns>
        /// <remarks>
        /// To be accepted, the cache data must have been generated for identical script code by
        /// the same V8 build. V8 script engines with debugging enabled cannot consume cache data.
        /// </remarks>
        /// <seealso cref="Compile(string, string, V8CacheKind, out byte[])"/>
        public V8Script Compile(string documentName, string code, V8CacheKind cacheKind, byte[] cacheBytes, out bool cacheAccepted)
        {
            VerifyNotDisposed();

            V8Script tempScript = null;

            cacheAccepted = ScriptInvoke(() =>
            {
                bool tempCacheAccepted;
                var uniqueName = documentNameManager.GetUniqueName(documentName, "Script Document");
                tempScript     = proxy.Compile(uniqueName, FormatCode ? MiscHelpers.FormatCode(code) : code, cacheKind, cacheBytes, out tempCacheAccepted);
                return(tempCacheAccepted);
            });

            return(tempScript);
        }
示例#14
0
        /// <summary>
        /// Creates a compiled script with the specified document information, generating cache data for accelerated recompilation.
        /// </summary>
        /// <param name="documentInfo">A structure containing information about the script document.</param>
        /// <param name="code">The script code to compile.</param>
        /// <param name="cacheKind">The kind of cache data to be generated.</param>
        /// <param name="cacheBytes">Cache data for accelerated recompilation.</param>
        /// <returns>A compiled script that can be executed multiple times without recompilation.</returns>
        /// <remarks>
        /// The generated cache data can be stored externally and is usable in other V8 script
        /// engines and application processes. V8 script engines with debugging enabled cannot
        /// generate cache data.
        /// </remarks>
        /// <seealso cref="Compile(DocumentInfo, string, V8CacheKind, byte[], out bool)"/>
        public V8Script Compile(DocumentInfo documentInfo, string code, V8CacheKind cacheKind, out byte[] cacheBytes)
        {
            VerifyNotDisposed();

            V8Script tempScript = null;

            cacheBytes = ScriptInvoke(() =>
            {
                byte[] tempCacheBytes;
                documentInfo.UniqueName = documentNameManager.GetUniqueName(documentInfo.Name, DocumentInfo.DefaultName);
                tempScript = proxy.Compile(documentInfo, FormatCode ? MiscHelpers.FormatCode(code) : code, cacheKind, out tempCacheBytes);
                return(tempCacheBytes);
            });

            return(tempScript);
        }
示例#15
0
            private object Require(string specifier)
            {
                var settings = engine.DocumentSettings;
                var document = settings.LoadDocument(DocumentInfo.Info, specifier, ModuleCategory.CommonJS, null);

                var code = document.GetTextContents();

                if (engine.FormatCode)
                {
                    code = MiscHelpers.FormatCode(code);
                }

                var target = manager.GetOrCreateModule(document.Info.MakeUnique(engine), code);

                target.Process();

                return(target.exports);
            }
示例#16
0
        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));
        }
示例#17
0
 /// <summary>
 /// Creates a compiled script with the specified document information, consuming previously generated cache data.
 /// </summary>
 /// <param name="documentInfo">A structure containing information about the script document.</param>
 /// <param name="code">The script code to compile.</param>
 /// <param name="cacheKind">The kind of cache data to be consumed.</param>
 /// <param name="cacheBytes">Cache data for accelerated compilation.</param>
 /// <param name="cacheAccepted"><c>True</c> if <paramref name="cacheBytes"/> was accepted, <c>false</c> otherwise.</param>
 /// <returns>A compiled script that can be executed by multiple V8 script engine instances.</returns>
 /// <remarks>
 /// To be accepted, the cache data must have been generated for identical script code by
 /// the same V8 build. V8 runtimes with debugging enabled cannot consume cache data.
 /// </remarks>
 /// <seealso cref="Compile(DocumentInfo, string, V8CacheKind, out byte[])"/>
 public V8Script Compile(DocumentInfo documentInfo, string code, V8CacheKind cacheKind, byte[] cacheBytes, out bool cacheAccepted)
 {
     VerifyNotDisposed();
     documentInfo.UniqueName = name + ":" + documentNameManager.GetUniqueName(documentInfo.Name, DocumentInfo.DefaultName);
     return(proxy.Compile(documentInfo, FormatCode ? MiscHelpers.FormatCode(code) : code, cacheKind, cacheBytes, out cacheAccepted));
 }