paulbartrum / jurassic

A .NET library to parse and execute JavaScript code.
MIT License
868 stars 121 forks source link

Information about the function where the error occurred is lost #188

Open Taritsyn opened 3 years ago

Taritsyn commented 3 years ago

Hello, Paul!

If you run the following console application by using the latest version of Jurassic (2f1eb7b):

using System;

using Jurassic;
using Jurassic.Library;

namespace TestJurassicJavaScriptException
{
    class Program
    {
        static void Main(string[] args)
        {
            var engine = new ScriptEngine();
            var source = new StringScriptSource(@"function foo(x, y) {
    var z = x + y;
    if (z > 20) {
        bar();
    }
}

(function (foo) {
    var a = 8;
    var b = 15;

    foo(a, b);
})(foo);",
            "functions.js");

            try
            {
                engine.Execute(source);
            }
            catch (JavaScriptException e)
            {
                Console.WriteLine("During working of JavaScript engine an error occurred.");
                Console.WriteLine();
                Console.WriteLine("Message: {0}", e.Message);
                Console.WriteLine("Name: {0}", e.Name);
                Console.WriteLine("Source path: {0}", e.SourcePath ?? "null");
                Console.WriteLine("Line number: {0}", e.LineNumber);
                Console.WriteLine("Function name: {0}", e.FunctionName);

                var errorValue = e.ErrorObject as ErrorInstance;
                if (errorValue != null)
                {
                    Console.WriteLine("Stack: {0}", errorValue.Stack);
                }
            }

            Console.ReadLine();
        }
    }
}

Then you will see that information about the function where the error occurred is lost.

Current version (2f1eb7b):

…
Function name:
Stack: ReferenceError: bar is not defined.
    at functions.js:4
    at anonymous (functions.js:12)
    at functions.js:8

Previous version (14b9886):

…
Function name: foo
Stack: ReferenceError: bar is not defined
    at foo (functions.js:4)
    at anonymous (functions.js:12)
    at functions.js:8
Taritsyn commented 3 years ago

ping

paulbartrum commented 3 years ago

I'm not happy with the way stack traces work now, so it's on my radar.