def GetFieldType(kind, field=None): if mojom.IsAnyArrayKind(kind): arguments = [] if kind.kind in _kind_to_typecode_for_native_array: arguments.append('%r' % _kind_to_typecode_for_native_array[kind.kind]) elif kind.kind != mojom.BOOL: arguments.append(GetFieldType(kind.kind)) if mojom.IsNullableKind(kind): arguments.append('nullable=True') if mojom.IsFixedArrayKind(kind): arguments.append('length=%d' % kind.length) array_type = 'GenericArrayType' if kind.kind == mojom.BOOL: array_type = 'BooleanArrayType' elif kind.kind in _kind_to_typecode_for_native_array: array_type = 'NativeArrayType' return '_descriptor.%s(%s)' % (array_type, ', '.join(arguments)) if mojom.IsStructKind(kind): arguments = [GetStructClass(kind)] if mojom.IsNullableKind(kind): arguments.append('nullable=True') return '_descriptor.StructType(%s)' % ', '.join(arguments) if mojom.IsEnumKind(kind): return GetFieldType(mojom.INT32) return _kind_to_type.get(kind, '_descriptor.TYPE_NONE')
def CodecType(kind): if kind in mojom.PRIMITIVES: return _kind_to_codec_type[kind] if mojom.IsStructKind(kind): pointer_type = "NullablePointerTo" if mojom.IsNullableKind(kind) \ else "PointerTo" return "new codec.%s(%s)" % (pointer_type, JavaScriptType(kind)) if mojom.IsUnionKind(kind): return JavaScriptType(kind) if mojom.IsArrayKind(kind): array_type = "NullableArrayOf" if mojom.IsNullableKind( kind) else "ArrayOf" array_length = "" if kind.length is None else ", %d" % kind.length element_type = ElementCodecType(kind.kind) return "new codec.%s(%s%s)" % (array_type, element_type, array_length) if mojom.IsInterfaceKind(kind): return "codec.%s" % ("NullableInterface" if mojom.IsNullableKind(kind) else "Interface") if mojom.IsInterfaceRequestKind(kind): return CodecType(mojom.MSGPIPE) if mojom.IsAssociatedInterfaceKind(kind): return "codec.AssociatedInterfaceNotSupported" if mojom.IsAssociatedInterfaceRequestKind(kind): return "codec.AssociatedInterfaceRequestNotSupported" if mojom.IsEnumKind(kind): return "new codec.Enum(%s)" % JavaScriptType(kind) if mojom.IsMapKind(kind): map_type = "NullableMapOf" if mojom.IsNullableKind(kind) else "MapOf" key_type = ElementCodecType(kind.key_kind) value_type = ElementCodecType(kind.value_kind) return "new codec.%s(%s, %s)" % (map_type, key_type, value_type) raise Exception("No codec type for %s" % kind)
def GetArrayValidateParamsCtorArgs(kind): if mojom.IsStringKind(kind): expected_num_elements = 0 element_is_nullable = False element_validate_params = "nullptr" enum_validate_func = "nullptr" elif mojom.IsMapKind(kind): expected_num_elements = 0 element_is_nullable = mojom.IsNullableKind(kind.value_kind) element_validate_params = GetNewArrayValidateParams(kind.value_kind) enum_validate_func = "nullptr" else: expected_num_elements = generator.ExpectedArraySize(kind) or 0 element_is_nullable = mojom.IsNullableKind(kind.kind) element_validate_params = GetNewArrayValidateParams(kind.kind) if mojom.IsEnumKind(kind.kind): enum_validate_func = ( "%s::Validate" % GetQualifiedNameForKind(kind.kind, internal=True)) else: enum_validate_func = "nullptr" if enum_validate_func == "nullptr": return "%d, %s, %s" % (expected_num_elements, "true" if element_is_nullable else "false", element_validate_params) else: return "%d, %s" % (expected_num_elements, enum_validate_func)
def GetCppWrapperType(kind): if IsTypemappedKind(kind): return GetNativeTypeName(kind) if mojom.IsEnumKind(kind): return GetNameForKind(kind) if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind): return "%sPtr" % GetNameForKind(kind) if mojom.IsArrayKind(kind): pattern = None if _use_new_wrapper_types: if mojom.IsNullableKind(kind): pattern = ("WTF::Optional<WTF::Vector<%s>>" if _for_blink else "base::Optional<std::vector<%s>>") else: pattern = "WTF::Vector<%s>" if _for_blink else "std::vector<%s>" else: pattern = "mojo::WTFArray<%s>" if _for_blink else "mojo::Array<%s>" return pattern % GetCppWrapperType(kind.kind) if mojom.IsMapKind(kind): pattern = None if _use_new_wrapper_types: if mojom.IsNullableKind(kind): pattern = ("WTF::Optional<WTF::HashMap<%s, %s>>" if _for_blink else "base::Optional<std::unordered_map<%s, %s>>") else: pattern = ("WTF::HashMap<%s, %s>" if _for_blink else "std::unordered_map<%s, %s>") else: pattern = "mojo::WTFMap<%s, %s>" if _for_blink else "mojo::Map<%s, %s>" return pattern % (GetCppWrapperType( kind.key_kind), GetCppWrapperType(kind.value_kind)) if mojom.IsInterfaceKind(kind): return "%sPtr" % GetNameForKind(kind) if mojom.IsInterfaceRequestKind(kind): return "%sRequest" % GetNameForKind(kind.kind) if mojom.IsAssociatedInterfaceKind(kind): return "%sAssociatedPtrInfo" % GetNameForKind(kind.kind) if mojom.IsAssociatedInterfaceRequestKind(kind): return "%sAssociatedRequest" % GetNameForKind(kind.kind) if mojom.IsStringKind(kind): if _for_blink: return "WTF::String" if not _use_new_wrapper_types: return "mojo::String" return ("base::Optional<std::string>" if mojom.IsNullableKind(kind) else "std::string") if mojom.IsGenericHandleKind(kind): return "mojo::ScopedHandle" if mojom.IsDataPipeConsumerKind(kind): return "mojo::ScopedDataPipeConsumerHandle" if mojom.IsDataPipeProducerKind(kind): return "mojo::ScopedDataPipeProducerHandle" if mojom.IsMessagePipeKind(kind): return "mojo::ScopedMessagePipeHandle" if mojom.IsSharedBufferKind(kind): return "mojo::ScopedSharedBufferHandle" if not kind in _kind_to_cpp_type: raise Exception("Unrecognized kind %s" % kind.spec) return _kind_to_cpp_type[kind]
def get_spec(kind): if self._IsPrimitiveKind(kind): return _kind_to_lite_js_type[kind] if mojom.IsArrayKind(kind): return "mojo.internal.Array(%s, %s)" % (get_spec( kind.kind), "true" if mojom.IsNullableKind(kind.kind) else "false") if mojom.IsMapKind(kind): return "mojo.internal.Map(%s, %s, %s)" % ( get_spec(kind.key_kind), get_spec(kind.value_kind), "true" if mojom.IsNullableKind(kind.value_kind) else "false") if (mojom.IsAssociatedKind(kind) or mojom.IsInterfaceRequestKind(kind) or mojom.IsPendingRemoteKind(kind) or mojom.IsPendingReceiverKind(kind) or mojom.IsPendingAssociatedRemoteKind(kind) or mojom.IsPendingAssociatedReceiverKind(kind)): named_kind = kind.kind else: named_kind = kind name = [] qualified = (not for_module) or (self.module is not named_kind.module) if qualified and named_kind.module: name.append(named_kind.module.namespace) if named_kind.parent_kind: parent_name = named_kind.parent_kind.name if mojom.IsStructKind( named_kind.parent_kind) and not for_module: parent_name += "Spec" name.append(parent_name) name.append(named_kind.name) name = ".".join(name) if for_module: name = name.replace(".", "_") if (mojom.IsStructKind(kind) or mojom.IsUnionKind(kind) or mojom.IsEnumKind(kind)): return "%sSpec.$" % name if mojom.IsInterfaceKind(kind) or mojom.IsPendingRemoteKind(kind): return "mojo.internal.InterfaceProxy(%sRemote)" % name if mojom.IsInterfaceRequestKind( kind) or mojom.IsPendingReceiverKind(kind): return "mojo.internal.InterfaceRequest(%sPendingReceiver)" % name if (mojom.IsAssociatedInterfaceKind(kind) or mojom.IsPendingAssociatedRemoteKind(kind)): # TODO(rockot): Implement associated interfaces. return "mojo.internal.AssociatedInterfaceProxy(%sRemote)" % ( name) if (mojom.IsAssociatedInterfaceRequestKind(kind) or mojom.IsPendingAssociatedReceiverKind(kind)): return "mojo.internal.AssociatedInterfaceRequest(%sPendingReceiver)" % ( name) return name
def _LiteJavaScriptType(self, kind): if self._IsPrimitiveKind(kind): return _kind_to_lite_js_type[kind] if mojom.IsArrayKind(kind): return "mojo.internal.Array(%s, %s)" % (self._LiteJavaScriptType( kind.kind), "true" if mojom.IsNullableKind(kind.kind) else "false") if mojom.IsMapKind(kind): return "mojo.internal.Map(%s, %s, %s)" % (self._LiteJavaScriptType( kind.key_kind), self._LiteJavaScriptType( kind.value_kind), "true" if mojom.IsNullableKind( kind.value_kind) else "false") if (mojom.IsAssociatedKind(kind) or mojom.IsInterfaceRequestKind(kind) or mojom.IsPendingRemoteKind(kind) or mojom.IsPendingReceiverKind(kind) or mojom.IsPendingAssociatedRemoteKind(kind) or mojom.IsPendingAssociatedReceiverKind(kind)): named_kind = kind.kind else: named_kind = kind name = [] if named_kind.module: name.append(named_kind.module.namespace) if named_kind.parent_kind: parent_name = named_kind.parent_kind.name if mojom.IsStructKind(named_kind.parent_kind): parent_name += "Spec" name.append(parent_name) name.append(named_kind.name) name = ".".join(name) if (mojom.IsStructKind(kind) or mojom.IsUnionKind(kind) or mojom.IsEnumKind(kind)): return "%sSpec.$" % name if mojom.IsInterfaceKind(kind) or mojom.IsPendingRemoteKind(kind): remote_name = name + self._GetPrimitivesNames()["remote"] return "mojo.internal.InterfaceProxy(%s)" % remote_name if mojom.IsInterfaceRequestKind(kind) or mojom.IsPendingReceiverKind( kind): request_name = name + self._GetPrimitivesNames( )["pending_receiver"] return "mojo.internal.InterfaceRequest(%s)" % request_name if (mojom.IsAssociatedInterfaceKind(kind) or mojom.IsPendingAssociatedRemoteKind(kind)): remote_name = name + self._GetPrimitivesNames()["remote"] # TODO(rockot): Implement associated interfaces. return "mojo.internal.AssociatedInterfaceProxy(%s)" % (remote_name) if (mojom.IsAssociatedInterfaceRequestKind(kind) or mojom.IsPendingAssociatedReceiverKind(kind)): request_name = name + self._GetPrimitivesNames( )["pending_receiver"] return "mojo.internal.AssociatedInterfaceRequest(%s)" % request_name return name
def GetFieldType(kind, field=None): if mojom.IsArrayKind(kind): arguments = [] if kind.kind in _kind_to_typecode_for_native_array: arguments.append('%r' % _kind_to_typecode_for_native_array[kind.kind]) elif kind.kind != mojom.BOOL: arguments.append(GetFieldType(kind.kind)) if mojom.IsNullableKind(kind): arguments.append('nullable=True') if kind.length is not None: arguments.append('length=%d' % kind.length) array_type = 'GenericArrayType' if kind.kind == mojom.BOOL: array_type = 'BooleanArrayType' elif kind.kind in _kind_to_typecode_for_native_array: array_type = 'NativeArrayType' return '_descriptor.%s(%s)' % (array_type, ', '.join(arguments)) if mojom.IsMapKind(kind): arguments = [ GetFieldType(kind.key_kind), GetFieldType(kind.value_kind), ] if mojom.IsNullableKind(kind): arguments.append('nullable=True') return '_descriptor.MapType(%s)' % ', '.join(arguments) if mojom.IsStructKind(kind): arguments = ['lambda: %s' % GetFullyQualifiedName(kind)] if mojom.IsNullableKind(kind): arguments.append('nullable=True') return '_descriptor.StructType(%s)' % ', '.join(arguments) if mojom.IsEnumKind(kind): return GetFieldType(mojom.INT32) if mojom.IsInterfaceKind(kind): arguments = ['lambda: %s' % GetFullyQualifiedName(kind)] if mojom.IsNullableKind(kind): arguments.append('nullable=True') return '_descriptor.InterfaceType(%s)' % ', '.join(arguments) if mojom.IsInterfaceRequestKind(kind): arguments = [] if mojom.IsNullableKind(kind): arguments.append('nullable=True') return '_descriptor.InterfaceRequestType(%s)' % ', '.join(arguments) return _kind_to_type[kind]
def _GetContainerValidateParamsCtorArgs(self, kind): if mojom.IsStringKind(kind): expected_num_elements = 0 element_is_nullable = False key_validate_params = "nullptr" element_validate_params = "nullptr" enum_validate_func = "nullptr" elif mojom.IsMapKind(kind): expected_num_elements = 0 element_is_nullable = False key_validate_params = self._GetNewContainerValidateParams( mojom.Array(kind=kind.key_kind)) element_validate_params = self._GetNewContainerValidateParams( mojom.Array(kind=kind.value_kind)) enum_validate_func = "nullptr" else: # mojom.IsArrayKind(kind) expected_num_elements = generator.ExpectedArraySize(kind) or 0 element_is_nullable = mojom.IsNullableKind(kind.kind) key_validate_params = "nullptr" element_validate_params = self._GetNewContainerValidateParams( kind.kind) if mojom.IsEnumKind(kind.kind): enum_validate_func = ( "%s::Validate" % self._GetQualifiedNameForKind( kind.kind, internal=True, flatten_nested_kind=True)) else: enum_validate_func = "nullptr" if enum_validate_func == "nullptr": if key_validate_params == "nullptr": return "%d, %s, %s" % (expected_num_elements, "true" if element_is_nullable else "false", element_validate_params) return "%s, %s" % (key_validate_params, element_validate_params) return "%d, %s" % (expected_num_elements, enum_validate_func)
def GetMapValidateParams(value_kind): # Unlike GetArrayValidateParams, we are given the wrapped kind, instead of # the raw array kind. So we wrap the return value of GetArrayValidateParams. element_is_nullable = mojom.IsNullableKind(value_kind) return "mojo::internal::ArrayValidateParams<0, %s,\n%s> " % ( 'true' if element_is_nullable else 'false', GetArrayValidateParams(value_kind))
def Check(kind): if kind.spec in checked: return True checked.add(kind.spec) if mojom.IsNullableKind(kind): return False elif mojom.IsStructKind(kind): if kind.native_only: return False if (self._IsTypemappedKind(kind) and not self.typemap[self._GetFullMojomNameForKind(kind)]["hashable"]): return False return all(Check(field.kind) for field in kind.fields) elif mojom.IsEnumKind(kind): return not self._IsTypemappedKind(kind) or self.typemap[ self._GetFullMojomNameForKind(kind)]["hashable"] elif mojom.IsUnionKind(kind): return all(Check(field.kind) for field in kind.fields) elif mojom.IsAnyHandleKind(kind): return False elif mojom.IsAnyInterfaceKind(kind): return False # TODO(crbug.com/735301): Arrays and maps could be made hashable. We just # don't have a use case yet. elif mojom.IsArrayKind(kind): return False elif mojom.IsMapKind(kind): return False else: return True
def _GetProtoFieldType(self, kind, quantified=True): # TODO(markbrand): This will not handle array<array> or array<map> # TODO(markbrand): This also will not handle array<x, 10> unquantified = '' if (mojom.IsEnumKind(kind) or mojom.IsStructKind(kind) or mojom.IsUnionKind(kind)): unquantified = self._GetProtoNameForKind(kind) elif mojom.IsArrayKind(kind): return "repeated %sEntry" % self._GetProtoFieldType( kind.kind, quantified=False) elif mojom.IsMapKind(kind): return ( "map<%sKey, %sValue>" % (self._GetProtoFieldType(kind.key_kind, quantified=False), self._GetProtoFieldType(kind.value_kind, quantified=False))) elif mojom.IsInterfaceKind(kind): unquantified = "%s.Ptr" % self._GetProtoNameForKind(kind) elif mojom.IsInterfaceRequestKind(kind): unquantified = "%s.Request" % self._GetProtoNameForKind(kind.kind) elif mojom.IsAssociatedInterfaceKind(kind): unquantified = "%s.AssociatedPtr" % self._GetProtoNameForKind( kind.kind) elif mojom.IsAssociatedInterfaceRequestKind(kind): unquantified = ("%s.AssociatedRequest" % self._GetProtoNameForKind(kind.kind)) elif mojom.IsPendingRemoteKind(kind): unquantified = "%s.PendingRemote" % self._GetProtoNameForKind( kind.kind) elif mojom.IsPendingReceiverKind(kind): unquantified = "%s.PendingReceiver" % self._GetProtoNameForKind( kind.kind) elif mojom.IsPendingAssociatedRemoteKind(kind): unquantified = ("%s.PendingAssociatedRemote" % self._GetProtoNameForKind(kind.kind)) elif mojom.IsPendingAssociatedReceiverKind(kind): unquantified = ("%s.PendingAssociatedReceiver" % self._GetProtoNameForKind(kind.kind)) elif mojom.IsStringKind(kind): unquantified = "string" elif mojom.IsGenericHandleKind(kind): unquantified = "mojolpm.Handle" elif mojom.IsDataPipeConsumerKind(kind): unquantified = "mojolpm.DataPipeConsumerHandle" elif mojom.IsDataPipeProducerKind(kind): unquantified = "mojolpm.DataPipeProducerHandle" elif mojom.IsMessagePipeKind(kind): unquantified = "mojolpm.MessagePipeHandle" elif mojom.IsSharedBufferKind(kind): unquantified = "mojolpm.SharedBufferHandle" elif mojom.IsPlatformHandleKind(kind): unquantified = "mojolpm.PlatformHandle" else: unquantified = _kind_to_proto_type[kind] if quantified and mojom.IsNullableKind(kind): return 'optional %s' % unquantified elif quantified: return 'required %s' % unquantified else: return unquantified
def _WrapIfNullable(inner_lines): """Check if kind is nullable if so yield code to check whether it has value. Args: inner_lines: {function} Function taking single argument and returning iterable. If kind is nullable, yield from this method with |cpp_parameter_name+'.value()'| otherwise yield with the parameter |cpp_parameter_name|. Args from the surrounding method: kind cpp_parameter_name output_context.AddSingleValue """ if mojom.IsNullableKind(kind): yield 'if (%s.has_value()) {' % cpp_parameter_name for line in inner_lines(cpp_parameter_name + '.value()'): yield ' ' + line yield '} else {' yield ' ' + output_context.AddSingleValue('String', '"base::nullopt"') yield '}' else: # |yield from| is introduced in Python3.3. for line in inner_lines(cpp_parameter_name): yield line
def GetArrayValidateParamsCtorArgs(kind): if mojom.IsStringKind(kind): expected_num_elements = 0 element_is_nullable = False element_validate_params = "nullptr" elif mojom.IsMapKind(kind): expected_num_elements = 0 element_is_nullable = mojom.IsNullableKind(kind.value_kind) element_validate_params = GetNewArrayValidateParams(kind.value_kind) else: expected_num_elements = generator.ExpectedArraySize(kind) or 0 element_is_nullable = mojom.IsNullableKind(kind.kind) element_validate_params = GetNewArrayValidateParams(kind.kind) return "%d, %s, %s" % (expected_num_elements, "true" if element_is_nullable else "false", element_validate_params)
def _GetFieldTypeForNewBindings(self, kind, for_module=False): if mojom.IsNullableKind(kind): return "({}|undefined)".format( self._GetTypeNameForNewBindings(kind, for_module=for_module)) else: return "!" + self._GetTypeNameForNewBindings(kind, for_module=for_module)
def Check(kind): if kind.spec in checked: return True checked.add(kind.spec) if mojom.IsNullableKind(kind): return False elif mojom.IsStructKind(kind): if (IsTypemappedKind(kind) and not _current_typemap[ GetFullMojomNameForKind(kind)]["hashable"]): return False return all(Check(field.kind) for field in kind.fields) elif mojom.IsEnumKind(kind): return not IsTypemappedKind(kind) or _current_typemap[ GetFullMojomNameForKind(kind)]["hashable"] elif mojom.IsUnionKind(kind): return all(Check(field.kind) for field in kind.fields) elif mojom.IsAnyHandleKind(kind): return False elif mojom.IsAnyInterfaceKind(kind): return False # TODO(tibell): Arrays and maps could be made hashable. We just don't have a # use case yet. elif mojom.IsArrayKind(kind): return False elif mojom.IsMapKind(kind): return False else: return True
def GetMojomTypeValue(kind, typepkg=''): if not kind in _kind_infos: return '' nullable = 'true' if mojom.IsNullableKind(kind) else 'false' if kind == mojom.BOOL or kind == mojom.FLOAT or kind == mojom.DOUBLE or \ mojom.IsIntegralKind(kind): kind_name = UpperCamelCase(_kind_infos[kind].decode_suffix.upper()) if kind == mojom.FLOAT: kind_name = "Float" elif kind == mojom.DOUBLE: kind_name = "Double" return '%sTypeSimpleType{%sSimpleType_%s}' % (typepkg, typepkg, kind_name) elif mojom.IsAnyHandleKind(kind): kind_name = 'Unspecified' if kind == mojom.DCPIPE: kind_name = 'DataPipeConsumer' elif kind == mojom.DPPIPE: kind_name = 'DataPipeProducer' elif kind == mojom.MSGPIPE: kind_name = 'MessagePipe' elif kind == mojom.SHAREDBUFFER: kind_name = 'SharedBuffer' return '%sTypeHandleType{%sHandleType{' \ 'Nullable: %s, Kind: %sHandleType_Kind_%s}}' % \ (typepkg, typepkg, nullable, typepkg, kind_name) elif mojom.IsStringKind(kind): return '%sTypeStringType{%sStringType{%s}}' % (typepkg, typepkg, nullable) else: raise Exception('Missing case for kind: %s' % kind)
def _GetObjCPropertyModifiers(self, kind): modifiers = ['nonatomic'] if (mojom.IsArrayKind(kind) or mojom.IsStringKind(kind) or mojom.IsMapKind(kind) or self._IsMojomStruct(kind)): modifiers.append('copy') if mojom.IsNullableKind(kind): modifiers.append('nullable') return ', '.join(modifiers)
def JavaScriptValidateMapParams(field): nullable = JavaScriptNullableParam(field) keys_type = ElementCodecType(field.kind.key_kind) values_kind = field.kind.value_kind values_type = ElementCodecType(values_kind) values_nullable = "true" if mojom.IsNullableKind(values_kind) else "false" return "%s, %s, %s, %s" % \ (nullable, keys_type, values_type, values_nullable)
def _WriteInputParamForTracing(self, kind, mojo_prefix, parameter_name, value): """Generates lines of C++ to log parameter |parameter_name| into TracedValue |value|. Args: kind: {Kind} The kind of the parameter (corresponds to its C++ type). mojo_prefix: {string} The prefix of the auto-generated parameter. parameter_name: {string} The mojom parameter name to be logged (auto-generated C++ parameter name is |mojo_prefix+parameter_name|). value: {string} The C++ TracedValue* variable name to be logged into. Yields: {string} C++ lines of code that trace |parameter_name| into |value|. """ cpp_parameter_name = mojo_prefix + parameter_name value_name_cppname = (value, parameter_name, cpp_parameter_name) # TODO(crbug.com/1103623): Support more involved types. if mojom.IsEnumKind(kind): # TODO(crbug.com/1103623): Pretty-print enums. yield '%s->SetInteger("%s", static_cast<int>(%s));' % value_name_cppname return if mojom.IsStringKind(kind): if self.for_blink: # WTF::String is nullable on its own. yield '%s->SetString("%s", %s.Utf8());' % value_name_cppname return if mojom.IsNullableKind(kind): # base::Optional<std::string> yield 'if (%s.has_value()) {' % cpp_parameter_name yield ' %s->SetString("%s", %s.value());' % value_name_cppname yield '} else {' yield ' %s->SetString("%s", "base::nullopt");' % ( value, parameter_name) yield '}' return else: yield '%s->SetString("%s", %s);' % value_name_cppname return if kind == mojom.BOOL: yield '%s->SetBoolean("%s", %s);' % value_name_cppname return # TODO(crbug.com/1103623): Make TracedValue support int64_t, then move to # mojom.IsIntegralKind. if kind in [ mojom.INT8, mojom.UINT8, mojom.INT16, mojom.UINT16, mojom.INT32 ]: # Parameter is representable as 32bit int. yield '%s->SetInteger("%s", %s);' % value_name_cppname return if kind in [mojom.UINT32, mojom.INT64, mojom.UINT64]: yield '%s->SetString("%s", std::to_string(%s));' % value_name_cppname return if mojom.IsFloatKind(kind) or mojom.IsDoubleKind(kind): yield '%s->SetDouble("%s", %s);' % value_name_cppname return yield '%s->SetString("%s", "<value of type %s>");' % ( value, parameter_name, self._GetCppWrapperParamType(kind))
def JavaScriptValidateMapParams(packed_field): nullable = JavaScriptNullableParam(packed_field) field_offset = JavaScriptFieldOffset(packed_field) keys_type = MapCodecType(packed_field.field.kind.key_kind) values_kind = packed_field.field.kind.value_kind values_type = MapCodecType(values_kind) values_nullable = "true" if mojom.IsNullableKind(values_kind) else "false" return "%s, %s, %s, %s, %s" % \ (field_offset, nullable, keys_type, values_type, values_nullable)
def CodecType(kind): if kind in mojom.PRIMITIVES: return _kind_to_codec_type[kind] if mojom.IsStructKind(kind): pointer_type = "NullablePointerTo" if mojom.IsNullableKind(kind) \ else "PointerTo" return "new codec.%s(%s)" % (pointer_type, JavaScriptType(kind)) if mojom.IsAnyArrayKind(kind): array_type = "NullableArrayOf" if mojom.IsNullableKind( kind) else "ArrayOf" element_type = "codec.PackedBool" if mojom.IsBoolKind(kind.kind) \ else CodecType(kind.kind) return "new codec.%s(%s)" % (array_type, element_type) if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind): return CodecType(mojom.MSGPIPE) if mojom.IsEnumKind(kind): return _kind_to_codec_type[mojom.INT32] return kind
def _GetObjCPropertyModifiers(self, kind, inside_union=False): modifiers = ['nonatomic'] if (mojom.IsArrayKind(kind) or mojom.IsStringKind(kind) or mojom.IsMapKind(kind) or mojom.IsStructKind(kind)): modifiers.append('copy') if ((inside_union and mojom.IsObjectKind(kind)) or mojom.IsNullableKind(kind)): modifiers.append('nullable') return ', '.join(modifiers)
def CodecType(kind): if kind in mojom.PRIMITIVES: return _kind_to_codec_type[kind] if mojom.IsStructKind(kind): pointer_type = "NullablePointerTo" if mojom.IsNullableKind(kind) \ else "PointerTo" return "new bindings.%s(%s)" % (pointer_type, DartType(kind)) if mojom.IsArrayKind(kind): array_type = "NullableArrayOf" if mojom.IsNullableKind(kind) else "ArrayOf" array_length = "" if kind.length is None else ", %d" % kind.length element_type = "bindings.PackedBool" if mojom.IsBoolKind(kind.kind) \ else CodecType(kind.kind) return "new bindings.%s(%s%s)" % (array_type, element_type, array_length) if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind): return CodecType(mojom.MSGPIPE) if mojom.IsEnumKind(kind): return _kind_to_codec_type[mojom.INT32] return kind
def _ObjcPropertyNeedsDefaultValueAssignment(self, field): kind = field.kind typemap = MojoTypemapForKind(kind) if not field.default and mojom.IsNullableKind(kind): return False if typemap.DefaultObjCValue(field.default) is None: return False if typemap is NumberMojoTypemap and field.default == 0: # 0 by default anyways return False return True
def _CppToObjCAssign(self, field, obj=None): kind = field.kind accessor = "%s%s" % (obj + "." if obj else "", field.name) typemap = MojoTypemapForKind(kind) if mojom.IsNullableKind(kind): if mojom.IsStructKind(kind): value_accessor = accessor else: value_accessor = "%s.value()" % accessor return "%s ? %s : nil" % (accessor, typemap.CppToObjC(value_accessor)) return typemap.CppToObjC(accessor)
def GetArrayNullabilityFlags(kind): """Returns nullability flags for an array type, see codec.dart. As we have dedicated decoding functions for arrays, we have to pass nullability information about both the array itself, as well as the array element type there. """ assert mojom.IsArrayKind(kind) ARRAY_NULLABLE = 'bindings.kArrayNullable' ELEMENT_NULLABLE = 'bindings.kElementNullable' NOTHING_NULLABLE = 'bindings.kNothingNullable' flags_to_set = [] if mojom.IsNullableKind(kind): flags_to_set.append(ARRAY_NULLABLE) if mojom.IsNullableKind(kind.kind): flags_to_set.append(ELEMENT_NULLABLE) if not flags_to_set: flags_to_set = [NOTHING_NULLABLE] return ' | '.join(flags_to_set)
def GetArrayValidateParams(kind): if (not mojom.IsArrayKind(kind) and not mojom.IsMapKind(kind) and not mojom.IsStringKind(kind)): return "mojo::internal::NoValidateParams" if mojom.IsStringKind(kind): expected_num_elements = 0 element_is_nullable = False element_validate_params = "mojo::internal::NoValidateParams" elif mojom.IsMapKind(kind): expected_num_elements = 0 element_is_nullable = mojom.IsNullableKind(kind.value_kind) element_validate_params = GetArrayValidateParams(kind.value_kind) else: expected_num_elements = generator.ExpectedArraySize(kind) or 0 element_is_nullable = mojom.IsNullableKind(kind.kind) element_validate_params = GetArrayValidateParams(kind.kind) return "mojo::internal::ArrayValidateParams<%d, %s,\n%s> " % ( expected_num_elements, 'true' if element_is_nullable else 'false', element_validate_params)
def _LiteJavaScriptType(self, kind): if self._IsPrimitiveKind(kind): return _kind_to_lite_js_type[kind] if mojom.IsArrayKind(kind): return "mojo.internal.Array(%s, %s)" % (self._LiteJavaScriptType( kind.kind), "true" if mojom.IsNullableKind(kind.kind) else "false") if mojom.IsMapKind(kind): return "mojo.internal.Map(%s, %s, %s)" % (self._LiteJavaScriptType( kind.key_kind), self._LiteJavaScriptType( kind.value_kind), "true" if mojom.IsNullableKind( kind.value_kind) else "false") if mojom.IsAssociatedKind(kind) or mojom.IsInterfaceRequestKind(kind): named_kind = kind.kind else: named_kind = kind name = [] if named_kind.module: name.append(named_kind.module.namespace) if named_kind.parent_kind: name.append(named_kind.parent_kind.name) name.append(named_kind.name) name = ".".join(name) if (mojom.IsStructKind(kind) or mojom.IsUnionKind(kind) or mojom.IsEnumKind(kind)): return "%s.$" % name if mojom.IsInterfaceKind(kind): return "mojo.internal.InterfaceProxy(%sProxy)" % name if mojom.IsInterfaceRequestKind(kind): return "mojo.internal.InterfaceRequest(%sRequest)" % name if mojom.IsAssociatedInterfaceKind(kind): return "mojo.internal.AssociatedInterfaceProxy(%sAssociatedProxy)" % ( name) if mojom.IsAssociatedInterfaceRequestKind(kind): return "mojo.internal.AssociatedInterfaceRequest(%s)" % name return name
def AppendEncodeParams(initial_params, kind, bit): """ Appends standard parameters shared between encode and decode calls. """ params = list(initial_params) if (kind == mojom.BOOL): params.append(str(bit)) if mojom.IsReferenceKind(kind): if mojom.IsArrayKind(kind): params.append(GetArrayNullabilityFlags(kind)) else: params.append(GetDartTrueFalse(mojom.IsNullableKind(kind))) if mojom.IsArrayKind(kind): params.append(GetArrayExpectedLength(kind)) return params
def _ObjcPropertyNeedsDefaultValueAssignment(self, field): kind = field.kind if not field.default: # If there's no specified default, only make defaults for required types return (not mojom.IsNullableKind(kind) and (mojom.IsStringKind(kind) or mojom.IsArrayKind(kind) or mojom.IsMapKind(kind) or self._IsMojomStruct(kind))) if self._IsObjCNumberKind(kind) and field.default == 0: # 0 by default anyways return False return True