OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import code | 5 import code |
6 import cpp_util | 6 import cpp_util |
7 from model import Platforms | 7 from model import Platforms |
8 from schema_util import CapitalizeFirstLetter | 8 from schema_util import CapitalizeFirstLetter |
9 from schema_util import JsFunctionNameToClassName | 9 from schema_util import JsFunctionNameToClassName |
10 | 10 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 if model_object.platforms is None: | 75 if model_object.platforms is None: |
76 return None | 76 return None |
77 ifdefs = [] | 77 ifdefs = [] |
78 for platform in model_object.platforms: | 78 for platform in model_object.platforms: |
79 if platform == Platforms.CHROMEOS: | 79 if platform == Platforms.CHROMEOS: |
80 ifdefs.append('defined(OS_CHROMEOS)') | 80 ifdefs.append('defined(OS_CHROMEOS)') |
81 else: | 81 else: |
82 raise ValueError("Unsupported platform ifdef: %s" % platform.name) | 82 raise ValueError("Unsupported platform ifdef: %s" % platform.name) |
83 return ' and '.join(ifdefs) | 83 return ' and '.join(ifdefs) |
84 | 84 |
85 def _GetNamespaceFunctions(self, namespace): | 85 def _GenerateRegisterFunctions(self, namespace_name, function): |
86 functions = list(namespace.functions.values()) | 86 c = code.Code() |
87 if namespace.compiler_options.get("generate_type_functions", False): | 87 function_ifdefs = self._GetPlatformIfdefs(function) |
88 for type_ in namespace.types.values(): | 88 if function_ifdefs is not None: |
89 functions += list(type_.functions.values()) | 89 c.Append("#if %s" % function_ifdefs, indent_level=0) |
90 return functions | 90 |
| 91 function_name = JsFunctionNameToClassName(namespace_name, function.name) |
| 92 c.Append("registry->RegisterFunction<%sFunction>();" % ( |
| 93 function_name)) |
| 94 |
| 95 if function_ifdefs is not None: |
| 96 c.Append("#endif // %s" % function_ifdefs, indent_level=0) |
| 97 return c |
91 | 98 |
92 def _GenerateFunctionRegistryRegisterAll(self): | 99 def _GenerateFunctionRegistryRegisterAll(self): |
93 c = code.Code() | 100 c = code.Code() |
94 c.Append('// static') | 101 c.Append('// static') |
95 c.Sblock('void GeneratedFunctionRegistry::RegisterAll(' | 102 c.Sblock('void GeneratedFunctionRegistry::RegisterAll(' |
96 'ExtensionFunctionRegistry* registry) {') | 103 'ExtensionFunctionRegistry* registry) {') |
97 for namespace in self._model.namespaces.values(): | 104 for namespace in self._model.namespaces.values(): |
98 namespace_ifdefs = self._GetPlatformIfdefs(namespace) | 105 namespace_ifdefs = self._GetPlatformIfdefs(namespace) |
99 if namespace_ifdefs is not None: | 106 if namespace_ifdefs is not None: |
100 c.Append("#if %s" % namespace_ifdefs, indent_level=0) | 107 c.Append("#if %s" % namespace_ifdefs, indent_level=0) |
101 | 108 |
102 namespace_name = CapitalizeFirstLetter(namespace.name.replace( | 109 namespace_name = CapitalizeFirstLetter(namespace.name.replace( |
103 "experimental.", "")) | 110 "experimental.", "")) |
104 for function in self._GetNamespaceFunctions(namespace): | 111 for function in namespace.functions.values(): |
105 if function.nocompile: | 112 if function.nocompile: |
106 continue | 113 continue |
107 function_ifdefs = self._GetPlatformIfdefs(function) | 114 c.Concat(self._GenerateRegisterFunctions(namespace.name, function)) |
108 if function_ifdefs is not None: | |
109 c.Append("#if %s" % function_ifdefs, indent_level=0) | |
110 | 115 |
111 function_name = JsFunctionNameToClassName(namespace.name, function.name) | 116 for type_ in namespace.types.values(): |
112 c.Append("registry->RegisterFunction<%sFunction>();" % ( | 117 for function in type_.functions.values(): |
113 function_name)) | 118 if function.nocompile: |
114 | 119 continue |
115 if function_ifdefs is not None: | 120 namespace_types_name = JsFunctionNameToClassName( |
116 c.Append("#endif // %s" % function_ifdefs, indent_level=0) | 121 namespace.name, type_.name) |
| 122 c.Concat(self._GenerateRegisterFunctions(namespace_types_name, |
| 123 function)) |
117 | 124 |
118 if namespace_ifdefs is not None: | 125 if namespace_ifdefs is not None: |
119 c.Append("#endif // %s" % namespace_ifdefs, indent_level=0) | 126 c.Append("#endif // %s" % namespace_ifdefs, indent_level=0) |
120 c.Eblock("}") | 127 c.Eblock("}") |
121 return c | 128 return c |
122 | 129 |
123 class _APIHGenerator(object): | 130 class _APIHGenerator(object): |
124 """Generates the header for API registration / declaration""" | 131 """Generates the header for API registration / declaration""" |
125 def __init__(self, cpp_bundle): | 132 def __init__(self, cpp_bundle): |
126 self._bundle = cpp_bundle | 133 self._bundle = cpp_bundle |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 separators=(',', ':')) | 248 separators=(',', ':')) |
242 # Escape all double-quotes and backslashes. For this to output a valid | 249 # Escape all double-quotes and backslashes. For this to output a valid |
243 # JSON C string, we need to escape \ and ". | 250 # JSON C string, we need to escape \ and ". |
244 json_content = json_content.replace('\\', '\\\\').replace('"', '\\"') | 251 json_content = json_content.replace('\\', '\\\\').replace('"', '\\"') |
245 c.Append('(*schemas)["%s"] = "%s";' % (namespace.name, json_content)) | 252 c.Append('(*schemas)["%s"] = "%s";' % (namespace.name, json_content)) |
246 c.Eblock('}') | 253 c.Eblock('}') |
247 c.Append() | 254 c.Append() |
248 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) | 255 c.Concat(cpp_util.CloseNamespace(self._bundle._cpp_namespace)) |
249 c.Append() | 256 c.Append() |
250 return c | 257 return c |
OLD | NEW |