def CppBaseClassString(scope, type_defn):
    """Gets the representation of a type for a base class.

  Args:
    scope: a Definition for the scope in which the expression will be written.
    type_defn: a Definition for the type.

  Returns:
    a (string, boolean) pair, the first element being the string representing
    the type, the second element indicating whether or not the definition of
    the type is needed for the expression to be valid.
  """
    return cpp_utils.GetScopedName(scope, type_defn)
def CppParameterString(scope, type_defn):
    """Gets the representation of a type when used for a function parameter.

  Args:
    scope: a Definition for the scope in which the expression will be written.
    type_defn: a Definition for the type.

  Returns:
    a (string, boolean) pair, the first element being the representation of
    the type, the second element indicating whether or not the definition of
    the type is needed for the expression to be valid.
  """
    return '%s*' % cpp_utils.GetScopedName(scope, type_defn), False
def PpapiBindingGlueHeader(scope, type_defn):
    """Gets the PPAPI glue header for a given type.

  Args:
    scope: a Definition for the scope in which the glue will be written.
    type_defn: a Definition, representing the type.

  Returns:
    a string, the glue header.
  """
    class_name = cpp_utils.GetScopedName(scope, type_defn)
    return (_ppapi_binding_glue_header_template.substitute(Class=class_name),
            '', 'pp::deprecated::ScriptableObject')
Example #4
0
def CppParameterString(scope, type_defn):
    """Gets the representation of a type when used for a function parameter.

  Args:
    scope: a Definition for the scope in which the expression will be written.
    type_defn: a Definition for the type.

  Returns:
    a (string, boolean) pair, the first element being the representation of
    the type, the second element indicating whether or not the definition of
    the type is needed for the expression to be valid.

  Raises:
    BadVoidUsage: type_defn is a 'void' POD type.
  """
    final_type = type_defn.GetFinalType()
    if final_type.podtype == 'void':
        raise BadVoidUsage
    elif final_type.podtype == 'string' or final_type.podtype == 'wstring':
        return 'const %s&' % cpp_utils.GetScopedName(scope, type_defn), True
    else:
        return cpp_utils.GetScopedName(scope, type_defn), True
Example #5
0
def PpapiExprToPPVar(scope, type_defn, variable, expression, output, success,
                     npp):
    """Gets the string to store a value into a pp::Var.

  This function creates a string containing a C++ code snippet that is used to
  store a value into a pp::Var. That operation takes two phases, one that
  allocates necessary PPAPI resources, and that can fail, and one that actually
  sets the pp::Var (that can't fail). If an error occurs, the snippet will
  set the success status variable to false.

  Args:
    scope: a Definition for the scope in which the glue will be written.
    type_defn: a Definition, representing the type of the value.
    variable: a string, representing a name of a variable that can be used to
      store a reference to the value.
    expression: a string representing the expression that yields the value to
      be stored.
    output: an expression representing a pointer to the pp::Var to store the
      value into.
    success: the name of a bool variable containing the current success status.
    npp: a string, representing the name of the variable that holds the pointer
      to the pp::Instance.

  Returns:
    a (string, string) pair, the first string being the code snippet for the
    first phase, and the second one being the code snippet for the second phase.

  Raises:
    UnknownPODType: type_defn is not a known POD type.
  """
    (npp, success) = (npp, success)  # silence gpylint.
    type_name = cpp_utils.GetScopedName(scope, type_defn)
    final_type = type_defn.GetFinalType()
    if final_type.podtype == 'void':
        return ('%s;' % expression, '*%s = pp::Var();' % output)
    elif final_type.podtype == 'int':
        return ('%s %s = %s;' % (type_name, variable, expression),
                '*%s = pp::Var((int32_t)%s);' % (output, variable))
    elif final_type.podtype == 'bool':
        return ('%s %s = %s;' % (type_name, variable, expression),
                '*%s = pp::Var(%s);' % (output, variable))
    elif final_type.podtype == 'float':
        return ('%s %s = %s;' % (type_name, variable, expression),
                '*%s = pp::Var(static_cast<double>(%s));' % (output, variable))
    elif final_type.podtype == 'variant':
        raise UnimplementedPODType
    elif final_type.podtype == 'string':
        return ('*%s = pp::Var(%s);' % (output, expression), '')
    else:
        raise UnknownPODType(final_type.podtype)
def CppGetStatic(scope, namespace, field):
    """Gets the representation of an expression getting a static field.

  Args:
    scope: a Definition for the scope in which the expression will be written.
    namespace: a Definition, representing the namespace containing the field
      being retrieved.
    field: a string, the name of the field to be retrieved.

  Returns:
    a string, which is the expression for getting the field.
  """
    namespace = namespace  # silence gpylint.
    return cpp_utils.GetScopedName(scope, field)
Example #7
0
def CppGetStatic(scope, type_defn, field):
    """Gets the representation of an expression getting a static field.

  Args:
    scope: a Definition for the scope in which the expression will be written.
    type_defn: a Definition, representing the type of the object containing the
      field being retrieved.
    field: a string, the name of the field to be retrieved.

  Returns:
    a string, which is the expression for getting the field.
  """
    return '%s::%s()' % (cpp_utils.GetScopedName(
        scope, type_defn), cpp_utils.GetGetterName(field))
Example #8
0
def NpapiFromNPVariant(scope, type_defn, input_expr, variable, success,
                       exception_context, npp):
    """Gets the string to get a value from a NPVariant.

  This function creates a string containing a C++ code snippet that is used to
  retrieve a value from a NPVariant. If an error occurs, like if the NPVariant
  is not of the correct type, the snippet will set the success status variable
  to false.

  Args:
    scope: a Definition for the scope in which the glue will be written.
    type_defn: a Definition, representing the type of the value.
    input_expr: an expression representing the NPVariant to get the value from.
    variable: a string, representing a name of a variable that can be used to
      store a reference to the value.
    success: the name of a bool variable containing the current success status.
    exception_context: the name of a string containing context information, for
      use in exception reporting.
    npp: a string, representing the name of the variable that holds the pointer
      to the NPP instance.

  Returns:
    a (string, string) pair, the first string being the code snippet and the
    second one being the expression to access that value.
  """
    npp = npp  # silence gpylint.
    class_name = cpp_utils.GetScopedName(scope, type_defn)
    glue_namespace = npapi_utils.GetGlueFullNamespace(type_defn)
    marshaling_attributes = GetMarshalingAttributes(type_defn)

    # If the value type has marshaling getter then the C++ type is never exposed
    # to JavaScript and we don't need to check. Otherwise, assume that we might
    # see a wrapped C++ type or an alternative JavaScript representation.
    if 'getter' in marshaling_attributes:
        template = _from_npvariant_template_marshaled
    else:
        if 'setter' in marshaling_attributes:
            template = _from_npvariant_template_dual_interface
        else:
            template = _from_npvariant_template

    text = template.substitute(npp=npp,
                               Class=class_name,
                               ClassGlueNS=glue_namespace,
                               variable=variable,
                               input=input_expr,
                               success=success,
                               context=exception_context)
    return (text, variable)
Example #9
0
def CppSetStatic(scope, type_defn, field, param_expr):
    """Gets the representation of an expression setting a static field.

  Args:
    scope: a Definition for the scope in which the expression will be written.
    type_defn: a Definition, representing the type of the object containing the
      field being set.
    field: a string, the name of the field to be set.
    param_expr: a strings, being the expression for the value to be set.

  Returns:
    a string, which is the expression for setting the field.
  """
    return '%s::%s(%s)' % (cpp_utils.GetScopedName(
        scope, type_defn), cpp_utils.GetSetterName(field), param_expr)
def CppSetStatic(scope, namespace, field, param_expr):
    """Gets the representation of an expression setting a static field.

  Args:
    scope: a Definition for the scope in which the expression will be written.
    namespace: a Definition, representing the namespace containing the field
      being set.
    field: a string, the name of the field to be set.
    param_expr: a strings, being the expression for the value to be set.

  Returns:
    a string, which is the expression for setting the field.
  """
    namespace = namespace  # silence gpylint.
    return '%s = %s' % (cpp_utils.GetScopedName(scope, field), param_expr)
