Skip to content

hasJSFunctions always returns false, so multi-round JavaScript properties are never processed #17

Description

@justadreamer

hasJSFunctions returns false on every call, so a response that arrives carrying further JavaScript properties to execute is never processed and the flow declares itself complete after a single round.

var hasJSFunctions = function() {
for (var i = i; i < json.javascriptProperties; i++) {
var body = getFromJson(json.javascriptProperties[i]);
if (body !== undefined && body.length > 0) {
return true;
}
}
return false;
}

Two things are wrong with the loop header:

  • var i = i initialises the index from itself. Hoisting means i exists but is undefined at that point, so it stays undefined.
  • The bound is json.javascriptProperties, the array itself, rather than json.javascriptProperties.length. The next line indexes into the same array, so the length is clearly what was intended.

The condition therefore evaluates undefined < ["device.javascripthardwareprofile", ...], both sides are coerced to primitives, and any comparison involving undefined or NaN is false. The body never runs. Correcting only the initialiser would not help either, since 0 < "device.javascript..." is also false.

Effect

The only caller is loadJSON:

if (hasJSFunctions()) {
    // json updated so fire 'on change' functions
    // before executing any new JS properties that
    // have come back.
    fireChangeFuncs(json);
    process(resolve, reject);
} else {
    failed = false;
    completed = true;
    fireChangeFuncs(json);
    resolve(json);
}

Because the test is always false, the else branch is always taken. Multi-round evidence collection, where the first response asks the browser to gather more evidence and a second round of snippets has to run, never happens. device.javascripthardwareprofile is the case this path exists for.

Suggested fix

    var hasJSFunctions = function() {
        if (!json.javascriptProperties) {
            return false;
        }
        for (var i = 0; i < json.javascriptProperties.length; i++) {
            var body = getFromJson(json.javascriptProperties[i]);
            if (body !== undefined && body.length > 0) {
                return true;
            }
        }
        return false;
    }

Why this is worth its own change

Repairing the loop makes process() reachable from loadJSON for the first time, so a code path that has been dead in every shipped version starts running, in every language SDK that consumes this template. It has no test coverage today. Worth landing on its own, with a browser test covering a response that comes back with outstanding JavaScript properties, rather than riding along with an unrelated change.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions