Skip to content

Commit 4acf008

Browse files
committed
Deprecate calling FFI::cast(), FFI::new(), and FFI::type() statically
1 parent 134441e commit 4acf008

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+311
-110
lines changed

ext/ffi/ffi.c

+33-9
Original file line numberDiff line numberDiff line change
@@ -3736,6 +3736,7 @@ ZEND_METHOD(FFI, new) /* {{{ */
37363736
bool owned = 1;
37373737
bool persistent = 0;
37383738
bool is_const = 0;
3739+
bool is_static_call = Z_TYPE(EX(This)) != IS_OBJECT;
37393740
zend_ffi_flags flags = ZEND_FFI_FLAG_OWNED;
37403741

37413742
ZEND_FFI_VALIDATE_API_RESTRICTION();
@@ -3746,6 +3747,13 @@ ZEND_METHOD(FFI, new) /* {{{ */
37463747
Z_PARAM_BOOL(persistent)
37473748
ZEND_PARSE_PARAMETERS_END();
37483749

3750+
if (is_static_call) {
3751+
zend_error(E_DEPRECATED, "Calling FFI::new() statically is deprecated");
3752+
if (EG(exception)) {
3753+
RETURN_THROWS();
3754+
}
3755+
}
3756+
37493757
if (!owned) {
37503758
flags &= ~ZEND_FFI_FLAG_OWNED;
37513759
}
@@ -3757,7 +3765,7 @@ ZEND_METHOD(FFI, new) /* {{{ */
37573765
if (type_def) {
37583766
zend_ffi_dcl dcl = ZEND_FFI_ATTR_INIT;
37593767

3760-
if (Z_TYPE(EX(This)) == IS_OBJECT) {
3768+
if (!is_static_call) {
37613769
zend_ffi *ffi = (zend_ffi*)Z_OBJ(EX(This));
37623770
FFI_G(symbols) = ffi->symbols;
37633771
FFI_G(tags) = ffi->tags;
@@ -3770,7 +3778,7 @@ ZEND_METHOD(FFI, new) /* {{{ */
37703778

37713779
if (zend_ffi_parse_type(ZSTR_VAL(type_def), ZSTR_LEN(type_def), &dcl) == FAILURE) {
37723780
zend_ffi_type_dtor(dcl.type);
3773-
if (Z_TYPE(EX(This)) != IS_OBJECT) {
3781+
if (is_static_call) {
37743782
if (FFI_G(tags)) {
37753783
zend_hash_destroy(FFI_G(tags));
37763784
efree(FFI_G(tags));
@@ -3790,7 +3798,7 @@ ZEND_METHOD(FFI, new) /* {{{ */
37903798
is_const = 1;
37913799
}
37923800

3793-
if (Z_TYPE(EX(This)) != IS_OBJECT) {
3801+
if (is_static_call) {
37943802
if (FFI_G(tags)) {
37953803
zend_ffi_tags_cleanup(&dcl);
37963804
}
@@ -3886,6 +3894,7 @@ ZEND_METHOD(FFI, cast) /* {{{ */
38863894
zend_ffi_type *old_type, *type, *type_ptr;
38873895
zend_ffi_cdata *old_cdata, *cdata;
38883896
bool is_const = 0;
3897+
bool is_static_call = Z_TYPE(EX(This)) != IS_OBJECT;
38893898
zval *zv, *arg;
38903899
void *ptr;
38913900

@@ -3895,13 +3904,20 @@ ZEND_METHOD(FFI, cast) /* {{{ */
38953904
Z_PARAM_ZVAL(zv)
38963905
ZEND_PARSE_PARAMETERS_END();
38973906

3907+
if (is_static_call) {
3908+
zend_error(E_DEPRECATED, "Calling FFI::cast() statically is deprecated");
3909+
if (EG(exception)) {
3910+
RETURN_THROWS();
3911+
}
3912+
}
3913+
38983914
arg = zv;
38993915
ZVAL_DEREF(zv);
39003916

39013917
if (type_def) {
39023918
zend_ffi_dcl dcl = ZEND_FFI_ATTR_INIT;
39033919

3904-
if (Z_TYPE(EX(This)) == IS_OBJECT) {
3920+
if (!is_static_call) {
39053921
zend_ffi *ffi = (zend_ffi*)Z_OBJ(EX(This));
39063922
FFI_G(symbols) = ffi->symbols;
39073923
FFI_G(tags) = ffi->tags;
@@ -3914,7 +3930,7 @@ ZEND_METHOD(FFI, cast) /* {{{ */
39143930

39153931
if (zend_ffi_parse_type(ZSTR_VAL(type_def), ZSTR_LEN(type_def), &dcl) == FAILURE) {
39163932
zend_ffi_type_dtor(dcl.type);
3917-
if (Z_TYPE(EX(This)) != IS_OBJECT) {
3933+
if (is_static_call) {
39183934
if (FFI_G(tags)) {
39193935
zend_hash_destroy(FFI_G(tags));
39203936
efree(FFI_G(tags));
@@ -3934,7 +3950,7 @@ ZEND_METHOD(FFI, cast) /* {{{ */
39343950
is_const = 1;
39353951
}
39363952

3937-
if (Z_TYPE(EX(This)) != IS_OBJECT) {
3953+
if (is_static_call) {
39383954
if (FFI_G(tags)) {
39393955
zend_ffi_tags_cleanup(&dcl);
39403956
}
@@ -4061,13 +4077,21 @@ ZEND_METHOD(FFI, type) /* {{{ */
40614077
zend_ffi_ctype *ctype;
40624078
zend_ffi_dcl dcl = ZEND_FFI_ATTR_INIT;
40634079
zend_string *type_def;
4080+
bool is_static_call = Z_TYPE(EX(This)) != IS_OBJECT;
40644081

40654082
ZEND_FFI_VALIDATE_API_RESTRICTION();
40664083
ZEND_PARSE_PARAMETERS_START(1, 1)
40674084
Z_PARAM_STR(type_def);
40684085
ZEND_PARSE_PARAMETERS_END();
40694086

4070-
if (Z_TYPE(EX(This)) == IS_OBJECT) {
4087+
if (is_static_call) {
4088+
zend_error(E_DEPRECATED, "Calling FFI::type() statically is deprecated");
4089+
if (EG(exception)) {
4090+
RETURN_THROWS();
4091+
}
4092+
}
4093+
4094+
if (!is_static_call) {
40714095
zend_ffi *ffi = (zend_ffi*)Z_OBJ(EX(This));
40724096
FFI_G(symbols) = ffi->symbols;
40734097
FFI_G(tags) = ffi->tags;
@@ -4080,7 +4104,7 @@ ZEND_METHOD(FFI, type) /* {{{ */
40804104

40814105
if (zend_ffi_parse_type(ZSTR_VAL(type_def), ZSTR_LEN(type_def), &dcl) == FAILURE) {
40824106
zend_ffi_type_dtor(dcl.type);
4083-
if (Z_TYPE(EX(This)) != IS_OBJECT) {
4107+
if (is_static_call) {
40844108
if (FFI_G(tags)) {
40854109
zend_hash_destroy(FFI_G(tags));
40864110
efree(FFI_G(tags));
@@ -4095,7 +4119,7 @@ ZEND_METHOD(FFI, type) /* {{{ */
40954119
return;
40964120
}
40974121

4098-
if (Z_TYPE(EX(This)) != IS_OBJECT) {
4122+
if (is_static_call) {
40994123
if (FFI_G(tags)) {
41004124
zend_ffi_tags_cleanup(&dcl);
41014125
}

ext/ffi/tests/005.phpt

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ ffi
66
ffi.enable=1
77
--FILE--
88
<?php
9-
$m = FFI::new("int[2][2]");
10-
$v = FFI::new("int[2]");
9+
$ffi = FFI::cdef();
10+
11+
$m = $ffi->new("int[2][2]");
12+
$v = $ffi->new("int[2]");
1113
$v[1] = 42;
1214
$m[1] = $v;
1315
var_dump($m);

ext/ffi/tests/006.phpt

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ ffi
66
ffi.enable=1
77
--FILE--
88
<?php
9-
$v = FFI::new("int*[2]");
10-
$v[1] = FFI::new("int[1]", false);
9+
$ffi = FFI::cdef();
10+
11+
$v = $ffi->new("int*[2]");
12+
$v[1] = $ffi->new("int[1]", false);
1113
$v[1][0] = 42;
1214
var_dump($v);
1315
FFI::free($v[1]);

ext/ffi/tests/007.phpt

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ ffi
66
ffi.enable=1
77
--FILE--
88
<?php
9-
$v = FFI::new("int*[3]");
10-
$v[0] = FFI::new("int[1]", false);
11-
$v[1] = FFI::new("int[1]", false);
9+
$ffi = FFI::cdef();
10+
11+
$v = $ffi->new("int*[3]");
12+
$v[0] = $ffi->new("int[1]", false);
13+
$v[1] = $ffi->new("int[1]", false);
1214
$v[2] = $v[1];
1315
$v[1][0] = 42;
1416
var_dump($v[0] == $v[1]);

ext/ffi/tests/008.phpt

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@ ffi
66
ffi.enable=1
77
--FILE--
88
<?php
9-
$a = FFI::new("int[3]");
9+
$ffi = FFI::cdef();
10+
11+
$a = $ffi->new("int[3]");
1012
$a[1] = 10;
1113
$a[2] = 20;
1214
var_dump(count($a));
1315
foreach ($a as $key => $val) {
1416
echo "$key => $val\n";
1517
}
1618

17-
$a = FFI::new("struct {int x,y;}");
19+
$a = $ffi->new("struct {int x,y;}");
1820
try {
1921
var_dump(count($a));
2022
} catch (Throwable $e) {

ext/ffi/tests/009.phpt

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ ffi
66
ffi.enable=1
77
--FILE--
88
<?php
9-
$a = FFI::new("int[3]");
9+
$ffi = FFI::cdef();
10+
11+
$a = $ffi->new("int[3]");
1012
$a[1] = 10;
1113
$a[2] = 20;
12-
$b = FFI::new("int[4]");
14+
$b = $ffi->new("int[4]");
1315
var_dump(FFI::memcmp($b, $a, FFI::sizeof($a)));
1416
FFI::memcpy($b, $a, FFI::sizeof($a));
1517
var_dump($b);

ext/ffi/tests/010.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ffi
66
ffi.enable=1
77
--FILE--
88
<?php
9-
$a = FFI::new("int[3]");
9+
$a = FFI::cdef()->new("int[3]");
1010
FFI::memset($a, ord("a"), FFI::sizeof($a));
1111
var_dump(FFI::string($a, FFI::sizeof($a)));
1212
?>

ext/ffi/tests/011.phpt

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ ffi
66
ffi.enable=1
77
--FILE--
88
<?php
9-
$a = FFI::new("uint8_t[4]");
9+
$ffi = FFI::cdef();
10+
11+
$a = $ffi->new("uint8_t[4]");
1012
$a[0] = 255;
1113
$a[1] = 255;
12-
var_dump(FFI::cast("uint16_t[2]", $a));
14+
var_dump($ffi->cast("uint16_t[2]", $a));
1315
?>
1416
--EXPECTF--
1517
object(FFI\CData:uint16_t[2])#%d (2) {

ext/ffi/tests/012.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ ffi.enable=1
77
--FILE--
88
<?php
99
try {
10-
var_dump(serialize(FFI::new("int[2]")));
10+
var_dump(serialize(FFI::cdef()->new("int[2]")));
1111
} catch (Throwable $e) {
1212
echo get_class($e) . ": " . $e->getMessage()."\n";
1313
}

ext/ffi/tests/013.phpt

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,21 @@ ffi
66
ffi.enable=1
77
--FILE--
88
<?php
9-
$a = FFI::new("int[1][2][3]");
9+
$ffi = FFI::cdef();
10+
11+
$a = $ffi->new("int[1][2][3]");
1012
var_dump(count($a));
1113
var_dump(count($a[0]));
1214
var_dump(count($a[0][0]));
1315

1416
try {
15-
var_dump(FFI::new("void"));
17+
var_dump($ffi->new("void"));
1618
} catch (Throwable $e) {
1719
echo get_class($e) . ": " . $e->getMessage()."\n";
1820
}
1921

2022
try {
21-
var_dump(FFI::new("void[1]"));
23+
var_dump($ffi->new("void[1]"));
2224
} catch (Throwable $e) {
2325
echo get_class($e) . ": " . $e->getMessage()."\n";
2426
}

ext/ffi/tests/014.phpt

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ ffi
66
ffi.enable=1
77
--FILE--
88
<?php
9-
var_dump(FFI::sizeof(FFI::new("uint32_t[2]")));
10-
var_dump(FFI::sizeof(FFI::new("uint32_t([2])")));
11-
var_dump(FFI::sizeof(FFI::new("uint32_t([2])[2]")));
9+
$ffi = FFI::cdef();
10+
11+
var_dump(FFI::sizeof($ffi->new("uint32_t[2]")));
12+
var_dump(FFI::sizeof($ffi->new("uint32_t([2])")));
13+
var_dump(FFI::sizeof($ffi->new("uint32_t([2])[2]")));
1214
?>
1315
ok
1416
--EXPECT--

ext/ffi/tests/017.phpt

+5
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,13 @@ try {
2424
?>
2525
ok
2626
--EXPECTF--
27+
Deprecated: Calling FFI::new() statically is deprecated in %s on line %d
2728
FFI\ParserException: function type is not allowed at line 1
29+
30+
Deprecated: Calling FFI::new() statically is deprecated in %s on line %d
2831
FFI\ParserException: Struct/union can't contain an instance of itself at line 1
32+
33+
Deprecated: Calling FFI::new() statically is deprecated in %s on line %d
2934
object(FFI\CData:struct X)#%d (1) {
3035
["ptr"]=>
3136
NULL

ext/ffi/tests/020.phpt

+8-6
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,47 @@ ffi
66
ffi.enable=1
77
--FILE--
88
<?php
9+
$ffi = FFI::cdef();
10+
911
try {
10-
$p = FFI::new("struct {int x; const int y;}");
12+
$p = $ffi->new("struct {int x; const int y;}");
1113
$p->x = 1;
1214
$p->y = 1;
1315
echo "ok\n";
1416
} catch (Throwable $e) {
1517
echo get_class($e) . ": " . $e->getMessage()."\n";
1618
}
1719
try {
18-
$p = FFI::new("struct {const int x; int y;}");
20+
$p = $ffi->new("struct {const int x; int y;}");
1921
$p->y = 1;
2022
$p->x = 1;
2123
echo "ok\n";
2224
} catch (Throwable $e) {
2325
echo get_class($e) . ": " . $e->getMessage()."\n";
2426
}
2527
try {
26-
$p = FFI::new("const struct {int x; int y;}");
28+
$p = $ffi->new("const struct {int x; int y;}");
2729
$p->x = 1;
2830
echo "ok\n";
2931
} catch (Throwable $e) {
3032
echo get_class($e) . ": " . $e->getMessage()."\n";
3133
}
3234
try {
33-
$p = FFI::new("const int[10]");
35+
$p = $ffi->new("const int[10]");
3436
$p[1] = 1;
3537
echo "ok\n";
3638
} catch (Throwable $e) {
3739
echo get_class($e) . ": " . $e->getMessage()."\n";
3840
}
3941
try {
40-
$p = FFI::new("const int * [1]");
42+
$p = $ffi->new("const int * [1]");
4143
$p[0] = null;
4244
echo "ok\n";
4345
} catch (Throwable $e) {
4446
echo get_class($e) . ": " . $e->getMessage()."\n";
4547
}
4648
try {
47-
$p = FFI::new("int * const [1]");
49+
$p = $ffi->new("int * const [1]");
4850
$p[0] = null;
4951
echo "ok\n";
5052
} catch (Throwable $e) {

0 commit comments

Comments
 (0)