Web Inspector: ObjC RWIProtocol codegen should better handle optional members
authorjoepeck@webkit.org <joepeck@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 24 Apr 2017 22:02:06 +0000 (22:02 +0000)
committerjoepeck@webkit.org <joepeck@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 24 Apr 2017 22:02:06 +0000 (22:02 +0000)
https://bugs.webkit.org/show_bug.cgi?id=171251
<rdar://problem/31697002>

Reviewed by Brian Burg.

* inspector/scripts/codegen/generate_objc_protocol_types_implementation.py:
(ObjCProtocolTypesImplementationGenerator._generate_getter_for_member):
* inspector/scripts/codegen/objc_generator.py:
(ObjCGenerator.protocol_to_objc_expression_for_member):
(ObjCGenerator.protocol_to_objc_code_block_for_object_member):
Always be safe and nil check object property accesses, optional or not.

* inspector/scripts/tests/generic/expected/type-declaration-object-type.json-result:
* inspector/scripts/tests/generic/expected/type-requiring-runtime-casts.json-result:
Rebaselined inspector generator tests.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@215698 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/JavaScriptCore/ChangeLog
Source/JavaScriptCore/inspector/scripts/codegen/generate_objc_protocol_types_implementation.py
Source/JavaScriptCore/inspector/scripts/codegen/objc_generator.py
Source/JavaScriptCore/inspector/scripts/tests/generic/expected/type-declaration-object-type.json-result
Source/JavaScriptCore/inspector/scripts/tests/generic/expected/type-requiring-runtime-casts.json-result

index 88f0518..4389d91 100644 (file)
@@ -1,3 +1,22 @@
+2017-04-24  Joseph Pecoraro  <pecoraro@apple.com>
+
+        Web Inspector: ObjC RWIProtocol codegen should better handle optional members
+        https://bugs.webkit.org/show_bug.cgi?id=171251
+        <rdar://problem/31697002>
+
+        Reviewed by Brian Burg.
+
+        * inspector/scripts/codegen/generate_objc_protocol_types_implementation.py:
+        (ObjCProtocolTypesImplementationGenerator._generate_getter_for_member):
+        * inspector/scripts/codegen/objc_generator.py:
+        (ObjCGenerator.protocol_to_objc_expression_for_member):
+        (ObjCGenerator.protocol_to_objc_code_block_for_object_member):
+        Always be safe and nil check object property accesses, optional or not.
+
+        * inspector/scripts/tests/generic/expected/type-declaration-object-type.json-result:
+        * inspector/scripts/tests/generic/expected/type-requiring-runtime-casts.json-result:
+        Rebaselined inspector generator tests.
+
 2017-04-24  Saam Barati  <sbarati@apple.com>
 
         ASSERTION FAILED: m_table seen with workers/wasm-hashset LayoutTests
index a319c94..9977483 100755 (executable)
@@ -31,7 +31,7 @@ from string import Template
 
 from generator import Generator, ucfirst
 from models import ObjectType, EnumType, Frameworks
-from objc_generator import ObjCGenerator
+from objc_generator import ObjCTypeCategory, ObjCGenerator
 from objc_generator_templates import ObjCGeneratorTemplates as ObjCTemplates
 
 log = logging.getLogger('global')
@@ -206,10 +206,18 @@ class ObjCProtocolTypesImplementationGenerator(ObjCGenerator):
         var_name = ObjCGenerator.identifier_to_objc_identifier(member.member_name)
         getter_method = ObjCGenerator.objc_getter_method_for_member(declaration, member)
         basic_expression = '[super %s:@"%s"]' % (getter_method, member.member_name)
-        conversion_expression = self.protocol_to_objc_expression_for_member(declaration, member, basic_expression)
-        lines = []
-        lines.append('- (%s)%s' % (objc_type, var_name))
-        lines.append('{')
-        lines.append('    return %s;' % conversion_expression)
-        lines.append('}')
+        category = ObjCTypeCategory.category_for_type(member.type)
+        if category is ObjCTypeCategory.Object:
+            lines = []
+            lines.append('- (%s)%s' % (objc_type, var_name))
+            lines.append('{')
+            lines.append(self.protocol_to_objc_code_block_for_object_member(declaration, member, basic_expression))
+            lines.append('}')
+        else:
+            conversion_expression = self.protocol_to_objc_expression_for_member(declaration, member, basic_expression)
+            lines = []
+            lines.append('- (%s)%s' % (objc_type, var_name))
+            lines.append('{')
+            lines.append('    return %s;' % conversion_expression)
+            lines.append('}')
         return '\n'.join(lines)
index 022c8e6..944d2e6 100755 (executable)
@@ -446,8 +446,7 @@ class ObjCGenerator(Generator):
                 return 'fromProtocolString<%s>(%s).value()' % (self.objc_enum_name_for_non_anonymous_enum(member.type), sub_expression)
             return sub_expression
         if category is ObjCTypeCategory.Object:
-            objc_class = self.objc_class_for_type(member.type)
-            return '[[%s alloc] initWithInspectorObject:[%s toInspectorObject].get()]' % (objc_class, sub_expression)
+            raise Exception("protocol_to_objc_expression_for_member does not support an Object type. See: protocol_to_objc_code_block_for_object_member")
         if category is ObjCTypeCategory.Array:
             protocol_type = ObjCGenerator.protocol_type_for_type(member.type.element_type)
             objc_class = self.objc_class_for_type(member.type.element_type)
@@ -460,6 +459,15 @@ class ObjCGenerator(Generator):
                 return 'objcIntegerArray(%s)' % sub_expression
             return 'objcArray<%s>(%s)' % (objc_class, sub_expression)
 
