From 29fc86f9ddefc918549c201ebe5ee8a978efde44 Mon Sep 17 00:00:00 2001 From: Fred Emmott Date: Thu, 11 May 2017 09:08:38 -0700 Subject: [PATCH] Change the way PHP7 exception aliasing works Summary: fixes #7537 fixes #6747 Old approach: when parsing, replace `Throwable` with `__SystemLib\Throwable` New approach: when loading the process, actually create `\Throwable` as an alias of `\__SystemLib\Throwable` get_class() and messages still say systemlib, but this is an improvement. Reviewed By: mofarrell Differential Revision: D5031367 fbshipit-source-id: 4985318804c5c5c190cc8141d721e3d3408fb21b --- hphp/compiler/parser/parser.cpp | 16 -------- hphp/compiler/parser/parser.h | 1 - hphp/system/php/lang/Error.php | 37 +++++++++++++++++++ hphp/system/php/lang/Throwable.php | 11 ++++++ hphp/system/systemlib.cpp | 27 ++++++++++++++ .../throwable_namespace_001.php | 9 +++++ .../throwable_namespace_001.php.expect | 3 ++ .../throwable_namespace_002.php | 8 ++++ .../throwable_namespace_002.php.expect | 1 + 9 files changed, 96 insertions(+), 17 deletions(-) create mode 100644 hphp/system/php/lang/Error.php create mode 100644 hphp/system/php/lang/Throwable.php create mode 100644 hphp/test/slow/php7_backported/engine-exceptions/throwable_namespace_001.php create mode 100644 hphp/test/slow/php7_backported/engine-exceptions/throwable_namespace_001.php.expect create mode 100644 hphp/test/slow/php7_backported/engine-exceptions/throwable_namespace_002.php create mode 100644 hphp/test/slow/php7_backported/engine-exceptions/throwable_namespace_002.php.expect diff --git a/hphp/compiler/parser/parser.cpp b/hphp/compiler/parser/parser.cpp index f61be2844ad2f0..7f0df5873359c3 100644 --- a/hphp/compiler/parser/parser.cpp +++ b/hphp/compiler/parser/parser.cpp @@ -2529,10 +2529,6 @@ Parser::AliasFlags Parser::getAliasFlags() { flags = flags | AliasFlags::PHP7_ScalarTypes; } - if (RuntimeOption::PHP7_EngineExceptions) { - flags = flags | AliasFlags::PHP7_EngineExceptions; - } - return flags; } @@ -2558,8 +2554,6 @@ Parser::AutoAliasMap getAutoAliasedClassesHelper() { #define HH_ALIAS(alias, name) \ ALIAS(#alias, "HH\\" #name, AliasFlags::HH) #define SCALAR_TYPE(name) HH_TYPE(name, AliasFlags::PHP7_ScalarTypes) -#define PHP7_TYPE(name, option) \ - ALIAS(#name, "__SystemLib\\" #name, AliasFlags::option) Parser::AutoAliasMap aliases { HH_ONLY_TYPE(AsyncIterator), HH_ONLY_TYPE(AsyncKeyedIterator), @@ -2630,17 +2624,7 @@ Parser::AutoAliasMap getAutoAliasedClassesHelper() { HH_ALIAS(integer, int), HH_ALIAS(double, float), HH_ALIAS(real, float), - - // Engine exception classes - PHP7_TYPE(Throwable, PHP7_EngineExceptions), - PHP7_TYPE(Error, PHP7_EngineExceptions), - PHP7_TYPE(ArithmeticError, PHP7_EngineExceptions), - PHP7_TYPE(AssertionError, PHP7_EngineExceptions), - PHP7_TYPE(DivisionByZeroError, PHP7_EngineExceptions), - PHP7_TYPE(ParseError, PHP7_EngineExceptions), - PHP7_TYPE(TypeError, PHP7_EngineExceptions), }; -#undef PHP7_TYPE #undef HH_ALIAS #undef SCALAR_TYPE #undef HH_ONLY_TYPE diff --git a/hphp/compiler/parser/parser.h b/hphp/compiler/parser/parser.h index ef41be82046fe4..b5f7d2160fbd36 100644 --- a/hphp/compiler/parser/parser.h +++ b/hphp/compiler/parser/parser.h @@ -532,7 +532,6 @@ struct Parser : ParserBase { None = 0, HH = 0x1, PHP7_ScalarTypes = 0x2, - PHP7_EngineExceptions = 0x4, }; struct AutoAlias { diff --git a/hphp/system/php/lang/Error.php b/hphp/system/php/lang/Error.php new file mode 100644 index 00000000000000..07513189d9d318 --- /dev/null +++ b/hphp/system/php/lang/Error.php @@ -0,0 +1,37 @@ +message === '') { + $this->message = $message; + } + + if ($code !== 0 || $this->code === 0) { + $this->code = $code; + } + + $this->previous = $previous; + } +} + +class ArithmeticError extends Error {} +class AssertionError extends Error {} +class DivisionByZeroError extends Error {} +class ParseError extends Error {} +class TypeError extends Error {} diff --git a/hphp/system/php/lang/Throwable.php b/hphp/system/php/lang/Throwable.php new file mode 100644 index 00000000000000..e69e0f145e8edb --- /dev/null +++ b/hphp/system/php/lang/Throwable.php @@ -0,0 +1,11 @@ +m_cachedClass.bind(rds::Mode::Persistent); \ + x##Ne->setCachedClass(SystemLib::s_##x##Class) + +InitFiniNode aliasPhp7Classes( + []() { + if (!RuntimeOption::PHP7_EngineExceptions) { + return; + } + PHP7_ROOT_ALIAS(Throwable); + PHP7_ROOT_ALIAS(Error); + PHP7_ROOT_ALIAS(ArithmeticError); + PHP7_ROOT_ALIAS(AssertionError); + PHP7_ROOT_ALIAS(DivisionByZeroError); + PHP7_ROOT_ALIAS(ParseError); + PHP7_ROOT_ALIAS(TypeError); + }, + InitFiniNode::When::ProcessInit +); + +#undef PHP7_ROOT_ALIAS +} // namespace + ///////////////////////////////////////////////////////////////////////////// }} // namespace HPHP::SystemLib diff --git a/hphp/test/slow/php7_backported/engine-exceptions/throwable_namespace_001.php b/hphp/test/slow/php7_backported/engine-exceptions/throwable_namespace_001.php new file mode 100644 index 00000000000000..d138c2e412acd0 --- /dev/null +++ b/hphp/test/slow/php7_backported/engine-exceptions/throwable_namespace_001.php @@ -0,0 +1,9 @@ +