OLD | NEW |
1 {%- set class_name = struct.name ~ "_Data" %} | 1 {%- set class_name = struct.name ~ "_Data" %} |
2 | 2 |
3 {#- TODO(yzshen): Consider eliminating _validate_object() and | 3 {#- TODO(yzshen): Consider eliminating _validate_object() and |
4 _validate_handle(). #} | 4 _validate_handle(). #} |
5 | 5 |
6 {#- Validates the specified struct field, which is supposed to be an object | 6 {#- Validates the specified struct field, which is supposed to be an object |
7 (struct/array/string/map/union). | 7 (struct/array/string/map/union). |
8 This macro is expanded by the Validate() method. #} | 8 This macro is expanded by the Validate() method. #} |
9 {%- macro _validate_object(struct, packed_field) %} | 9 {%- macro _validate_object(struct, packed_field) %} |
10 {%- set name = packed_field.field.name %} | 10 {%- set name = packed_field.field.name %} |
11 {%- set kind = packed_field.field.kind %} | 11 {%- set kind = packed_field.field.kind %} |
12 {%- set wrapper_type = kind|cpp_wrapper_type %} | 12 {%- set wrapper_type = kind|cpp_wrapper_type %} |
13 {%- if not kind|is_nullable_kind %} | 13 {%- if not kind|is_nullable_kind %} |
| 14 {%- if kind|is_union_kind %} |
| 15 if (object->{{name}}.is_null()) { |
| 16 {%- else %} |
14 if (!object->{{name}}.offset) { | 17 if (!object->{{name}}.offset) { |
| 18 {%- endif %} |
15 ReportValidationError( | 19 ReportValidationError( |
16 mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, | 20 mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, |
17 "null {{name}} field in {{struct.name}} struct"); | 21 "null {{name}} field in {{struct.name}} struct"); |
18 return false; | 22 return false; |
19 } | 23 } |
20 {%- endif %} | 24 {%- endif %} |
| 25 {%- if not kind|is_union_kind %} |
21 if (!mojo::internal::ValidateEncodedPointer(&object->{{name}}.offset)) { | 26 if (!mojo::internal::ValidateEncodedPointer(&object->{{name}}.offset)) { |
22 ReportValidationError(mojo::internal::VALIDATION_ERROR_ILLEGAL_POINTER); | 27 ReportValidationError(mojo::internal::VALIDATION_ERROR_ILLEGAL_POINTER); |
23 return false; | 28 return false; |
24 } | 29 } |
| 30 {%- endif %} |
25 {%- if kind|is_array_kind or kind|is_string_kind %} | 31 {%- if kind|is_array_kind or kind|is_string_kind %} |
26 if (!{{wrapper_type}}::Data_::Validate< | 32 if (!{{wrapper_type}}::Data_::Validate< |
27 {{kind|get_array_validate_params|indent(10)}}>( | 33 {{kind|get_array_validate_params|indent(10)}}>( |
28 mojo::internal::DecodePointerRaw(&object->{{name}}.offset), | 34 mojo::internal::DecodePointerRaw(&object->{{name}}.offset), |
29 bounds_checker)) { | 35 bounds_checker)) { |
30 {%- elif kind|is_map_kind %} | 36 {%- elif kind|is_map_kind %} |
31 if (!{{wrapper_type}}::Data_::Validate< | 37 if (!{{wrapper_type}}::Data_::Validate< |
32 {{kind.value_kind|get_map_validate_params|indent(10)}}>( | 38 {{kind.value_kind|get_map_validate_params|indent(10)}}>( |
33 mojo::internal::DecodePointerRaw(&object->{{name}}.offset), | 39 mojo::internal::DecodePointerRaw(&object->{{name}}.offset), |
34 bounds_checker)) { | 40 bounds_checker)) { |
35 {%- elif kind|is_struct_kind %} | 41 {%- elif kind|is_struct_kind %} |
36 if (!{{kind|get_name_for_kind}}::Data_::Validate( | 42 if (!{{kind|get_name_for_kind}}::Data_::Validate( |
37 mojo::internal::DecodePointerRaw(&object->{{name}}.offset), | 43 mojo::internal::DecodePointerRaw(&object->{{name}}.offset), |
38 bounds_checker)) { | 44 bounds_checker)) { |
| 45 {%- elif kind|is_union_kind %} |
| 46 if (!{{kind|get_name_for_kind}}::Data_::Validate( |
| 47 &object->{{name}}, bounds_checker, true)) { |
39 {%- else %} | 48 {%- else %} |
40 if (!{{wrapper_type}}::Data_::Validate( | 49 if (!{{wrapper_type}}::Data_::Validate( |
41 mojo::internal::DecodePointerRaw(&object->{{name}}.offset), | 50 mojo::internal::DecodePointerRaw(&object->{{name}}.offset), |
42 bounds_checker)) { | 51 bounds_checker)) { |
43 {%- endif %} | 52 {%- endif %} |
44 return false; | 53 return false; |
45 } | 54 } |
46 {%- endmacro %} | 55 {%- endmacro %} |
47 | 56 |
48 {#- Validates the specified struct field, which is supposed to be a handle. | 57 {#- Validates the specified struct field, which is supposed to be a handle. |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 {%- endif %} | 141 {%- endif %} |
133 {%- endfor %} | 142 {%- endfor %} |
134 | 143 |
135 return true; | 144 return true; |
136 } | 145 } |
137 | 146 |
138 void {{class_name}}::EncodePointersAndHandles( | 147 void {{class_name}}::EncodePointersAndHandles( |
139 std::vector<mojo::Handle>* handles) { | 148 std::vector<mojo::Handle>* handles) { |
140 MOJO_CHECK(header_.version == {{struct.versions[-1].version}}); | 149 MOJO_CHECK(header_.version == {{struct.versions[-1].version}}); |
141 {%- for pf in struct.packed.packed_fields_in_ordinal_order %} | 150 {%- for pf in struct.packed.packed_fields_in_ordinal_order %} |
142 {%- if pf.field.kind|is_object_kind %} | 151 {%- if pf.field.kind|is_object_kind and not pf.field.kind|is_union_kind %} |
143 mojo::internal::Encode(&{{pf.field.name}}, handles); | 152 mojo::internal::Encode(&{{pf.field.name}}, handles); |
144 {%- elif pf.field.kind|is_any_handle_kind %} | 153 {%- elif pf.field.kind|is_any_handle_kind %} |
145 mojo::internal::EncodeHandle(&{{pf.field.name}}, handles); | 154 mojo::internal::EncodeHandle(&{{pf.field.name}}, handles); |
146 {%- endif %} | 155 {%- endif %} |
147 {%- endfor %} | 156 {%- endfor %} |
148 } | 157 } |
149 | 158 |
150 void {{class_name}}::DecodePointersAndHandles( | 159 void {{class_name}}::DecodePointersAndHandles( |
151 std::vector<mojo::Handle>* handles) { | 160 std::vector<mojo::Handle>* handles) { |
152 // NOTE: The memory backing |this| may has be smaller than |sizeof(*this)|, if | 161 // NOTE: The memory backing |this| may has be smaller than |sizeof(*this)|, if |
153 // the message comes from an older version. | 162 // the message comes from an older version. |
154 {#- Before decoding fields introduced at a certain version, we need to add | 163 {#- Before decoding fields introduced at a certain version, we need to add |
155 a version check, which makes sure we skip further decoding if |this| | 164 a version check, which makes sure we skip further decoding if |this| |
156 is from an earlier version. |last_checked_version| records the last | 165 is from an earlier version. |last_checked_version| records the last |
157 version that we have added such version check. #} | 166 version that we have added such version check. #} |
158 {%- set last_checked_version = 0 %} | 167 {%- set last_checked_version = 0 %} |
159 {%- for pf in struct.packed.packed_fields_in_ordinal_order %} | 168 {%- for pf in struct.packed.packed_fields_in_ordinal_order %} |
160 {%- set name = pf.field.name %} | 169 {%- set name = pf.field.name %} |
161 {%- set kind = pf.field.kind %} | 170 {%- set kind = pf.field.kind %} |
162 {%- if kind|is_object_kind or kind|is_any_handle_kind %} | 171 {%- if kind|is_object_kind or kind|is_any_handle_kind %} |
163 {%- if pf.min_version > last_checked_version %} | 172 {%- if pf.min_version > last_checked_version %} |
164 {%- set last_checked_version = pf.min_version %} | 173 {%- set last_checked_version = pf.min_version %} |
165 if (header_.version < {{pf.min_version}}) | 174 if (header_.version < {{pf.min_version}}) |
166 return; | 175 return; |
167 {%- endif %} | 176 {%- endif %} |
168 {%- if kind|is_object_kind %} | 177 {%- if kind|is_union_kind %} |
| 178 // TODO(azani): Decode handles in union. |
| 179 {%- elif kind|is_object_kind %} |
169 mojo::internal::Decode(&{{name}}, handles); | 180 mojo::internal::Decode(&{{name}}, handles); |
170 {%- else %} | 181 {%- else %} |
171 mojo::internal::DecodeHandle(&{{name}}, handles); | 182 mojo::internal::DecodeHandle(&{{name}}, handles); |
172 {%- endif %} | 183 {%- endif %} |
173 {%- endif %} | 184 {%- endif %} |
174 {%- endfor %} | 185 {%- endfor %} |
175 } | 186 } |
176 | 187 |
177 {{class_name}}::{{class_name}}() { | 188 {{class_name}}::{{class_name}}() { |
178 header_.num_bytes = sizeof(*this); | 189 header_.num_bytes = sizeof(*this); |
179 header_.version = {{struct.versions[-1].version}}; | 190 header_.version = {{struct.versions[-1].version}}; |
180 } | 191 } |
OLD | NEW |