|
3 | 3 | /** |
4 | 4 | * Native-backed parser node. |
5 | 5 | * |
6 | | - * This subclass keeps the regular WP_Parser_Node API while delegating lazy AST |
7 | | - * reads to the optional native MySQL parser extension. The base node remains a |
8 | | - * plain PHP tree node for the polyfill parser. |
| 6 | + * Instances of this class are constructed exclusively by the native MySQL |
| 7 | + * parser extension: when the extension parses a query, it produces a tree of |
| 8 | + * `WP_MySQL_Native_Parser_Node` objects whose `$native_ast` and |
| 9 | + * `$native_node_index` fields point into a Rust-owned AST buffer. Read methods |
| 10 | + * (`get_start`, `has_child`, `get_children`, ...) delegate to the extension so |
| 11 | + * children are never materialized into PHP arrays unless something actually |
| 12 | + * asks for them. |
| 13 | + * |
| 14 | + * Read methods eagerly call `materialize_native_children()` — once the |
| 15 | + * children have been copied into PHP, `was_mutated()` returns true and the |
| 16 | + * call falls through to the parent implementation. The `was_mutated` flag is |
| 17 | + * NOT a runtime check for whether the native extension is loaded — if this |
| 18 | + * class is in use, the extension is loaded by definition. It tracks whether |
| 19 | + * THIS specific node has had its children pulled into the inherited |
| 20 | + * `$children` array (which happens on first read or first mutation via |
| 21 | + * `append_child()` / `merge_fragment()`). From that point on, the node is a |
| 22 | + * plain PHP-backed `WP_Parser_Node`. |
| 23 | + * |
| 24 | + * Mutation from PHP is real and intentional — query rewriters in |
| 25 | + * `WP_PDO_MySQL_On_SQLite` (e.g. building synthetic `count(*)` expressions) |
| 26 | + * call `append_child()` on parsed nodes. The lazy-then-materialize design |
| 27 | + * keeps the fast path (read-only traversal) cheap while still allowing |
| 28 | + * mutation when callers need it. |
9 | 29 | */ |
10 | 30 | class WP_MySQL_Native_Parser_Node extends WP_Parser_Node { |
11 | 31 | private $native_ast = null; |
12 | 32 | private $native_node_index = null; |
| 33 | + private $was_mutated = false; |
13 | 34 |
|
14 | 35 | public function __construct( $rule_id, $rule_name, $native_ast = null, $native_node_index = null ) { |
15 | 36 | parent::__construct( $rule_id, $rule_name ); |
@@ -129,17 +150,38 @@ public function get_length(): int { |
129 | 150 | return parent::get_length(); |
130 | 151 | } |
131 | 152 |
|
132 | | - private function has_native_ast(): bool { |
133 | | - return null !== $this->native_ast; |
134 | | - } |
135 | | - |
| 153 | + /** |
| 154 | + * Indicates whether this node has been mutated from PHP. |
| 155 | + * |
| 156 | + * Returns false for freshly-parsed nodes whose children still live in the |
| 157 | + * Rust-owned AST buffer; returns true once `append_child()` or |
| 158 | + * `merge_fragment()` has copied the children into the inherited |
| 159 | + * `$children` array and dropped the native AST reference. |
| 160 | + * |
| 161 | + * This is a per-instance state check, not a check for whether the native |
| 162 | + * extension is loaded. |
| 163 | + */ |
| 164 | + private function was_mutated(): bool { |
| 165 | + return $this->was_mutated; |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Copies native children into the inherited PHP $children array and drops |
| 170 | + * the native AST reference for this node. |
| 171 | + * |
| 172 | + * Called before any mutation (append_child, merge_fragment) so the node's |
| 173 | + * authoritative state lives in PHP from that point on. After this runs, |
| 174 | + * was_mutated() returns true and read methods fall through to the parent |
| 175 | + * WP_Parser_Node implementation. |
| 176 | + */ |
136 | 177 | private function materialize_native_children(): void { |
137 | | - if ( ! $this->has_native_ast() ) { |
| 178 | + if ( $this->was_mutated ) { |
138 | 179 | return; |
139 | 180 | } |
140 | 181 |
|
141 | 182 | $this->children = wp_sqlite_mysql_native_ast_get_children( $this->native_ast, $this->native_node_index ); |
142 | 183 | $this->native_ast = null; |
143 | 184 | $this->native_node_index = null; |
| 185 | + $this->was_mutated = true; |
144 | 186 | } |
145 | 187 | } |
0 commit comments