Example #11
0
def CppCallStaticMethod(scope, type_defn, method, param_exprs):
    """Gets the representation of a static function call.

  Args:
    scope: a Definition for the scope in which the expression will be written.
    type_defn: a Definition, representing the type of the object being called.
    method: a Function, representing the function to call.
    param_exprs: a list of strings, each being the expression for the value of
      each parameter.

  Returns:
    a string, which is the expression for the function call.
  """
    return '%s::%s(%s)' % (cpp_utils.GetScopedName(
        scope, type_defn), method.name, ', '.join(param_exprs))
def NpapiFromNPVariant(scope, type_defn, input_expr, variable, success,
                       exception_context, npp):
    """Gets the string to get a value from a NPVariant.

  This function creates a string containing a C++ code snippet that is used to
  retrieve a value from a NPVariant. If an error occurs, like if the NPVariant
  is not of the correct type, the snippet will set the success status variable
  to false.

  Args:
    scope: a Definition for the scope in which the glue will be written.
    type_defn: a Definition, representing the type of the value.
    input_expr: an expression representing the NPVariant to get the value from.
    variable: a string, representing a name of a variable that can be used to
      store a reference to the value.
    success: the name of a bool variable containing the current success status.
    exception_context: the name of a string containing context information, for
      use in exception reporting.
    npp: a string, representing the name of the variable that holds the pointer
      to the NPP instance.

  Returns:
    a (string, string) pair, the first string being the code snippet and the
    second one being the expression to access that value.

  Raises:
    InvalidEnumUsage: if the type is not an enum, or the enum doesn't have any
      values.
  """
    npp = npp  # silence gpylint.
    final_type = type_defn.GetFinalType()
    if final_type.defn_type != 'Enum':
        raise InvalidEnumUsage
    if not final_type.values:
        raise InvalidEnumUsage
    type_name = cpp_utils.GetScopedName(scope, type_defn)
    first_value = (cpp_utils.GetScopePrefix(scope, final_type) +
                   final_type.values[0].name)
    last_value = (cpp_utils.GetScopePrefix(scope, final_type) +
                  final_type.values[-1].name)
    text = _enum_from_npvariant_template.substitute(type=type_name,
                                                    variable=variable,
                                                    first=first_value,
                                                    last=last_value,
                                                    input=input_expr,
                                                    result=success,
                                                    context=exception_context)
    return text, variable
Example #13
0
def CppCallConstructor(scope, type_defn, method, param_exprs):
    """Gets the representation of a constructor call.

  Args:
    scope: a Definition for the scope in which the expression will be written.
    type_defn: a Definition, representing the type of the object being called.
    method: a Function, representing the constructor to call.
    param_exprs: a list of strings, each being the expression for the value of
      each parameter.

  Returns:
    a string, which is the expression for the constructor call.
  """
    method = method  # silence gpylint.
    return 'new %s(%s)' % (cpp_utils.GetScopedName(
        scope, type_defn), ', '.join(param_exprs))
Example #14
0
def PpapiBindingGlueHeader(scope, type_defn):
    """Gets the PPAPI glue header for a given type.

  Args:
    scope: a Definition for the scope in which the glue will be written.
    type_defn: a Definition, representing the type.

  Returns:
    a string, the glue header.
  """
    glue_class = type_defn.name + '_glue'
    base_class = cpp_utils.GetScopedName(scope, type_defn)
    run_function, unused_check = cpp_utils.GetFunctionPrototype(
        scope, _MakeRunFunction(scope, type_defn), '')
    return _ppapi_binding_glue_header_template.substitute(
        GlueClass=glue_class, BaseClass=base_class, RunFunction=run_function)
