Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function declaration invokes the function body #29

Closed
RoboPhred opened this issue Feb 20, 2020 · 3 comments
Closed

Function declaration invokes the function body #29

RoboPhred opened this issue Feb 20, 2020 · 3 comments

Comments

@RoboPhred
Copy link
Contributor

RoboPhred commented Feb 20, 2020

Declaring a function causes the function to be invoked. This is particularly nasty for array iteration methods, where the iterator will get invoked an extra time with a null value.

Here is a test that pinpoints the issue:

test('function declaration does not invoke function', function(t) {
    t.plan(1);

    var invoked = false;
    var variables = {
        noop: function(){},
        onInvoke: function() {invoked = true}
    };
    var src = `noop(function(){ onInvoke(); })`;
    var ast = parse(src).body[0].expression;
    evaluate(ast, variables);
    t.equal(invoked, false);
})

Here is a failing test that demonstrates the array use case:

test('array methods invocation count', function(t) {
    t.plan(2);

    var variables = {
        values: [1, 2, 3],
        receiver: []
    };
    var src = 'values.forEach(function(x) { receiver.push(x); })'
    var ast = parse(src).body[0].expression;
    evaluate(ast, variables);
    t.equal(variables.receiver.length, 3);
    t.deepEqual(variables.receiver, [1, 2, 3]);
})

The result of the deepEqual check is:

    operator: deepEqual
    expected: [ 1, 2, 3 ]
    actual:   [ null, 1, 2, 3 ]
@RoboPhred RoboPhred changed the title Function declaration invokes function body Array iteration methods have extra invocation with a null value. Feb 20, 2020
@RoboPhred RoboPhred changed the title Array iteration methods have extra invocation with a null value. Function declaration invokes the function body Feb 20, 2020
@RoboPhred
Copy link
Contributor Author

The issue seems to be the walk on the function body here. We probably need a "checkOnly" flag to walk to suppress code invocation and allow it to simply check the validity of the body.

@RoboPhred
Copy link
Contributor Author

Possible fix: #30

@RoboPhred
Copy link
Contributor Author

Fix has been merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant