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

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

Issue 2380303003: [Mojo] Make javascript enums extensible. (Closed)
Patch Set: Created 4 years, 3 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..bd0a4310253d230a1670599ee25f013c30739295 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 typeof cls.isKnownEnumValue === 'function';
+ }
+
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, nullable);
return validationError.NONE;
}

Powered by Google App Engine
This is Rietveld 408576698