phplang 0.2.2

PHP as a fusevm frontend: lexer, parser, and compiler to fusevm::Chunk on a PhpHost heap — the first compiled standalone PHP runtime
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
//! PHP standard-library `types` functions. Part of the `stdlib` chain; see
//! `src/stdlib/mod.rs`. `dispatch` returns `None` for names it does not handle.
//!
//! Core (`builtins::call_library`) already owns `gettype`, `is_array`,
//! `is_int`/`integer`/`long`, `is_float`/`double`, `is_string`, `is_bool`,
//! `is_null`, `is_numeric`, `is_callable`, `boolval`, `intval`, `floatval`/
//! `doubleval`, `strval`, `var_export`, `var_dump`, `print_r`; those never reach
//! here. This module adds the remaining type predicates plus `get_debug_type`
//! and the `serialize`/`unserialize` round trip.

use crate::host::with_host;
use fusevm::Value;

use super::common::arg;

/// Dispatch a `types`-category PHP function by lowercased name.
pub fn dispatch(name: &str, args: &[Value]) -> Option<Result<Value, String>> {
    let v = match name {
        // ── type predicates ─────────────────────────────────────────────────
        // A scalar is int/float/string/bool. null, arrays, and objects are not.
        "is_scalar" => Value::bool(matches!(
            arg(args, 0),
            Value::Int(_) | Value::Float(_) | Value::Str(_) | Value::Bool(_)
        )),
        // `is_object` is true for class instances and closures, but not arrays.
        "is_object" => {
            let a = arg(args, 0);
            with_host(|h| Value::bool(h.is_object(&a) || h.is_closure(&a)))
        }
        // An array is both. Beyond that the two ask different questions: a
        // `Countable` can be counted, while anything `foreach` can drive — an
        // `Iterator`, an `IteratorAggregate`, a `Generator` — is iterable.
        "is_countable" => {
            let a = arg(args, 0);
            with_host(|h| {
                Value::bool(
                    h.is_array(&a)
                        || h.object_class(&a)
                            .is_some_and(|c| h.class_is_a_pub(&c, "Countable")),
                )
            })
        }
        "is_iterable" => {
            let a = arg(args, 0);
            with_host(|h| {
                Value::bool(
                    h.is_array(&a)
                        || h.is_generator_val(&a)
                        || h.object_class(&a).is_some_and(|c| {
                            h.class_is_a_pub(&c, "Traversable")
                                || h.class_is_a_pub(&c, "Iterator")
                                || h.class_is_a_pub(&c, "IteratorAggregate")
                        }),
                )
            })
        }

        // ── get_debug_type ──────────────────────────────────────────────────
        // PHP 8's precise type name: scalar keywords, "array", "null", or the
        // class name for objects ("Closure" for a closure).
        "get_debug_type" => {
            let a = arg(args, 0);
            let name = with_host(|h| debug_type(h, &a));
            Value::str(name)
        }

        // ── serialization ───────────────────────────────────────────────────
        "serialize" => {
            let a = arg(args, 0);
            let out = with_host(|h| php_serialize(h, &a));
            Value::str(out)
        }
        "unserialize" => {
            let s = with_host(|h| h.to_str(&arg(args, 0)));
            let bytes = s.as_bytes();
            let total = bytes.len();
            // An empty payload is the one malformed input PHP rejects silently.
            if total == 0 {
                return Some(Ok(Value::bool(false)));
            }
            match php_unserialize(bytes) {
                Unser::Value(v) => v,
                Unser::Saturated(v) => {
                    with_host(|h| h.warn("unserialize(): Numerical result out of range"));
                    v
                }
                // Trailing bytes do NOT fail the call: the value stands and PHP
                // warns about what it did not read.
                Unser::Extra(v, at) => {
                    with_host(|h| {
                        h.warn(format_args!(
                            "unserialize(): Extra data starting at offset {at} of {total} bytes"
                        ))
                    });
                    v
                }
                // A malformed payload is a WARNING plus `false` (not null).
                Unser::Failed(at, short) => {
                    with_host(|h| {
                        if short {
                            h.warn("unserialize(): Unexpected end of serialized data");
                        }
                        h.warn(format_args!(
                            "unserialize(): Error at offset {at} of {total} bytes"
                        ))
                    });
                    Value::bool(false)
                }
            }
        }

        _ => return None,
    };
    Some(Ok(v))
}

