Skip to content

Commit

Permalink
Switch to version 2.0.0, add reflection types
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
TysonAndre committed Aug 25, 2022
1 parent 5b01510 commit d5a201f
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 66 deletions.
23 changes: 4 additions & 19 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@
</lead>
<date>2022-08-15</date>
<version>
<release>1.0.0</release>
<api>1.0.0</api>
<release>2.0.0</release>
<api>2.0.0</api>
</version>
<stability>
<release>stable</release>
<api>stable</api>
</stability>
<license uri="https://www.php.net/license/3_0.txt">PHP License</license>
<notes>
* Initial release
* Initial PECL release
</notes>
<contents>
<dir name="/">
Expand All @@ -50,6 +50,7 @@
<file name="decode_invalid_property.phpt" role="test"/>
<file name="decode_max_depth.phpt" role="test"/>
<file name="decode_result.phpt" role="test"/>
<file name="decode_strict_types.phpt" role="test"/>
<file name="decode_types.phpt" role="test"/>
<file name="depth.phpt" role="test"/>
<file name="is_valid.phpt" role="test"/>
Expand Down Expand Up @@ -82,21 +83,5 @@
<providesextension>simdjson</providesextension>
<extsrcrelease/>
<changelog>
<release>
<date>2022-08-15</date>
<time>00:00:00</time>
<version>
<release>1.0.0</release>
<api>1.0.0</api>
</version>
<stability>
<release>stable</release>
<api>stable</api>
</stability>
<license uri="http://www.php.net/license/3_0.txt">PHP License</license>
<notes>
* Initial release
</notes>
</release>
</changelog>
</package>
68 changes: 29 additions & 39 deletions php_simdjson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion php_simdjson.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions tests/decode_args.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ echo $reflection;
Function [ <internal:simdjson> function simdjson_decode ] {

- Parameters [3] {
Parameter #0 [ <required> $json ]
Parameter #1 [ <optional> $assoc%S ]
Parameter #2 [ <optional> $depth%S ]
Parameter #0 [ <required> string $json ]
Parameter #1 [ <optional> bool%S $assoc%S ]
Parameter #2 [ <optional> int%S $depth%S ]
}
}
20 changes: 20 additions & 0 deletions tests/decode_strict_types.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
simdjson_decode uses strict types
--FILE--
<?php
declare(strict_types = 1);

try {
var_dump(simdjson_decode(null));
} catch (Error $e) {
printf("Caught %s: %s\n", get_class($e), $e->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
3 changes: 2 additions & 1 deletion tests/is_valid_args.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ echo $reflection;
Function [ <internal:simdjson> function simdjson_is_valid ] {

- Parameters [2] {
Parameter #0 [ <required> $json ]
Parameter #0 [ <required> string $json ]
Parameter #1 [ <optional> int%S $depth%S ]
}
- Return [ %Sbool%S ]
}
3 changes: 2 additions & 1 deletion tests/key_count_args.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ echo $reflection;
Function [ <internal:simdjson> function simdjson_key_count ] {

- Parameters [3] {
Parameter #0 [ <required> $json ]
Parameter #0 [ <required> string $json ]
Parameter #1 [ <required> string $key ]
Parameter #2 [ <optional> int%S $depth%S ]
}
- Return [ %Sint%S ]
}
3 changes: 2 additions & 1 deletion tests/key_exists_args.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ echo $reflection;
Function [ <internal:simdjson> function simdjson_key_exists ] {

- Parameters [3] {
Parameter #0 [ <required> $json ]
Parameter #0 [ <required> string $json ]
Parameter #1 [ <required> string $key ]
Parameter #2 [ <optional> int%S $depth%S ]
}
- Return [ %Sbool%S ]
}
2 changes: 1 addition & 1 deletion tests/key_value_args.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ echo $reflection;
Function [ <internal:simdjson> function simdjson_key_value ] {

- Parameters [4] {
Parameter #0 [ <required> $json ]
Parameter #0 [ <required> string $json ]
Parameter #1 [ <required> string $key ]
Parameter #2 [ <optional> bool%S $assoc%S ]
Parameter #3 [ <optional> int%S $depth%S ]
Expand Down

0 comments on commit d5a201f

Please sign in to comment.