Skip to content

Commit 8a7cf51

Browse files
committed
Fall back to pure PHP when the native extension grammar ABI mismatches
The PHP bridge now exports the parser grammar as per-token branch selectors (`branches_for_token` / `nullable_branches`) instead of the previous coarse `lookahead_is_match_possible` table - a backward-incompatible change to the ABI shared between the extension binary and the PHP driver. Until now load.php selected the native lexer/parser purely on class existence, so an extension built against a different grammar ABI - most commonly a plugin update that outpaces the installed binary - would be selected and then fatal during native parser construction, with no fallback. Track grammar-ABI compatibility by the extension's minor version (the 0.x line) and bump it to 0.2.0 for this change. Gate native selection on `phpversion( 'wp_mysql_parser' )` falling within the supported line (0.2.x); the native lexer and parser are a matched pair (the native lexer emits a token stream only the native parser can consume), so select both or neither. An unsupported or absent version falls back cleanly to pure PHP, erring on the safe side for unknown binaries. Document the versioning contract in the extension README and add a unit test covering the gate's boundaries.
1 parent c8c0029 commit 8a7cf51

5 files changed

Lines changed: 123 additions & 12 deletions

File tree

‎packages/mysql-on-sqlite/src/load.php‎

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,57 @@
1313
require_once __DIR__ . '/parser/class-wp-parser-token.php';
1414
require_once __DIR__ . '/mysql/class-wp-mysql-token.php';
1515

