Skip to content

Commit dfeff6f

Browse files
committed
Rename native node helper to was_mutated()
The previous helper name (has_unmaterialized_native_ast) implied a runtime check for native-extension presence. It's actually a per-instance state flag tracking whether this node's children have been copied into PHP. was_mutated() reads that intent more directly.
1 parent 31a2778 commit dfeff6f

1 file changed

Lines changed: 43 additions & 5 deletions

File tree

packages/mysql-on-sqlite/src/mysql/native/class-wp-mysql-native-parser-node.php

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,29 @@
33
/**
44
* Parser node backed by a native (Rust) AST.
55
*
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.
1129
*/
1230
class WP_MySQL_Native_Parser_Node extends WP_Parser_Node {
1331
private $native_ast = null;
@@ -164,10 +182,30 @@ public function get_length(): int {
164182
return wp_sqlite_mysql_native_ast_get_length( $this->native_ast, $this->native_node_index );
165183
}
166184

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+
*/
167196
private function was_mutated(): bool {
168197
return $this->was_mutated;
169198
}
170199

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+
*/
171209
private function materialize_native_children(): void {
172210
if ( $this->was_mutated ) {
173211
return;

0 commit comments

Comments
 (0)