Clearscript: Debug with Visual Studio Code - duplicate loaded scripts

Created on 28 Jun 2019  路  2Comments  路  Source: microsoft/ClearScript

I debug javascript running on V8 engine with Visual Studio Code. Whenever I execute a script, the debugger loads a copy of that script. Is this normal?

image

Below is my code:

using Microsoft.ClearScript;
using Microsoft.ClearScript.V8;
using System;

namespace ClearScriptDemo
{
    class Person
    {
        public string GetName()
        {
            return "John Doe";
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            using (var engine = new V8ScriptEngine(V8ScriptEngineFlags.EnableDebugging, 9222))
            {
                // expose a host type
                engine.AddHostType("Console", typeof(Console));

                // expose a host object
                engine.AccessContext = typeof(Program);
                engine.AddHostObject("person", new Person());

                DocumentInfo documentInfo = new DocumentInfo("My Script");

                Console.WriteLine("Press any key to run script, ESC to exit.");

                while (Console.ReadKey().Key != ConsoleKey.Escape)
                {
                    engine.Execute(documentInfo,
                        "Console.WriteLine('{0} is an interesting number.', Math.PI);\r\n" +
                        "Console.WriteLine(person.GetName());");
                }
            }
        }
    }
}
question

All 2 comments

Hi @tu-lehong,

This behavior is by design. It's meant to ensure that each script is accessible in the debugger even if the host provided the same name for all of them.

You can avoid this behavior by specifying a URI instead of a name:

C# Uri uri = new Uri("My Script", UriKind.RelativeOrAbsolute); DocumentInfo documentInfo = new DocumentInfo(uri);

Keep in mind however that a script with a given URI clobbers any previous script with the same URI, making the latter inaccessible in the debugger.

Good luck!

Thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kugarliyifan picture kugarliyifan  路  6Comments

JohnLudlow picture JohnLudlow  路  4Comments

JohnLudlow picture JohnLudlow  路  4Comments

yevgeni-zolotko picture yevgeni-zolotko  路  5Comments

roblarky picture roblarky  路  3Comments