X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=src%2Fui%2Fwebui%2Fresources%2Fjs%2Fassert.js;h=4c8e7b051380e777075b48fcb5020df551c5d207;hb=3545e9f2671f595d2a2f3ee75ca0393b01e35ef6;hp=7b38398ed0175179cfc6efaf510b489a73bbf591;hpb=7d210d4c7e9ba36e635eabc5b5780495f8a63292;p=platform%2Fframework%2Fweb%2Fcrosswalk.git diff --git a/src/ui/webui/resources/js/assert.js b/src/ui/webui/resources/js/assert.js index 7b38398..4c8e7b0 100644 --- a/src/ui/webui/resources/js/assert.js +++ b/src/ui/webui/resources/js/assert.js @@ -7,11 +7,13 @@ */ /** - * Simple common assertion API - * @param {*} condition The condition to test. Note that this may be used to - * test whether a value is defined or not, and we don't want to force a - * cast to Boolean. - * @param {string=} opt_message A message to use in any error. + * Verify |condition| is truthy and return |condition| if so. + * @template T + * @param {T} condition A condition to check for truthiness. Note that this + * may be used to test whether a value is defined or not, and we don't want + * to force a cast to Boolean. + * @param {string=} opt_message A message to show on failure. + * @return {T} A non-null |condition|. */ function assert(condition, opt_message) { 'use strict'; @@ -21,6 +23,7 @@ function assert(condition, opt_message) { msg = msg + ': ' + opt_message; throw new Error(msg); } + return condition; } /** @@ -45,5 +48,17 @@ function assert(condition, opt_message) { * @param {string=} opt_message A message to show when this is hit. */ function assertNotReached(opt_message) { - throw new Error(opt_message || "Unreachable code hit"); + throw new Error(opt_message || 'Unreachable code hit'); +} + +/** + * @param {*} value The value to check. + * @param {function(new: T, ...)} type A user-defined constructor. + * @return {T} + * @template T + */ +function assertInstanceof(value, type) { + if (!(value instanceof type)) + throw new Error(value + ' is not a[n] ' + (type.name || typeof type)); + return value; }