[c-interface] compact functions

fix whitespace. this module has good coverage in test-c-interface.scm.
This commit is contained in:
Christopher Lam 2019-07-28 11:22:36 +08:00
parent 887f7fac32
commit 66511f17bb

View File

@ -20,30 +20,25 @@
(captured-error #f) (captured-error #f)
(result #f)) (result #f))
(catch #t (catch #t
(lambda () (lambda ()
;; Execute the code in which ;; Execute the code in which you want to catch errors here.
;; you want to catch errors here. (cond
(if (procedure? cmd) ((procedure? cmd) (set! result (apply cmd args)))
(set! result (apply cmd args))) ((string? cmd) (set! result (eval-string cmd)))))
(if (string? cmd) (lambda (key . parameters)
(set! result (eval-string cmd))) ;; Put the code which you want to handle an error after the
) ;; stack has been unwound here.
(lambda (key . parameters) (set! captured-error
;; Put the code which you want (call-with-output-string
;; to handle an error after the (lambda (port)
;; stack has been unwound here. (display-backtrace captured-stack port)
(let* ((str-port (open-output-string))) (newline port)
(display-backtrace captured-stack str-port) (print-exception port #f key parameters)))))
(display "\n" str-port) (lambda (key . parameters)
(print-exception str-port #f key parameters) ;; Capture the stack here, cut the last 3 frames which are
(set! captured-error (get-output-string str-port)))) ;; make-stack, this one, and the throw handler.
(lambda (key . parameters) (set! captured-stack (make-stack #t 3))))
;; Capture the stack here, cut the last 3 frames which are (list result captured-error)))
;; make-stack, this one, and the throw handler.
(set! captured-stack (make-stack #t 3))))
(list result captured-error)
))
;; gnc:eval-string-with-error-handling will evaluate the input string (cmd) ;; gnc:eval-string-with-error-handling will evaluate the input string (cmd)
;; an captures any exception that would be generated. It returns ;; an captures any exception that would be generated. It returns
@ -53,7 +48,7 @@
;; We'll use this to wrap guile calls in C(++), allowing ;; We'll use this to wrap guile calls in C(++), allowing
;; the C(++) code to decide how to handle the errors. ;; the C(++) code to decide how to handle the errors.
(define (gnc:eval-string-with-error-handling cmd) (define (gnc:eval-string-with-error-handling cmd)
(gnc:call-with-error-handling cmd '())) (gnc:call-with-error-handling cmd '()))
;; gnc:apply-with-error-handling will call guile's apply to run func with args ;; gnc:apply-with-error-handling will call guile's apply to run func with args
;; an captures any exception that would be generated. It returns ;; an captures any exception that would be generated. It returns
@ -63,33 +58,28 @@
;; We'll use this to wrap guile calls in C(++), allowing ;; We'll use this to wrap guile calls in C(++), allowing
;; the C(++) code to decide how to handle the errors. ;; the C(++) code to decide how to handle the errors.
(define (gnc:apply-with-error-handling func args) (define (gnc:apply-with-error-handling func args)
(gnc:call-with-error-handling func args)) (gnc:call-with-error-handling func args))
(define (gnc:backtrace-if-exception proc . args) (define (gnc:backtrace-if-exception proc . args)
(let* ((apply-result (gnc:apply-with-error-handling proc args)) (let* ((apply-result (gnc:apply-with-error-handling proc args))
(result (car apply-result)) (result (car apply-result))
(error (cadr apply-result))) (error (cadr apply-result)))
(if error (cond
(begin (error
(display error (current-error-port)) (display error (current-error-port))
(if (defined? 'gnc:warn) (when (defined? 'gnc:warn)
(gnc:warn error))) (gnc:warn error)))
result))) (else result))))
;; This database can be used to store and retrieve translatable ;; This database can be used to store and retrieve translatable
;; strings. Strings that are returned by the lookup function are ;; strings. Strings that are returned by the lookup function are
;; translated with gettext. ;; translated with gettext.
(define (gnc:make-string-database) (define (gnc:make-string-database)
(define string-hash (make-hash-table))
(define string-hash (make-hash-table 23))
(define (lookup key) (define (lookup key)
(_ (hash-ref string-hash key))) (_ (hash-ref string-hash key)))
(define (store key string) (define (store key string)
(hash-set! string-hash key string)) (hash-set! string-hash key string))
(define (dispatch message . args) (define (dispatch message . args)
(let ((func (case message (let ((func (case message
((lookup) lookup) ((lookup) lookup)
@ -98,5 +88,4 @@
(if func (if func
(apply func args) (apply func args)
(gnc:warn "string-database: bad message" message "\n")))) (gnc:warn "string-database: bad message" message "\n"))))
dispatch) dispatch)