Esempio n. 1
0
    def _AddFutureifiedOperation(self, info, html_name):
        """Given a API function that uses callbacks, convert it to using Futures.

    This conversion assumes the success callback is always provided before the
    error callback (and so far in the DOM API, this is the case)."""
        callback_info = GetCallbackInfo(
            self._database.GetInterface(info.callback_args[0].type_id))

        extensions = GetDDC_Extension(self._interface, info.declared_name)
        if extensions:
            ddc_extensions = "\n".join(extensions)
        else:
            ddc_extensions = ''

        param_list = info.ParametersAsArgumentList()
        metadata = ''
        if '_RenamingAnnotation' in dir(self):
            metadata = (
                self._RenamingAnnotation(info.declared_name, html_name) +
                self._Metadata(info.type_name, info.declared_name, None))
        self._members_emitter.Emit(
            '\n'
            '  $METADATA$MODIFIERS$TYPE$FUTURE_GENERIC $NAME($PARAMS) {\n'
            '    var completer = new Completer$(FUTURE_GENERIC)();\n'
            '    $ORIGINAL_FUNCTION($PARAMS_LIST\n'
            '        $NAMED_PARAM($VARIABLE_NAME) { '
            '$DDC_EXTENSION\n'
            'completer.complete($VARIABLE_NAME); }'
            '$ERROR_CALLBACK);\n'
            '    return completer.future;\n'
            '  }\n',
            METADATA=metadata,
            MODIFIERS='static ' if info.IsStatic() else '',
            TYPE=self.SecureOutputType(info.type_name),
            NAME=html_name[1:],
            PARAMS=info.ParametersAsDeclaration(
                self._NarrowInputType if '_NarrowInputType' in
                dir(self) else self._DartType),
            PARAMS_LIST='' if param_list == '' else param_list + ',',
            NAMED_PARAM=('%s : ' % info.callback_args[0].name
                         if info.requires_named_arguments
                         and info.callback_args[0].is_optional else ''),
            VARIABLE_NAME=''
            if len(callback_info.param_infos) == 0 else 'value',
            DDC_EXTENSION=ddc_extensions,
            ERROR_CALLBACK=('' if len(info.callback_args) == 1 else (
                ',\n        %s(error) { completer.completeError(error); }' %
                ('%s : ' %
                 info.callback_args[1].name if info.requires_named_arguments
                 and info.callback_args[1].is_optional else ''))),
            FUTURE_GENERIC=(
                '' if len(callback_info.param_infos) == 0
                or not callback_info.param_infos[0].type_id else '<%s>' %
                self._DartType(callback_info.param_infos[0].type_id)),
            ORIGINAL_FUNCTION=html_name)
Esempio n. 2
0
    def _AddFutureifiedOperation(self, info, html_name):
        """Given a API function that uses callbacks, convert it to using Futures.

    This conversion assumes the success callback is always provided before the
    error callback (and so far in the DOM API, this is the case)."""
        callback_info = GetCallbackInfo(
            self._database.GetInterface(info.callback_args[0].type_id))

        # Generated private members never have named arguments.
        ignore_named_parameters = True if html_name.startswith('_') else False

        # If more than one callback then the second argument is the error callback.
        # Some error callbacks have 2 args (e.g., executeSql) where the second arg
        # is the error - this is the argument we want.
        error_callback = ""
        if len(info.callback_args) > 1:
            error_callback_info = GetCallbackInfo(
                self._database.GetInterface(info.callback_args[1].type_id))
            error_callbackNames = []
            for paramInfo in error_callback_info.param_infos:
                error_callbackNames.append(paramInfo.name)
            errorCallbackVariables = ", ".join(error_callbackNames)
            errorName = error_callback_info.param_infos[-1].name
            error_callback = (
                ',\n        %s(%s) { completer.completeError(%s); }' %
                (('%s : ' %
                  info.callback_args[1].name if info.requires_named_arguments
                  and info.callback_args[1].is_optional
                  and not (ignore_named_parameters) else ''),
                 errorCallbackVariables, errorName))

        extensions = GetDDC_Extension(self._interface, info.declared_name)
        if extensions:
            ddc_extensions = "\n".join(extensions)
        else:
            ddc_extensions = ''

        # Some callbacks have more than one parameters.  If so use all of
        # those parameters.  However, if more than one argument use the
        # type of the last argument to be returned e.g., executeSql the callback
        # is (transaction, resultSet) and only the resultSet is returned SqlResultSet.
        callbackArgsLen = len(callback_info.param_infos)
        future_generic = ''
        callbackVariables = ''
        completerVariable = ''
        if callbackArgsLen == 1:
            callbackVariables = 'value'
            completerVariable = callbackVariables
            if callback_info.param_infos[0].type_id:
                future_generic = '<%s>' % self._DartType(
                    callback_info.param_infos[0].type_id)
        elif callbackArgsLen > 1:
            callbackNames = []
            for paramInfo in callback_info.param_infos:
                callbackNames.append(paramInfo.name)
            callbackVariables = ",".join(callbackNames)
            completerVariable = callbackNames[-1]
            future_generic = '<%s>' % self._DartType(
                callback_info.param_infos[-1].type_id)

        param_list = info.ParametersAsArgumentList(None,
                                                   ignore_named_parameters)
        dictionary_argument = info.dictionaryArgumentName()

        convert_map = ''
        if dictionary_argument is not None:
            mapArg = dictionary_argument[0]
            tempVariable = '%s_dict' % mapArg
            mapArgOptional = dictionary_argument[1]

            if not (extensions):
                if not (param_list.endswith(', mapArg')
                        or param_list.endswith(', options')
                        or param_list == mapArg):
                    print "ERROR: %s.%s - Last parameter or only parameter %s is not of type Map" % (
                        self._interface.id, html_name, mapArg)
                param_list = '%s_dict' % param_list

                if mapArgOptional:
                    convert_map = '    var %s = null;\n'\
                                  '    if (%s != null) {\n'\
                                  '      %s = convertDartToNative_Dictionary(%s);\n'\
                                  '    }\n' % (tempVariable, mapArg, tempVariable, mapArg)
                else:
                    convert_map = '    var %s = convertDartToNative_Dictionary(%s);\n' % (
                        tempVariable, mapArg)

        metadata = ''
        if '_RenamingAnnotation' in dir(self):
            metadata = (
                self._RenamingAnnotation(info.declared_name, html_name) +
                self._Metadata(info.type_name, info.declared_name, None,
                               info.type_nullable))
        self._members_emitter.Emit(
            '\n'
            '  $METADATA$MODIFIERS$TYPE$FUTURE_GENERIC $NAME($PARAMS) {\n'
            '    $CONVERT_DICTIONARY'
            '    var completer = new Completer$(FUTURE_GENERIC)();\n'
            '    $ORIGINAL_FUNCTION($PARAMS_LIST\n'
            '        $NAMED_PARAM($VARIABLE_NAME) { '
            '$DDC_EXTENSION\n'
            'completer.complete($COMPLETER_NAME); }'
            '$ERROR_CALLBACK);\n'
            '    return completer.future;\n'
            '  }\n',
            METADATA=metadata,
            MODIFIERS='static ' if info.IsStatic() else '',
            TYPE=self.SecureOutputType(info.type_name,
                                       nullable=info.type_nullable),
            NAME=html_name[1:],
            PARAMS=info.ParametersAsDeclaration(
                self._NarrowInputType if '_NarrowInputType' in
                dir(self) else self._DartType),
            CONVERT_DICTIONARY=convert_map,
            PARAMS_LIST='' if param_list == '' else param_list + ',',
            NAMED_PARAM=('%s : ' % info.callback_args[0].name
                         if info.requires_named_arguments
                         and info.callback_args[0].is_optional
                         and not (ignore_named_parameters) else ''),
            VARIABLE_NAME=callbackVariables,
            COMPLETER_NAME=completerVariable,
            DDC_EXTENSION=ddc_extensions,
            ERROR_CALLBACK=error_callback,
            FUTURE_GENERIC=future_generic,
            ORIGINAL_FUNCTION=html_name)
