|
3 | 3 | (require "mathlink.ss" |
4 | 4 | "translation.ss" |
5 | 5 | racket/contract |
6 | | - (only-in ffi/unsafe register-finalizer) |
7 | 6 | ffi/unsafe/custodian) |
8 | 7 |
|
9 | 8 | (provide/contract (MathKernel |
10 | | - (->* () () #:rest (listof string?) any)) |
| 9 | + (-> string? ... MathLink?)) |
11 | 10 | (MathEval |
12 | 11 | (->* ((flat-rec-contract Mexp |
13 | 12 | number? boolean? symbol? string? void? eof-object? |
|
164 | 163 | "/Applications/Mathematica.app/Contents/MacOS/MathKernel -mathlink")))) |
165 | 164 | (arg |
166 | 165 | (let ((link (apply MLOpen arg))) |
167 | | - (set-MathLink-ref! link |
168 | | - (register-custodian-shutdown link MLClose #:at-exit? #t)) |
169 | | - (register-finalizer link MathExit) |
| 166 | + (register-finalizer-and-custodian-shutdown link MLClose #:at-exit? #t) |
170 | 167 | (current-mathlink link) |
171 | 168 | link)))) |
172 | 169 |
|
173 | 170 | (define (MathEval exp (link (or (current-mathlink) (MathKernel)))) |
174 | 171 | (call-with-semaphore (MathLink-sema link) |
175 | 172 | (lambda () |
176 | | - (unless (MathLink-ref link) |
177 | | - (mathlink-error "MathEval: MathLink is closed")) |
178 | 173 | (let ((lp (MathLink-lp link))) |
| 174 | + (unless lp |
| 175 | + (mathlink-error "MathEval: MathLink is closed")) |
179 | 176 | (MLPutFunction lp #"EvaluatePacket" 1) |
180 | 177 | (MLPut lp exp) |
181 | 178 | (MLEndPacket lp) |
|
186 | 183 | (() |
187 | 184 | (cond ((current-mathlink) => MathExit))) |
188 | 185 | ((link) |
189 | | - (call-with-semaphore (MathLink-sema link) |
190 | | - (lambda () |
191 | | - (when (eq? link (current-mathlink)) |
192 | | - (current-mathlink #f)) |
193 | | - (let ((ref (MathLink-ref link))) |
194 | | - (when ref |
195 | | - (set-MathLink-ref! link #f) |
196 | | - (unregister-custodian-shutdown link ref) |
197 | | - (MLClose link) |
198 | | - (set-phantom-bytes! (MathLink-phantom link) 0)))))))) |
| 186 | + (when (eq? link (current-mathlink)) |
| 187 | + (current-mathlink #f)) |
| 188 | + (MLClose link)))) |
0 commit comments