/// How an error message names a *value* (PHP's `zend_zval_value_name`), which
/// is not how [`debug_type`] names its type: a bool is reported as the literal
/// `true` / `false` it is, not as `bool`.
///
/// `Cannot use "::class" on true` is the wording this exists for.
pub fn value_name(h: &crate::host::PhpHost, v: &Value) -> String {
    match v {
        Value::Bool(true) => "true".to_string(),
        Value::Bool(false) => "false".to_string(),
        _ => debug_type(h, v),
    }
}

/// `get_debug_type` name for a value.
pub fn debug_type(h: &crate::host::PhpHost, v: &Value) -> String {
    match v {
        Value::Undef => "null".to_string(),
        Value::Bool(_) => "bool".to_string(),
        Value::Int(_) => "int".to_string(),
        Value::Float(_) => "float".to_string(),
        Value::Str(_) => "string".to_string(),
        Value::Obj(_) => {
            if h.is_array(v) {
                "array".to_string()
            } else if h.is_closure(v) {
                "Closure".to_string()
            } else {
                // Reported the way a message reports a class name, so an
                // anonymous class shows its readable head alone.
                h.object_class(v).map_or_else(
                    || "object".to_string(),
                    |c| crate::host::display_class(&c).to_string(),
                )
            }
        }
        _ => "unknown".to_string(),
    }
}

// ── serialize ────────────────────────────────────────────────────────────────

/// Render a value in PHP's serialization format: null, bool, int, float, string,
/// and (recursively) arrays, objects and `enum` cases. A closure or a resource
/// has no serializable form and falls back to `N;` (null).
fn php_serialize(h: &crate::host::PhpHost, v: &Value) -> String {
    php_serialize_seen(h, v, &mut crate::host::Visiting::default())
}

/// [`php_serialize`] with the cycle guard threaded through.
///
/// DIVERGENCE: the reference encodes a repeat as a BACK-REFERENCE — `r:<n>;` for
/// an object, `R:<n>;` for a reference — where `<n>` is the position the value
/// first occupied in the serialization. phplang has no such position table, so a
/// cycle is written as `N;` instead. The output therefore no longer round-trips
/// a self-referential structure, but it is finite and syntactically valid; before
/// this guard the walk exhausted the native stack and aborted the process.
fn php_serialize_seen(
    h: &crate::host::PhpHost,
    v: &Value,
    seen: &mut crate::host::Visiting,
) -> String {
    if (h.is_array(v) || h.is_object(v)) && !seen.enter(v) {
        return "N;".to_string();
    }
    let out = php_serialize_body(h, v, seen);
    if h.is_array(v) || h.is_object(v) {
        seen.leave();
    }
    out
}

fn php_serialize_body(
    h: &crate::host::PhpHost,
    v: &Value,
    seen: &mut crate::host::Visiting,
) -> String {
    match v {
        Value::Undef => "N;".to_string(),
        Value::Bool(b) => format!("b:{};", *b as i32),
        Value::Int(n) => format!("i:{n};"),
        Value::Float(f) => format!("d:{};", serialize_float(*f)),
        // Length is the byte length, matching PHP's binary-safe strings.
        Value::Str(s) => format!("s:{}:\"{}\";", s.len(), s),
        Value::Obj(_) if h.is_array(v) => {
            let pairs = h.array_pairs(v).unwrap_or_default();
            let mut out = format!("a:{}:{{", pairs.len());
            for (k, val) in pairs {
                out.push_str(&php_serialize_seen(h, &k, seen));
                out.push_str(&php_serialize_seen(h, &val, seen));
            }
            out.push('}');
            out
        }
        Value::Obj(_) if h.is_object(v) => php_serialize_object(h, v, seen),
        _ => "N;".to_string(),
    }
}

