Skip to content

Commit 57a6d43

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 c1dc9b3 commit 57a6d43

1 file changed

Lines changed: 50 additions & 8 deletions

File tree

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

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,34 @@
33
/**
44
* Native-backed parser node.
55
*
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.
929
*/
1030
class WP_MySQL_Native_Parser_Node extends WP_Parser_Node {
1131
private $native_ast = null;
1232
private $native_node_index = null;
33+
private $was_mutated = false;
1334

1435
public function __construct( $rule_id, $rule_name, $native_ast = null, $native_node_index = null ) {
1536
parent::__construct( $rule_id, $rule_name );
@@ -129,17 +150,38 @@ public function get_length(): int {
129150
return parent::get_length();
130151
}
131152

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+
*/
136177
private function materialize_native_children(): void {
137-
if ( ! $this->has_native_ast() ) {
178+
if ( $this->was_mutated ) {
138179
return;
139180
}
140181

141182
$this->children = wp_sqlite_mysql_native_ast_get_children( $this->native_ast, $this->native_node_index );
142183
$this->native_ast = null;
143184
$this->native_node_index = null;
185+
$this->was_mutated = true;
144186
}
145187
}

0 commit comments

Comments
 (0)