16-
/*
17-
* The MySQL lexer and parser have an optional native (e.g. Rust) implementation.
18-
* When the native extension is loaded, it pre-declares WP_MySQL_Native_Lexer /
19-
* WP_MySQL_Native_Parser; otherwise we fall back to the pure-PHP classes shipped
20-
* here. WP_MySQL_Lexer / WP_MySQL_Parser is the public entrypoint either way.
16+
/**
17+
* Whether the loaded "wp_mysql_parser" extension speaks a grammar ABI that this
18+
* code supports.
19+
*
20+
* The native parser and PHP exchange the parser grammar via
21+
* "wp_sqlite_mysql_native_export_grammar()"; the shape of that data is an ABI.
22+
* Compatibility is tracked by the extension's minor version (the "x" in "0.x"):
23+
* a backward-incompatible change to the grammar ABI bumps the minor version.
24+
* This code supports the "0.2.x" line. A version outside the supported range -
25+
* e.g. an older extension binary lagging a plugin update - cannot exchange the
26+
* grammar safely and must fall back to the pure-PHP path.
27+
*
28+
* Keep the supported range in sync with the extension's "Cargo.toml" version
29+
* (see "packages/php-ext-wp-mysql-parser/README.md").
30+
*
31+
* @param string|false $extension_version Version reported by "phpversion( 'wp_mysql_parser' )".
32+
* @return bool Whether the native lexer/parser path can be used.
2133
*/
22-
if ( class_exists( 'WP_MySQL_Native_Lexer', false ) ) {
23-
require_once __DIR__ . '/mysql/native/class-wp-mysql-lexer.php';
24-
} else {
25-
require_once __DIR__ . '/mysql/class-wp-mysql-lexer.php';
34+
function wp_sqlite_mysql_native_grammar_abi_supported( $extension_version ): bool {
35+
if ( ! is_string( $extension_version ) ) {
36+
return false;
37+
}
38+
return version_compare( $extension_version, '0.2.0', '>=' )
39+
&& version_compare( $extension_version, '0.3.0', '<' );
2640
}
2741

28-
if ( class_exists( 'WP_MySQL_Native_Parser', false ) ) {
42+
/*
43+
* The MySQL lexer and parser have an optional native (e.g. Rust) implementation,
44+
* registered by the "wp_mysql_parser" extension. When loaded, it pre-declares
45+
* WP_MySQL_Native_Lexer / WP_MySQL_Native_Parser; otherwise we use the pure-PHP
46+
* classes shipped here. WP_MySQL_Lexer / WP_MySQL_Parser is the public entrypoint
47+
* either way.
48+
*
49+
* The native lexer and parser are a matched pair - the native lexer emits a token
50+
* stream that only the native parser can consume - so they are selected together
51+
* or not at all. We only select the native path when the loaded extension speaks a
52+
* grammar ABI this code supports; otherwise (including a stale extension binary) we
53+
* fall back to the pure-PHP path cleanly instead of failing at parse time.
54+
*/
55+
$wp_sqlite_use_native_parser =
56+
class_exists( 'WP_MySQL_Native_Lexer', false )
57+
&& class_exists( 'WP_MySQL_Native_Parser', false )
58+
&& wp_sqlite_mysql_native_grammar_abi_supported( phpversion( 'wp_mysql_parser' ) );
59+
60+
if ( $wp_sqlite_use_native_parser ) {
61+
require_once __DIR__ . '/mysql/native/class-wp-mysql-lexer.php';
2962
require_once __DIR__ . '/mysql/native/mysql-rust-bridge.php';
3063
require_once __DIR__ . '/mysql/native/trait-wp-mysql-native-parser-impl.php';
3164
require_once __DIR__ . '/mysql/native/class-wp-mysql-parser.php';
3265
} else {
66+
require_once __DIR__ . '/mysql/class-wp-mysql-lexer.php';
3367
require_once __DIR__ . '/mysql/class-wp-mysql-parser.php';
3468
}
3569
require_once __DIR__ . '/sqlite/class-wp-sqlite-connection.php';
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
5+
/**
6+
* Tests for the native-extension grammar-ABI gate used by "load.php".
7+
*
8+
* The native parser exchanges the parser grammar with PHP across an ABI that is
9+
* tracked by the extension's minor version. "load.php" selects the native path
10+
* only when the loaded extension's version is within the supported range, so a
11+
* stale or mismatched extension binary falls back to pure PHP instead of failing
12+
* at parse time. The helper "wp_sqlite_mysql_native_grammar_abi_supported()" is
13+
* defined by "load.php" (loaded via the test bootstrap).
14+
*/
15+
class WP_MySQL_Native_Grammar_Abi_Tests extends TestCase {
16+
17+
/**
18+
* @dataProvider supported_versions
19+
*/
20+
public function test_supported_versions_are_accepted( string $version ): void {
21+
$this->assertTrue( wp_sqlite_mysql_native_grammar_abi_supported( $version ) );
22+
}
23+
24+
/**
25+
* @dataProvider unsupported_versions
26+
* @param string|false $version
27+
*/
28+
public function test_unsupported_versions_are_rejected( $version ): void {
29+
$this->assertFalse( wp_sqlite_mysql_native_grammar_abi_supported( $version ) );
30+
}
31+
32+
/**
33+
* @return array<string,array{0:string}>
34+
*/
35+
public function supported_versions(): array {
36+
return array(
37+
'minor line lower bound' => array( '0.2.0' ),
38+
'patch within the line' => array( '0.2.1' ),
39+
'higher patch' => array( '0.2.99' ),
40+
);
41+
}
42+
43+
/**
44+
* @return array<string,array{0:string|false}>
45+
*/
46+
public function unsupported_versions(): array {
47+
return array(
48+
'extension not loaded' => array( false ),
49+
'older ABI line' => array( '0.1.0' ),
50+
'older ABI line high patch' => array( '0.1.99' ),
51+
'next (breaking) ABI line' => array( '0.3.0' ),
52+
'future major' => array( '1.0.0' ),
53+
);
54+
}
55+
}

‎packages/php-ext-wp-mysql-parser/Cargo.lock‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/php-ext-wp-mysql-parser/Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "wp_mysql_parser"
3-
version = "0.1.0"
3+
version = "0.2.0"
44
edition = "2021"
55
license = "GPL-2.0-or-later"
66

‎packages/php-ext-wp-mysql-parser/README.md‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,28 @@
44

55
When the extension is loaded before `packages/mysql-on-sqlite/src/load.php`, it registers native base classes used by the public `WP_MySQL_Lexer` and `WP_MySQL_Parser` wrappers. Without the extension, those public wrappers extend the pure-PHP implementations instead.
66

7+
## Versioning and the grammar ABI
8+
9+
The native parser and the PHP driver exchange the parser grammar at runtime via
10+
`wp_sqlite_mysql_native_export_grammar()`. The shape of that data is an ABI shared
11+
between the extension binary and the PHP code, and it can change between releases
12+
(for example, the move from a coarse lookahead table to per-token branch selectors).
13+
14+
Compatibility is tracked by the extension's **minor** version (the `x` in `0.x`):
15+
16+
- **Bump the minor version on any backward-incompatible change to the grammar ABI**
17+
(the data exchanged by `wp_sqlite_mysql_native_export_grammar()` or consumed by the
18+
native parser). Patch releases must keep the ABI unchanged.
19+
- The PHP side (`packages/mysql-on-sqlite/src/load.php`) pins the supported minor
20+
line and selects the native lexer/parser only when `phpversion( 'wp_mysql_parser' )`
21+
falls within it. A mismatch — most commonly a plugin update that outpaces the
22+
installed extension binary — falls back cleanly to the pure-PHP path instead of
23+
failing at parse time.
24+
25+
When you change the grammar ABI, bump `version` in `Cargo.toml` and update the
26+
supported range in `wp_sqlite_mysql_native_grammar_abi_supported()` in `load.php`
27+
together.
28+
729
## Published WASM build for Playground
830

931
Published WASM builds are listed on this repository's GitHub Pages site, with manifest links and a “Run in Playground” link for each release:

0 commit comments

Comments
 (0)