1 # Mojom IDL and Bindings Generator
2 This document is a subset of the [Mojo documentation](/mojo/README.md).
3
4 [TOC]
5
6 ## Overview
7
8 Mojom is the IDL for Mojo bindings interfaces. Given a `.mojom` file, the
9 [bindings
10 generator](https://cs.chromium.org/chromium/src/mojo/public/tools/bindings/)
11 outputs bindings for all supported languages: **C++**, **JavaScript**, and
12 **Java**.
13
14 For a trivial example consider the following hypothetical Mojom file we write to
15 `//services/widget/public/interfaces/frobinator.mojom`:
16
17 ```
18 module widget.mojom;
19
20 interface Frobinator {
21 Frobinate();
22 };
23 ```
24
25 This defines a single [interface](#Interfaces) named `Frobinator` in a
26 [module](#Modules) named `widget.mojom` (and thus fully qualified in Mojom as
27 `widget.mojom.Frobinator`.) Note that many interfaces and/or other types of
28 definitions may be included in a single Mojom file.
29
30 If we add a corresponding GN target to
31 `//services/widget/public/interfaces/BUILD.gn`:
32
33 ```
34 import("mojo/public/tools/bindings/mojom.gni")
35
36 mojom("interfaces") {
37 sources = [
38 "frobinator.mojom",
39 ]
40 }
41 ```
42
43 and then build this target:
44
45 ```
46 ninja -C out/r services/widget/public/interfaces
47 ```
48
49 we'll find several generated sources in our output directory:
50
51 ```
52 out/r/gen/services/widget/public/interfaces/frobinator.mojom.cc
53 out/r/gen/services/widget/public/interfaces/frobinator.mojom.h
54 out/r/gen/services/widget/public/interfaces/frobinator.mojom.js
55 out/r/gen/services/widget/public/interfaces/frobinator.mojom.srcjar
56 ...
57 ```
58
59 Each of these generated source modules includes a set of definitions
60 representing the Mojom contents within the target language. For more details
61 regarding the generated outputs please see
62 [documentation for individual target languages](#Generated-Code-For-Target-Languages).
63
64 ## Mojom Syntax
65
66 Mojom IDL allows developers to define **structs**, **unions**, **interfaces**,
67 **constants**, and **enums**, all within the context of a **module**. These
68 definitions are used to generate code in the supported target languages at build
69 time.
70
71 Mojom files may **import** other Mojom files in order to reference their
72 definitions.
73
74 ### Primitive Types
75 Mojom supports a few basic data types which may be composed into structs or used
76 for message parameters.
77
78 | Type | Description
79 |-------------------------------|-------------------------------------------------------|
80 | `bool` | Boolean type (`true` or `false`.)
81 | `int8`, `uint8` | Signed or unsigned 8-bit integer.
82 | `int16`, `uint16` | Signed or unsigned 16-bit integer.
83 | `int32`, `uint32` | Signed or unsigned 32-bit integer.
84 | `int64`, `uint64` | Signed or unsigned 64-bit integer.
85 | `float`, `double` | 32- or 64-bit floating point number.
86 | `string` | UTF-8 encoded string.
87 | `array<T>` | Array of any Mojom type *T*; for example, `array<uint8>` or `array<array<string>>`.
88 | `array<T, N>` | Fixed-length array of any Mojom type *T*. The parameter *N* must be an integral constant.
89 | `map<S, T>` | Associated array maping values of type *S* to values of type *T*. *S* may be a `string`, `enum`, or numeric type.
90 | `handle` | Generic Mojo handle. May be any type of handle, including a wrapped native platform handle.
91 | `handle<message_pipe>` | Generic message pipe handle.
92 | `handle<shared_buffer>` | Shared buffer handle.
93 | `handle<data_pipe_producer>` | Data pipe producer handle.
94 | `handle<data_pipe_consumer>` | Data pipe consumer handle.
95 | *`InterfaceType`* | Any user-defined Mojom interface type. This is sugar for a strongly-typed message pipe handle which should eventually be used to make outgoing calls on the interface.
96 | *`InterfaceType&`* | An interface request for any user-defined Mojom interface type. This is sugar for a more strongly-typed message pipe handle which is expected to receive request messages and should therefore eventually be bound to an implementation of the interface.
97 | *`associated InterfaceType`* | An associated interface handle. See [Associated Interfaces](#Associated-Interfaces)
98 | *`associated InterfaceType&`* | An associated interface request. See [Associated Interfaces](#Associated-Interfaces)
99 | *T*? | An optional (nullable) value. Primitive numeric types (integers, floats, booleans, and enums) are not nullable. All other types are nullable.
100
101 ### Modules
102
103 Every Mojom file may optionally specify a single **module** to which it belongs.
104
105 This is used strictly for aggregaging all defined symbols therein within a
106 common Mojom namespace. The specific impact this has on generated binidngs code
107 varies for each target language. For example, if the following Mojom is used to
108 generate bindings:
109
110 ```
111 module business.stuff;
112
113 interface MoneyGenerator {
114 GenerateMoney();
115 };
116 ```
117
118 Generated C++ bindings will define a class interface `MoneyGenerator` in the
119 `business::stuff` namespace, while Java bindings will define an interface
120 `MoneyGenerator` in the `org.chromium.business.stuff` package. JavaScript
121 bindings at this time are unaffected by module declarations.
122
123 **NOTE:** By convention in the Chromium codebase, **all** Mojom files should
124 declare a module name with at least (and preferrably exactly) one top-level name
125 as well as an inner `mojom` module suffix. *e.g.*, `chrome.mojom`,
126 `business.mojom`, *etc.*
127
128 This convention makes it easy to tell which symbols are generated by Mojom when
129 reading non-Mojom code, and it also avoids namespace collisions in the fairly
130 common scenario where you have a real C++ or Java `Foo` along with a
131 corresponding Mojom `Foo` for its serialized representation.
132
133 ### Imports
134
135 If your Mojom references definitions from other Mojom files, you must **import**
136 those files. Import syntax is as follows:
137
138 ```
139 import "services/widget/public/interfaces/frobinator.mojom";
140 ```
141
142 Import paths are always relative to the top-level directory.
143
144 Note that circular imports are **not** supported.
145
146 ### Structs
147
148 Structs are defined using the **struct** keyword, and they provide a way to
149 group related fields together:
150
151 ``` cpp
152 struct StringPair {
153 string first;
154 string second;
155 };
156 ```
157
158 Struct fields may be comprised of any of the types listed above in the
159 [Primitive Types](#Primitive-Types) section.
160
161 Default values may be specified as long as they are constant:
162
163 ``` cpp
164 struct Request {
165 int32 id = -1;
166 string details;
167 };
168 ```
169
170 What follows is a fairly
171 comprehensive example using the supported field types:
172
173 ``` cpp
174 struct StringPair {
175 string first;
176 string second;
177 };
178
179 enum AnEnum {
180 YES,
181 NO
182 };
183
184 interface SampleInterface {
185 DoStuff();
186 };
187
188 struct AllTheThings {
189 // Note that these types can never be marked nullable!
190 bool boolean_value;
191 int8 signed_8bit_value = 42;
192 uint8 unsigned_8bit_value;
193 int16 signed_16bit_value;
194 uint16 unsigned_16bit_value;
195 int32 signed_32bit_value;
196 uint32 unsigned_32bit_value;
197 int64 signed_64bit_value;
198 uint64 unsigned_64bit_value;
199 float float_value_32bit;
200 double float_value_64bit;
201 AnEnum enum_value = AnEnum.YES;
202
203 // Strings may be nullable.
204 string? maybe_a_string_maybe_not;
205
206 // Structs may contain other structs. These may also be nullable.
207 StringPair some_strings;
208 StringPair? maybe_some_more_strings;
209
210 // In fact structs can also be nested, though in practice you must always make
211 // such fields nullable -- otherwise messages would need to be infinitely long
212 // in order to pass validation!
213 AllTheThings? more_things;
214
215 // Arrays may be templated over any Mojom type, and are always nullable:
216 array<int32> numbers;
217 array<int32>? maybe_more_numbers;
218
219 // Arrays of arrays of arrays... are fine.
220 array<array<array<AnEnum>>> this_works_but_really_plz_stop;
221
222 // The element type may be nullable if it's a type which is allowed to be
223 // nullable.
224 array<AllTheThings?> more_maybe_things;
225
226 // Fixed-size arrays get some extra validation on the receiving end to ensure
227 // that the correct number of elements is always received.
228 array<uint64, 2> uuid;
229
230 // Maps follow many of the same rules as arrays. Key types may be any
231 // non-handle, non-collection type, and value types may be any supported
232 // struct field type. Maps may also be nullable.
233 map<string, int32> one_map;
234 map<AnEnum, string>? maybe_another_map;
235 map<StringPair, AllTheThings?>? maybe_a_pretty_weird_but_valid_map;
236 map<StringPair, map<int32, array<map<string, string>?>?>?> ridiculous;
237
238 // And finally, all handle types are valid as struct fields and may be
239 // nullable. Note that interfaces and interface requests (the "Foo" and
240 // "Foo&" type syntax respectively) are just strongly-typed message pipe
241 // handles.
242 handle generic_handle;
243 handle<data_pipe_consumer> reader;
244 handle<data_pipe_producer>? maybe_writer;
245 handle<shared_buffer> dumping_ground;
246 handle<message_pipe> raw_message_pipe;
247 SampleInterface? maybe_a_sample_interface_client_pipe;
248 SampleInterface& non_nullable_sample_interface_request;
249 SampleInterface&? nullable_sample_interface_request;
250 associated SampleInterface associated_interface_client;
251 associated SampleInterface& associated_interface_request;
252 associated SampleInterface&? maybe_another_associated_request;
253 };
254 ```
255
256 For details on how all of these different types translate to usable generated
257 code, see
258 [documentation for individual target languages](#Generated-Code-For-Target-Languages).
259
260 ### Unions
261
262 Mojom supports tagged unions using the **union** keyword. A union is a
263 collection of fields which may taken the value of any single one of those fields
264 at a time. Thus they provide a way to represent a variant value type while
265 minimizing storage requirements.
266
267 Union fields may be of any type supported by [struct](#Structs) fields. For
268 example:
269
270 ```cpp
271 union ExampleUnion {
272 string str;
273 StringPair pair;
274 int64 id;
275 array<uint64, 2> guid;
276 SampleInterface iface;
277 };
278 ```
279
280 For details on how unions like this translate to generated bindings code, see
281 [documentation for individual target languages](#Generated-Code-For-Target-Languages).
282
283 ### Enumeration Types
284
285 Enumeration types may be defined using the **enum** keyword either directly
286 within a module or within the namespace of some struct or interface:
287
288 ```
289 module business.mojom;
290
291 enum Department {
292 SALES = 0,
293 DEV,
294 };
295
296 struct Employee {
297 enum Type {
298 FULL_TIME,
299 PART_TIME,
300 };
301
302 Type type;
303 // ...
304 };
305 ```
306
307 That that similar to C-style enums, individual values may be explicitly assigned
308 within an enum definition. By default values are based at zero and incremenet by
309 1 sequentially.
310
311 The effect of nested definitions on generated bindings varies depending on the
312 target language. See [documentation for individual target languages](#Generated-Code-For-Target-Languages)
313
314 ### Constants
315
316 Constants may be defined using the **const** keyword either directly within a
317 module or within the namespace of some struct or interface:
318
319 ```
320 module business.mojom;
321
322 const string kServiceName = "business";
323
324 struct Employee {
325 const uint64 kInvalidId = 0;
326
327 enum Type {
328 FULL_TIME,
329 PART_TIME,
330 };
331
332 uint64 id = kInvalidId;
333 Type type;
334 };
335 ```
336
337 The effect of nested definitions on generated bindings varies depending on the
338 target language. See [documentation for individual target languages](#Generated-Code-For-Target-Languages)
339
340 ### Interfaces
341
342 An **interface** is a logical bundle of parameterized request messages. Each
343 request message may optionally define a parameterized response message. Here's
344 syntax to define an interface `Foo` with various kinds of requests:
345
346 ```
347 interface Foo {
348 // A request which takes no arguments and expects no response.
349 MyMessage();
350
351 // A request which has some arguments and expects no response.
352 MyOtherMessage(string name, array<uint8> bytes);
353
354 // A request which expects a single-argument response.
355 MyMessageWithResponse(string command) => (bool success);
356
357 // A request which expects a response with multiple arguments.
358 MyMessageWithMoarResponse(string a, string b) => (int8 c, int8 d);
359 };
360 ```
361
362 Anything which is a valid struct field type (see [Structs](#Structs)) is also a
363 valid request or response argument type. The type notation is the same for both.
364
365 ### Attributes
366
367 Mojom definitions may have their meaning altered by **attributes**, specified
368 with a syntax similar to Java or C# attributes. There are a handle of
369 interesting attributes supported today.
370
371 **`[Sync]`**
372 : The `Sync` attribute may be specified for any interface method which expects
373 a response. This makes it so that callers of the method can wait
374 synchronously for a response. See
375 [Synchronous Calls](/mojo/public/cpp/bindings/README.md#Synchronous-Calls)
376 in the C++ bindings documentation. Note that sync calls are not currently
377 supported in other target languages.
378
379 **`[Extensible]`**
380 : The `Extensible` attribute may be specified for any enum definition. This
381 essentially disables builtin range validation when receiving values of the
382 enum type in a message, allowing older bindings to tolerate unrecognized
383 values from newer versions of the enum.
384
385 **`[Native]`**
386 : The `Native` attribute may be specified for an empty struct declaration to
387 provide a nominal bridge between Mojo IPC and legacy `IPC::ParamTraits` or
388 `IPC_STRUCT_TRAITS*` macros.
389 See [Using Legacy IPC Traits](/ipc/README.md#Using-Legacy-IPC-Traits) for
390 more details. Note support for this attribute is strictly limited to C++
391 bindings generation.
392
393 **`[MinVersion=N]`**
394 : The `MinVersion` attribute is used to specify the version at which a given
395 field, enum value, interface method, or method parameter was introduced.
396 See [Versioning](#Versioning) for more details.
397
398 **`[EnableIf=value]`**
399 : The `EnableIf` attribute is used to conditionally enable definitions when
400 the mojom is parsed. If the `mojom` target in the GN file does not include
401 the matching `value` in the list of `enabled_features`, the definition
402 will be disabled. This is useful for mojom definitions that only make
403 sense on one platform. Note that the `EnableIf` attribute can only be set
404 once per definition.
405
406 ## Generated Code For Target Languages
407
408 When the bindings generator successfully processes an input Mojom file, it emits
409 corresponding code for each supported target language. For more details on how
410 Mojom concepts translate to a given target langauge, please refer to the
411 bindings API documentation for that language:
412
413 * [C++ Bindings](/mojo/public/cpp/bindings/README.md)
414 * [JavaScript Bindings](/mojo/public/js/README.md)
415 * [Java Bindings](/mojo/public/java/bindings/README.md)
416
417 ## Message Validation
418
419 Regardless of target language, all interface messages are validated during
420 deserialization before they are dispatched to a receiving implementation of the
421 interface. This helps to ensure consitent validation across interfaces without
422 leaving the burden to developers and security reviewers every time a new message
423 is added.
424
425 If a message fails validation, it is never dispatched. Instead a **connection
426 error** is raised on the binding object (see
427 [C++ Connection Errors](/mojo/public/cpp/bindings/README.md#Connection-Errors),
428 [Java Connection Errors](/mojo/public/java/bindings/README.md#Connection-Errors),
429 or
430 [JavaScript Connection Errors](/mojo/public/js/README.md#Connection-Errors) for
431 details.)
432
433 Some baseline level of validation is done automatically for primitive Mojom
434 types.
435
436 ### Non-Nullable Objects
437
438 Mojom fields or parameter values (*e.g.*, structs, interfaces, arrays, *etc.*)
439 may be marked nullable in Mojom definitions (see
440 [Primitive Types](#Primitive-Types).) If a field or parameter is **not** marked
441 nullable but a message is received with a null value in its place, that message
442 will fail validation.
443
444 ### Enums
445
446 Enums declared in Mojom are automatically validated against the range of legal
447 values. For example if a Mojom declares the enum:
448
449 ``` cpp
450 enum AdvancedBoolean {
451 TRUE = 0,
452 FALSE = 1,
453 FILE_NOT_FOUND = 2,
454 };
455 ```
456
457 and a message is received with the integral value 3 (or anything other than 0,
458 1, or 2) in place of some `AdvancedBoolean` field or parameter, the message will
459 fail validation.
460
461 *** note
462 NOTE: It's possible to avoid this type of validation error by explicitly marking
463 an enum as [Extensible](#Attributes) if you anticipate your enum being exchanged
464 between two different versions of the binding interface. See
465 [Versioning](#Versioning).
466 ***
467
468 ### Other failures
469
470 There are a host of internal validation errors that may occur when a malformed
471 message is received, but developers should not be concerned with these
472 specifically; in general they can only result from internal bindings bugs,
473 compromised processes, or some remote endpoint making a dubious effort to
474 manually encode their own bindings messages.
475
476 ### Custom Validation
477
478 It's also possible for developers to define custom validation logic for specific
479 Mojom struct types by exploiting the
480 [type mapping](/mojo/public/cpp/bindings/README.md#Type-Mapping) system for C++
481 bindings. Messages rejected by custom validation logic trigger the same
482 validation failure behavior as the built-in type validation routines.
483
484 ## Associated Interfaces
485
486 As mentioned in the [Primitive Types](#Primitive-Types) section above, interface
487 and interface request fields and parameters may be marked as `associated`. This
488 essentially means that they are piggy-backed on some other interface's message
489 pipe.
490
491 Because individual interface message pipes operate independently there can be no
492 relative ordering guarantees among them. Associated interfaces are useful when
493 one interface needs to guarantee strict FIFO ordering with respect to one or
494 more other interfaces, as they allow interfaces to share a single pipe.
495
496 Currenly associated interfaces are only supported in generated C++ bindings.
497 See the documentation for
498 [C++ Associated Interfaces](/mojo/public/cpp/bindings/README.md#Associated-Interfaces).
499
500 ## Versioning
501
502 ### Overview
503
504 *** note
505 **NOTE:** You don't need to worry about versioning if you don't care about
506 backwards compatibility. Specifically, all parts of Chrome are updated
507 atomically today and there is not yet any possibility of any two Chrome
508 processes communicating with two different versions of any given Mojom
509 interface.
510 ***
511
512 Services extend their interfaces to support new features over time, and clients
513 want to use those new features when they are available. If services and clients
514 are not updated at the same time, it's important for them to be able to
515 communicate with each other using different snapshots (versions) of their
516 interfaces.
517
518 This document shows how to extend Mojom interfaces in a backwards-compatible
519 way. Changing interfaces in a non-backwards-compatible way is not discussed,
520 because in that case communication between different interface versions is
521 impossible anyway.
522
523 ### Versioned Structs
524
525 You can use the `MinVersion` [attribute](#Attributes) to indicate from which
526 version a struct field is introduced. Assume you have the following struct:
527
528 ``` cpp
529 struct Employee {
530 uint64 employee_id;
531 string name;
532 };
533 ```
534
535 and you would like to add a birthday field. You can do:
536
537 ``` cpp
538 struct Employee {
539 uint64 employee_id;
540 string name;
541 [MinVersion=1] Date? birthday;
542 };
543 ```
544
545 By default, fields belong to version 0. New fields must be appended to the
546 struct definition (*i.e*., existing fields must not change **ordinal value**)
547 with the `MinVersion` attribute set to a number greater than any previous
548 existing versions.
549
550 **Ordinal value** refers to the relative positional layout of a struct's fields
551 (and an interface's methods) when encoded in a message. Implicitly, ordinal
552 numbers are assigned to fields according to lexical position. In the example
553 above, `employee_id` has an ordinal value of 0 and `name` has an ordinal value
554 of 1.
555
556 Ordinal values can be specified explicitly using `**@**` notation, subject to
557 the following hard constraints:
558
559 * For any given struct or interface, if any field or method explicitly specifies
560 an ordinal value, all fields or methods must explicitly specify an ordinal
561 value.
562 * For an *N*-field struct or *N*-method interface, the set of explicitly
563 assigned ordinal values must be limited to the range *[0, N-1]*.
564
565 You may reorder fields, but you must ensure that the ordinal values of existing
566 fields remain unchanged. For example, the following struct remains
567 backwards-compatible:
568
569 ``` cpp
570 struct Employee {
571 uint64 employee_id@0;
572 [MinVersion=1] Date? birthday@2;
573 string name@1;
574 };
575 ```
576
577 *** note
578 **NOTE:** Newly added fields of Mojo object or handle types MUST be nullable.
579 See [Primitive Types](#Primitive-Types).
580 ***
581
582 ### Versioned Interfaces
583
584 There are two dimensions on which an interface can be extended
585
586 **Appending New Parameters To Existing Methods**
587 : Parameter lists are treated as structs internally, so all the rules of
588 versioned structs apply to method parameter lists. The only difference is
589 that the version number is scoped to the whole interface rather than to any
590 individual parameter list.
591
592 Please note that adding a response to a message which did not previously
593 expect a response is a not a backwards-compatible change.
594
595 **Appending New Methods**
596 : Similarly, you can reorder methods with explicit ordinal values as long as
597 the ordinal values of existing methods are unchanged.
598
599 For example:
600
601 ``` cpp
602 // Old version:
603 interface HumanResourceDatabase {
604 AddEmployee(Employee employee) => (bool success);
605 QueryEmployee(uint64 id) => (Employee? employee);
606 };
607
608 // New version:
609 interface HumanResourceDatabase {
610 AddEmployee(Employee employee) => (bool success);
611
612 QueryEmployee(uint64 id, [MinVersion=1] bool retrieve_finger_print)
613 => (Employee? employee,
614 [MinVersion=1] array<uint8>? finger_print);
615
616 [MinVersion=1]
617 AttachFingerPrint(uint64 id, array<uint8> finger_print)
618 => (bool success);
619 };
620 ```
621
622 Similar to [versioned structs](#Versioned-Structs), when you pass the parameter
623 list of a request or response method to a destination using an older version of
624 an interface, unrecognized fields are silently discarded. However, if the method
625 call itself is not recognized, it is considered a validation error and the
626 receiver will close its end of the interface pipe. For example, if a client on
627 version 1 of the above interface sends an `AttachFingerPrint` request to an
628 implementation of version 0, the client will be disconnected.
629
630 Bindings target languages that support versioning expose means to query or
631 assert the remote version from a client handle (*e.g.*, an
632 `InterfacePtr<T>` in C++ bindings.)
633
634 See
635 [C++ Versioning Considerations](/mojo/public/cpp/bindings/README.md#Versioning-Considerations)
636 and
637 [Java Versioning Considerations](/mojo/public/java/bindings/README.md#Versioning-Considerations)
638
639 ### Versioned Enums
640
641 **By default, enums are non-extensible**, which means that generated message
642 validation code does not expect to see new values in the future. When an unknown
643 value is seen for a non-extensible enum field or parameter, a validation error
644 is raised.
645
646 If you want an enum to be extensible in the future, you can apply the
647 `[Extensible]` [attribute](#Attributes):
648
649 ``` cpp
650 [Extensible]
651 enum Department {
652 SALES,
653 DEV,
654 };
655 ```
656
657 And later you can extend this enum without breaking backwards compatibility:
658
659 ``` cpp
660 [Extensible]
661 enum Department {
662 SALES,
663 DEV,
664 [MinVersion=1] RESEARCH,
665 };
666 ```
667
668 *** note
669 **NOTE:** For versioned enum definitions, the use of a `[MinVersion]` attribute
670 is strictly for documentation purposes. It has no impact on the generated code.
671 ***
672
673 With extensible enums, bound interface implementations may receive unknown enum
674 values and will need to deal with them gracefully. See
675 [C++ Versioning Considerations](/mojo/public/cpp/bindings/README.md#Versioning-Considerations)
676 for details.
677
678 ## Grammar Reference
679
680 Below is the (BNF-ish) context-free grammar of the Mojom language:
681
682 ```
683 MojomFile = StatementList
684 StatementList = Statement StatementList | Statement
685 Statement = ModuleStatement | ImportStatement | Definition
686
687 ModuleStatement = AttributeSection "module" Identifier ";"
688 ImportStatement = "import" StringLiteral ";"
689 Definition = Struct Union Interface Enum Const
690
691 AttributeSection = "[" AttributeList "]"
692 AttributeList = <empty> | NonEmptyAttributeList
693 NonEmptyAttributeList = Attribute
694 | Attribute "," NonEmptyAttributeList
695 Attribute = Name
696 | Name "=" Name
697 | Name "=" Literal
698
699 Struct = AttributeSection "struct" Name "{" StructBody "}" ";"
700 | AttributeSection "struct" Name ";"
701 StructBody = <empty>
702 | StructBody Const
703 | StructBody Enum
704 | StructBody StructField
705 StructField = AttributeSection TypeSpec Name Orginal Default ";"
706
707 Union = AttributeSection "union" Name "{" UnionBody "}" ";"
708 UnionBody = <empty> | UnionBody UnionField
709 UnionField = AttributeSection TypeSpec Name Ordinal ";"
710
711 Interface = AttributeSection "interface" Name "{" InterfaceBody "}" ";"
712 InterfaceBody = <empty>
713 | InterfaceBody Const
714 | InterfaceBody Enum
715 | InterfaceBody Method
716 Method = AttributeSection Name Ordinal "(" ParamterList ")" Response ";"
717 ParameterList = <empty> | NonEmptyParameterList
718 NonEmptyParameterList = Parameter
719 | Parameter "," NonEmptyParameterList
720 Parameter = AttributeSection TypeSpec Name Ordinal
721 Response = <empty> | "=>" "(" ParameterList ")"
722
723 TypeSpec = TypeName "?" | TypeName
724 TypeName = BasicTypeName
725 | Array
726 | FixedArray
727 | Map
728 | InterfaceRequest
729 BasicTypeName = Identifier | "associated" Identifier | HandleType | NumericType
730 NumericType = "bool" | "int8" | "uint8" | "int16" | "uint16" | "int32"
731 | "uint32" | "int64" | "uint64" | "float" | "double"
732 HandleType = "handle" | "handle" "<" SpecificHandleType ">"
733 SpecificHandleType = "message_pipe"
734 | "shared_buffer"
735 | "data_pipe_consumer"
736 | "data_pipe_producer"
737 Array = "array" "<" TypeSpec ">"
738 FixedArray = "array" "<" TypeSpec "," IntConstDec ">"
739 Map = "map" "<" Identifier "," TypeSpec ">"
740 InterfaceRequest = Identifier "&" | "associated" Identifier "&"
741
742 Ordinal = <empty> | OrdinalValue
743
744 Default = <empty> | "=" Constant
745
746 Enum = AttributeSection "enum" Name "{" NonEmptyEnumValueList "}" ";"
747 | AttributeSection "enum" Name "{" NonEmptyEnumValueList "," "}" ";"
748 NonEmptyEnumValueList = EnumValue | NonEmptyEnumValueList "," EnumValue
749 EnumValue = AttributeSection Name
750 | AttributeSection Name "=" Integer
751 | AttributeSection Name "=" Identifier
752
753 Const = "const" TypeSpec Name "=" Constant ";"
754
755 Constant = Literal | Identifier ";"
756
757 Identifier = Name | Name "." Identifier
758
759 Literal = Integer | Float | "true" | "false" | "default" | StringLiteral
760
761 Integer = IntConst | "+" IntConst | "-" IntConst
762 IntConst = IntConstDec | IntConstHex
763
764 Float = FloatConst | "+" FloatConst | "-" FloatConst
765
766 ; The rules below are for tokens matched strictly according to the given regexes
767
768 Identifier = /[a-zA-Z_][0-9a-zA-Z_]*/
769 IntConstDec = /0|(1-9[0-9]*)/
770 IntConstHex = /0[xX][0-9a-fA-F]+/
771 OrdinalValue = /@(0|(1-9[0-9]*))/
772 FloatConst = ... # Imagine it's close enough to C-style float syntax.
773 StringLiteral = ... # Imagine it's close enough to C-style string literals, including escapes.
774 ```
775
776 ## Additional Documentation
777
778 [Mojom Message Format](https://docs.google.com/document/d/13pv9cFh5YKuBggDBQ1-AL8VReF-IYpFOFpRfvWFrwio/edit)
779 : Describes the wire format used by Mojo bindings interfaces over message
780 pipes.
781
782 [Input Format of Mojom Message Validation Tests](https://docs.google.com/document/d/1-y-2IYctyX2NPaLxJjpJfzVNWCC2SR2MJAD9MpIytHQ/edit)
783 : Describes a text format used to facilitate bindings message validation
784 tests.
785