/// `serialize` of an object: `O:<len>:"Class":<n>:{<key><value>...}`.
///
/// The property KEYS carry the visibility, mangled the way the engine stores
/// them: a protected `$b` is `"\0*\0b"` and a private `$c` declared in `P` is
/// `"\0P\0c"`. Those NUL bytes are counted in the string length, which is why the
/// reference prints `s:4:" * b"` — four bytes, two of them NUL.
///
/// An `enum` case is not an object here at all: it serializes as
/// `E:<len>:"Class:CASE";`, carrying only enough to look the singleton back up.
fn php_serialize_object(
    h: &crate::host::PhpHost,
    v: &Value,
    seen: &mut crate::host::Visiting,
) -> String {
    let class = h.object_class(v).unwrap_or_else(|| "stdClass".to_string());
    if let Some((case, _)) = h.enum_case_of(v) {
        let tag = format!("{class}:{case}");
        return format!("E:{}:\"{tag}\";", tag.len());
    }
    let props = h.object_props(v);
    let mut out = format!("O:{}:\"{class}\":{}:{{", class.len(), props.len());
    for (name, val) in props {
        let key = match h.prop_visibility(&class, &name) {
            Some((_, crate::ast::Visibility::Protected)) => format!("\0*\0{name}"),
            Some((declaring, crate::ast::Visibility::Private)) => format!("\0{declaring}\0{name}"),
            _ => name,
        };
        out.push_str(&format!("s:{}:\"{key}\";", key.len()));
        out.push_str(&php_serialize_seen(h, &val, seen));
    }
    out.push('}');
    out
}

/// Format a float at `serialize_precision = -1` — the shortest digit string that
/// round-trips. This is the representation `serialize`, `var_dump`, `var_export`
/// and `json_encode` all share; it is *not* the `precision = 14` rendering that
/// `echo` and string casts use (`echo 1/3` gives `0.33333333333333` while
/// `var_dump(1/3)` gives `float(0.3333333333333333)`).
///
/// PHP calls `zend_gcvt` on the shortest round-tripping digit string (`zend_dtoa`
/// mode 0) and switches to E-notation when the decimal point position `decpt`
/// (= exponent + 1) exceeds 17 or drops below -3 — i.e. large or small magnitudes
/// render as `1.0E+20` / `1.0E-10`, everything in between stays fixed. Rust's
/// `{:e}` already produces the shortest round-tripping mantissa (Ryu), so parse
/// that and reformat PHP-style. Verified against reference PHP 8:
/// `serialize(1e17)` = `d:1.0E+17;` but `serialize(1e16)` = `d:10000000000000000;`.
pub fn serialize_float(f: f64) -> String {
    if f.is_nan() {
        return "NAN".to_string();
    }
    if f.is_infinite() {
        return if f < 0.0 { "-INF" } else { "INF" }.to_string();
    }
    if f == 0.0 {
        // PHP renders negative zero as "-0".
        return if f.is_sign_negative() { "-0" } else { "0" }.to_string();
    }
    let neg = f < 0.0;
    let a = f.abs();
    // `{:e}` yields e.g. "1.5e0", "1e20", "1.844674407371e19".
    let sci = format!("{a:e}");
    let (mant, exp_s) = sci.split_once('e').expect("`{:e}` always contains 'e'");
    let exp: i32 = exp_s.parse().expect("`{:e}` exponent is an integer");
    let digits: String = mant.chars().filter(|c| *c != '.').collect();
    // `decpt`: position of the decimal point relative to the digit string, the
    // same quantity `zend_dtoa` reports.
    let decpt = exp + 1;
    let body = if !(-3..=17).contains(&decpt) {
        // Exponential: one leading digit, then the rest (or ".0"), signed exponent.
        let (first, rest) = digits.split_at(1);
        let frac = if rest.is_empty() { "0" } else { rest };
        let e = decpt - 1;
        format!(
            "{first}.{frac}E{}{}",
            if e < 0 { "-" } else { "+" },
            e.abs()
        )
    } else if decpt <= 0 {
        // 0.00…digits — leading zeros equal to -decpt.
        format!("0.{}{}", "0".repeat((-decpt) as usize), digits)
    } else if decpt as usize >= digits.len() {
        // Integer-valued: pad trailing zeros out to the decimal point.
        format!("{digits}{}", "0".repeat(decpt as usize - digits.len()))
    } else {
        // Decimal point falls inside the digit string.
        let (int_part, frac) = digits.split_at(decpt as usize);
        format!("{int_part}.{frac}")
    };
    if neg {
        format!("-{body}")
    } else {
        body
    }
}

