Skip to content
Taritsyn edited this page Sep 1, 2024 · 13 revisions

JavaScriptEngineSwitcher.Yantra contains a YantraJsEngine adapter (wrapper for the YantraJS version 1.2.225).

Engine settings

You can specify a settings of JS engine during its registration:

engineSwitcher.EngineFactories
    .AddYantra(new YantraSettings
    {
        ConsoleCallback = (string type, object[] args) =>
        {
            TextWriter writer = type == "error" ? Console.Error : Console.Out;

            if (type != "log")
            {
                ConsoleColor typeColor;

                switch (type)
                {
                    case "warn":
                        typeColor = ConsoleColor.Yellow;
                        break;
                    case "error":
                        typeColor = ConsoleColor.Red;
                        break;
                    default:
                        typeColor = ConsoleColor.White;
                        break;
                }

                Console.ForegroundColor = typeColor;
                writer.Write("{0}: ", type);
            }

            Console.ForegroundColor = ConsoleColor.White;

            for (int argIndex = 0; argIndex < args.Length; argIndex++)
            {
                if (argIndex > 0)
                {
                    writer.Write(" ");
                }

                writer.Write(args[argIndex] ?? "null");
            }

            writer.WriteLine();
        }
    })
    ;

If you manually create an instance of JS engine, then you can pass settings via the constructor:

IJsEngine engine = new YantraJsEngine(
    new YantraSettings
    {
        ConsoleCallback = (string type, object[] args) =>
        {

        }
    }
);

Consider in detail properties of the YantraSettings class:

Property name Data type Default value Description
ConsoleCallback YantraJsConsoleCallback delegate null JS debugging console callback.
Debugger YantraJS.Debugger.JSDebugger null Instance of JS debugger (for example, the YantraJS.Core.Debugger.V8Debugger)