PageRenderTime 55ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/js-native-error.el

http://ejacs.googlecode.com/
Emacs Lisp | 161 lines | 105 code | 35 blank | 21 comment | 3 complexity | 50dacda33f9328bcff0e89461f916b5d MD5 | raw file
  1. ;;; js-native-error: implementation of JavaScript Error type
  2. ;; Author: Steve Yegge (steve.yegge@gmail.com)
  3. ;; Version: 2008.10.30
  4. ;; Keywords: javascript languages
  5. ;; This program is free software; you can redistribute it and/or
  6. ;; modify it under the terms of the GNU General Public License as
  7. ;; published by the Free Software Foundation; either version 2 of
  8. ;; the License, or (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be
  10. ;; useful, but WITHOUT ANY WARRANTY; without even the implied
  11. ;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  12. ;; PURPOSE. See the GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public
  14. ;; License along with this program; if not, write to the Free
  15. ;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  16. ;; MA 02111-1307 USA
  17. ;;; Code:
  18. (require 'js-parse) ; for `js-error-helper'
  19. (defun js-init-native-Error (obj-proto func-proto)
  20. "ECMA 15.11 - initialize built-in native error constructors."
  21. (let ((Error (make-js-Function :proto func-proto))
  22. (error-proto (make-js-Error :proto obj-proto))) ; 15.11.4
  23. ;; 15.11.1 -- call is the same as construct
  24. (setf (js-Function-call-slot Error) 'js-Error--construct--)
  25. (setf (js-Function-construct-slot Error) 'js-Error--construct--)
  26. (js-define Error "length" 1 t t t) ; 15.11.3
  27. (js-define Error "prototype" error-proto t t t) ; 15.11.3.1
  28. ;; 15.11.4 -- Properties of the Error Prototype Object
  29. (js-define error-proto "constructor" Error nil nil t) ; 15.11.4.1
  30. (js-define error-proto "name" "Error") ; 15.11.4.2
  31. (js-define error-proto "message" "") ; 15.11.4.3
  32. (js-define-builtin error-proto "toString" ; 15.11.4.4
  33. 'js-Error-toString 'no-wrap
  34. t nil t)
  35. (js-define-builtin error-proto "toSource" ; extension
  36. 'js-Error-toSource 'no-wrap
  37. t nil t)
  38. Error))
  39. (defun js-init-native-errors (obj-proto func-proto error-proto global)
  40. "ECMA 15.11.7 - NativeError constructors"
  41. (dolist (ctor '(EvalError RangeError ReferenceError
  42. SyntaxError TypeError URIError))
  43. (let* ((ctor-obj (make-js-Function :proto func-proto))
  44. (ctor-name (symbol-name ctor))
  45. (ctor-call (intern (concat "js-" ctor-name "--construct--")))
  46. (proto (make-js-Error :proto error-proto))) ; 15.11.7.7
  47. (setf (js-Function-call-slot ctor-obj) ctor-call)
  48. (setf (js-Function-construct-slot ctor-obj) ctor-call)
  49. (setf (js-Object-proto ctor-obj) func-proto) ; 15.11.7.5
  50. (js-define ctor-obj "length" 1 t t t) ; 15.11.7.5
  51. (js-define ctor-obj "prototype" proto t t t) ; 15.11.7.6
  52. (js-define global ctor-name ctor-obj nil nil t) ; {DontEnum}
  53. (js-define proto "constructor" ctor-obj) ; 15.11.7.8
  54. (js-define proto "name" ctor-name) ; 15.11.7.9
  55. (js-define proto "message" "")))) ; 15.11.7.10
  56. (defun js-Error--construct-- (ctor args)
  57. "ECMA 15.11.2 -- the Error constructor."
  58. (funcall 'js-construct-error "Error" args))
  59. (defun js-EvalError--construct-- (ctor args)
  60. (funcall 'js-construct-error "EvalError" args))
  61. (defun js-RangeError--construct-- (ctor args)
  62. (funcall 'js-construct-error "RangeError" args))
  63. (defun js-ReferenceError--construct-- (ctor args)
  64. (funcall 'js-construct-error "ReferenceError" args))
  65. (defun js-SyntaxError--construct-- (ctor args)
  66. (funcall 'js-construct-error "SyntaxError" args))
  67. (defun js-TypeError--construct-- (ctor args)
  68. (funcall 'js-construct-error "TypeError" args))
  69. (defun js-URIError--construct-- (ctor args)
  70. (funcall 'js-construct-error "URIError" args))
  71. (defun js-construct-error (type &optional msg file line)
  72. (let ((e (funcall (intern (concat "make-js-" type)))))
  73. (setf (js-Object-proto e) (js-get
  74. (js-global-get "Error")
  75. "prototype")) ; 15.11.2.1
  76. (if msg (js-define e "message" msg)) ; 15.11.2.1
  77. (if file (js-define e "file" file)) ; nonstandard
  78. (if line (js-define e "line" line)) ; nonstandard
  79. e))
  80. (defun js-Error-toString (errobj args)
  81. "ECMA 15.11.4.4 -- implementation-defined string"
  82. (let ((msg (js-get errobj "message")))
  83. (if (and msg (not (string= msg "")))
  84. (concat (js-Error-name errobj) ": " msg)
  85. (js-Error-name errobj))))
  86. (defun js-Error-toSource (errobj args)
  87. "ECMA extension - return source for creating ERROBJ."
  88. (let ((msg (js-get errobj "message")))
  89. (concat "(new " (js-Error-name errobj) "("
  90. (if msg (js-to-source msg) "")
  91. ", \"\"))")))
  92. ;;; Error utilities
  93. (defun js-eval-error (&optional msg node lineno)
  94. "Signal a EvalError."
  95. (js-error-helper "EvalError" msg node lineno))
  96. (defun js-type-error (&optional msg node lineno)
  97. "Signal a TypeError with MSG.
  98. NODE, if specified, is the associated AST node, for line info."
  99. (js-error-helper "TypeError" msg node lineno))
  100. (defun js-range-error (&optional msg node lineno)
  101. "Signal a RangeError with MSG in FILENAME."
  102. (js-error-helper "RangeError" msg node lineno))
  103. (defun js-reference-error (&optional msg node lineno)
  104. "Signal a ReferenceError."
  105. (js-error-helper "ReferenceError" msg node lineno))
  106. (defun js-syntax-error (&optional msg node lineno)
  107. "Signal a SyntaxError."
  108. (js-error-helper "SyntaxError" msg node lineno))
  109. (defun js-uri-error (&optional msg node lineno)
  110. "Signal a URIError."
  111. (js-error-helper "URIError" msg node lineno))
  112. (defun js-error-helper (type &optional msg node lineno)
  113. (let (file line)
  114. (setq line
  115. (cond
  116. (lineno lineno)
  117. ((js-node-p node)
  118. (js-node-get node 'line))
  119. (t nil)))
  120. (setq file (if (js-node-p node)
  121. (js-node-buffer node)))
  122. (setf (js-Context-result js-current-context)
  123. (js-construct-error type msg file line))
  124. (throw 'js-THROW 'throw)))
  125. (provide 'js-native-error)
  126. ;;; js-native-error.el ends here