|
| 1 | +/* |
| 2 | + * PHPStanTurbo\ExpressionResultStorage — native implementation of |
| 3 | + * PHPStan\Analyser\ExpressionResultStorage. |
| 4 | + * |
| 5 | + * Not final: a PHP stub subclass extends this class. duplicate() creates |
| 6 | + * instances of the object's own class (the stub), so userland type hints |
| 7 | + * keep working without a configured Impl entry. |
| 8 | + * |
| 9 | + * The before-scope table is two id-keyed arrays in private property slots, |
| 10 | + * exactly like the PHP twin: exprsById pins each stored Expr so its object |
| 11 | + * handle cannot be reused while scopesById still maps it. duplicate() copies |
| 12 | + * the two array zvals by refcount (copy-on-write) — the eager per-entry copy |
| 13 | + * of the twin's former SplObjectStorage is what made this worth porting. |
| 14 | + * pendingFibers/parkedFibers are ordinary public properties read and written |
| 15 | + * by FiberNodeScopeResolver in PHP; the native code never touches them. |
| 16 | + */ |
| 17 | + |
| 18 | +#include "support.h" |
| 19 | +#include "zv.h" |
| 20 | + |
| 21 | +#define PT_ERS_PROP_EXPRS 0 |
| 22 | +#define PT_ERS_PROP_SCOPES 1 |
| 23 | + |
| 24 | +namespace phpstanturbo { |
| 25 | + |
| 26 | +/* Mirrors PHPStan\Analyser\ExpressionResultStorage. State lives in the PHP |
| 27 | + * object's exprsById/scopesById properties. */ |
| 28 | +class ExpressionResultStorage |
| 29 | +{ |
| 30 | +public: |
| 31 | + explicit ExpressionResultStorage(zval *self) : self(self) {} |
| 32 | + |
| 33 | + zv::Val duplicate() const |
| 34 | + { |
| 35 | + zval newObj; |
| 36 | + if (UNEXPECTED(object_init_ex(&newObj, Z_OBJCE_P(self)) != SUCCESS)) { |
| 37 | + return zv::Val(); |
| 38 | + } |
| 39 | + zv::ObjRef src(self); |
| 40 | + zv::ObjRef dst(&newObj); |
| 41 | + dst.propAtWrite(PT_ERS_PROP_EXPRS, zv::Val::copyOf(src.propAt(PT_ERS_PROP_EXPRS))); |
| 42 | + dst.propAtWrite(PT_ERS_PROP_SCOPES, zv::Val::copyOf(src.propAt(PT_ERS_PROP_SCOPES))); |
| 43 | + return zv::Val::adopt(newObj); |
| 44 | + } |
| 45 | + |
| 46 | + void storeBeforeScope(zval *expr, zval *scope) |
| 47 | + { |
| 48 | + zend_ulong id = Z_OBJ_HANDLE_P(expr); |
| 49 | + zv::ObjRef obj(self); |
| 50 | + zv::ArrRef(obj.propAt(PT_ERS_PROP_EXPRS).raw()).setIndex(id, zv::Ref(expr)); |
| 51 | + zv::ArrRef(obj.propAt(PT_ERS_PROP_SCOPES).raw()).setIndex(id, zv::Ref(scope)); |
| 52 | + } |
| 53 | + |
| 54 | + zv::Val findBeforeScope(zval *expr) const |
| 55 | + { |
| 56 | + zv::Ref found = zv::ArrRef(zv::ObjRef(self).propAt(PT_ERS_PROP_SCOPES).raw()).findIndex(Z_OBJ_HANDLE_P(expr)); |
| 57 | + if (found.raw() == NULL) { |
| 58 | + return zv::Val::null(); |
| 59 | + } |
| 60 | + return zv::Val::copyOf(found); |
| 61 | + } |
| 62 | + |
| 63 | +private: |
| 64 | + zval *self; |
| 65 | +}; |
| 66 | + |
| 67 | +} // namespace phpstanturbo |
| 68 | + |
| 69 | +using phpstanturbo::ExpressionResultStorage; |
| 70 | + |
| 71 | +/* {{{ engine ABI glue: parameter parsing + registration */ |
| 72 | + |
| 73 | +#include "reg.h" |
| 74 | + |
| 75 | +void pt_register_expression_result_storage() |
| 76 | +{ |
| 77 | + reg::Class cls("PHPStanTurbo\\ExpressionResultStorage"); |
| 78 | + /* not final: a PHP stub subclass extends this class; exprsById/scopesById |
| 79 | + * must stay in this order (OBJ_PROP_NUM slots) */ |
| 80 | + cls.privateArrayProperty("exprsById"); |
| 81 | + cls.privateArrayProperty("scopesById"); |
| 82 | + cls.publicArrayProperty("pendingFibers"); |
| 83 | + cls.publicArrayProperty("parkedFibers"); |
| 84 | + |
| 85 | + cls.method("duplicate", reg::Public, 0, {}, [](INTERNAL_FUNCTION_PARAMETERS) { |
| 86 | + ZEND_PARSE_PARAMETERS_NONE(); |
| 87 | + zv::Val result = ExpressionResultStorage(ZEND_THIS).duplicate(); |
| 88 | + if (UNEXPECTED(result.isUndef())) { |
| 89 | + RETURN_THROWS(); |
| 90 | + } |
| 91 | + result.intoReturnValue(return_value); |
| 92 | + }); |
| 93 | + |
| 94 | + cls.method("storeBeforeScope", reg::Public, 2, { reg::any("expr"), reg::any("scope") }, [](INTERNAL_FUNCTION_PARAMETERS) { |
| 95 | + zval *expr, *scope; |
| 96 | + ZEND_PARSE_PARAMETERS_START(2, 2) |
| 97 | + Z_PARAM_OBJECT(expr) |
| 98 | + Z_PARAM_OBJECT(scope) |
| 99 | + ZEND_PARSE_PARAMETERS_END(); |
| 100 | + ExpressionResultStorage(ZEND_THIS).storeBeforeScope(expr, scope); |
| 101 | + }); |
| 102 | + |
| 103 | + cls.method("findBeforeScope", reg::Public, 1, { reg::any("expr") }, [](INTERNAL_FUNCTION_PARAMETERS) { |
| 104 | + zval *expr; |
| 105 | + ZEND_PARSE_PARAMETERS_START(1, 1) |
| 106 | + Z_PARAM_OBJECT(expr) |
| 107 | + ZEND_PARSE_PARAMETERS_END(); |
| 108 | + ExpressionResultStorage(ZEND_THIS).findBeforeScope(expr).intoReturnValue(return_value); |
| 109 | + }); |
| 110 | + |
| 111 | + cls.register_(); |
| 112 | +} |
| 113 | + |
| 114 | +/* }}} */ |
0 commit comments