From d5a201f4ff0013a357f70a07324de83a54d638ea Mon Sep 17 00:00:00 2001 From: Tyson Andre Date: Thu, 25 Aug 2022 07:28:22 -0400 Subject: [PATCH] Switch to version 2.0.0, add reflection types In php 8, many remaining functions started adding reflection types (e.g. json_decode started typing `json` as string in 8.0) Also add return types, accounting for functions currently emitting notices and returning null. Change the version to 2.0.0 to avoid confusion with installations of older 1.0.0 commits of this repo when a PECL release gets created. --- package.xml | 23 ++---------- php_simdjson.cpp | 68 +++++++++++++++------------------- php_simdjson.h | 2 +- tests/decode_args.phpt | 6 +-- tests/decode_strict_types.phpt | 20 ++++++++++ tests/is_valid_args.phpt | 3 +- tests/key_count_args.phpt | 3 +- tests/key_exists_args.phpt | 3 +- tests/key_value_args.phpt | 2 +- 9 files changed, 64 insertions(+), 66 deletions(-) create mode 100644 tests/decode_strict_types.phpt diff --git a/package.xml b/package.xml index 822e66b..e30a423 100644 --- a/package.xml +++ b/package.xml @@ -18,8 +18,8 @@ 2022-08-15 - 1.0.0 - 1.0.0 + 2.0.0 + 2.0.0 stable @@ -27,7 +27,7 @@ PHP License -* Initial release +* Initial PECL release @@ -50,6 +50,7 @@ + @@ -82,21 +83,5 @@ simdjson - - 2022-08-15 - - - 1.0.0 - 1.0.0 - - - stable - stable - - PHP License - -* Initial release - - diff --git a/php_simdjson.cpp b/php_simdjson.cpp index 1c79034..a5dd9d6 100644 --- a/php_simdjson.cpp +++ b/php_simdjson.cpp @@ -28,32 +28,40 @@ extern "C" { ZEND_DECLARE_MODULE_GLOBALS(simdjson); -ZEND_BEGIN_ARG_INFO_EX(simdjson_is_valid_arginfo, 0, 0, 1) - ZEND_ARG_INFO(0, json) +#if PHP_VERSION_ID >= 70200 +#define SIMDJSON_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(name, return_reference, required_num_args, type, allow_null) \ + ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(name, return_reference, required_num_args, type, allow_null) +#else +#define SIMDJSON_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(name, return_reference, required_num_args, type, allow_null) \ + ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(name, return_reference, required_num_args, type, NULL, allow_null) +#endif + +SIMDJSON_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(simdjson_is_valid_arginfo, 0, 1, _IS_BOOL, 1) + ZEND_ARG_TYPE_INFO(0, json, IS_STRING, 0) ZEND_ARG_TYPE_INFO(0, depth, IS_LONG, 0) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(simdjson_decode_arginfo, 0, 0, 1) - ZEND_ARG_INFO(0, json) - ZEND_ARG_INFO(0, assoc) - ZEND_ARG_INFO(0, depth) + ZEND_ARG_TYPE_INFO(0, json, IS_STRING, 0) + ZEND_ARG_TYPE_INFO(0, assoc, _IS_BOOL, 0) + ZEND_ARG_TYPE_INFO(0, depth, IS_LONG, 0) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(simdjson_key_value_arginfo, 0, 0, 2) - ZEND_ARG_INFO(0, json) + ZEND_ARG_TYPE_INFO(0, json, IS_STRING, 0) ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 0) ZEND_ARG_TYPE_INFO(0, assoc, _IS_BOOL, 0) ZEND_ARG_TYPE_INFO(0, depth, IS_LONG, 0) ZEND_END_ARG_INFO() -ZEND_BEGIN_ARG_INFO_EX(simdjson_key_exists_arginfo, 0, 0, 2) - ZEND_ARG_INFO(0, json) +SIMDJSON_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(simdjson_key_exists_arginfo, 0, 2, _IS_BOOL, 1) + ZEND_ARG_TYPE_INFO(0, json, IS_STRING, 0) ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 0) ZEND_ARG_TYPE_INFO(0, depth, IS_LONG, 0) ZEND_END_ARG_INFO() -ZEND_BEGIN_ARG_INFO_EX(simdjson_key_count_arginfo, 0, 0, 2) - ZEND_ARG_INFO(0, json) +SIMDJSON_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(simdjson_key_count_arginfo, 0, 2, IS_LONG, 1) + ZEND_ARG_TYPE_INFO(0, json, IS_STRING, 0) ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 0) ZEND_ARG_TYPE_INFO(0, depth, IS_LONG, 0) ZEND_END_ARG_INFO() @@ -125,61 +133,43 @@ PHP_FUNCTION (simdjson_decode) { PHP_FUNCTION (simdjson_key_value) { + zend_string *json = NULL; + zend_string *key = NULL; zend_bool assoc = 0; - zval *json = NULL; zend_long depth = SIMDJSON_PARSE_DEFAULT_DEPTH; - zend_string *key = NULL; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "zS|bl", &json, &key, &assoc, &depth) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS|bl", &json, &key, &assoc, &depth) == FAILURE) { return; } if (!simdjson_validate_depth(depth)) { RETURN_NULL(); } - if (IS_STRING == Z_TYPE_P(json)) { - zend_string *zd_json = Z_STR_P(json); - cplus_simdjson_key_value(simdjson_get_parser(), ZSTR_VAL(zd_json), ZSTR_LEN(zd_json), ZSTR_VAL(key), return_value, assoc, depth); - } else { - php_error_docref(NULL, E_WARNING, "expects parameter 1 to be string"); - } - + cplus_simdjson_key_value(simdjson_get_parser(), ZSTR_VAL(json), ZSTR_LEN(json), ZSTR_VAL(key), return_value, assoc, depth); } PHP_FUNCTION (simdjson_key_count) { - zval *json = NULL; - zend_long depth = SIMDJSON_PARSE_DEFAULT_DEPTH; + zend_string *json = NULL; zend_string *key = NULL; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "zS|l", &json, &key, &depth) == FAILURE) { + zend_long depth = SIMDJSON_PARSE_DEFAULT_DEPTH; + if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS|l", &json, &key, &depth) == FAILURE) { return; } if (!simdjson_validate_depth(depth)) { RETURN_NULL(); } - if (IS_STRING == Z_TYPE_P(json)) { - zend_string *zd_json = Z_STR_P(json); - cplus_simdjson_key_count(simdjson_get_parser(), ZSTR_VAL(zd_json), ZSTR_LEN(zd_json), ZSTR_VAL(key), return_value, depth); - } else { - php_error_docref(NULL, E_WARNING, "expects parameter 1 to be string"); - } - + cplus_simdjson_key_count(simdjson_get_parser(), ZSTR_VAL(json), ZSTR_LEN(json), ZSTR_VAL(key), return_value, depth); } PHP_FUNCTION (simdjson_key_exists) { - zval *json = NULL; + zend_string *json = NULL; zend_string *key = NULL; zend_long depth = SIMDJSON_PARSE_DEFAULT_DEPTH; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "zS|l", &json, &key, &depth) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS|l", &json, &key, &depth) == FAILURE) { return; } if (!simdjson_validate_depth(depth)) { return; } - u_short stats = SIMDJSON_PARSE_FAIL; - if (IS_STRING == Z_TYPE_P(json)) { - zend_string *zd_json = Z_STR_P(json); - stats = cplus_simdjson_key_exists(simdjson_get_parser(), ZSTR_VAL(zd_json), ZSTR_LEN(zd_json), ZSTR_VAL(key), depth); - } else { - php_error_docref(NULL, E_WARNING, "expects parameter 1 to be string"); - } + u_short stats = cplus_simdjson_key_exists(simdjson_get_parser(), ZSTR_VAL(json), ZSTR_LEN(json), ZSTR_VAL(key), depth); if (SIMDJSON_PARSE_FAIL == stats) { RETURN_NULL(); } else if (SIMDJSON_PARSE_KEY_EXISTS == stats) { diff --git a/php_simdjson.h b/php_simdjson.h index 4661cba..58034fe 100644 --- a/php_simdjson.h +++ b/php_simdjson.h @@ -17,7 +17,7 @@ extern zend_module_entry simdjson_module_entry; #define phpext_simdjson_ptr &simdjson_module_entry -#define PHP_SIMDJSON_VERSION "1.0.0" +#define PHP_SIMDJSON_VERSION "2.0.0" #define SIMDJSON_SUPPORT_URL "https://github.com/crazyxman/simdjson_php" #define SIMDJSON_PARSE_FAIL 0 #define SIMDJSON_PARSE_SUCCESS 1 diff --git a/tests/decode_args.phpt b/tests/decode_args.phpt index 85c12a6..4150d68 100644 --- a/tests/decode_args.phpt +++ b/tests/decode_args.phpt @@ -14,8 +14,8 @@ echo $reflection; Function [ function simdjson_decode ] { - Parameters [3] { - Parameter #0 [ $json ] - Parameter #1 [ $assoc%S ] - Parameter #2 [ $depth%S ] + Parameter #0 [ string $json ] + Parameter #1 [ bool%S $assoc%S ] + Parameter #2 [ int%S $depth%S ] } } \ No newline at end of file diff --git a/tests/decode_strict_types.phpt b/tests/decode_strict_types.phpt new file mode 100644 index 0000000..8b4c5cb --- /dev/null +++ b/tests/decode_strict_types.phpt @@ -0,0 +1,20 @@ +--TEST-- +simdjson_decode uses strict types +--FILE-- +getMessage()); +} +try { + var_dump(simdjson_key_exists('{}', null)); +} catch (Error $e) { + printf("Caught %s: %s\n", get_class($e), $e->getMessage()); +} +?> +--EXPECTF-- +Caught TypeError: %Ssimdjson_decode()%S must be of %Stype string, null given +Caught TypeError: %Ssimdjson_key_exists()%S must be of %Stype string, null given diff --git a/tests/is_valid_args.phpt b/tests/is_valid_args.phpt index 779bcd5..1c88ece 100644 --- a/tests/is_valid_args.phpt +++ b/tests/is_valid_args.phpt @@ -14,7 +14,8 @@ echo $reflection; Function [ function simdjson_is_valid ] { - Parameters [2] { - Parameter #0 [ $json ] + Parameter #0 [ string $json ] Parameter #1 [ int%S $depth%S ] } + - Return [ %Sbool%S ] } \ No newline at end of file diff --git a/tests/key_count_args.phpt b/tests/key_count_args.phpt index 09bd27f..8bf5508 100644 --- a/tests/key_count_args.phpt +++ b/tests/key_count_args.phpt @@ -14,8 +14,9 @@ echo $reflection; Function [ function simdjson_key_count ] { - Parameters [3] { - Parameter #0 [ $json ] + Parameter #0 [ string $json ] Parameter #1 [ string $key ] Parameter #2 [ int%S $depth%S ] } + - Return [ %Sint%S ] } \ No newline at end of file diff --git a/tests/key_exists_args.phpt b/tests/key_exists_args.phpt index 0b41de8..73d1930 100644 --- a/tests/key_exists_args.phpt +++ b/tests/key_exists_args.phpt @@ -14,8 +14,9 @@ echo $reflection; Function [ function simdjson_key_exists ] { - Parameters [3] { - Parameter #0 [ $json ] + Parameter #0 [ string $json ] Parameter #1 [ string $key ] Parameter #2 [ int%S $depth%S ] } + - Return [ %Sbool%S ] } \ No newline at end of file diff --git a/tests/key_value_args.phpt b/tests/key_value_args.phpt index 9493377..81472a6 100644 --- a/tests/key_value_args.phpt +++ b/tests/key_value_args.phpt @@ -14,7 +14,7 @@ echo $reflection; Function [ function simdjson_key_value ] { - Parameters [4] { - Parameter #0 [ $json ] + Parameter #0 [ string $json ] Parameter #1 [ string $key ] Parameter #2 [ bool%S $assoc%S ] Parameter #3 [ int%S $depth%S ]