sebastienros / jint

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

caller is undefined,but it's not undefined in broswer. #1971

Open sgf opened 4 days ago

sgf commented 4 days ago

Version used

4.0.2

Describe the bug

caller is undefined,but it's not undefined in broswer.

To Reproduce

  <script type="text/javascript">
    function Test(Lf_) {
        function h() {
            return getName();
        };
        return h();
        return 'Lf'
    };
    function getName() {
        debugger;
        var caller = getName.caller;
        if (caller.name) {
            return caller.name
        }
        var str = caller.toString().replace(/[\s]*/g, "");
        var name = str.match(/^function([^\(]+?)\(/);
        if (name && name[1]) {
            return name[1];
        } else {
            return '';
        }
    }
    Test();
</script>

Expected behavior

caller should be not undefined,in up code,its should be "h()";

Additional context

im using AngleSharp.Js. https://github.com/AngleSharp/AngleSharp.Js/issues/98

sgf commented 4 days ago

https://github.com/sebastienros/jint/issues/819

Although this is not a standard definition feature. But I think Jint should be closer to the actual behavior of browsers while respecting the standard. After all, in the end, we all aim for practicality.

lahma commented 3 days ago

Would you like to propose a PR to implement this?

sgf commented 3 days ago

Would you like to propose a PR to implement this?

I don't mind propose an RP .

But I'm probably not good at this,due to I'm usually too lazy to write unit tests.

But as far as this function is concerned, there should be no side effects.

Engine.cs >FunctionDeclarationInstantiation

    function.SetProperty(KnownKeys.Caller, new PropertyDescriptor(this.GetExecutionContext(1).Function ?? ScriptFunction.Undefined, PropertyFlag.Configurable));

image