sebastienros / jint

Javascript Interpreter for .NET
BSD 2-Clause "Simplified" License
4.02k stars 556 forks source link

Engine.Execute fails when executing Array.map (d3.js) #1899

Closed JCKortlang closed 1 month ago

JCKortlang commented 1 month ago

Version used

3.1.3

Describe the bug

Engine.Execute fails with

Jint.Runtime.JavaScriptException: Cannot read property 'length' of undefined
 ---> Error: Cannot read property 'length' of undefined
   at map <anonymous>:17033:27
   at factory (exports) <anonymous>:17072:20
   at (anonymous) (global, factory) <anonymous>:5:88
   at <anonymous>:6:2
   --- End of inner exception stack trace ---

when executing https://observablehq.com/plot/d3.js

    function colors(specifier) {
        var n = specifier.length / 6 | 0, colors = new Array(n), i = 0;       //L 17033:27
        while (i < n) colors[i] = "#" + specifier.slice(i * 6, ++i * 6);
        return colors;
    }
    var scheme$q = new Array(3).concat(
        "d8b365f5f5f55ab4ac",
        "a6611adfc27d80cdc1018571",
        "a6611adfc27df5f5f580cdc1018571",
        "8c510ad8b365f6e8c3c7eae55ab4ac01665e",
        "8c510ad8b365f6e8c3f5f5f5c7eae55ab4ac01665e",
        "8c510abf812ddfc27df6e8c3c7eae580cdc135978f01665e",
        "8c510abf812ddfc27df6e8c3f5f5f5c7eae580cdc135978f01665e",
        "5430058c510abf812ddfc27df6e8c3c7eae580cdc135978f01665e003c30",
        "5430058c510abf812ddfc27df6e8c3f5f5f5c7eae580cdc135978f01665e003c30"
    ).map(colors); //17072:20

Unable to reproduce in browser execution context.

(function() {
  "use strict";
    function colors(specifier) {
        var n = specifier.length / 6 | 0, colors = new Array(n), i = 0;       //L 17033:27
        while (i < n) colors[i] = "#" + specifier.slice(i * 6, ++i * 6);
        return colors;
    }
        return new Array(3).map(colors)

})() 
 //Result: Array(3) [ <3 empty slots> ] length: 3 <prototype>: Array []

To Reproduce

    [Test]
    public void Test1()
    {
        //Assuming d3.js has been downloaded locally and added to .csproj
        // Source: https://observablehq.com/plot/d3.js
        // <None Update="d3.js">
        //     <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        // </None>
        string path = Path.Combine(TestContext.CurrentContext.TestDirectory, "d3.js");

        var engine = new Engine(o =>
        {
            // o.Strict = true;
            o.DebugMode();
        });
        engine = engine.Execute(File.ReadAllText(path), new ScriptParsingOptions
        {
            AllowReturnOutsideFunction = true,
            CompileRegex = false,
            Tolerant = true
        });

Expected behavior

I expect the execution to succeed without error

Additional context

I am attempting to execute the module and read the module interface in C#.

lahma commented 1 month ago

Thanks for the detailed repro, should now be fixed in 3.x maintenance branch and main.