def JavaScriptValidateArrayParams(packed_field): nullable = JavaScriptNullableParam(packed_field) field_offset = JavaScriptFieldOffset(packed_field) element_kind = packed_field.field.kind.kind element_size = pack.PackedField.GetSizeForKind(element_kind) element_count = generator.ExpectedArraySize(packed_field.field.kind) element_type = "codec.PackedBool" if mojom.IsBoolKind(element_kind) \ else CodecType(element_kind) return "%s, %s, %s, %s, %s" % \ (field_offset, element_size, element_count, element_type, nullable)
def JavaScriptValidateArrayParams(packed_field): nullable = JavaScriptNullableParam(packed_field) field_offset = JavaScriptFieldOffset(packed_field) element_kind = packed_field.field.kind.kind element_size = pack.PackedField.GetSizeForKind(element_kind) expected_dimension_sizes = GetArrayExpectedDimensionSizes( packed_field.field.kind) element_type = "codec.PackedBool" if mojom.IsBoolKind(element_kind) \ else CodecType(element_kind) return "%s, %s, %s, %s, %s, 0" % \ (field_offset, element_size, element_type, nullable, expected_dimension_sizes)
def JavaScriptEncodeSnippet(kind): if kind in mojom.PRIMITIVES: return "encodeStruct(%s, " % CodecType(kind) if mojom.IsStructKind(kind): return "encodeStructPointer(%s, " % CodecType(kind.name) if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind): return "encodeArrayPointer(new codec.ArrayOf(codec.PackedBool), " if mojom.IsAnyArrayKind(kind): return "encodeArrayPointer(%s, " % CodecType(kind.kind) if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind): return JavaScriptEncodeSnippet(mojom.MSGPIPE) if mojom.IsEnumKind(kind): return JavaScriptEncodeSnippet(mojom.INT32)
def JavaScriptDecodeSnippet(kind): if kind in mojom.PRIMITIVES: return "decodeStruct(%s)" % CodecType(kind) if mojom.IsStructKind(kind): return "decodeStructPointer(%s)" % JavaScriptType(kind) if mojom.IsAnyArrayKind(kind) and mojom.IsBoolKind(kind.kind): return "decodeArrayPointer(codec.PackedBool)" if mojom.IsAnyArrayKind(kind): return "decodeArrayPointer(%s)" % CodecType(kind.kind) if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind): return JavaScriptDecodeSnippet(mojom.MSGPIPE) if mojom.IsEnumKind(kind): return JavaScriptDecodeSnippet(mojom.INT32)
def CodecType(kind): if kind in mojom.PRIMITIVES: return _kind_to_codec_type[kind] if mojom.IsStructKind(kind): return "new codec.PointerTo(%s)" % CodecType(kind.name) if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind): return "new codec.ArrayOf(new codec.ArrayOf(codec.PackedBool))" if mojom.IsArrayKind(kind): return "new codec.ArrayOf(%s)" % CodecType(kind.kind) 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 DartDecodeSnippet(kind): if kind in mojom.PRIMITIVES: return "decodeStruct(%s)" % CodecType(kind) if mojom.IsStructKind(kind): return "decodeStructPointer(%s)" % DartType(kind) if mojom.IsMapKind(kind): return "decodeMapPointer(%s, %s)" % \ (MapCodecType(kind.key_kind), MapCodecType(kind.value_kind)) if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind): return "decodeArrayPointer(bindings.PackedBool)" if mojom.IsArrayKind(kind): return "decodeArrayPointer(%s)" % CodecType(kind.kind) if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind): return DartDecodeSnippet(mojom.MSGPIPE) if mojom.IsEnumKind(kind): return DartDecodeSnippet(mojom.INT32)
def JavaScriptEncodeSnippet(kind): if kind in mojom.PRIMITIVES: return "encodeStruct(%s, " % CodecType(kind) if mojom.IsStructKind(kind): return "encodeStructPointer(%s, " % JavaScriptType(kind) if mojom.IsMapKind(kind): return "encodeMapPointer(%s, %s, " % \ (MapCodecType(kind.key_kind), MapCodecType(kind.value_kind)) if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind): return "encodeArrayPointer(codec.PackedBool, " if mojom.IsArrayKind(kind): return "encodeArrayPointer(%s, " % CodecType(kind.kind) if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind): return JavaScriptEncodeSnippet(mojom.MSGPIPE) if mojom.IsEnumKind(kind): return JavaScriptEncodeSnippet(mojom.INT32)
def JavaScriptDecodeSnippet(kind): if (kind in mojom.PRIMITIVES or mojom.IsUnionKind(kind) or mojom.IsAnyInterfaceKind(kind)): return "decodeStruct(%s)" % CodecType(kind) if mojom.IsStructKind(kind): return "decodeStructPointer(%s)" % JavaScriptType(kind) if mojom.IsMapKind(kind): return "decodeMapPointer(%s, %s)" % \ (ElementCodecType(kind.key_kind), ElementCodecType(kind.value_kind)) if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind): return "decodeArrayPointer(codec.PackedBool)" if mojom.IsArrayKind(kind): return "decodeArrayPointer(%s)" % CodecType(kind.kind) if mojom.IsUnionKind(kind): return "decodeUnion(%s)" % CodecType(kind) if mojom.IsEnumKind(kind): return JavaScriptDecodeSnippet(mojom.INT32) raise Exception("No decode snippet for %s" % kind)
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 _JavaScriptEncodeSnippet(self, kind): if (kind in mojom.PRIMITIVES or mojom.IsUnionKind(kind) or mojom.IsAnyInterfaceKind(kind)): return "encodeStruct(%s, " % self._CodecType(kind) if mojom.IsUnionKind(kind): return "encodeStruct(%s, " % self._JavaScriptType(kind) if mojom.IsStructKind(kind): return "encodeStructPointer(%s, " % self._JavaScriptType(kind) if mojom.IsMapKind(kind): return "encodeMapPointer(%s, %s, " % (self._ElementCodecType( kind.key_kind), self._ElementCodecType(kind.value_kind)) if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind): return "encodeArrayPointer(codec.PackedBool, " if mojom.IsArrayKind(kind): return "encodeArrayPointer(%s, " % self._CodecType(kind.kind) if mojom.IsEnumKind(kind): return self._JavaScriptEncodeSnippet(mojom.INT32) raise Exception("No encode snippet for %s" % kind)
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 _ResolveNumericEnumValues(enum): """ Given a reference to a mojom.Enum, resolves and assigns the numeric value of each field, and also computes the min_value and max_value of the enum. """ # map of <mojom_name> -> integral value prev_value = -1 min_value = None max_value = None for field in enum.fields: # This enum value is +1 the previous enum value (e.g: BEGIN). if field.value is None: prev_value += 1 # Integral value (e.g: BEGIN = -0x1). elif _IsStrOrUnicode(field.value): prev_value = int(field.value, 0) # Reference to a previous enum value (e.g: INIT = BEGIN). elif isinstance(field.value, mojom.EnumValue): prev_value = field.value.field.numeric_value elif isinstance(field.value, mojom.ConstantValue): constant = field.value.constant kind = constant.kind if not mojom.IsIntegralKind(kind) or mojom.IsBoolKind(kind): raise ValueError( 'Enum values must be integers. %s is not an integer.' % constant.mojom_name) prev_value = int(constant.value, 0) else: raise Exception('Unresolved enum value for %s' % field.value.GetSpec()) #resolved_enum_values[field.mojom_name] = prev_value field.numeric_value = prev_value if min_value is None or prev_value < min_value: min_value = prev_value if max_value is None or prev_value > max_value: max_value = prev_value enum.min_value = min_value enum.max_value = max_value
def JavaScriptEncodeSnippet(kind): if (kind in mojom.PRIMITIVES or mojom.IsUnionKind(kind) or mojom.IsInterfaceKind(kind) or mojom.IsAssociatedKind(kind)): return "encodeStruct(%s, " % CodecType(kind) if mojom.IsUnionKind(kind): return "encodeStruct(%s, " % JavaScriptType(kind) if mojom.IsStructKind(kind): return "encodeStructPointer(%s, " % JavaScriptType(kind) if mojom.IsMapKind(kind): return "encodeMapPointer(%s, %s, " % \ (ElementCodecType(kind.key_kind), ElementCodecType(kind.value_kind)) if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind): return "encodeArrayPointer(codec.PackedBool, " if mojom.IsArrayKind(kind): return "encodeArrayPointer(%s, " % CodecType(kind.kind) if mojom.IsInterfaceRequestKind(kind): return JavaScriptEncodeSnippet(mojom.MSGPIPE) if mojom.IsEnumKind(kind): return JavaScriptEncodeSnippet(mojom.INT32) raise Exception("No encode snippet for %s" % kind)
def _ElementCodecType(self, kind): return ("codec.PackedBool" if mojom.IsBoolKind(kind) else self._CodecType(kind))
def ElementCodecType(kind): return "bindings.PackedBool" if mojom.IsBoolKind(kind) else CodecType(kind)
def ElementCodecType(kind): if mojom.IsUnionKind(kind): wrapper_type = "NullableUnionWrapper" if mojom.IsNullableKind(kind) \ else "UnionWrapper" return "new codec.%s(%s)" % (wrapper_type, JavaScriptType(kind)) return "codec.PackedBool" if mojom.IsBoolKind(kind) else CodecType(kind)
def MapCodecType(kind): return "codec.PackedBool" if mojom.IsBoolKind(kind) else CodecType(kind)
def IsBoolField(field): return mojom.IsBoolKind(field.kind)