PageRenderTime 157ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/js-runtime/Error.scm.in

https://code.google.com/p/js2scheme/
Autoconf | 325 lines | 288 code | 37 blank | 0 comment | 1 complexity | 9f72383e2b54c743f6b69cf4f75367fb MD5 | raw file
Possible License(s): BSD-3-Clause
  1. ;; Copyright (c) 2007-2011, Florian Loitsch
  2. ;; All rights reserved.
  3. ;;
  4. ;; Redistribution and use in source and binary forms, with or without
  5. ;; modification, are permitted provided that the following conditions are met:
  6. ;; * Redistributions of source code must retain the above copyright
  7. ;; notice, this list of conditions and the following disclaimer.
  8. ;; * Redistributions in binary form must reproduce the above copyright
  9. ;; notice, this list of conditions and the following disclaimer in the
  10. ;; documentation and/or other materials provided with the distribution.
  11. ;; * Neither the name of the <organization> nor the
  12. ;; names of its contributors may be used to endorse or promote products
  13. ;; derived from this software without specific prior written permission.
  14. ;;
  15. ;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. ;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. ;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. ;; DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  19. ;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. ;; (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. ;; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. ;; ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. ;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. ;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. (module jsre-Error
  26. (import jsre-base-object
  27. jsre-ht-object
  28. jsre-property-entry
  29. jsre-base-string)
  30. (use jsre-Function
  31. jsre-Array
  32. jsre-Math
  33. jsre-Arguments
  34. jsre-scope-object
  35. jsre-undefined
  36. jsre-Date
  37. jsre-Bool
  38. jsre-String
  39. jsre-Function
  40. jsre-Object
  41. jsre-Number
  42. jsre-conversion
  43. jsre-global-object)
  44. (export *jsg-Error*
  45. *jsg-Eval-Error*
  46. *jsg-Range-Error*
  47. *jsg-Reference-Error*
  48. *jsg-Syntax-Error*
  49. *jsg-Type-Error*
  50. *jsg-URI-Error*
  51. (Error-init)
  52. (final-class NatO-Error::Js-HT-Object)
  53. (range-error msg::@JS_STRING@ val)
  54. (type-error msg::@JS_STRING@ val)
  55. (type-procedure-error val)
  56. (undeclared-error id::@JS_STRING@)
  57. (syntax-error msg::@JS_STRING@ obj)
  58. (eval-error)
  59. (delete-error msg::@JS_STRING@)
  60. (uri-error msg::@JS_STRING@)
  61. (any->safe-string::bstring any)
  62. (error->js-exception e)))
  63. (define *jsg-Error* #unspecified)
  64. (define *js-Error-orig* (lambda () 'to-be-replaced))
  65. (define *jsg-Eval-Error* #unspecified)
  66. (define *js-Eval-Error-orig* (lambda () 'to-be-replaced))
  67. (define *jsg-Range-Error* #unspecified)
  68. (define *js-Range-Error-orig* (lambda () 'to-be-replaced))
  69. (define *jsg-Reference-Error* #unspecified)
  70. (define *js-Reference-Error-orig* (lambda () 'to-be-replaced))
  71. (define *jsg-Syntax-Error* #unspecified)
  72. (define *js-Syntax-Error-orig* (lambda () 'to-be-replaced))
  73. (define *jsg-Type-Error* #unspecified)
  74. (define *js-Type-Error-orig* (lambda () 'to-be-replaced))
  75. (define *jsg-URI-Error* #unspecified)
  76. (define *js-URI-Error-orig* (lambda () 'to-be-replaced))
  77. (define (Error-init)
  78. (define *error-prototype* #unspecified)
  79. (define (create-Error-class name native-error?)
  80. (let* ((proc (Error-lambda name))
  81. (text-repr (js-string-append (STR "function(msg) { /* native ")
  82. name
  83. (STR " */ throw 'native'; }")))
  84. (error-object (create-function-object proc
  85. (Error-new)
  86. Error-construct
  87. text-repr))
  88. (prototype (instantiate::NatO-Error
  89. (props (make-props-hashtable))
  90. ;; prototype is either object-prototype (15.11.4) or
  91. ;; the Error-prototype (15.11.7.7)
  92. (proto (if native-error?
  93. *error-prototype*
  94. (natO-object-prototype))))))
  95. (unless native-error? (set! *error-prototype* prototype))
  96. (js-property-generic-set! error-object ;; 15.11.3 / 15.11.7 assumed
  97. (STR "length")
  98. 1.0
  99. (length-attributes))
  100. (js-property-generic-set! error-object ;; 15.11.3.1 / 15.11.7.6
  101. (STR "prototype")
  102. prototype
  103. (get-Attributes dont-enum dont-delete
  104. read-only))
  105. (js-property-generic-set! prototype ;; 15.11.4.1 / 15.11.7.8
  106. (STR "constructor")
  107. proc
  108. (constructor-attributes))
  109. (js-property-generic-set! prototype ;; 15.11.4.2 / 15.11.7.9
  110. (STR "name")
  111. name
  112. (built-in-attributes))
  113. (js-property-generic-set! prototype ;; 15.11.4.3 / 15.11.7.10
  114. (STR "message")
  115. (STR "")
  116. (built-in-attributes))
  117. (unless native-error?
  118. (js-property-generic-set! prototype ;; 15.11.4.4
  119. (STR "toString")
  120. (toString)
  121. (built-in-attributes)))
  122. proc))
  123. ;; 15.11
  124. (set! *js-Error-orig* (create-Error-class (STR "Error") #f))
  125. (set! *jsg-Error* (create-runtime-global (STR "Error") *js-Error-orig*))
  126. ;; 15.11.6.1
  127. (set! *js-Eval-Error-orig* (create-Error-class (STR "EvalError") #t))
  128. (set! *jsg-Eval-Error* (create-runtime-global (STR "EvalError")
  129. *js-Eval-Error-orig*))
  130. ;; 15.11.6.2
  131. (set! *js-Range-Error-orig* (create-Error-class (STR "RangeError") #t))
  132. (set! *jsg-Range-Error* (create-runtime-global (STR "RangeError")
  133. *js-Range-Error-orig*))
  134. ;; 15.11.6.3
  135. (set! *js-Reference-Error-orig* (create-Error-class (STR "ReferenceError") #t))
  136. (set! *jsg-Reference-Error*
  137. (create-runtime-global (STR "ReferenceError")
  138. *js-Reference-Error-orig*))
  139. ;; 15.11.6.4
  140. (set! *js-Syntax-Error-orig* (create-Error-class (STR "SyntaxError") #t))
  141. (set! *jsg-Syntax-Error*
  142. (create-runtime-global (STR "SyntaxError") *js-Syntax-Error-orig*))
  143. ;; 15.11.6.5
  144. (set! *js-Type-Error-orig* (create-Error-class (STR "TypeError") #t))
  145. (set! *jsg-Type-Error* (create-runtime-global (STR "TypeError")
  146. *js-Type-Error-orig*))
  147. ;; 15.11.6.6
  148. (set! *js-URI-Error-orig* (create-Error-class (STR "URIError") #t))
  149. (set! *jsg-URI-Error* (create-runtime-global (STR "URIError")
  150. *js-URI-Error-orig*)))
  151. (define-method (js-class-name::bstring o::NatO-Error)
  152. "Error")
  153. (define (Error-lambda name)
  154. ;; 15.11.1.1 / 15.11.7.1
  155. (letrec ((error-proc (js-fun-lambda #f #f #f
  156. (msg)
  157. ;; uniquization-hack
  158. (if (= 1 2) (warning name))
  159. (js-new error-proc msg))))
  160. error-proc))
  161. (define (Error-new)
  162. ;; 15.11.2.1 / 15.11.7.4
  163. (js-fun-lambda this #f #f
  164. (msg)
  165. (unless (js-undefined? msg)
  166. (js-property-set! this (STR "message")
  167. (any->safe-js-string msg)))
  168. this))
  169. (define (Error-construct::NatO-Error f-o::NatO-Function)
  170. (instantiate::NatO-Error
  171. (props (make-props-hashtable))
  172. (proto (js-property-get f-o (STR "prototype")))))
  173. (define (toString)
  174. (js-fun this #f #f (STR "Error.prototype.toString")
  175. ()
  176. (if (not (NatO-Error? this))
  177. (STR "ERROR")
  178. (js-string-append
  179. ;; (format "~a: ~a" ...)
  180. (any->safe-js-string (js-property-get this (STR "name")))
  181. (STR ": ")
  182. (any->safe-js-string (js-property-get this
  183. (STR "message")))))))
  184. (define (range-error msg val)
  185. (raise (js-new *js-Range-Error-orig*
  186. ;; (format "~a: ~a" msg (any->safe-string val)))))
  187. (js-string-append msg (STR ": ")
  188. (any->safe-js-string val)))))
  189. (define (type-error msg val)
  190. (raise (js-new *js-Type-Error-orig*
  191. ;; (format "~a: ~a" msg (any->safe-string val)))))
  192. (js-string-append msg (STR ": ")
  193. (any->safe-js-string val)))))
  194. ;; real reason for this procedure is that otherwise the js-call/etc. would
  195. ;; contain a literal string argument, thus complicating the STR handling.
  196. (define (type-procedure-error val)
  197. (type-error (STR "function expected") val))
  198. (define (undeclared-error id)
  199. ;; TODO: is undeclared-error really a reference-error?
  200. (raise (js-new *js-Reference-Error-orig* id)))
  201. (define (syntax-error msg obj)
  202. (raise (js-new *js-Syntax-Error-orig*
  203. (js-string-append msg
  204. (STR ": ")
  205. (any->safe-js-string obj)))))
  206. (define (eval-error)
  207. (raise (js-new *js-Eval-Error-orig*
  208. (STR "eval function must not be copied"))))
  209. (define (delete-error msg)
  210. (raise (js-new *js-Type-Error-orig*
  211. (js-string-append (STR "can't delete ") msg))))
  212. (define (uri-error msg)
  213. (raise (js-new *js-URI-Error-orig* msg)))
  214. (define (error->js-exception e)
  215. (cond
  216. ((&type-error? e)
  217. (js-new *js-Type-Error-orig*
  218. (js-string-append (any->safe-js-string (&error-msg e))
  219. (STR ": ")
  220. (any->safe-js-string (&error-obj e)))))
  221. ((&error? e)
  222. (js-new *js-Error-orig*
  223. ;; (format "~a ~a\n~a:\n~a\n~a"
  224. (js-string-append
  225. (utf8->js-string
  226. (format "~a ~a\n~a:\n"
  227. (&error-fname e)
  228. (&error-location e)
  229. (&error-proc e)))
  230. (any->safe-js-string (&error-msg e))
  231. (STR "\n")
  232. (any->safe-js-string (&error-obj e)))))
  233. ((&exception? e)
  234. (js-new *js-Error-orig*
  235. (STR "unknown exception")))
  236. (else e)))
  237. (define (any->safe-string any)
  238. (js-string->utf8 (any->safe-js-string any)))
  239. (define (any->safe-js-string any)
  240. (cond
  241. ((string? any)
  242. (utf8->js-string any))
  243. ((js-string? any)
  244. any)
  245. ((or (js-null? any)
  246. (js-undefined? any)
  247. (boolean? any)
  248. (flonum? any))
  249. (any->js-string any))
  250. ((Js-Arguments? any) (STR "Arguments"))
  251. ((NatO-Array? any) (STR "Array"))
  252. ((NatO-Bool? any) (if (NatO-Bool-val any)
  253. (STR "Bool<true>")
  254. (STR "Bool<false>")))
  255. ((NatO-Date? any)
  256. ;; (format "Date<~a>"
  257. (js-string-append
  258. (STR "Date<")
  259. (if (nanfl? (NatO-Date-t any))
  260. (STR "invalid")
  261. (utf8->js-string
  262. (date->string (seconds->date (flonum->elong
  263. (/fl (NatO-Date-t any) 1000.0))))))
  264. (STR ">")))
  265. ((NatO-Function? any) (STR "Function-object"))
  266. ((NatO-Math? any) (STR "Math"))
  267. ((NatO-Number? any)
  268. ;; (format "Number<~a>" (NatO-Number-value any)))
  269. (js-string-append (STR "Number<")
  270. (real->js-string (NatO-Number-value any))
  271. (STR ">")))
  272. ((NatO-String? any)
  273. ;; (format "String<~a>" (NatO-String-str any)))
  274. (js-string-append (STR "String<")
  275. (NatO-String-str any)
  276. (STR ">")))
  277. ((NatO-Error? any)
  278. (let ((name (js-property-get any (STR "name")))
  279. (msg (js-property-get any (STR "message"))))
  280. (if (and (js-string? name)
  281. (js-string? msg))
  282. (js-string-append name (STR ": ") msg)
  283. (utf8->js-string (format "Error <~a ~a>" name msg)))))
  284. ((Js-Object? any) (STR "Js-Object"))
  285. (else
  286. (utf8->js-string
  287. (with-output-to-string (lambda ()
  288. (write-circle any)))))))