aboutsummaryrefslogtreecommitdiffhomepage
path: root/php/ext/google/protobuf/protobuf.c
diff options
context:
space:
mode:
authorGravatar Bo Yang <teboring@google.com>2017-09-11 15:51:57 -0700
committerGravatar Bo Yang <teboring@google.com>2017-09-11 15:51:57 -0700
commitddb9ef9cc3f9a7eab1ddf7ff90ecf2400e93c2b8 (patch)
tree9fe37ac0ea2ab2d19ad3d9bb39a0ab6d3ef3a94a /php/ext/google/protobuf/protobuf.c
parent89069ded5a34d552e63c541ed1132cefe34d91d1 (diff)
Change array to map for reserved names in c extension
Diffstat (limited to 'php/ext/google/protobuf/protobuf.c')
-rw-r--r--php/ext/google/protobuf/protobuf.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/php/ext/google/protobuf/protobuf.c b/php/ext/google/protobuf/protobuf.c
index dc730030..43b4e584 100644
--- a/php/ext/google/protobuf/protobuf.c
+++ b/php/ext/google/protobuf/protobuf.c
@@ -46,6 +46,7 @@ static HashTable* upb_def_to_php_obj_map;
// Global map from message/enum's php class entry to corresponding wrapper
// Descriptor/EnumDescriptor instances.
static HashTable* ce_to_php_obj_map;
+static HashTable* reserved_names;
// -----------------------------------------------------------------------------
// Global maps.
@@ -111,6 +112,36 @@ bool class_added(const void* ce) {
}
// -----------------------------------------------------------------------------
+// Reserved Name.
+// -----------------------------------------------------------------------------
+
+// Although we already have kReservedNames, we still add them to hash table to
+// speed up look up.
+const char *const kReservedNames[] = {
+ "abstract", "and", "array", "as", "break",
+ "callable", "case", "catch", "class", "clone",
+ "const", "continue", "declare", "default", "die",
+ "do", "echo", "else", "elseif", "empty",
+ "enddeclare", "endfor", "endforeach", "endif", "endswitch",
+ "endwhile", "eval", "exit", "extends", "final",
+ "for", "foreach", "function", "global", "goto",
+ "if", "implements", "include", "include_once", "instanceof",
+ "insteadof", "interface", "isset", "list", "namespace",
+ "new", "or", "print", "private", "protected",
+ "public", "require", "require_once", "return", "static",
+ "switch", "throw", "trait", "try", "unset",
+ "use", "var", "while", "xor", "int",
+ "float", "bool", "string", "true", "false",
+ "null", "void", "iterable"};
+const int kReservedNamesSize = 73;
+
+bool is_reserved_name(const char* name) {
+ void** value;
+ return (php_proto_zend_hash_find(reserved_names, name, strlen(name),
+ (void**)&value) == SUCCESS);
+}
+
+// -----------------------------------------------------------------------------
// Utilities.
// -----------------------------------------------------------------------------
@@ -157,12 +188,21 @@ static void php_proto_hashtable_descriptor_release(zval* value) {
#endif
static PHP_RINIT_FUNCTION(protobuf) {
+ int i = 0;
+
ALLOC_HASHTABLE(upb_def_to_php_obj_map);
zend_hash_init(upb_def_to_php_obj_map, 16, NULL, HASHTABLE_VALUE_DTOR, 0);
ALLOC_HASHTABLE(ce_to_php_obj_map);
zend_hash_init(ce_to_php_obj_map, 16, NULL, HASHTABLE_VALUE_DTOR, 0);
+ ALLOC_HASHTABLE(reserved_names);
+ zend_hash_init(reserved_names, 16, NULL, NULL, 0);
+ for (i = 0; i < kReservedNamesSize; i++) {
+ php_proto_zend_hash_update(reserved_names, kReservedNames[i],
+ strlen(kReservedNames[i]));
+ }
+
generated_pool = NULL;
generated_pool_php = NULL;
internal_generated_pool_php = NULL;
@@ -177,6 +217,9 @@ static PHP_RSHUTDOWN_FUNCTION(protobuf) {
zend_hash_destroy(ce_to_php_obj_map);
FREE_HASHTABLE(ce_to_php_obj_map);
+ zend_hash_destroy(reserved_names);
+ FREE_HASHTABLE(reserved_names);
+
#if PHP_MAJOR_VERSION < 7
if (generated_pool_php != NULL) {
zval_dtor(generated_pool_php);