Skip to content

Commit 398ff22

Browse files
committed
Use lightweight options for inline helpers
1 parent ecdaf53 commit 398ff22

3 files changed

Lines changed: 121 additions & 80 deletions

File tree

‎src/BlockHelperOptions.php‎

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
namespace DevTheorem\Handlebars;
4+
5+
use Closure;
6+
7+
/**
8+
* @internal
9+
*/
10+
final class BlockHelperOptions extends HelperOptions
11+
{
12+
/**
13+
* @param array<mixed> $hash
14+
* @param array<mixed> $data
15+
* @param array<string> $blockParamNames
16+
*/
17+
public function __construct(
18+
string $name,
19+
array $hash,
20+
int $blockParams,
21+
mixed &$scope,
22+
array &$data,
23+
private readonly RuntimeContext $cx,
24+
private readonly ?Closure $cb = null,
25+
private readonly ?Closure $inv = null,
26+
private readonly array $blockParamNames = [],
27+
) {
28+
parent::__construct($name, $hash, $blockParams, $scope, $data);
29+
}
30+
31+
/**
32+
* Allows isset($options->fn) and isset($options->inverse) to check whether the block exists.
33+
*/
34+
public function __isset(string $name): bool
35+
{
36+
if ($name === 'fn') {
37+
return $this->cb !== null;
38+
} elseif ($name === 'inverse') {
39+
return $this->inv !== null;
40+
}
41+
return false;
42+
}
43+
44+
public function fn(mixed $context = Scope::Use, mixed $data = null): string
45+
{
46+
if ($this->cb === null) {
47+
return '';
48+
}
49+
$cx = $this->cx;
50+
$cb = $this->cb;
51+
$scope = $this->scope;
52+
53+
$savedPartials = $cx->partials;
54+
55+
if (isset($data['data'])) {
56+
$savedFrame = $cx->frame;
57+
if (count($savedFrame) === 1) {
58+
// Fast path: only root in frame, no user @-data to inherit
59+
$newFrame = $data['data'];
60+
} else {
61+
$newFrame = array_replace($savedFrame, $data['data']);
62+
}
63+
$newFrame['root'] = &$cx->data['root'];
64+
$newFrame['_parent'] = $savedFrame;
65+
$cx->frame = $newFrame;
66+
}
67+
68+
$hasBp = isset($data['blockParams']) && $this->blockParamNames;
69+
if ($hasBp) {
70+
$ex = [];
71+
foreach ($this->blockParamNames as $i => $name) {
72+
$ex[$name] = $data['blockParams'][$i];
73+
}
74+
array_unshift($cx->blParam, $ex);
75+
}
76+
77+
if ($context === $scope) {
78+
// fn($currentContext): explicit same-context pass, no depths push.
79+
// Equivalent to HBS.js options.fn(this).
80+
$ret = $cb($cx, $scope);
81+
} else {
82+
// fn() or fn($newContext): push enclosing context onto depths.
83+
// fn() equivalent to HBS.js options.fn() (undefined context uses current scope).
84+
$cx->depths[] = $scope;
85+
$ret = $cb($cx, $context === Scope::Use ? $scope : $context);
86+
array_pop($cx->depths);
87+
}
88+
89+
if (isset($savedFrame)) {
90+
$cx->frame = $savedFrame;
91+
}
92+
$cx->partials = $savedPartials;
93+
if ($hasBp) {
94+
array_shift($cx->blParam);
95+
}
96+
return $ret;
97+
}
98+
99+
public function inverse(mixed $context = null): string
100+
{
101+
if ($this->inv === null) {
102+
return '';
103+
}
104+
$cx = $this->cx;
105+
$inv = $this->inv;
106+
107+
if ($context === null) {
108+
return $inv($cx, $this->scope);
109+
}
110+
$cx->depths[] = $this->scope;
111+
$ret = $inv($cx, $context);
112+
array_pop($cx->depths);
113+
return $ret;
114+
}
115+
}

‎src/HelperOptions.php‎

Lines changed: 3 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace DevTheorem\Handlebars;
44

