paulbartrum / jurassic

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

Information about the error location is lost #186

Closed Taritsyn closed 3 years ago

Taritsyn commented 3 years ago

Hello, Paul!

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

using System;

using Jurassic;
using Jurassic.Library;

namespace TestJurassicJavaScriptException
{
    class Program
    {
        static void Main(string[] args)
        {
            var engine = new ScriptEngine();
            var source = new StringScriptSource(@"var $variable1 = 611;
var _variable2 = 711;
var variable3 = 678;

$variable1 + -variable2 - variable3;",
            "variables.js");

            try
            {
                int result = engine.Evaluate<int>(source);
                Console.WriteLine("Result: {0}", result);
            }
            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);

                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 error location is lost.

Current version (5a8822c):

Message: ReferenceError: variable2 is not defined.
Name: ReferenceError
Source path: null
Line number: 0
Stack: ReferenceError: variable2 is not defined.

Previous version (14b9886):

Message: ReferenceError: variable2 is not defined
Name: ReferenceError
Source path: variables.js
Line number: 5
Stack: ReferenceError: variable2 is not defined
    at variables.js:5
paulbartrum commented 3 years ago

Thanks for raising this, I rewrote pretty much the entirety of how variables work as part of supporting 'let'. I guess I shouldn't be surprised that I broke something :-)

paulbartrum commented 3 years ago

Fixed in commit a515cc10deb34514b970c177557329e9df4897c0

Taritsyn commented 3 years ago

Thanks!