Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(18)

Unified Diff: mojo/public/js/validator.js

Issue 2394873003: [Mojo] Make javascript enums extensible. (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: mojo/public/js/validator.js
diff --git a/mojo/public/js/validator.js b/mojo/public/js/validator.js
index cbf7521b5c3bfa4717a500ee53836342d4a6fc5b..48e36c61d224b2b8064af45b011014e6769a2865 100644
--- a/mojo/public/js/validator.js
+++ b/mojo/public/js/validator.js
@@ -24,10 +24,15 @@ define("mojo/public/js/validator", [
'VALIDATION_ERROR_DIFFERENT_SIZED_ARRAYS_IN_MAP',
INVALID_UNION_SIZE: 'VALIDATION_ERROR_INVALID_UNION_SIZE',
UNEXPECTED_NULL_UNION: 'VALIDATION_ERROR_UNEXPECTED_NULL_UNION',
+ UNKNOWN_ENUM_VALUE: 'VALIDATION_ERROR_UNKNOWN_ENUM_VALUE',
};
var NULL_MOJO_POINTER = "NULL_MOJO_POINTER";
+ function isEnumClass(cls) {
+ return cls instanceof codec.Enum;
+ }
+
function isStringClass(cls) {
return cls === codec.String || cls === codec.NullableString;
}
@@ -98,6 +103,13 @@ define("mojo/public/js/validator", [
return true;
}
+ Validator.prototype.validateEnum = function(offset, enumClass, nullable) {
+ // Note: Assumes that enums are always 32 bits! But this matches
+ // mojom::generate::pack::PackedField::GetSizeForKind, so it should be okay.
+ var value = this.message.buffer.getInt32(offset);
+ return enumClass.validate(value);
+ }
+
Validator.prototype.validateHandle = function(offset, nullable) {
var index = this.message.buffer.getUint32(offset);
@@ -347,6 +359,8 @@ define("mojo/public/js/validator", [
return this.validateArrayElements(
elementsOffset, numElements, elementType.cls, nullable,
expectedDimensionSizes, currentDimension + 1);
+ if (isEnumClass(elementType))
+ return this.validateEnum(elementsOffset, elementType.cls, nullable);
return validationError.NONE;
}
« no previous file with comments | « mojo/public/js/validation_unittests.js ('k') | mojo/public/tools/bindings/generators/js_templates/enum_definition.tmpl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698