Skip to content

Commit 271106d

Browse files
committed
Do not lookup pathed helpers on the helper stack
Fixes #764
1 parent 1fb7b51 commit 271106d

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

lib/handlebars/compiler/compiler.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ Compiler.prototype = {
280280
id.falsy = true;
281281

282282
this.ID(id);
283-
this.opcode('invokeHelper', params.length, id.original, sexpr.isRoot);
283+
this.opcode('invokeHelper', params.length, id.original, id.isSimple, sexpr.isRoot);
284284
}
285285
},
286286

lib/handlebars/compiler/javascript-compiler.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ JavaScriptCompiler.prototype = {
369369
return ' != null ? ' + lookup + ' : ' + current;
370370
} else {
371371
// Otherwise we can use generic falsy handling
372-
return ' != null && ' + lookup;
372+
return (falsy ? ' && ' : ' != null && ') + lookup;
373373
}
374374
}, true);
375375
}
@@ -527,19 +527,18 @@ JavaScriptCompiler.prototype = {
527527
// and pushes the helper's return value onto the stack.
528528
//
529529
// If the helper is not found, `helperMissing` is called.
530-
invokeHelper: function(paramSize, name, isRoot) {
530+
invokeHelper: function(paramSize, name, isSimple, isRoot) {
531531
this.aliases.helperMissing = 'helpers.helperMissing';
532-
this.useRegister('helper');
533532

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

537-
var lookup = 'helper = ' + helper.name + ' || ' + nonHelper + ' || helperMissing';
536+
var lookup = (isSimple ? helper.name + ' || ' : '') + nonHelper + ' || helperMissing';
538537
if (helper.paramsInit) {
539538
lookup += ',' + helper.paramsInit;
540539
}
541540

542-
this.push('(' + lookup + ',helper.call(' + helper.callParams + '))');
541+
this.push('((' + lookup + ').call(' + helper.callParams + '))');
543542

544543
// Always flush subexpressions. This is both to prevent the compounding size issue that
545544
// occurs when the code has to be duplicated for inlining and also to prevent errors

spec/helpers.js

+16
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,22 @@ describe('helpers', function() {
158158
shouldCompileTo(messageString, [rootMessage, { list: list }], "<p>Nobody&#x27;s here</p>", "the context of an inverse is the parent of the block");
159159
});
160160

161+
it('pathed lambas with parameters', function() {
162+
var hash = {
163+
helper: function() {
164+
return 'winning';
165+
}
166+
};
167+
hash.hash = hash;
168+
var helpers = {
169+
'./helper': function() {
170+
return 'fail';
171+
}
172+
};
173+
shouldCompileTo('{{./helper 1}}', [hash, helpers], 'winning');
174+
shouldCompileTo('{{hash/helper 1}}', [hash, helpers], 'winning');
175+
});
176+
161177
describe("helpers hash", function() {
162178
it("providing a helpers hash", function() {
163179
shouldCompileTo("Goodbye {{cruel}} {{world}}!", [{cruel: "cruel"}, {world: function() { return "world"; }}], "Goodbye cruel world!",

0 commit comments

Comments
 (0)