Esempio n. 3
0
  def _AddFutureifiedOperation(self, info, html_name):
    """Given a API function that uses callbacks, convert it to using Futures.

    This conversion assumes the success callback is always provided before the
    error callback (and so far in the DOM API, this is the case)."""
    callback_info = GetCallbackInfo(
        self._database.GetInterface(info.callback_args[0].type_id))

    # If more than one callback then the second argument is the error callback.
    # Some error callbacks have 2 args (e.g., executeSql) where the second arg
    # is the error - this is the argument we want.
    error_callback = ""
    if len(info.callback_args) > 1:
      error_callback_info = GetCallbackInfo(
            self._database.GetInterface(info.callback_args[1].type_id))
      error_callbackNames = []
      for paramInfo in error_callback_info.param_infos:
          error_callbackNames.append(paramInfo.name)
      errorCallbackVariables = ", ".join(error_callbackNames)
      errorName = error_callback_info.param_infos[-1].name
      error_callback = (',\n        %s(%s) { completer.completeError(%s); }' %
                        (('%s : ' % info.callback_args[1].name
                          if info.requires_named_arguments and
                             info.callback_args[1].is_optional else ''),
                         errorCallbackVariables, errorName))

    extensions = GetDDC_Extension(self._interface, info.declared_name)
    if extensions:
      ddc_extensions = "\n".join(extensions);
    else:
      ddc_extensions = ''

    # Some callbacks have more than one parameters.  If so use all of
    # those parameters.  However, if more than one argument use the
    # type of the last argument to be returned e.g., executeSql the callback
    # is (transaction, resultSet) and only the resultSet is returned SqlResultSet.
    callbackArgsLen = len(callback_info.param_infos)
    future_generic = ''
    callbackVariables = ''
    completerVariable = ''
    if callbackArgsLen == 1:
      callbackVariables = 'value'
      completerVariable = callbackVariables
      if callback_info.param_infos[0].type_id:
        future_generic = '<%s>' % self._DartType(callback_info.param_infos[0].type_id)
    elif callbackArgsLen > 1:
      callbackNames = []
      for paramInfo in callback_info.param_infos:
        callbackNames.append(paramInfo.name)
      callbackVariables = ",".join(callbackNames)
      completerVariable = callbackNames[-1]
      future_generic = '<%s>' % self._DartType(callback_info.param_infos[-1].type_id)

    param_list = info.ParametersAsArgumentList()
    metadata = ''
    if '_RenamingAnnotation' in dir(self):
      metadata = (self._RenamingAnnotation(info.declared_name, html_name) +
          self._Metadata(info.type_name, info.declared_name, None))
    self._members_emitter.Emit(
        '\n'
        '  $METADATA$MODIFIERS$TYPE$FUTURE_GENERIC $NAME($PARAMS) {\n'
        '    var completer = new Completer$(FUTURE_GENERIC)();\n'
        '    $ORIGINAL_FUNCTION($PARAMS_LIST\n'
        '        $NAMED_PARAM($VARIABLE_NAME) { '
        '$DDC_EXTENSION\n'
        'completer.complete($COMPLETER_NAME); }'
        '$ERROR_CALLBACK);\n'
        '    return completer.future;\n'
        '  }\n',
        METADATA=metadata,
        MODIFIERS='static ' if info.IsStatic() else '',
        TYPE=self.SecureOutputType(info.type_name),
        NAME=html_name[1:],
        PARAMS=info.ParametersAsDeclaration(self._NarrowInputType
            if '_NarrowInputType' in dir(self) else self._DartType),
        PARAMS_LIST='' if param_list == '' else param_list + ',',
        NAMED_PARAM=('%s : ' % info.callback_args[0].name
            if info.requires_named_arguments and
              info.callback_args[0].is_optional else ''),
        VARIABLE_NAME=callbackVariables,
        COMPLETER_NAME=completerVariable,
        DDC_EXTENSION=ddc_extensions,
        ERROR_CALLBACK=error_callback,
        FUTURE_GENERIC=future_generic,
        ORIGINAL_FUNCTION=html_name)