• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //     * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 //     * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #ifndef PHP_PROTOBUF_H_
32 #define PHP_PROTOBUF_H_
33 
34 #include <php.h>
35 #include <stdbool.h>
36 
37 #include "php-upb.h"
38 
39 const zval *get_generated_pool();
40 
41 #if PHP_VERSION_ID < 70300
42 #define GC_ADDREF(h) ++GC_REFCOUNT(h)
43 #define GC_DELREF(h) --GC_REFCOUNT(h)
44 #endif
45 
46 // Since php 7.4, the write_property() object handler now returns the assigned
47 // value (after possible type coercions) rather than void.
48 // https://github.com/php/php-src/blob/PHP-7.4.0/UPGRADING.INTERNALS#L171-L173
49 #if PHP_VERSION_ID < 70400
50 #define PROTO_RETURN_VAL void
51 #else
52 #define PROTO_RETURN_VAL zval*
53 #endif
54 
55 // Sine php 8.0, the Object Handlers API was changed to receive zend_object*
56 // instead of zval* and zend_string* instead of zval* for property names.
57 // https://github.com/php/php-src/blob/php-8.0.0beta1/UPGRADING.INTERNALS#L37-L39
58 #if PHP_VERSION_ID < 80000
59 #define PROTO_VAL zval
60 #define PROTO_STR zval
61 #define PROTO_MSG_P(obj) (Message*)Z_OBJ_P(obj)
62 #define PROTO_STRVAL_P(obj) Z_STRVAL_P(obj)
63 #define PROTO_STRLEN_P(obj) Z_STRLEN_P(obj)
64 #else
65 #define PROTO_VAL zend_object
66 #define PROTO_STR zend_string
67 #define PROTO_MSG_P(obj) (Message*)(obj)
68 #define PROTO_STRVAL_P(obj) ZSTR_VAL(obj)
69 #define PROTO_STRLEN_P(obj) ZSTR_LEN(obj)
70 #endif
71 
72 #define PHP_PROTOBUF_VERSION "3.13.0"
73 
74 // ptr -> PHP object cache. This is a weak map that caches lazily-created
75 // wrapper objects around upb types:
76 //  * upb_msg* -> Message
77 //  * upb_array* -> RepeatedField
78 //  * upb_map*, -> MapField
79 //  * upb_msgdef* -> Descriptor
80 //  * upb_enumdef* -> EnumDescriptor
81 //  * zend_class_entry* -> Descriptor
82 //
83 // Each wrapped object should add itself to the map when it is constructed, and
84 // remove itself from the map when it is destroyed. This is how we ensure that
85 // the map only contains live objects. The map is weak so it does not actually
86 // take references to the cached objects.
87 void ObjCache_Add(const void *key, zend_object *php_obj);
88 void ObjCache_Delete(const void *key);
89 bool ObjCache_Get(const void *key, zval *val);
90 
91 // PHP class name map. This is necessary because the pb_name->php_class_name
92 // transformation is non-reversible, so when we need to look up a msgdef or
93 // enumdef by PHP class, we can't turn the class name into a pb_name.
94 //  * php_class_name -> upb_msgdef*
95 //  * php_class_name -> upb_enumdef*
96 void NameMap_AddMessage(const upb_msgdef *m);
97 void NameMap_AddEnum(const upb_enumdef *m);
98 const upb_msgdef *NameMap_GetMessage(zend_class_entry *ce);
99 const upb_enumdef *NameMap_GetEnum(zend_class_entry *ce);
100 
101 // We need our own assert() because PHP takes control of NDEBUG in its headers.
102 #ifdef PBPHP_ENABLE_ASSERTS
103 #define PBPHP_ASSERT(x)                                                    \
104   do {                                                                     \
105     if (!(x)) {                                                            \
106       fprintf(stderr, "Assertion failure at %s:%d %s", __FILE__, __LINE__, \
107               #x);                                                         \
108       abort();                                                             \
109     }                                                                      \
110   } while (false)
111 #else
112 #define PBPHP_ASSERT(x) \
113   do {                  \
114   } while (false && (x))
115 #endif
116 
117 #endif  // PHP_PROTOBUF_H_
118