def CppCallStaticMethod(scope, namespace, method, param_exprs):
    """Gets the representation of a static function call.

  Args:
    scope: a Definition for the scope in which the expression will be written.
    namespace: a Definition, representing the namespace containing the function
      being called.
    method: a Function, representing the function to call.
    param_exprs: a list of strings, each being the expression for the value of
      each parameter.

  Returns:
    a string, which is the expression for the function call.
  """
    namespace = namespace  # silence gpylint.
    return '%s(%s)' % (cpp_utils.GetScopedName(scope,
                                               method), ', '.join(param_exprs))
Example #16
0
def CppTypedefString(scope, type_defn):
    """Gets the representation of a type when used in a C++ typedef.

  Args:
    scope: a Definition for the scope in which the expression will be written.
    type_defn: a Definition for the type.

  Returns:
    a (string, boolean) pair, the first element being the representation of
    the type, the second element indicating whether or not the definition of
    the type is needed for the expression to be valid.

  Raises:
    BadVoidUsage: type_defn is a 'void' POD type.
  """
    if type_defn.GetFinalType().podtype == 'void':
        raise BadVoidUsage
    return cpp_utils.GetScopedName(scope, type_defn), True
Example #17
0
def PpapiExprToPPVar(scope, type_defn, variable, expression, output, success,
                     npp):
    """Gets the string to store a value into a pp::Var.

  This function creates a string containing a C++ code snippet that is used to
  store a value into a pp::Var. That operation takes two phases, one that
  allocates necessary PPAPI resources, and that can fail, and one that actually
  sets the pp::Var (that can't fail). If an error occurs, the snippet will
  set the success status variable to false.

  Args:
    scope: a Definition for the scope in which the glue will be written.
    type_defn: a Definition, representing the type of the value.
    variable: a string, representing a name of a variable that can be used to
      store a reference to the value.
    expression: a string representing the expression that yields the value to
      be stored.
    output: an expression representing a pointer to the pp::Var to store the
      value into.
    success: the name of a bool variable containing the current success status.
    npp: a string, representing the name of the variable that holds the pointer
      to the pp::Instance.

  Returns:
    a (string, string) pair, the first string being the code snippet for the
    first phase, and the second one being the code snippet for the second phase.
  """
    class_name = cpp_utils.GetScopedName(scope, type_defn)
    glue_namespace = npapi_utils.GetGlueFullNamespace(type_defn)
    marshaling_attributes = GetMarshalingAttributes(type_defn)
    if 'getter' in marshaling_attributes:
        template = _ppapi_expr_to_ppvar_template_marshaled
    else:
        template = _ppapi_expr_to_ppvar_template
    text = template.substitute(Class=class_name,
                               ClassGlueNS=glue_namespace,
                               variable=variable,
                               output=output,
                               npp=npp,
                               expr=expression,
                               success=success)
    return (text, '')
Example #18
0
def PpapiFromPPVar(scope, type_defn, input_expr, variable, success,
                   exception_context, npp):
    """Gets the string to get a value from a pp::Var.

  This function creates a string containing a C++ code snippet that is used to
  retrieve a value from a pp::Var. If an error occurs, like if the pp::Var
  is not of the correct type, the snippet will set the success status variable
  to false.

  Args:
    scope: a Definition for the scope in which the glue will be written.
    type_defn: a Definition, representing the type of the value.
    input_expr: an expression representing the pp::Var to get the value from.
    variable: a string, representing a name of a variable that can be used to
      store a reference to the value.
    success: the name of a bool variable containing the current success status.
    exception_context: the name of a string containing context information, for
      use in exception reporting.
    npp: a string, representing the name of the variable that holds the pointer
      to the pp::Instance.

  Returns:
    a (string, string) pair, the first string being the code snippet and the
    second one being the expression to access that value.
  """
    glue_class = type_defn.name + '_glue'
    type_name = cpp_utils.GetScopedName(scope, type_defn)
    callback_namespace = npapi_utils.GetGlueFullNamespace(type_defn)
    text = _ppapi_from_ppvar_template.substitute(success=success,
                                                 context=exception_context,
                                                 input_expr=input_expr,
                                                 type_name=type_name,
                                                 type_defn=type_defn,
                                                 variable=variable,
                                                 namespace=callback_namespace,
                                                 GlueClass=glue_class,
                                                 npp=npp)
    return text, variable
