assert.js (1653B)
1 "use strict"; 2 module.exports = (function(){ 3 var AssertionError = (function() { 4 function AssertionError(a) { 5 this.constructor$(a); 6 this.message = a; 7 this.name = "AssertionError"; 8 } 9 AssertionError.prototype = new Error(); 10 AssertionError.prototype.constructor = AssertionError; 11 AssertionError.prototype.constructor$ = Error; 12 return AssertionError; 13 })(); 14 15 function getParams(args) { 16 var params = []; 17 for (var i = 0; i < args.length; ++i) params.push("arg" + i); 18 return params; 19 } 20 21 function nativeAssert(callName, args, expect) { 22 try { 23 var params = getParams(args); 24 var constructorArgs = params; 25 constructorArgs.push("return " + 26 callName + "("+ params.join(",") + ");"); 27 var fn = Function.apply(null, constructorArgs); 28 return fn.apply(null, args); 29 } catch (e) { 30 if (!(e instanceof SyntaxError)) { 31 throw e; 32 } else { 33 return expect; 34 } 35 } 36 } 37 38 return function assert(boolExpr, message) { 39 if (boolExpr === true) return; 40 41 if (typeof boolExpr === "string" && 42 boolExpr.charAt(0) === "%") { 43 var nativeCallName = boolExpr; 44 var $_len = arguments.length;var args = new Array(Math.max($_len - 2, 0)); for(var $_i = 2; $_i < $_len; ++$_i) {args[$_i - 2] = arguments[$_i];}; 45 if (nativeAssert(nativeCallName, args, message) === message) return; 46 message = (nativeCallName + " !== " + message); 47 } 48 49 var ret = new AssertionError(message); 50 if (Error.captureStackTrace) { 51 Error.captureStackTrace(ret, assert); 52 } 53 throw ret; 54 }; 55 })();