PageRenderTime 33ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/source/stdlib/error.js

https://github.com/deadalnix/Higgs
JavaScript | 169 lines | 49 code | 20 blank | 100 comment | 3 complexity | f5373d77bbb8def06e0fafba153b2865 MD5 | raw file
  1. /* _________________________________________________________________________
  2. *
  3. * Tachyon : A Self-Hosted JavaScript Virtual Machine
  4. *
  5. *
  6. * This file is part of the Tachyon JavaScript project. Tachyon is
  7. * distributed at:
  8. * http://github.com/Tachyon-Team/Tachyon
  9. *
  10. *
  11. * Copyright (c) 2012, Universite de Montreal
  12. * All rights reserved.
  13. *
  14. * This software is licensed under the following license (Modified BSD
  15. * License):
  16. *
  17. * Redistribution and use in source and binary forms, with or without
  18. * modification, are permitted provided that the following conditions are
  19. * met:
  20. * * Redistributions of source code must retain the above copyright
  21. * notice, this list of conditions and the following disclaimer.
  22. * * Redistributions in binary form must reproduce the above copyright
  23. * notice, this list of conditions and the following disclaimer in the
  24. * documentation and/or other materials provided with the distribution.
  25. * * Neither the name of the Universite de Montreal nor the names of its
  26. * contributors may be used to endorse or promote products derived
  27. * from this software without specific prior written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  30. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  31. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  32. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL UNIVERSITE DE
  33. * MONTREAL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  34. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  35. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  36. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  37. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  38. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  39. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40. * _________________________________________________________________________
  41. */
  42. /**
  43. @fileOverview
  44. Implementation of JavaScript native error classes.
  45. @author
  46. Maxime Chevalier-Boisvert
  47. @copyright
  48. Copyright (c) 2012 Maxime Chevalier-Boisvert, All Rights Reserved
  49. */
  50. /**
  51. Function to create an error constructor function
  52. */
  53. function makeErrorCtor(errorName, protoParent)
  54. {
  55. // Get the global this value
  56. var globalThis = this;
  57. // Error constructor function
  58. function ErrorCtor(message)
  59. {
  60. if (this === globalThis)
  61. var newObj = new ErrorCtor(message);
  62. else
  63. var newObj = this;
  64. if (message !== undefined)
  65. this.message = message.toString();
  66. return newObj;
  67. }
  68. // FIXME
  69. // Create the prototype object for this error constructor
  70. //ErrorCtor.prototype = Object.create(protoParent);
  71. // Set the error name in the error prototype object
  72. ErrorCtor.prototype.name = errorName;
  73. // The default error message is the empty string
  74. ErrorCtor.prototype.message = '';
  75. // Set the prototype constructor to the error constructor
  76. ErrorCtor.prototype.constructor = ErrorCtor;
  77. // Return the new error constructor function
  78. return ErrorCtor;
  79. }
  80. /**
  81. Constructor function for error objects
  82. */
  83. var Error = makeErrorCtor(
  84. 'Error',
  85. $ir_get_obj_proto()
  86. );
  87. /**
  88. ToString function of the error prototype object
  89. */
  90. Error.prototype.toString = function ()
  91. {
  92. if (this.message === undefined)
  93. return undefined;
  94. var name = (this.name === undefined)? 'Error':this.name;
  95. return name + ': ' + this.message;
  96. };
  97. /*
  98. @class RangeError
  99. @description
  100. 15.11.6.2 RangeError
  101. Indicates a numeric value has exceeded the allowable range.
  102. */
  103. var RangeError = makeErrorCtor(
  104. 'RangeError',
  105. Error.prototype
  106. );
  107. /*
  108. @class ReferenceError
  109. @description
  110. 15.11.6.3 ReferenceError
  111. Indicate that an invalid reference value has been detected.
  112. */
  113. var ReferenceError = makeErrorCtor(
  114. 'ReferenceError',
  115. Error.prototype
  116. );
  117. /**
  118. @class SyntaxError
  119. @description
  120. 15.11.6.4 SyntaxError
  121. Indicates that a parsing error has occurred.
  122. */
  123. var SyntaxError = makeErrorCtor(
  124. 'SyntaxError',
  125. Error.prototype
  126. );
  127. /**
  128. @class TypeError
  129. @description
  130. 15.11.6.5 TypeError
  131. Indicates the actual type of an operand is different than the expected type.
  132. */
  133. var TypeError = makeErrorCtor(
  134. 'TypeError',
  135. Error.prototype
  136. );
  137. /**
  138. @class URIError
  139. @description
  140. 15.11.6.6 URIError
  141. Indicates that one of the global URI handling functions was used in a way
  142. that is incompatible with its definition.
  143. */
  144. var URIError = makeErrorCtor(
  145. 'URIError',
  146. Error.prototype
  147. );