shared-binding.scm 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. ; -*- Mode: Scheme; Syntax: Scheme; Package: Scheme; -*-
  2. ; Copyright (c) 1993-2008 by Richard Kelsey and Jonathan Rees. See file COPYING.
  3. ; Shared binding between Scheme and external C code
  4. ;----------------
  5. ; Two tables of shared bindings: those we import from the outside and those
  6. ; that we provide to the outside.
  7. (define *imported-bindings* false)
  8. (define *exported-bindings* false)
  9. ; When resuming a statically-linked image these tables are FALSE.
  10. (define (install-shared-bindings!+gc imported-bindings exported-bindings)
  11. (if (vm-eq? imported-bindings false)
  12. (begin
  13. (set! *imported-bindings* (make-hash-table+gc))
  14. (set! *exported-bindings* (make-hash-table+gc)))
  15. (begin
  16. (set! *imported-bindings* imported-bindings)
  17. (set! *exported-bindings* exported-bindings))))
  18. (define shared-binding-table-size
  19. (* hash-table-size 2))
  20. (let ((tracer (table-tracer shared-binding-next
  21. set-shared-binding-next!
  22. s48-trace-value)))
  23. (add-gc-root!
  24. (lambda ()
  25. (set! *imported-bindings* (tracer *imported-bindings*))
  26. (set! *exported-bindings* (tracer *exported-bindings*)))))
  27. ; These next two procedure are used to write the bindings tables out in images.
  28. (define (s48-exported-bindings)
  29. *exported-bindings*)
  30. (define (s48-imported-bindings)
  31. *imported-bindings*)
  32. ; Imported bindings.
  33. (define lookup-imported-binding
  34. (let* ((maker (lambda (string next key)
  35. (make-shared-binding string true unspecific-value next key)))
  36. (lookup (table-searcher shared-binding-name
  37. shared-binding-next
  38. maker)))
  39. (lambda (name key)
  40. (lookup *imported-bindings* name key))))
  41. ; Exporting bindings.
  42. (define lookup-exported-binding
  43. (let* ((maker (lambda (string next key)
  44. (make-shared-binding string false unspecific-value next key)))
  45. (lookup (table-searcher shared-binding-name
  46. shared-binding-next
  47. maker)))
  48. (lambda (name key)
  49. (lookup *exported-bindings* name key))))
  50. ; Print warnings about all imported bindings which the external code
  51. ; has not yet defined.
  52. (define (shared-binding-undefined? binding)
  53. (undefined? (shared-binding-ref binding)))
  54. (define for-each-imported-binding
  55. (let ((walker (table-while-walker shared-binding-next)))
  56. (lambda (proc)
  57. (walker proc *imported-bindings*))))
  58. (define undefine-shared-binding!
  59. (table-remover shared-binding-name
  60. shared-binding-next
  61. set-shared-binding-next!))
  62. (define (get-imported-binding name)
  63. (save-temp0! (enter-string+gc name))
  64. (let* ((key (ensure-space shared-binding-size))
  65. (name (recover-temp0!)))
  66. (lookup-imported-binding name key)))
  67. ;----------------
  68. ; The following two functions are exported to C, hence the reversal of the
  69. ; export/import terminology.
  70. (define (s48-define-exported-binding name value)
  71. (save-temp0! value)
  72. (let ((name (enter-string+gc name)))
  73. (save-temp1! name)
  74. (let ((key (ensure-space shared-binding-size)))
  75. (let ((name (recover-temp1!))
  76. (value (recover-temp0!)))
  77. (shared-binding-set! (lookup-imported-binding name key)
  78. value)))))
  79. (define (s48-get-imported-binding name)
  80. (save-temp0! (enter-string+gc name))
  81. (let* ((key (ensure-space shared-binding-size))
  82. (name (recover-temp0!)))
  83. (lookup-exported-binding name key)))