~chust/+junk/MzXPCOM

« back to all changes in this revision

Viewing changes to src/mzxpcom/base.ss

  • Committer: Thomas Chust
  • Date: 2009-06-21 23:47:17 UTC
  • Revision ID: chust@web.de-20090621234717-cpvhijrhmyhhmnjd
Added descriptive comments to the mzxpcom/base Scheme module.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
;;
20
20
(require scheme/contract scheme/function scheme/port)
21
21
 
 
22
;; The MzXPCOM service loads this module and imports it into the base
 
23
;; namespace during initialization.
22
24
(provide (all-from-out scheme/base))
23
25
 
 
26
;; Module identifiers for XPCOM result codes.
24
27
(define ns-result:module-xpcom 1)
25
28
(define ns-result:module-base 2)
26
29
(define ns-result:module-gfx 3)
91
94
        ns-result:module-dom-file
92
95
        ns-result:module-general)
93
96
 
 
97
;; Severity flags for XPCOM result codes.
94
98
(define ns-result:severity-success 0)
95
99
(define ns-result:severity-error 1)
96
100
 
97
101
(provide ns-result:severity-success ns-result:severity-error)
98
102
 
 
103
;; XPCOM success result code.
99
104
(define ns-ok 0)
 
105
 
100
106
(define ns-error:base #xC1F30000)
101
107
 
102
108
(provide ns-ok)
103
109
 
 
110
;; XPCOM result code checks
104
111
(define (ns-failed? result)
105
112
        (bitwise-bit-set? result 31))
106
 
 
107
113
(define (ns-succeeded? result)
108
114
        (not (bitwise-bit-set? result 31)))
109
115
 
111
117
  [ns-failed? (-> natural-number/c any)]
112
118
        [ns-succeeded? (-> natural-number/c any)])
113
119
 
 
120
;; Constructors for complex XPCOM result codes.
114
121
(define (make-ns-result severity module code)
115
122
        (bitwise-ior
116
123
                (arithmetic-shift severity 31)
117
124
                (arithmetic-shift (+ ns-result:module-base-offset module) 16)
118
125
                code))
119
 
 
120
126
(define (make-ns-success module code)
121
127
        (make-ns-result ns-result:severity-success module code))
122
 
 
123
128
(define (make-ns-error module code)
124
129
        (make-ns-result ns-result:severity-error module code))
125
130
 
132
137
        [make-ns-error (-> (integer-in 0 #x1fff) (integer-in 0 #xffff)
133
138
                                                                                         natural-number/c)])
134
139
 
 
140
;; Accessors for the parts of complex XPCOM result codes.
135
141
(define (ns-result-severity result)
136
142
        (bitwise-bit-field result 31 32))
137
 
 
138
143
(define (ns-result-module result)
139
144
  (bitwise-and (- (arithmetic-shift ns-error:no-aggregation -16)
140
145
                                                                        ns-result:module-base-offset)
141
146
                                                         #x1fff))
142
 
 
143
147
(define (ns-result-code result)
144
148
        (bitwise-bit-field result 0 16))
145
149
 
148
152
        [ns-result-module (-> natural-number/c (integer-in 0 #x1fff))]
149
153
        [ns-result-code (-> natural-number/c (integer-in 0 #xffff))])
150
154
 
 
155
;; Predefined XPCOM result codes.
151
156
(define ns-error:not-initialized
152
157
        (+ ns-error:base 1))
153
158
(define ns-error:already-initialized
301
306
        ns-success:loss-of-insignificant-data
302
307
        ns-error:illegal-during-shutdown)
303
308
 
 
309
;; An exception type intended to signal XPCOM error results from Scheme.
304
310
(define-struct (exn:fail:xpcom exn:fail)
305
311
        (ns-result)
306
312
        #:transparent)
311
317
                 [continuation-marks continuation-mark-set?]
312
318
                 [ns-result ns-failed?])))
313
319
 
 
320
;; Exception handling utilities for XPCOM <-> Scheme interaction. The
 
321
;; MzXPCOM service provides functions to send an error notification to
 
322
;; interested XPCOM components and to set the result code for a
 
323
;; currently running call from XPCOM to Scheme. These are passed to the
 
324
;; functions below to construct an uncaught exception handler and an
 
325
;; exit handler. The resulting uncaught exception handler works similar
 
326
;; to the default used by MzScheme but collects the error message in a
 
327
;; string passed to the notification function provided by MzXPCOM and
 
328
;; uses exit to abort the computation, passing an exit code potentially
 
329
;; taken from the exception itself if it is of type exn:fail:xpcom. The
 
330
;; exit handler in turn uses the result value setter provided by MzXPCOM
 
331
;; to set the given XPCOM status code or ns-error:failure if the
 
332
;; parameter given to exit is not a valid exit code. Then it calls the
 
333
;; current error escape handler to jump out of the current computation.
314
334
(define ((make-xpcom-exception-handler notify-error) v)
315
335
        (let ([message (if (exn? v)
316
336
                                                                                 (exn-message v)
324
344
                                        (parameterize ([current-output-port out] [current-error-port out])
325
345
                                                ((error-display-handler) message v)))))
326
346
                (exit ns-result)))
327
 
 
328
347
(define ((make-xpcom-exit-handler set-result!) v)
329
348
        (set-result! v)
330
349
        ((error-escape-handler)))