OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python2.4 |
| 2 # |
| 3 # Copyright 2008 Google Inc. |
| 4 # |
| 5 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 # you may not use this file except in compliance with the License. |
| 7 # You may obtain a copy of the License at |
| 8 # |
| 9 # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 # |
| 11 # Unless required by applicable law or agreed to in writing, software |
| 12 # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 # See the License for the specific language governing permissions and |
| 15 # limitations under the License. |
| 16 |
| 17 """enum binding model module. |
| 18 |
| 19 This module implements the glue functions for the enum binding model, used by |
| 20 enum types. Enums behave almost exactly like ints, except range checking is |
| 21 performed when getting values from JavaScript. |
| 22 |
| 23 In C++, objects using this binding model are passed and returned by value (or |
| 24 by pointer when mutable). For example: |
| 25 void SetValue(Enum value); |
| 26 Enum GetValue(); |
| 27 |
| 28 For JS bindings, they are directly represented by variants. |
| 29 """ |
| 30 |
| 31 import string |
| 32 import cpp_utils |
| 33 import java_utils |
| 34 |
| 35 |
| 36 class InvalidEnumUsage(Exception): |
| 37 """Raised when an enum type is used incorrectly.""" |
| 38 pass |
| 39 |
| 40 |
| 41 def JavaMemberString(scope, type_defn): |
| 42 """Gets the representation of a member name in Java. |
| 43 |
| 44 Args: |
| 45 scope: a Definition for the scope in which the expression will be written. |
| 46 type_defn: a Definition for the type. |
| 47 |
| 48 Returns: |
| 49 a string representing the type |
| 50 """ |
| 51 return java_utils.GetScopedName(scope, type_defn) |
| 52 |
| 53 |
| 54 def CppTypedefString(scope, type_defn): |
| 55 """Gets the representation of a type when used in a C++ typedef. |
| 56 |
| 57 Args: |
| 58 scope: a Definition for the scope in which the expression will be written. |
| 59 type_defn: a Definition for the type. |
| 60 |
| 61 Returns: |
| 62 a (string, boolean) pair, the first element being the representation of |
| 63 the type, the second element indicating whether or not the definition of |
| 64 the type is needed for the expression to be valid. |
| 65 """ |
| 66 return cpp_utils.GetScopedName(scope, type_defn), True |
| 67 |
| 68 |
| 69 def CppMemberString(scope, type_defn): |
| 70 """Gets the representation of a type when used as a C++ class member. |
| 71 |
| 72 Args: |
| 73 scope: a Definition for the scope in which the expression will be written. |
| 74 type_defn: a Definition for the type. |
| 75 |
| 76 Returns: |
| 77 a (string, boolean) pair, the first element being the representation of |
| 78 the type, the second element indicating whether or not the definition of |
| 79 the type is needed for the expression to be valid. |
| 80 """ |
| 81 return cpp_utils.GetScopedName(scope, type_defn), True |
| 82 |
| 83 |
| 84 def CppReturnValueString(scope, type_defn): |
| 85 """Gets the representation of a type when used as a C++ function return value. |
| 86 |
| 87 Args: |
| 88 scope: a Definition for the scope in which the expression will be written. |
| 89 type_defn: a Definition for the type. |
| 90 |
| 91 Returns: |
| 92 a (string, boolean) pair, the first element being the representation of |
| 93 the type, the second element indicating whether or not the definition of |
| 94 the type is needed for the expression to be valid. |
| 95 """ |
| 96 return cpp_utils.GetScopedName(scope, type_defn), True |
| 97 |
| 98 |
| 99 def CppParameterString(scope, type_defn): |
| 100 """Gets the representation of a type when used for a function parameter. |
| 101 |
| 102 Args: |
| 103 scope: a Definition for the scope in which the expression will be written. |
| 104 type_defn: a Definition for the type. |
| 105 |
| 106 Returns: |
| 107 a (string, boolean) pair, the first element being the representation of |
| 108 the type, the second element indicating whether or not the definition of |
| 109 the type is needed for the expression to be valid. |
| 110 """ |
| 111 return cpp_utils.GetScopedName(scope, type_defn), True |
| 112 |
| 113 |
| 114 def CppMutableParameterString(scope, type_defn): |
| 115 """Gets the representation of a type for a mutable function parameter. |
| 116 |
| 117 Args: |
| 118 scope: a Definition for the scope in which the expression will be written. |
| 119 type_defn: a Definition for the type. |
| 120 |
| 121 Returns: |
| 122 a (string, boolean) pair, the first element being the string representing |
| 123 the type, the second element indicating whether or not the definition of |
| 124 the type is needed for the expression to be valid. |
| 125 """ |
| 126 return '%s*' % cpp_utils.GetScopedName(scope, type_defn), True |
| 127 |
| 128 |
| 129 def CppMutableToNonMutable(scope, type_defn, expr): |
| 130 """Gets the string converting a mutable expression to a non-mutable one. |
| 131 |
| 132 Args: |
| 133 scope: a Definition for the scope in which the expression will be written. |
| 134 type_defn: a Definition for the type. |
| 135 expr: a string for the mutable expression. |
| 136 |
| 137 Returns: |
| 138 a string, which is the non-mutable expression. |
| 139 """ |
| 140 (scope, type_defn) = (scope, type_defn) # silence gpylint. |
| 141 return '*(%s)' % expr |
| 142 |
| 143 |
| 144 def CppBaseClassString(scope, type_defn): |
| 145 """Gets the representation of a type for a base class. |
| 146 |
| 147 Args: |
| 148 scope: a Definition for the scope in which the expression will be written. |
| 149 type_defn: a Definition for the type. |
| 150 |
| 151 Returns: |
| 152 a (string, boolean) pair, the first element being the string representing |
| 153 the type, the second element indicating whether or not the definition of |
| 154 the type is needed for the expression to be valid. |
| 155 |
| 156 Raises: |
| 157 InvalidEnumUsage: always. This function can't be called for an enum type. |
| 158 """ |
| 159 raise InvalidEnumUsage |
| 160 |
| 161 |
| 162 def CppCallMethod(scope, type_defn, object_expr, mutable, method, param_exprs): |
| 163 """Gets the representation of a member function call. |
| 164 |
| 165 Args: |
| 166 scope: a Definition for the scope in which the expression will be written. |
| 167 type_defn: a Definition, representing the type of the object being called. |
| 168 object_expr: a string, which is the expression for the object being called. |
| 169 mutable: a boolean, whether or not the 'object_expr' expression is mutable |
| 170 or not |
| 171 method: a Function, representing the function to call. |
| 172 param_exprs: a list of strings, each being the expression for the value of |
| 173 each parameter. |
| 174 |
| 175 Returns: |
| 176 a string, which is the expression for the function call. |
| 177 |
| 178 Raises: |
| 179 InvalidEnumUsage: always. This function can't be called for an enum type. |
| 180 """ |
| 181 raise InvalidEnumUsage |
| 182 |
| 183 |
| 184 def CppCallStaticMethod(scope, type_defn, method, param_exprs): |
| 185 """Gets the representation of a static function call. |
| 186 |
| 187 Args: |
| 188 scope: a Definition for the scope in which the expression will be written. |
| 189 type_defn: a Definition, representing the type of the object being called. |
| 190 method: a Function, representing the function to call. |
| 191 param_exprs: a list of strings, each being the expression for the value of |
| 192 each parameter. |
| 193 |
| 194 Returns: |
| 195 a string, which is the expression for the function call. |
| 196 |
| 197 Raises: |
| 198 InvalidEnumUsage: always. This function can't be called for an enum type. |
| 199 """ |
| 200 raise InvalidEnumUsage |
| 201 |
| 202 |
| 203 def CppCallConstructor(scope, type_defn, method, param_exprs): |
| 204 """Gets the representation of a constructor call. |
| 205 |
| 206 Args: |
| 207 scope: a Definition for the scope in which the expression will be written. |
| 208 type_defn: a Definition, representing the type of the object being called. |
| 209 method: a Function, representing the constructor to call. |
| 210 param_exprs: a list of strings, each being the expression for the value of |
| 211 each parameter. |
| 212 |
| 213 Returns: |
| 214 a string, which is the expression for the constructor call. |
| 215 |
| 216 Raises: |
| 217 InvalidEnumUsage: always. This function can't be called for an enum type. |
| 218 """ |
| 219 raise InvalidEnumUsage |
| 220 |
| 221 |
| 222 def CppSetField(scope, type_defn, object_expr, field, param_expr): |
| 223 """Gets the representation of an expression setting a field in an object. |
| 224 |
| 225 Args: |
| 226 scope: a Definition for the scope in which the expression will be written. |
| 227 type_defn: a Definition, representing the type of the object containing the |
| 228 field being set. |
| 229 object_expr: a string, which is the expression for the object containing |
| 230 the field being set. |
| 231 field: a string, the name of the field to be set. |
| 232 param_expr: a strings, being the expression for the value to be set. |
| 233 |
| 234 Returns: |
| 235 a string, which is the expression for setting the field. |
| 236 |
| 237 Raises: |
| 238 InvalidEnumUsage: always. This function can't be called for an enum type. |
| 239 """ |
| 240 raise InvalidEnumUsage |
| 241 |
| 242 |
| 243 def CppGetField(scope, type_defn, object_expr, field): |
| 244 """Gets the representation of an expression getting a field in an object. |
| 245 |
| 246 Args: |
| 247 scope: a Definition for the scope in which the expression will be written. |
| 248 type_defn: a Definition, representing the type of the object containing the |
| 249 field being retrieved. |
| 250 object_expr: a string, which is the expression for the object containing |
| 251 the field being retrieved. |
| 252 field: a string, the name of the field to be retrieved. |
| 253 |
| 254 Returns: |
| 255 a string, which is the expression for getting the field. |
| 256 |
| 257 Raises: |
| 258 InvalidEnumUsage: always. This function can't be called for an enum type. |
| 259 """ |
| 260 raise InvalidEnumUsage |
| 261 |
| 262 |
| 263 def CppSetStatic(scope, type_defn, field, param_expr): |
| 264 """Gets the representation of an expression setting a static field. |
| 265 |
| 266 Args: |
| 267 scope: a Definition for the scope in which the expression will be written. |
| 268 type_defn: a Definition, representing the type of the object containing the |
| 269 field being set. |
| 270 field: a string, the name of the field to be set. |
| 271 param_expr: a strings, being the expression for the value to be set. |
| 272 |
| 273 Returns: |
| 274 a string, which is the expression for setting the field. |
| 275 |
| 276 Raises: |
| 277 InvalidEnumUsage: always. This function can't be called for an enum type. |
| 278 """ |
| 279 raise InvalidEnumUsage |
| 280 |
| 281 |
| 282 def CppGetStatic(scope, type_defn, field): |
| 283 """Gets the representation of an expression getting a static field. |
| 284 |
| 285 Args: |
| 286 scope: a Definition for the scope in which the expression will be written. |
| 287 type_defn: a Definition, representing the type of the object containing the |
| 288 field being retrieved. |
| 289 field: a string, the name of the field to be retrieved. |
| 290 |
| 291 Returns: |
| 292 a string, which is the expression for getting the field. |
| 293 |
| 294 Raises: |
| 295 InvalidEnumUsage: always. This function can't be called for an enum type. |
| 296 """ |
| 297 raise InvalidEnumUsage |
| 298 |
| 299 |
| 300 def NpapiBindingGlueHeader(scope, type_defn): |
| 301 """Gets the NPAPI glue header for a given type. |
| 302 |
| 303 Args: |
| 304 scope: a Definition for the scope in which the glue will be written. |
| 305 type_defn: a Definition, representing the type. |
| 306 |
| 307 Returns: |
| 308 a string, the glue header. |
| 309 |
| 310 Raises: |
| 311 InvalidEnumUsage: always. This function can't be called for an enum type. |
| 312 """ |
| 313 raise InvalidEnumUsage |
| 314 |
| 315 |
| 316 def NpapiBindingGlueCpp(scope, type_defn): |
| 317 """Gets the NPAPI glue implementation for a given type. |
| 318 |
| 319 Args: |
| 320 scope: a Definition for the scope in which the glue will be written. |
| 321 type_defn: a Definition, representing the type. |
| 322 |
| 323 Returns: |
| 324 a string, the glue implementation. |
| 325 |
| 326 Raises: |
| 327 InvalidEnumUsage: always. This function can't be called for an enum type. |
| 328 """ |
| 329 raise InvalidEnumUsage |
| 330 |
| 331 |
| 332 def JSDocTypeString(type_defn): |
| 333 """Gets the representation of a type in JSDoc notation. |
| 334 |
| 335 Args: |
| 336 type_defn: a Definition for the type. |
| 337 |
| 338 Returns: |
| 339 a string that is the JSDoc notation of type_defn. |
| 340 """ |
| 341 type_defn = type_defn.GetFinalType() |
| 342 type_stack = type_defn.GetParentScopeStack() |
| 343 name = type_defn.name |
| 344 return '.'.join([s.name for s in type_stack[1:]] + [name]) |
| 345 |
| 346 |
| 347 def NpapiDispatchFunctionHeader(scope, type_defn, variable, npp, success): |
| 348 """Gets a header for NPAPI glue dispatch functions. |
| 349 |
| 350 This function creates a string containing a C++ code snippet that should be |
| 351 included at the beginning of NPAPI glue dispatch functions like Invoke or |
| 352 GetProperty. This code snippet will declare and initialize certain variables |
| 353 that will be used in the dispatch functions, like the NPObject representing |
| 354 the object, or a pointer to the NPP instance. |
| 355 |
| 356 Args: |
| 357 scope: a Definition for the scope in which the glue will be written. |
| 358 type_defn: a Definition, representing the type. |
| 359 variable: a string, representing a name of a variable that can be used to |
| 360 store a reference to the object. |
| 361 npp: a string, representing the name of the variable that holds the pointer |
| 362 to the NPP instance. Will be declared by the code snippet. |
| 363 success: the name of a bool variable containing the current success status. |
| 364 (is not declared by the code snippet). |
| 365 |
| 366 Returns: |
| 367 a (string, string) pair, the first string being the code snippet, and the |
| 368 second string being an expression to access the object. |
| 369 |
| 370 Raises: |
| 371 InvalidEnumUsage: always. This function can't be called for an enum type. |
| 372 """ |
| 373 raise InvalidEnumUsage |
| 374 |
| 375 |
| 376 _enum_from_npvariant_template = string.Template(""" |
| 377 ${type} ${variable} = ${first}; |
| 378 if (NPVARIANT_IS_NUMBER(${input})) { |
| 379 ${variable} = (${type})(int)NPVARIANT_TO_NUMBER(${input}); |
| 380 if (${variable} < ${first} || ${variable} > ${last}) { |
| 381 *error_handle = "Error in " ${context} |
| 382 ": value out of range."; |
| 383 ${result} = false; |
| 384 } |
| 385 } else { |
| 386 *error_handle = "Error in " ${context} |
| 387 ": was expecting a number."; |
| 388 ${result} = false; |
| 389 }""") |
| 390 |
| 391 |
| 392 def NpapiFromNPVariant(scope, type_defn, input_expr, variable, success, |
| 393 exception_context, npp): |
| 394 """Gets the string to get a value from a NPVariant. |
| 395 |
| 396 This function creates a string containing a C++ code snippet that is used to |
| 397 retrieve a value from a NPVariant. If an error occurs, like if the NPVariant |
| 398 is not of the correct type, the snippet will set the success status variable |
| 399 to false. |
| 400 |
| 401 Args: |
| 402 scope: a Definition for the scope in which the glue will be written. |
| 403 type_defn: a Definition, representing the type of the value. |
| 404 input_expr: an expression representing the NPVariant to get the value from. |
| 405 variable: a string, representing a name of a variable that can be used to |
| 406 store a reference to the value. |
| 407 success: the name of a bool variable containing the current success status. |
| 408 exception_context: the name of a string containing context information, for |
| 409 use in exception reporting. |
| 410 npp: a string, representing the name of the variable that holds the pointer |
| 411 to the NPP instance. |
| 412 |
| 413 Returns: |
| 414 a (string, string) pair, the first string being the code snippet and the |
| 415 second one being the expression to access that value. |
| 416 |
| 417 Raises: |
| 418 InvalidEnumUsage: if the type is not an enum, or the enum doesn't have any |
| 419 values. |
| 420 """ |
| 421 npp = npp # silence gpylint. |
| 422 final_type = type_defn.GetFinalType() |
| 423 if final_type.defn_type != 'Enum': |
| 424 raise InvalidEnumUsage |
| 425 if not final_type.values: |
| 426 raise InvalidEnumUsage |
| 427 type_name = cpp_utils.GetScopedName(scope, type_defn) |
| 428 first_value = (cpp_utils.GetScopePrefix(scope, final_type) + |
| 429 final_type.values[0].name) |
| 430 last_value = (cpp_utils.GetScopePrefix(scope, final_type) + |
| 431 final_type.values[-1].name) |
| 432 text = _enum_from_npvariant_template.substitute(type=type_name, |
| 433 variable=variable, |
| 434 first=first_value, |
| 435 last=last_value, |
| 436 input=input_expr, |
| 437 result=success, |
| 438 context=exception_context) |
| 439 return text, variable |
| 440 |
| 441 |
| 442 def NpapiExprToNPVariant(scope, type_defn, variable, expression, output, |
| 443 success, npp): |
| 444 """Gets the string to store a value into a NPVariant. |
| 445 |
| 446 This function creates a string containing a C++ code snippet that is used to |
| 447 store a value into a NPVariant. That operation takes two phases, one that |
| 448 allocates necessary NPAPI resources, and that can fail, and one that actually |
| 449 sets the NPVariant (that can't fail). If an error occurs, the snippet will |
| 450 set the success status variable to false. |
| 451 |
| 452 Args: |
| 453 scope: a Definition for the scope in which the glue will be written. |
| 454 type_defn: a Definition, representing the type of the value. |
| 455 variable: a string, representing a name of a variable that can be used to |
| 456 store a reference to the value. |
| 457 expression: a string representing the expression that yields the value to |
| 458 be stored. |
| 459 output: an expression representing a pointer to the NPVariant to store the |
| 460 value into. |
| 461 success: the name of a bool variable containing the current success status. |
| 462 npp: a string, representing the name of the variable that holds the pointer |
| 463 to the NPP instance. |
| 464 |
| 465 Returns: |
| 466 a (string, string) pair, the first string being the code snippet for the |
| 467 first phase, and the second one being the code snippet for the second phase. |
| 468 """ |
| 469 (success, npp) = (success, npp) # silence gpylint. |
| 470 type_name = cpp_utils.GetScopedName(scope, type_defn) |
| 471 return ('%s %s = %s;' % (type_name, variable, expression), |
| 472 'INT32_TO_NPVARIANT(%s, *%s);' % (variable, output)) |
| 473 |
| 474 |
| 475 def main(): |
| 476 pass |
| 477 |
| 478 if __name__ == '__main__': |
| 479 main() |
OLD | NEW |