// ── unserialize ──────────────────────────────────────────────────────────────

/// The outcome of parsing a serialization payload, with everything
/// `unserialize` needs to reproduce the reference's diagnostics.
enum Unser {
    /// A complete value, consuming the whole payload.
    Value(Value),
    /// A complete value followed by bytes the parser did not read. PHP KEEPS the
    /// value here and warns — it does not fail.
    Extra(Value, usize),
    /// A complete value whose `i:` literal was clamped to the platform integer
    /// range. The value stands; only the clamp is reported.
    Saturated(Value),
    /// Malformed, at the given byte offset. `unexpected_end` marks the payload
    /// that claimed more container elements than it carried.
    Failed(usize, bool),
}

/// Parse a PHP serialization payload. Byte-oriented so string lengths are
/// binary-safe.
fn php_unserialize(b: &[u8]) -> Unser {
    let mut p = Parser {
        b,
        pos: 0,
        fail_at: None,
        unexpected_end: false,
        saturated: false,
    };
    let Some(v) = p.value() else {
        return Unser::Failed(p.fail_at.unwrap_or(0), p.unexpected_end);
    };
    if p.pos != b.len() {
        Unser::Extra(v, p.pos)
    } else if p.saturated {
        Unser::Saturated(v)
    } else {
        Unser::Value(v)
    }
}

struct Parser<'a> {
    b: &'a [u8],
    pos: usize,
    /// Where the reference would report the parse as having failed. Written by
    /// the FIRST branch to fail, which is the innermost one — a bad element deep
    /// in an array is reported at its own offset, not the array's.
    fail_at: Option<usize>,
    /// Set when a container's closing brace arrived while declared elements were
    /// still owed. The reference prints an extra note before the offset error in
    /// that case alone — a payload that merely stops mid-element does not get it.
    unexpected_end: bool,
    /// Set when an `i:` literal was clamped to `PHP_INT_MIN`/`PHP_INT_MAX`. The
    /// value still stands; the reference just warns about the clamp.
    saturated: bool,
}

impl<'a> Parser<'a> {
    /// Consume the exact byte sequence `s`, or fail.
    fn eat(&mut self, s: &[u8]) -> Option<()> {
        if self.b[self.pos..].starts_with(s) {
            self.pos += s.len();
            Some(())
        } else {
            None
        }
    }