Example #19
0
def NpapiExprToNPVariant(scope, type_defn, variable, expression, output,
                         success, npp):
    """Gets the string to store a value into a NPVariant.

  This function creates a string containing a C++ code snippet that is used to
  store a value into a NPVariant. That operation takes two phases, one that
  allocates necessary NPAPI resources, and that can fail, and one that actually
  sets the NPVariant (that can't fail). If an error occurs, the snippet will
  set the success status variable to false.

  Args:
    scope: a Definition for the scope in which the glue will be written.
    type_defn: a Definition, representing the type of the value.
    variable: a string, representing a name of a variable that can be used to
      store a reference to the value.
    expression: a string representing the expression that yields the value to
      be stored.
    output: an expression representing a pointer to the NPVariant to store the
      value into.
    success: the name of a bool variable containing the current success status.
    npp: a string, representing the name of the variable that holds the pointer
      to the NPP instance.

  Returns:
    a (string, string) pair, the first string being the code snippet for the
    first phase, and the second one being the code snippet for the second phase.
  """
    class_name = cpp_utils.GetScopedName(scope, type_defn)
    glue_namespace = npapi_utils.GetGlueFullNamespace(type_defn)
    text = _expr_to_npvariant_template.substitute(Class=class_name,
                                                  ClassGlueNS=glue_namespace,
                                                  variable=variable,
                                                  npp=npp,
                                                  expr=expression,
                                                  success=success)
    return (text, 'OBJECT_TO_NPVARIANT(%s, *%s);' % (variable, output))
Example #20
0
def NpapiFromNPVariant(scope, type_defn, input_expr, variable, success,
                       exception_context, npp):
    """Gets the string to get a value from a NPVariant.

  This function creates a string containing a C++ code snippet that is used to
  retrieve a value from a NPVariant. If an error occurs, like if the NPVariant
  is not of the correct type, the snippet will set the success status variable
  to false.

  Args:
    scope: a Definition for the scope in which the glue will be written.
    type_defn: a Definition, representing the type of the value.
    input_expr: an expression representing the NPVariant to get the value from.
    variable: a string, representing a name of a variable that can be used to
      store a reference to the value.
    success: the name of a bool variable containing the current success status.
    exception_context: the name of a string containing context information, for
      use in exception reporting.
    npp: a string, representing the name of the variable that holds the pointer
      to the NPP instance.

  Returns:
    a (string, string) pair, the first string being the code snippet and the
    second one being the expression to access that value.

  Raises:
    BadVoidUsage: type_defn is a 'void' POD type.
    UnknownPODType: type_defn is not a known POD type.
  """
    npp = npp  # silence gpylint.
    type_name = cpp_utils.GetScopedName(scope, type_defn)
    final_type = type_defn.GetFinalType()
    if final_type.podtype == 'void':
        return '', 'void(0)'
    elif final_type.podtype == 'int':
        text = _int_from_npvariant_template.substitute(
            type=type_name,
            input=input_expr,
            variable=variable,
            success=success,
            context=exception_context)
        return text, variable
    elif final_type.podtype == 'bool':
        text = _bool_from_npvariant_template.substitute(
            type=type_name,
            input=input_expr,
            variable=variable,
            success=success,
            context=exception_context)
        return text, variable
    elif final_type.podtype == 'float':
        text = _float_from_npvariant_template.substitute(
            type=type_name,
            input=input_expr,
            variable=variable,
            success=success,
            context=exception_context)
        return text, variable
    elif final_type.podtype == 'variant':
        return '%s %s(npp, %s);' % (type_name, variable, input_expr), variable
    elif final_type.podtype == 'string':
        text = _string_from_npvariant_template.substitute(
            type=type_name,
            input=input_expr,
            variable=variable,
            success=success,
            context=exception_context)
        return text, variable
    elif final_type.podtype == 'wstring':
        text = _wstring_from_npvariant_template.substitute(
            type=type_name,
            input=input_expr,
            variable=variable,
            success=success,
            context=exception_context)
        return text, variable
    else:
        raise UnknownPODType(final_type.podtype)