Skip to content

Commit

Permalink
Do not lookup pathed helpers on the helper stack
Browse files Browse the repository at this point in the history
Fixes #764
  • Loading branch information
kpdecker committed Jul 12, 2014
1 parent 1fb7b51 commit 271106d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/handlebars/compiler/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ Compiler.prototype = {
id.falsy = true;

this.ID(id);
this.opcode('invokeHelper', params.length, id.original, sexpr.isRoot);
this.opcode('invokeHelper', params.length, id.original, id.isSimple, sexpr.isRoot);
}
},

Expand Down
9 changes: 4 additions & 5 deletions lib/handlebars/compiler/javascript-compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ JavaScriptCompiler.prototype = {
return ' != null ? ' + lookup + ' : ' + current;
} else {
// Otherwise we can use generic falsy handling
return ' != null && ' + lookup;
return (falsy ? ' && ' : ' != null && ') + lookup;
}
}, true);
}
Expand Down Expand Up @@ -527,19 +527,18 @@ JavaScriptCompiler.prototype = {
// and pushes the helper's return value onto the stack.
//
// If the helper is not found, `helperMissing` is called.
invokeHelper: function(paramSize, name, isRoot) {
invokeHelper: function(paramSize, name, isSimple, isRoot) {
this.aliases.helperMissing = 'helpers.helperMissing';
this.useRegister('helper');

var nonHelper = this.popStack();
var helper = this.setupHelper(paramSize, name);

var lookup = 'helper = ' + helper.name + ' || ' + nonHelper + ' || helperMissing';
var lookup = (isSimple ? helper.name + ' || ' : '') + nonHelper + ' || helperMissing';
if (helper.paramsInit) {
lookup += ',' + helper.paramsInit;
}

this.push('(' + lookup + ',helper.call(' + helper.callParams + '))');
this.push('((' + lookup + ').call(' + helper.callParams + '))');

// Always flush subexpressions. This is both to prevent the compounding size issue that
// occurs when the code has to be duplicated for inlining and also to prevent errors
Expand Down
16 changes: 16 additions & 0 deletions spec/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,22 @@ describe('helpers', function() {
shouldCompileTo(messageString, [rootMessage, { list: list }], "<p>Nobody&#x27;s here</p>", "the context of an inverse is the parent of the block");
});

it('pathed lambas with parameters', function() {
var hash = {
helper: function() {
return 'winning';
}
};
hash.hash = hash;
var helpers = {
'./helper': function() {
return 'fail';
}
};
shouldCompileTo('{{./helper 1}}', [hash, helpers], 'winning');
shouldCompileTo('{{hash/helper 1}}', [hash, helpers], 'winning');
});

describe("helpers hash", function() {
it("providing a helpers hash", function() {
shouldCompileTo("Goodbye {{cruel}} {{world}}!", [{cruel: "cruel"}, {world: function() { return "world"; }}], "Goodbye cruel world!",
Expand Down

0 comments on commit 271106d

Please sign in to comment.