    /// Read up to (and consuming) the delimiter byte, returning the bytes before it.
    fn read_until(&mut self, delim: u8) -> Option<&'a [u8]> {
        let start = self.pos;
        while self.pos < self.b.len() && self.b[self.pos] != delim {
            self.pos += 1;
        }
        if self.pos >= self.b.len() {
            return None;
        }
        let slice = &self.b[start..self.pos];
        self.pos += 1; // consume the delimiter
        Some(slice)
    }

    /// Parse the decimal integer that runs up to `delim`, consuming the delimiter.
    /// Strict: overflow or non-numeric input fails. Used for lengths and counts.
    fn int_until(&mut self, delim: u8) -> Option<i64> {
        let s = std::str::from_utf8(self.read_until(delim)?).ok()?;
        s.parse().ok()
    }

    /// Parse a serialized integer *value* up to `delim`. Unlike `int_until`, a
    /// magnitude beyond `i64` saturates to `PHP_INT_MAX`/`PHP_INT_MIN` rather than
    /// failing — PHP's `unserialize` clamps an out-of-range `i:` literal (emitting
    /// a warning) instead of returning `false`.
    fn int_value(&mut self, delim: u8) -> Option<i64> {
        let s = std::str::from_utf8(self.read_until(delim)?).ok()?;
        match s.parse::<i64>() {
            Ok(n) => Some(n),
            Err(_) => {
                // Saturate only genuine numeric overflow; reject anything else.
                let (neg, body) = match s.strip_prefix('-') {
                    Some(rest) => (true, rest),
                    None => (false, s),
                };
                if !body.is_empty() && body.bytes().all(|c| c.is_ascii_digit()) {
                    // The clamp is reported: PHP warns "Numerical result out of
                    // range" and still hands back the saturated value.
                    self.saturated = true;
                    Some(if neg { i64::MIN } else { i64::MAX })
                } else {
                    None
                }
            }
        }
    }

    /// Record where a parse failed, if nothing deeper already has, and fail.
    ///
    /// Which offset the reference names depends on the tag, and the split is not
    /// arbitrary: `N`/`b`/`i`/`d` and an unrecognized tag are scanned as one token
    /// and reported at the token's own start, while `s`/`O`/`E` scan a length and
    /// then check the payload separately, so they are reported at the payload —
    /// two bytes in, just past `X:`. An `a` header follows the first rule; a bad
    /// element inside any container follows whichever rule that element's tag has.
    fn fail_at(&mut self, offset: usize) -> Option<Value> {
        self.fail_at.get_or_insert(offset);
        None
    }

    /// Called before each declared element of a container. `Some(_)` means the
    /// closing brace arrived while elements were still owed — a payload that
    /// claims more than it carries, which the reference reports as "Unexpected
    /// end of serialized data" BEFORE the offset error. A payload that simply
    /// stops mid-element is a plain offset error with no such note.
    fn container_exhausted(&mut self) -> Option<Option<Value>> {
        if self.b.get(self.pos) != Some(&b'}') {
            return None;
        }
        self.unexpected_end = true;
        Some(self.fail_at(self.pos))
    }

    /// Consume a container's closing brace, blaming the cursor (not the
    /// container's own start) when something else is there.
    fn close_container(&mut self) -> Option<()> {
        if self.b.get(self.pos) != Some(&b'}') {
            self.fail_at(self.pos);
            return None;
        }
        self.pos += 1;
        Some(())
    }

    /// Parse one serialized value at the cursor.
    fn value(&mut self) -> Option<Value> {
        let start = self.pos;
        // Past the end: the innermost failure is at the cursor itself.
        let Some(&tag) = self.b.get(self.pos) else {
            return self.fail_at(start);
        };
        // `s`, `O` and `E` are reported at their payload, everything else at the
        // tag. Computed up front so every failing path below agrees.
        let blame = match tag {
            b's' | b'O' | b'E' => start + 2,
            _ => start,
        };
        match self.value_inner(tag) {
            Some(v) => Some(v),
            None => self.fail_at(blame),
        }
    }

    /// The per-tag body of [`Parser::value`]; every `None` it returns is turned
    /// into a recorded failure by the caller.
    fn value_inner(&mut self, tag: u8) -> Option<Value> {
        match tag {
            b'N' => {
                self.eat(b"N;")?;
                Some(Value::Undef)
            }
            b'b' => {
                self.eat(b"b:")?;
                let d = *self.b.get(self.pos)?;
                self.pos += 1;
                self.eat(b";")?;
                match d {
                    b'0' => Some(Value::bool(false)),
                    b'1' => Some(Value::bool(true)),
                    _ => None,
                }
            }
            b'i' => {
                self.eat(b"i:")?;
                let n = self.int_value(b';')?;
                Some(Value::int(n))
            }
            b'd' => {
                self.eat(b"d:")?;
                let s = std::str::from_utf8(self.read_until(b';')?).ok()?;
                let f = match s {
                    "NAN" => f64::NAN,
                    "INF" => f64::INFINITY,
                    "-INF" => f64::NEG_INFINITY,
                    _ => s.parse().ok()?,
                };
                Some(Value::float(f))
            }
            b's' => {
                self.eat(b"s:")?;
                let len = self.int_until(b':')? as usize;
                self.eat(b"\"")?;
                let end = self.pos.checked_add(len)?;
                if end > self.b.len() {
                    return None;
                }
                let bytes = &self.b[self.pos..end];
                self.pos = end;
                self.eat(b"\";")?;
                Some(Value::str(String::from_utf8_lossy(bytes).into_owned()))
            }
            b'a' => {
                self.eat(b"a:")?;
                let count = self.int_until(b':')?;
                // A negative element count is malformed; PHP rejects it (returns
                // `false`) rather than treating it as an empty array.
                if count < 0 {
                    return None;
                }
                self.eat(b"{")?;
                let mut pairs: Vec<(Value, Value)> = Vec::new();
                for _ in 0..count {
                    if let Some(fail) = self.container_exhausted() {
                        return fail;
                    }
                    let k = self.value()?;
                    if let Some(fail) = self.container_exhausted() {
                        return fail;
                    }
                    let v = self.value()?;
                    pairs.push((k, v));
                }
                self.close_container()?;
                Some(super::common::make_map(pairs))
            }
            // `O:<len>:"Class":<n>:{...}` — an object, restored WITHOUT running
            // its constructor and with the property-name mangling undone.
            b'O' => {
                self.eat(b"O:")?;
                let class = self.length_prefixed_string()?;
                self.eat(b":")?;
                let count = self.int_until(b':')?;
                if count < 0 {
                    return None;
                }
                self.eat(b"{")?;
                let mut props: Vec<(String, Value)> = Vec::with_capacity(count as usize);
                for _ in 0..count {
                    if let Some(fail) = self.container_exhausted() {
                        return fail;
                    }
                    let Value::Str(k) = self.value()? else {
                        return None;
                    };
                    if let Some(fail) = self.container_exhausted() {
                        return fail;
                    }
                    props.push((demangle_prop(&k), self.value()?));
                }
                self.close_container()?;
                // An unknown class becomes `__PHP_Incomplete_Class`, whose real
                // name is carried in a leading synthetic property.
                let known = with_host(|h| h.class_exists(&class));
                if !known {
                    props.insert(
                        0,
                        (
                            "__PHP_Incomplete_Class_Name".to_string(),
                            Value::str(class.clone()),
                        ),
                    );
                }
                let target = if known {
                    class
                } else {
                    "__PHP_Incomplete_Class".to_string()
                };
                Some(with_host(|h| h.new_object_bare(&target, props)))
            }
            // `E:<len>:"Class:CASE";` — an enum case, resolved back to the
            // singleton rather than rebuilt as a new object.
            b'E' => {
                self.eat(b"E:")?;
                let tag = self.length_prefixed_string()?;
                self.eat(b";")?;
                let (class, case) = tag.split_once(':')?;
                crate::host::class_const(class, case).ok()
            }
            _ => None,
        }
    }

    /// The `<len>:"<bytes>"` payload shared by the `O:` and `E:` tags.
    fn length_prefixed_string(&mut self) -> Option<String> {
        let len = self.int_until(b':')? as usize;
        self.eat(b"\"")?;
        let end = self.pos.checked_add(len)?;
        if end > self.b.len() {
            return None;
        }
        let bytes = &self.b[self.pos..end];
        self.pos = end;
        self.eat(b"\"")?;
        Some(String::from_utf8_lossy(bytes).into_owned())
    }
}

/// Undo `serialize`'s visibility mangling on a property name: `"\0*\0b"` and
/// `"\0Declaring\0c"` both name the plain property they wrap.
fn demangle_prop(k: &str) -> String {
    match k.strip_prefix('\0') {
        Some(rest) => rest
            .split_once('\0')
            .map(|(_, name)| name.to_string())
            .unwrap_or_else(|| k.to_string()),
        None => k.to_string(),
    }
}