+    def protocol_to_objc_code_block_for_object_member(self, declaration, member, sub_expression):
+        objc_class = self.objc_class_for_type(member.type)
+        lines = []
+        lines.append('    %sJSONObject *object = %s;' % (ObjCGenerator.OBJC_STATIC_PREFIX, sub_expression))
+        lines.append('    if (!object)')
+        lines.append('        return nil;')
+        lines.append('    return [[%s alloc] initWithInspectorObject:[%s toInspectorObject].get()];' % (objc_class, sub_expression))
+        return '\n'.join(lines)
+
     def payload_to_objc_expression_for_member(self, declaration, member):
         _type = member.type
         if isinstance(_type, AliasedType):
index feb6370..70cb355 100644 (file)
@@ -1926,6 +1926,9 @@ using namespace Inspector;
 
 - (RWIProtocolJSONObject *)values
 {
+    RWIProtocolJSONObject *object = [super objectForKey:@"values"];
+    if (!object)
+        return nil;
     return [[RWIProtocolJSONObject alloc] initWithInspectorObject:[[super objectForKey:@"values"] toInspectorObject].get()];
 }
 
@@ -1936,6 +1939,9 @@ using namespace Inspector;
 
 - (RWIProtocolJSONObject *)payload
 {
+    RWIProtocolJSONObject *object = [super objectForKey:@"payload"];
+    if (!object)
+        return nil;
     return [[RWIProtocolJSONObject alloc] initWithInspectorObject:[[super objectForKey:@"payload"] toInspectorObject].get()];
 }
 
@@ -1946,6 +1952,9 @@ using namespace Inspector;
 
 - (TestProtocolDatabaseError *)error
 {
+    RWIProtocolJSONObject *object = [super objectForKey:@"error"];
+    if (!object)
+        return nil;
     return [[TestProtocolDatabaseError alloc] initWithInspectorObject:[[super objectForKey:@"error"] toInspectorObject].get()];
 }
 
@@ -2092,6 +2101,9 @@ using namespace Inspector;
 
 - (RWIProtocolJSONObject *)values
 {
+    RWIProtocolJSONObject *object = [super objectForKey:@"values"];
+    if (!object)
+        return nil;
     return [[RWIProtocolJSONObject alloc] initWithInspectorObject:[[super objectForKey:@"values"] toInspectorObject].get()];
 }
 
@@ -2102,6 +2114,9 @@ using namespace Inspector;
 
 - (RWIProtocolJSONObject *)payload
 {
+    RWIProtocolJSONObject *object = [super objectForKey:@"payload"];
+    if (!object)
+        return nil;
     return [[RWIProtocolJSONObject alloc] initWithInspectorObject:[[super objectForKey:@"payload"] toInspectorObject].get()];
 }
 
@@ -2112,6 +2127,9 @@ using namespace Inspector;
 
 - (TestProtocolDatabaseError *)error
 {
+    RWIProtocolJSONObject *object = [super objectForKey:@"error"];
+    if (!object)
+        return nil;
     return [[TestProtocolDatabaseError alloc] initWithInspectorObject:[[super objectForKey:@"error"] toInspectorObject].get()];
 }
 
@@ -2376,6 +2394,9 @@ using namespace Inspector;
 
 - (RWIProtocolJSONObject *)values
 {
+    RWIProtocolJSONObject *object = [super objectForKey:@"values"];
+    if (!object)
+        return nil;
     return [[RWIProtocolJSONObject alloc] initWithInspectorObject:[[super objectForKey:@"values"] toInspectorObject].get()];
 }
 
@@ -2386,6 +2407,9 @@ using namespace Inspector;
 
 - (RWIProtocolJSONObject *)payload
 {
+    RWIProtocolJSONObject *object = [super objectForKey:@"payload"];
+    if (!object)
+        return nil;
     return [[RWIProtocolJSONObject alloc] initWithInspectorObject:[[super objectForKey:@"payload"] toInspectorObject].get()];
 }
 
@@ -2396,6 +2420,9 @@ using namespace Inspector;
 
 - (TestProtocolDatabaseError *)error
 {
+    RWIProtocolJSONObject *object = [super objectForKey:@"error"];
+    if (!object)
+        return nil;
     return [[TestProtocolDatabaseError alloc] initWithInspectorObject:[[super objectForKey:@"error"] toInspectorObject].get()];
 }
 
index 8936929..1f22150 100644 (file)
@@ -1546,6 +1546,9 @@ using namespace Inspector;
 
 - (TestProtocolTestRecursiveObject1 *)tree
 {
+    RWIProtocolJSONObject *object = [super objectForKey:@"tree"];
+    if (!object)
+        return nil;
     return [[TestProtocolTestRecursiveObject1 alloc] initWithInspectorObject:[[super objectForKey:@"tree"] toInspectorObject].get()];
 }
 
@@ -1577,6 +1580,9 @@ using namespace Inspector;
 
 - (TestProtocolTestRecursiveObject2 *)obj
 {
+    RWIProtocolJSONObject *object = [super objectForKey:@"obj"];
+    if (!object)
+        return nil;
     return [[TestProtocolTestRecursiveObject2 alloc] initWithInspectorObject:[[super objectForKey:@"obj"] toInspectorObject].get()];
 }
 
@@ -1608,6 +1614,9 @@ using namespace Inspector;
 
 - (TestProtocolTestRecursiveObject1 *)obj
 {
+    RWIProtocolJSONObject *object = [super objectForKey:@"obj"];
+    if (!object)
+        return nil;
     return [[TestProtocolTestRecursiveObject1 alloc] initWithInspectorObject:[[super objectForKey:@"obj"] toInspectorObject].get()];
 }