|
3 | 3 | /** |
4 | 4 | * Parser node backed by a native (Rust) AST. |
5 | 5 | * |
6 | | - * Constructed by the native MySQL parser extension. Read methods delegate |
7 | | - * into the Rust-owned AST so children are never copied into PHP unless a |
8 | | - * caller actually walks the tree. On the first mutation (append_child or |
9 | | - * merge_fragment), the node materializes its children into the inherited |
10 | | - * `$children` array and behaves like a plain WP_Parser_Node from then on. |
| 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. |
11 | 29 | */ |
12 | 30 | class WP_MySQL_Native_Parser_Node extends WP_Parser_Node { |
13 | 31 | private $native_ast = null; |
@@ -164,10 +182,30 @@ public function get_length(): int { |
164 | 182 | return wp_sqlite_mysql_native_ast_get_length( $this->native_ast, $this->native_node_index ); |
165 | 183 | } |
166 | 184 |
|
| 185 | + /** |
| 186 | + * Indicates whether this node has been mutated from PHP. |
| 187 | + * |
| 188 | + * Returns false for freshly-parsed nodes whose children still live in the |
| 189 | + * Rust-owned AST buffer; returns true once `append_child()` or |
| 190 | + * `merge_fragment()` has copied the children into the inherited |
| 191 | + * `$children` array and dropped the native AST reference. |
| 192 | + * |
| 193 | + * This is a per-instance state check, not a check for whether the native |
| 194 | + * extension is loaded. |
| 195 | + */ |
167 | 196 | private function was_mutated(): bool { |
168 | 197 | return $this->was_mutated; |
169 | 198 | } |
170 | 199 |
|
| 200 | + /** |
| 201 | + * Copies native children into the inherited PHP $children array and drops |
| 202 | + * the native AST reference for this node. |
| 203 | + * |
| 204 | + * Called before any mutation (append_child, merge_fragment) so the node's |
| 205 | + * authoritative state lives in PHP from that point on. After this runs, |
| 206 | + * was_mutated() returns true and read methods fall through to the parent |
| 207 | + * WP_Parser_Node implementation. |
| 208 | + */ |
171 | 209 | private function materialize_native_children(): void { |
172 | 210 | if ( $this->was_mutated ) { |
173 | 211 | return; |
|
0 commit comments