From b0e52c333c0c411768cdc2eda709b3e83b0ce76c Mon Sep 17 00:00:00 2001 From: Mohamed Akram Date: Wed, 4 Sep 2024 17:36:54 +0400 Subject: [PATCH] Simplify helper wrapper This improves performance slightly and gives the wrapper function a more descriptive name to help with debugging/profiling. --- lib/handlebars/internal/wrapHelper.js | 13 ------------- lib/handlebars/runtime.js | 17 +++++++++++------ 2 files changed, 11 insertions(+), 19 deletions(-) delete mode 100644 lib/handlebars/internal/wrapHelper.js diff --git a/lib/handlebars/internal/wrapHelper.js b/lib/handlebars/internal/wrapHelper.js deleted file mode 100644 index 29d65b033..000000000 --- a/lib/handlebars/internal/wrapHelper.js +++ /dev/null @@ -1,13 +0,0 @@ -export function wrapHelper(helper, transformOptionsFn) { - if (typeof helper !== 'function') { - // This should not happen, but apparently it does in https://github.com/wycats/handlebars.js/issues/1639 - // We try to make the wrapper least-invasive by not wrapping it, if the helper is not a function. - return helper; - } - let wrapper = function(/* dynamic arguments */) { - const options = arguments[arguments.length - 1]; - arguments[arguments.length - 1] = transformOptionsFn(options); - return helper.apply(this, arguments); - }; - return wrapper; -} diff --git a/lib/handlebars/runtime.js b/lib/handlebars/runtime.js index e6b3f9c84..614fb9c11 100644 --- a/lib/handlebars/runtime.js +++ b/lib/handlebars/runtime.js @@ -7,7 +7,6 @@ import { REVISION_CHANGES } from './base'; import { moveHelperToHooks } from './helpers'; -import { wrapHelper } from './internal/wrapHelper'; import { createProtoAccessControl, resultIsAllowed @@ -426,14 +425,20 @@ function addHelpers(mergedHelpers, helpers, container) { if (!helpers) return; Object.keys(helpers).forEach(helperName => { let helper = helpers[helperName]; - mergedHelpers[helperName] = passLookupPropertyOption(helper, container); + mergedHelpers[helperName] = wrapHelper(helper, container); }); } -function passLookupPropertyOption(helper, container) { +function wrapHelper(helper, container) { + if (typeof helper !== 'function') { + // This should not happen, but apparently it does in https://github.com/wycats/handlebars.js/issues/1639 + // We try to make the wrapper least-invasive by not wrapping it, if the helper is not a function. + return helper; + } const lookupProperty = container.lookupProperty; - return wrapHelper(helper, options => { + return function invokeHelper(/* dynamic arguments */) { + const options = arguments[arguments.length - 1]; options.lookupProperty = lookupProperty; - return options; - }); + return helper.apply(this, arguments); + }; }