5-
use Closure;
6-
75
/** @internal */
86
enum Scope
97
{
@@ -18,103 +16,32 @@ class HelperOptions
1816
/**
1917
* @param array<mixed> $hash
2018
* @param array<mixed> $data
21-
* @param array<string> $blockParamNames
2219
*/
2320
public function __construct(
2421
public readonly string $name,
2522
public readonly array $hash,
2623
public readonly int $blockParams,
2724
public mixed &$scope,
2825
public array &$data,
29-
private readonly RuntimeContext $cx,
30-
private readonly ?Closure $cb = null,
31-
private readonly ?Closure $inv = null,
32-
private readonly array $blockParamNames = [],
3326
) {}
3427

3528
/**
3629
* Allows isset($options->fn) and isset($options->inverse) to check whether the block exists.
30+
* Always returns false for inline helpers.
3731
*/
3832
public function __isset(string $name): bool
3933
{
40-
if ($name === 'fn') {
41-
return $this->cb !== null;
42-
} elseif ($name === 'inverse') {
43-
return $this->inv !== null;
44-
}
4534
return false;
4635
}
4736

4837
public function fn(mixed $context = Scope::Use, mixed $data = null): string
4938
{
50-
if ($this->cb === null) {
51-
return '';
52-
}
53-
$cx = $this->cx;
54-
$cb = $this->cb;
55-
$scope = $this->scope;
56-
57-
$savedPartials = $cx->partials;
58-
59-
if (isset($data['data'])) {
60-
$savedFrame = $cx->frame;
61-
if (count($savedFrame) === 1) {
62-
// Fast path: only root in frame, no user @-data to inherit
63-
$newFrame = $data['data'];
64-
} else {
65-
$newFrame = array_replace($savedFrame, $data['data']);
66-
}
67-
$newFrame['root'] = &$cx->data['root'];
68-
$newFrame['_parent'] = $savedFrame;
69-
$cx->frame = $newFrame;
70-
}
71-
72-
$hasBp = isset($data['blockParams']) && $this->blockParamNames;
73-
if ($hasBp) {
74-
$ex = [];
75-
foreach ($this->blockParamNames as $i => $name) {
76-
$ex[$name] = $data['blockParams'][$i];
77-
}
78-
array_unshift($cx->blParam, $ex);
79-
}
80-
81-
if ($context === $scope) {
82-
// fn($currentContext): explicit same-context pass, no depths push.
83-
// Equivalent to HBS.js options.fn(this).
84-
$ret = $cb($cx, $scope);
85-
} else {
86-
// fn() or fn($newContext): push enclosing context onto depths.
87-
// fn() equivalent to HBS.js options.fn() (undefined context uses current scope).
88-
$cx->depths[] = $scope;
89-
$ret = $cb($cx, $context === Scope::Use ? $scope : $context);
90-
array_pop($cx->depths);
91-
}
92-
93-
if (isset($savedFrame)) {
94-
$cx->frame = $savedFrame;
95-
}
96-
$cx->partials = $savedPartials;
97-
if ($hasBp) {
98-
array_shift($cx->blParam);
99-
}
100-
return $ret;
39+
return '';
10140
}
10241

10342
public function inverse(mixed $context = null): string
10443
{
105-
if ($this->inv === null) {
106-
return '';
107-
}
108-
$cx = $this->cx;
109-
$inv = $this->inv;
110-
111-
if ($context === null) {
112-
return $inv($cx, $this->scope);
113-
}
114-
$cx->depths[] = $this->scope;
115-
$ret = $inv($cx, $context);
116-
array_pop($cx->depths);
117-
return $ret;
44+
return '';
11845
}
11946

12047
public function lookupProperty(mixed $obj, string $key): mixed

‎src/Runtime.php‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ public static function sec(RuntimeContext $cx, mixed $v, mixed $in, \Closure $cb
337337
// Lambda functions in block position receive HelperOptions directly.
338338
// This must be checked before blockHelperMissing routing.
339339
if ($v instanceof \Closure) {
340-
$options = new HelperOptions(
340+
$options = new BlockHelperOptions(
341341
name: '',
342342
hash: [],
343343
blockParams: 0,
@@ -504,7 +504,6 @@ private static function invokeInlineHelper(RuntimeContext $cx, \Closure $helper,
504504
blockParams: 0,
505505
scope: $_this,
506506
data: $cx->frame,
507-
cx: $cx,
508507
);
509508
$args = $positional;
510509
$args[] = $options;
@@ -518,7 +517,7 @@ private static function invokeInlineHelper(RuntimeContext $cx, \Closure $helper,
518517
*/
519518
private static function invokeBlockHelper(RuntimeContext $cx, \Closure $helper, string $name, array $positional, array $hash, array $blockParamNames, mixed &$_this, ?\Closure $cb, ?\Closure $else): string
520519
{
521-
$options = new HelperOptions(
520+
$options = new BlockHelperOptions(
522521
name: $name,
523522
hash: $hash,
524523
blockParams: count($blockParamNames),
@@ -624,7 +623,7 @@ public static function dynhbbch(RuntimeContext $cx, string $name, mixed $callabl
624623
);
625624
}
626625

627-
$options = new HelperOptions(
626+
$options = new BlockHelperOptions(
628627
name: '',
629628
hash: $hash,
630629
blockParams: count($blockParamNames),

0 commit comments

Comments
 (0)