Commit a18d8217 authored by Ken Rockot's avatar Ken Rockot Committed by Commit Bot

Format mojom_js_generator.py

In preparation for upcoming changes, this applies the repo's python
auto-formatting to the entire mojom_js_generator.py file to avoid extra
noise alongside more meaningful changes.

Bug: 1004256
Change-Id: I227d689b4eff4ee2a5f51bba068490f282151cba
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2450585
Commit-Queue: Oksana Zhuravlova <oksamyt@chromium.org>
Auto-Submit: Ken Rockot <rockot@google.com>
Reviewed-by: default avatarOksana Zhuravlova <oksamyt@chromium.org>
Cr-Commit-Position: refs/heads/master@{#813972}
parent 8e3b29e6
# Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Generates JavaScript source files from a mojom.Module."""
import mojom.generate.generator as generator
......@@ -260,7 +259,8 @@ class JavaScriptStylizer(generator.Stylizer):
return mojom_name
def StylizeModule(self, mojom_namespace):
return '.'.join(generator.ToCamel(word, lower_initial=True)
return '.'.join(
generator.ToCamel(word, lower_initial=True)
for word in mojom_namespace.split('.'))
......@@ -300,7 +300,8 @@ class Generator(generator.Generator):
"is_any_handle_or_interface_kind": mojom.IsAnyHandleOrInterfaceKind,
"is_array_kind": mojom.IsArrayKind,
"is_associated_interface_kind": mojom.IsAssociatedInterfaceKind,
"is_pending_associated_remote_kind": mojom.IsPendingAssociatedRemoteKind,
"is_pending_associated_remote_kind":
mojom.IsPendingAssociatedRemoteKind,
"is_associated_interface_request_kind":
mojom.IsAssociatedInterfaceRequestKind,
"is_pending_associated_receiver_kind":
......@@ -416,8 +417,7 @@ class Generator(generator.Generator):
return kind.module.namespace + "." + kind.name + "Ptr"
if mojom.IsPendingRemoteKind(kind):
return kind.kind.module.namespace + "." + kind.kind.name + "Ptr"
if (mojom.IsStructKind(kind) or
mojom.IsEnumKind(kind)):
if (mojom.IsStructKind(kind) or mojom.IsEnumKind(kind)):
return kind.module.namespace + "." + kind.name
# TODO(calamity): Support unions properly.
if mojom.IsUnionKind(kind):
......@@ -425,17 +425,17 @@ class Generator(generator.Generator):
if mojom.IsArrayKind(kind):
return "Array<%s>" % self._ClosureType(kind.kind)
if mojom.IsMapKind(kind):
return "Map<%s, %s>" % (
self._ClosureType(kind.key_kind), self._ClosureType(kind.value_kind))
return "Map<%s, %s>" % (self._ClosureType(
kind.key_kind), self._ClosureType(kind.value_kind))
if mojom.IsInterfaceRequestKind(kind) or mojom.IsPendingReceiverKind(kind):
return "mojo.InterfaceRequest"
# TODO(calamity): Support associated interfaces properly.
if (mojom.IsAssociatedInterfaceKind(kind) or
mojom.IsPendingAssociatedRemoteKind(kind)):
if (mojom.IsAssociatedInterfaceKind(kind)
or mojom.IsPendingAssociatedRemoteKind(kind)):
return "mojo.AssociatedInterfacePtrInfo"
# TODO(calamity): Support associated interface requests properly.
if (mojom.IsAssociatedInterfaceRequestKind(kind) or
mojom.IsPendingAssociatedReceiverKind(kind)):
if (mojom.IsAssociatedInterfaceRequestKind(kind)
or mojom.IsPendingAssociatedReceiverKind(kind)):
return "mojo.AssociatedInterfaceRequest"
# TODO(calamity): Support enums properly.
......@@ -445,9 +445,9 @@ class Generator(generator.Generator):
# Indicates whether a kind of suitable to stringify and use as an Object
# property name. This is checked for map key types to allow most kinds of
# mojom maps to be represented as either a Map or an Object.
return (mojom.IsIntegralKind(kind) or mojom.IsFloatKind(kind) or
mojom.IsDoubleKind(kind) or mojom.IsStringKind(kind) or
mojom.IsEnumKind(kind))
return (mojom.IsIntegralKind(kind) or mojom.IsFloatKind(kind)
or mojom.IsDoubleKind(kind) or mojom.IsStringKind(kind)
or mojom.IsEnumKind(kind))
def _LiteClosureType(self, kind):
if kind in mojom.PRIMITIVES:
......@@ -455,18 +455,16 @@ class Generator(generator.Generator):
if mojom.IsArrayKind(kind):
return "Array<%s>" % self._LiteClosureTypeWithNullability(kind.kind)
if mojom.IsMapKind(kind) and self._IsStringableKind(kind.key_kind):
return "Object<%s, %s>" % (
self._LiteClosureTypeWithNullability(kind.key_kind),
self._LiteClosureTypeWithNullability(kind.value_kind))
return "Object<%s, %s>" % (self._LiteClosureTypeWithNullability(
kind.key_kind), self._LiteClosureTypeWithNullability(kind.value_kind))
if mojom.IsMapKind(kind):
return "Map<%s, %s>" % (
self._LiteClosureTypeWithNullability(kind.key_kind),
self._LiteClosureTypeWithNullability(kind.value_kind))
if (mojom.IsAssociatedKind(kind) or mojom.IsInterfaceRequestKind(kind) or
mojom.IsPendingRemoteKind(kind) or mojom.IsPendingReceiverKind(kind) or
mojom.IsPendingAssociatedRemoteKind(kind) or
mojom.IsPendingAssociatedReceiverKind(kind)):
return "Map<%s, %s>" % (self._LiteClosureTypeWithNullability(
kind.key_kind), self._LiteClosureTypeWithNullability(kind.value_kind))
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
......@@ -484,20 +482,20 @@ class Generator(generator.Generator):
name.append("" + named_kind.name)
name = ".".join(name)
if (mojom.IsStructKind(kind) or mojom.IsUnionKind(kind) or
mojom.IsEnumKind(kind)):
if (mojom.IsStructKind(kind) or mojom.IsUnionKind(kind)
or mojom.IsEnumKind(kind)):
return name
if mojom.IsInterfaceKind(kind) or mojom.IsPendingRemoteKind(kind):
return name + "Remote"
if mojom.IsInterfaceRequestKind(kind) or mojom.IsPendingReceiverKind(kind):
return name + "PendingReceiver"
# TODO(calamity): Support associated interfaces properly.
if (mojom.IsAssociatedInterfaceKind(kind) or
mojom.IsPendingAssociatedRemoteKind(kind)):
if (mojom.IsAssociatedInterfaceKind(kind)
or mojom.IsPendingAssociatedRemoteKind(kind)):
return "Object"
# TODO(calamity): Support associated interface requests properly.
if (mojom.IsAssociatedInterfaceRequestKind(kind) or
mojom.IsPendingAssociatedReceiverKind(kind)):
if (mojom.IsAssociatedInterfaceRequestKind(kind)
or mojom.IsPendingAssociatedReceiverKind(kind)):
return "Object"
raise Exception("No valid closure type: %s" % kind)
......@@ -512,14 +510,13 @@ class Generator(generator.Generator):
if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind):
return prefix + "Object"
if mojom.IsArrayKind(kind):
return prefix + ("Array<%s>" %
self._LiteClosureParamType(kind.kind))
return prefix + ("Array<%s>" % self._LiteClosureParamType(kind.kind))
if mojom.IsMapKind(kind):
return "%sMap<%s, %s>|%sObject<%s, %s>" % (
prefix, self._LiteClosureParamType(kind.key_kind),
self._LiteClosureParamType(kind.value_kind),
prefix, self._LiteClosureParamType(kind.key_kind),
self._LiteClosureParamType(kind.value_kind))
self._LiteClosureParamType(
kind.value_kind), prefix, self._LiteClosureParamType(
kind.key_kind), self._LiteClosureParamType(kind.value_kind))
return prefix + self._LiteClosureType(kind)
......@@ -555,19 +552,18 @@ class Generator(generator.Generator):
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")
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)):
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
......@@ -583,19 +579,19 @@ class Generator(generator.Generator):
name.append(named_kind.name)
name = ".".join(name)
if (mojom.IsStructKind(kind) or mojom.IsUnionKind(kind) or
mojom.IsEnumKind(kind)):
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)):
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)):
if (mojom.IsAssociatedInterfaceRequestKind(kind)
or mojom.IsPendingAssociatedReceiverKind(kind)):
return "mojo.internal.AssociatedInterfaceRequest(%sPendingReceiver)" % (
name)
......@@ -630,14 +626,14 @@ class Generator(generator.Generator):
return "new %sPtr()" % self._JavaScriptType(field.kind)
if mojom.IsPendingRemoteKind(field.kind):
return "new %sPtr()" % self._JavaScriptType(field.kind.kind)
if (mojom.IsInterfaceRequestKind(field.kind) or
mojom.IsPendingReceiverKind(field.kind)):
if (mojom.IsInterfaceRequestKind(field.kind)
or mojom.IsPendingReceiverKind(field.kind)):
return "new bindings.InterfaceRequest()"
if (mojom.IsAssociatedInterfaceKind(field.kind) or
mojom.IsPendingAssociatedRemoteKind(field.kind)):
if (mojom.IsAssociatedInterfaceKind(field.kind)
or mojom.IsPendingAssociatedRemoteKind(field.kind)):
return "new associatedBindings.AssociatedInterfacePtrInfo()"
if (mojom.IsAssociatedInterfaceRequestKind(field.kind) or
mojom.IsPendingAssociatedReceiverKind(field.kind)):
if (mojom.IsAssociatedInterfaceRequestKind(field.kind)
or mojom.IsPendingAssociatedReceiverKind(field.kind)):
return "new associatedBindings.AssociatedInterfaceRequest()"
if mojom.IsEnumKind(field.kind):
return "0"
......@@ -647,7 +643,7 @@ class Generator(generator.Generator):
if field.default:
if mojom.IsStructKind(field.kind):
assert field.default == "default"
return "null";
return "null"
return self._ExpressionToTextLite(field.default)
if field.kind in mojom.PRIMITIVES:
return _kind_to_javascript_default_value[field.kind]
......@@ -665,8 +661,8 @@ class Generator(generator.Generator):
if mojom.IsUnionKind(kind):
return self._JavaScriptType(kind)
if mojom.IsArrayKind(kind):
array_type = ("NullableArrayOf" if mojom.IsNullableKind(kind)
else "ArrayOf")
array_type = ("NullableArrayOf"
if mojom.IsNullableKind(kind) else "ArrayOf")
array_length = "" if kind.length is None else ", %d" % kind.length
element_type = self._ElementCodecType(kind.kind)
return "new codec.%s(%s%s)" % (array_type, element_type, array_length)
......@@ -679,17 +675,18 @@ class Generator(generator.Generator):
"NullableInterface" if mojom.IsNullableKind(kind) else "Interface",
self._JavaScriptType(kind.kind))
if mojom.IsInterfaceRequestKind(kind) or mojom.IsPendingReceiverKind(kind):
return "codec.%s" % (
"NullableInterfaceRequest" if mojom.IsNullableKind(kind)
else "InterfaceRequest")
if (mojom.IsAssociatedInterfaceKind(kind) or
mojom.IsPendingAssociatedRemoteKind(kind)):
return "codec.%s" % ("NullableInterfaceRequest" if
mojom.IsNullableKind(kind) else "InterfaceRequest")
if (mojom.IsAssociatedInterfaceKind(kind)
or mojom.IsPendingAssociatedRemoteKind(kind)):
return "codec.%s" % ("NullableAssociatedInterfacePtrInfo"
if mojom.IsNullableKind(kind) else "AssociatedInterfacePtrInfo")
if (mojom.IsAssociatedInterfaceRequestKind(kind) or
mojom.IsPendingAssociatedReceiverKind(kind)):
if mojom.IsNullableKind(kind) else
"AssociatedInterfacePtrInfo")
if (mojom.IsAssociatedInterfaceRequestKind(kind)
or mojom.IsPendingAssociatedReceiverKind(kind)):
return "codec.%s" % ("NullableAssociatedInterfaceRequest"
if mojom.IsNullableKind(kind) else "AssociatedInterfaceRequest")
if mojom.IsNullableKind(kind) else
"AssociatedInterfaceRequest")
if mojom.IsEnumKind(kind):
return "new codec.Enum(%s)" % self._JavaScriptType(kind)
if mojom.IsMapKind(kind):
......@@ -700,19 +697,18 @@ class Generator(generator.Generator):
raise Exception("No codec type for %s" % kind)
def _ElementCodecType(self, kind):
return ("codec.PackedBool" if mojom.IsBoolKind(kind)
else self._CodecType(kind))
return ("codec.PackedBool"
if mojom.IsBoolKind(kind) else self._CodecType(kind))
def _JavaScriptDecodeSnippet(self, kind):
if (kind in mojom.PRIMITIVES or mojom.IsUnionKind(kind) or
mojom.IsAnyInterfaceKind(kind)):
if (kind in mojom.PRIMITIVES or mojom.IsUnionKind(kind)
or mojom.IsAnyInterfaceKind(kind)):
return "decodeStruct(%s)" % self._CodecType(kind)
if mojom.IsStructKind(kind):
return "decodeStructPointer(%s)" % self._JavaScriptType(kind)
if mojom.IsMapKind(kind):
return "decodeMapPointer(%s, %s)" % (
self._ElementCodecType(kind.key_kind),
self._ElementCodecType(kind.value_kind))
return "decodeMapPointer(%s, %s)" % (self._ElementCodecType(
kind.key_kind), self._ElementCodecType(kind.value_kind))
if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind):
return "decodeArrayPointer(codec.PackedBool)"
if mojom.IsArrayKind(kind):
......@@ -724,19 +720,18 @@ class Generator(generator.Generator):
raise Exception("No decode snippet for %s" % kind)
def _JavaScriptEncodeSnippet(self, kind):
if (kind in mojom.PRIMITIVES or mojom.IsUnionKind(kind) or
mojom.IsAnyInterfaceKind(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))
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, ";
return "encodeArrayPointer(codec.PackedBool, "
if mojom.IsArrayKind(kind):
return "encodeArrayPointer(%s, " % self._CodecType(kind.kind)
if mojom.IsEnumKind(kind):
......@@ -760,8 +755,7 @@ class Generator(generator.Generator):
nullable = self._JavaScriptNullableParam(field)
element_kind = field.kind.kind
element_size = pack.PackedField.GetSizeForKind(element_kind)
expected_dimension_sizes = GetArrayExpectedDimensionSizes(
field.kind)
expected_dimension_sizes = GetArrayExpectedDimensionSizes(field.kind)
element_type = self._ElementCodecType(element_kind)
return "%s, %s, %s, %s, 0" % \
(element_size, element_type, nullable,
......@@ -783,7 +777,7 @@ class Generator(generator.Generator):
def _JavaScriptValidateMapParams(self, field):
nullable = self._JavaScriptNullableParam(field)
keys_type = self._ElementCodecType(field.kind.key_kind)
values_kind = field.kind.value_kind;
values_kind = field.kind.value_kind
values_type = self._ElementCodecType(values_kind)
values_nullable = "true" if mojom.IsNullableKind(values_kind) else "false"
return "%s, %s, %s, %s" % \
......@@ -811,12 +805,12 @@ class Generator(generator.Generator):
if isinstance(token, mojom.BuiltinValue):
if token.value == "double.INFINITY" or token.value == "float.INFINITY":
return "Infinity";
return "Infinity"
if token.value == "double.NEGATIVE_INFINITY" or \
token.value == "float.NEGATIVE_INFINITY":
return "-Infinity";
return "-Infinity"
if token.value == "double.NAN" or token.value == "float.NAN":
return "NaN";
return "NaN"
return token
......@@ -848,8 +842,8 @@ class Generator(generator.Generator):
def _GenerateHtmlImports(self):
result = []
for full_import in self.module.imports:
result.append(os.path.relpath(full_import.path,
os.path.dirname(self.module.path)))
result.append(
os.path.relpath(full_import.path, os.path.dirname(self.module.path)))
return result
def _GetStructsFromMethods(self):
......@@ -863,20 +857,17 @@ class Generator(generator.Generator):
def _FuzzHandleName(self, kind):
if mojom.IsInterfaceRequestKind(kind) or mojom.IsPendingReceiverKind(kind):
return '{0}.{1}Request'.format(kind.kind.module.namespace,
kind.kind.name)
return '{0}.{1}Request'.format(kind.kind.module.namespace, kind.kind.name)
elif mojom.IsInterfaceKind(kind):
return '{0}.{1}Ptr'.format(kind.module.namespace,
kind.name)
return '{0}.{1}Ptr'.format(kind.module.namespace, kind.name)
elif mojom.IsPendingRemoteKind(kind):
return '{0}.{1}Ptr'.format(kind.kind.module.namespace,
kind.kind.name)
elif (mojom.IsAssociatedInterfaceRequestKind(kind) or
mojom.IsPendingAssociatedReceiverKind(kind)):
return '{0}.{1}Ptr'.format(kind.kind.module.namespace, kind.kind.name)
elif (mojom.IsAssociatedInterfaceRequestKind(kind)
or mojom.IsPendingAssociatedReceiverKind(kind)):
return '{0}.{1}AssociatedRequest'.format(kind.kind.module.namespace,
kind.kind.name)
elif (mojom.IsAssociatedInterfaceKind(kind) or
mojom.IsPendingAssociatedRemoteKind(kind)):
elif (mojom.IsAssociatedInterfaceKind(kind)
or mojom.IsPendingAssociatedRemoteKind(kind)):
return '{0}.{1}AssociatedPtr'.format(kind.kind.module.namespace,
kind.kind.name)
elif mojom.IsSharedBufferKind(kind):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment