Skip to content

php-in-php: JIT StringJsonEncode — route json_encode() through VmJson PHP not __compiler_json_encode_* LLVM (#1492) #9267

Description

@PurHur

Category

php-in-php

Problem

json_encode() VM path already uses ext/standard/VmJson.php + VmJsonFormat.php, but JIT/AOT still lowers through ~540 lines of hand-written LLVM in lib/JIT/Builtin/StringJsonEncode.php (__compiler_json_encode_value, __compiler_json_encode_array). This duplicates UTF-8/NaN/Inf handling, enum/object encoding, and json_last_error() state that already exist in PHP — and drifts from VM behavior (see #9205 invalid UTF-8).

php-src reference

  • ext/json/php_json.c — php_json_encode() / encoder error paths
  • ext/json/php_json_encoder.c — value walk + UTF-8 validation

Repro (VM vs JIT divergence today)

<?php
$s = "\xC3\x28"; // invalid UTF-8
var_dump(json_encode($s));
var_dump(json_last_error(), json_last_error_msg());
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && php bin/vm.php repro.php'
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && php bin/jit.php repro.php'

Zend: false, JSON_ERROR_UTF8, message about malformed UTF-8.
VM today: replacement char + json_last_error() === 0 (#9205).
JIT: uses LLVM encoder — same or worse drift from VmJson.

Scope (this repo)

Area Path Action
Stdlib SSOT ext/standard/VmJson.php, VmJsonFormat.php Keep encoder semantics here
JIT bridge new ext/standard/JitJsonEncode.php or extend existing Call VM helpers from JIT like other migrated builtins
Delete/shrink lib/JIT/Builtin/StringJsonEncode.php Remove LLVM value walk; thin ABI trampoline only if unavoidable
Registry lib/JIT/Builtin/Type.php Stop registering __compiler_json_encode_* bodies
Tests test/compliance/cases/ext/json_*.phpt VM + JIT/AOT must agree

PHP-in-PHP rule: do not grow runtime/*.c for JSON encoding; port any missing edge cases into ext/standard/.

Done when (php-src-strict)

  • JIT/AOT json_encode() calls the same PHP encoder path as VM (shared VmJson / helper), not LLVM hashtable walk
  • lib/JIT/Builtin/StringJsonEncode.php deleted or reduced to a thin bridge (<50 lines) — cite lines removed in PR
  • Stdlib: json_encode() invalid UTF-8 must return false + JSON_ERROR_UTF8 (ext/json/php_json.c) #9205 repro green on both VM and JIT after encoder unification
  • Targeted PHPUnit/compliance guard for JIT json_encode parity
  • docs/capabilities.md matrix unchanged or notes JIT PHPT green

Verification

./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && vendor/bin/phpunit --filter JsonEncode'
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && php bin/jit.php test/repro/json_encode_utf8_invalid.php'

Links

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:compilerCompiler / CFG / JITenhancementNew feature or requestimplementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claimphase-0:FoundationPhase 0 – foundation & DevExphase-4:stdlibPhase 4 – stdlib for web appsstdlib

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions