Skip to content

Commit 220b3d5

Browse files
authored
Merge pull request #4930 from unisonweb/fix/jit-misc
2 parents 1a439e4 + 7aa44c0 commit 220b3d5

File tree

7 files changed

+139
-40
lines changed

7 files changed

+139
-40
lines changed

parser-typechecker/src/Unison/Runtime/Interface.hs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import System.Directory
5555
createDirectoryIfMissing,
5656
getXdgDirectory,
5757
)
58+
import System.Environment (getArgs)
5859
import System.Exit (ExitCode (..))
5960
import System.FilePath ((<.>), (</>))
6061
import System.Process
@@ -869,6 +870,8 @@ nativeEvalInContext executable ppe ctx serv port codes base = do
869870
ensureRuntimeExists executable
870871
let cc = ccache ctx
871872
crs <- readTVarIO $ combRefs cc
873+
-- Seems a bit weird, but apparently this is how we do it
874+
args <- getArgs
872875
let bytes = serializeValue . compileValue base $ codes
873876

874877
decodeResult (Error msg) = pure . Left $ text msg
@@ -884,8 +887,14 @@ nativeEvalInContext executable ppe ctx serv port codes base = do
884887
(errs, dv) -> pure $ Right (listErrors errs, dv)
885888

886889
comm mv (sock, _) = do
887-
send sock . runPutS . putWord32be . fromIntegral $ BS.length bytes
890+
let encodeNum = runPutS . putWord32be . fromIntegral
891+
send sock . encodeNum $ BS.length bytes
888892
send sock bytes
893+
send sock . encodeNum $ length args
894+
for_ args $ \arg -> do
895+
let bs = encodeUtf8 $ pack arg
896+
send sock . encodeNum $ BS.length bs
897+
send sock bs
889898
UnliftIO.putMVar mv =<< receiveAll sock
890899

891900
callout _ _ _ ph = do

scheme-libs/racket/unison-runtime.rkt

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,28 @@
3434
unison/primops-generated
3535
unison/builtin-generated)
3636

37+
(define (grab-num port)
38+
(integer-bytes->integer (read-bytes 4 port) #f #t 0 4))
39+
3740
; Gets bytes using the expected input format. The format is simple:
3841
;
3942
; - 4 bytes indicating how many bytes follow
4043
; - the actual payload, with size matching the above
4144
(define (grab-bytes port)
42-
(let* ([size-bytes (read-bytes 4 port)]
43-
[size (integer-bytes->integer size-bytes #f #t 0 4)])
45+
(let ([size (grab-num port)])
4446
(read-bytes size port)))
4547

48+
; Gets args sent after the code payload. Format is:
49+
;
50+
; - 4 bytes indicating how many arguments
51+
; - for each argument
52+
; - 4 bytes indicating length of argument
53+
; - utf-8 bytes of that length
54+
(define (grab-args port)
55+
(let ([n (grab-num port)])
56+
(for/list ([i (range n)])
57+
(bytes->string/utf-8 (grab-bytes port)))))
58+
4659
; Reads and decodes the input. First uses `grab-bytes` to read the
4760
; payload, then uses unison functions to deserialize the `Value` that
4861
; is expected.
@@ -114,13 +127,15 @@
114127
; input. Then uses the dynamic loading machinery to add the code to
115128
; the runtime. Finally executes a specified main reference.
116129
(define (do-evaluate in out)
117-
(let-values ([(code main-ref) (decode-input in)])
130+
(let-values ([(code main-ref) (decode-input in)]
131+
[(args) (list->vector (grab-args in))])
118132
(add-runtime-code 'unison-main code)
119133
(with-handlers
120134
([exn:bug? (lambda (e) (encode-error e out))])
121135

122-
(handle [ref-exception:typelink] (eval-exn-handler out)
123-
((termlink->proc main-ref))))))
136+
(parameterize ([current-command-line-arguments args])
137+
(handle [ref-exception:typelink] (eval-exn-handler out)
138+
((termlink->proc main-ref)))))))
124139

125140
; Uses racket pretty printing machinery to instead generate a file
126141
; containing the given code, and which executes the main definition on

scheme-libs/racket/unison/core.ss

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
bytevector
3838
bytevector-append
3939

40-
directory-contents
4140
current-microseconds
4241

4342
decode-value
@@ -227,10 +226,6 @@
227226
(define (current-microseconds)
228227
(fl->fx (* 1000 (current-inexact-milliseconds))))
229228

230-
(define (directory-contents path-str)
231-
(define (extract path) (string->chunked-string (path->string path)))
232-
(map extract (directory-list (chunked-string->string path-str))))
233-
234229
(define (list-head l n)
235230
(let rec ([c l] [m n])
236231
(cond
@@ -476,19 +471,17 @@
476471
(next (fx1- i)))))))
477472

478473
(define (write-exn:bug ex port mode)
479-
(when mode
480-
(write-string "<exn:bug " port))
474+
(when mode (write-string "<exn:bug " port))
481475

482476
(let ([recur (case mode
483477
[(#t) write]
484478
[(#f) display]
485479
[else (lambda (v port) (print v port mode))])])
486-
(recur (chunked-string->string (exn:bug-msg ex)) port)
480+
(recur (exn:bug-msg ex) port)
487481
(if mode (write-string " " port) (newline port))
488482
(write-string (describe-value (exn:bug-val ex)) port))
489483

490-
(when mode
491-
(write-string ">")))
484+
(when mode (write-string ">" port)))
492485

493486
(struct exn:bug (msg val)
494487
#:constructor-name make-exn:bug

scheme-libs/racket/unison/io-handles.rkt

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
(provide
1616
unison-FOp-IO.stdHandle
17+
unison-FOp-IO.openFile.impl.v3
1718
(prefix-out
1819
builtin-IO.
1920
(combine-out
@@ -100,13 +101,23 @@
100101
ref-unit-unit)
101102
(ref-either-right char))))
102103

103-
(define-unison (getSomeBytes.impl.v1 handle bytes)
104-
(let* ([buffer (make-bytes bytes)]
104+
(define-unison (getSomeBytes.impl.v1 handle nbytes)
105+
(let* ([buffer (make-bytes nbytes)]
105106
[line (read-bytes-avail! buffer handle)])
106-
(if (eof-object? line)
107-
(ref-either-right (bytes->chunked-bytes #""))
108-
(ref-either-right (bytes->chunked-bytes buffer))
109-
)))
107+
(cond
108+
[(eof-object? line)
109+
(ref-either-right (bytes->chunked-bytes #""))]
110+
[(procedure? line)
111+
(Exception
112+
ref-iofailure:typelink
113+
"getSomeBytes.impl: special value returned"
114+
ref-unit-unit)]
115+
[else
116+
(ref-either-right
117+
(bytes->chunked-bytes
118+
(if (< line nbytes)
119+
(subbytes buffer 0 line)
120+
buffer)))])))
110121

111122
(define-unison (getBuffering.impl.v3 handle)
112123
(case (file-stream-buffer-mode handle)
@@ -194,6 +205,15 @@
194205
(ref-either-right
195206
(string->chunked-string (bytes->string/utf-8 value))))))
196207

208+
(define (unison-FOp-IO.openFile.impl.v3 fn0 mode)
209+
(define fn (chunked-string->string fn0))
210+
211+
(right (case mode
212+
[(0) (open-input-file fn)]
213+
[(1) (open-output-file fn #:exists 'truncate)]
214+
[(2) (open-output-file fn #:exists 'append)]
215+
[else (open-input-output-file fn #:exists 'can-update)])))
216+
197217
;; From https://github.com/sorawee/shlex/blob/5de06500e8c831cfc8dffb99d57a76decc02c569/main.rkt (MIT License)
198218
;; with is a port of https://github.com/python/cpython/blob/bf2f76ec0976c09de79c8827764f30e3b6fba776/Lib/shlex.py#L325
199219
(define unsafe-pattern #rx"[^a-zA-Z0-9_@%+=:,./-]")

scheme-libs/racket/unison/io.rkt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@
55
unison/data-info
66
racket/file
77
racket/flonum
8+
(only-in racket
9+
date-dst?
10+
date-time-zone-offset
11+
date*-time-zone-name)
812
(only-in unison/boot data-case define-unison)
913
(only-in
1014
rnrs/arithmetic/flonums-6
1115
flmod))
1216
(require racket/file)
1317

1418
(provide
19+
builtin-Clock.internals.systemTimeZone.v1
1520
(prefix-out
1621
unison-FOp-Clock.internals.
1722
(combine-out
@@ -35,13 +40,21 @@
3540
renameFile.impl.v3
3641
createDirectory.impl.v3
3742
removeDirectory.impl.v3
43+
directoryContents.impl.v3
3844
setCurrentDirectory.impl.v3
3945
renameDirectory.impl.v3
4046
isDirectory.impl.v3
4147
systemTime.impl.v3
4248
systemTimeMicroseconds.impl.v3
4349
createTempDirectory.impl.v3)))
4450

51+
(define (failure-result ty msg vl)
52+
(ref-either-left
53+
(ref-failure-failure
54+
ty
55+
(string->chunked-string msg)
56+
(unison-any-any vl))))
57+
4558
(define (getFileSize.impl.v3 path)
4659
(with-handlers
4760
[[exn:fail:filesystem?
@@ -81,6 +94,24 @@
8194
(current-directory (chunked-string->string path))
8295
(ref-either-right none))
8396

97+
(define-unison (directoryContents.impl.v3 path)
98+
(with-handlers
99+
[[exn:fail:filesystem?
100+
(lambda (e)
101+
(failure-result
102+
ref-iofailure:typelink
103+
(exception->string e)
104+
ref-unit-unit))]]
105+
(let* ([dirps (directory-list (chunked-string->string path))]
106+
[dirss (map path->string dirps)])
107+
(ref-either-right
108+
(vector->chunked-list
109+
(list->vector
110+
(map
111+
string->chunked-string
112+
(list* "." ".." dirss))))))))
113+
114+
84115
(define-unison (createTempDirectory.impl.v3 prefix)
85116
(ref-either-right
86117
(string->chunked-string
@@ -117,6 +148,14 @@
117148
(define-unison (systemTimeMicroseconds.impl.v3 unit)
118149
(ref-either-right (inexact->exact (* 1000 (current-inexact-milliseconds)))))
119150

151+
(define-unison (builtin-Clock.internals.systemTimeZone.v1 secs)
152+
(let* ([d (seconds->date secs)])
153+
(list->unison-tuple
154+
(list
155+
(date-time-zone-offset d)
156+
(if (date-dst? d) 1 0)
157+
(date*-time-zone-name d)))))
158+
120159
(define (threadCPUTime.v1)
121160
(right
122161
(integer->time

scheme-libs/racket/unison/primops-generated.rkt

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,24 @@
305305
(match v
306306
[(unison-data _ t (list rf rt bs0))
307307
#:when (= t ref-value-data:tag)
308-
(let ([bs (map reify-value (chunked-list->list bs0))])
309-
(make-data (reference->typelink rf) rt bs))]
308+
(let ([bs (map reify-value (chunked-list->list bs0))]
309+
[tl (reference->typelink rf)])
310+
(cond
311+
[(eqv? tl builtin-boolean:typelink)
312+
(cond
313+
[(not (null? bs))
314+
(raise
315+
(make-exn:bug
316+
"reify-value: boolean with arguments"
317+
bs0))]
318+
[(= rt 0) #f]
319+
[(= rt 1) #t]
320+
[else
321+
(raise
322+
(make-exn:bug
323+
"reify-value: unknown boolean tag"
324+
rt))])]
325+
[else (make-data tl rt bs)]))]
310326
[(unison-data _ t (list gr bs0))
311327
#:when (= t ref-value-partial:tag)
312328
(let ([bs (map reify-value (chunked-list->list bs0))]
@@ -317,11 +333,18 @@
317333
(reify-vlit vl)]
318334
[(unison-data _ t (list bs0 k))
319335
#:when (= t ref-value-cont:tag)
320-
(raise "reify-value: unimplemented cont case")]
336+
(raise
337+
(make-exn:bug
338+
"reify-value: unimplemented cont case"
339+
ref-unit-unit))]
321340
[(unison-data r t fs)
322-
(raise "reify-value: unimplemented data case")]
341+
(raise
342+
(make-exn:bug
343+
"reify-value: unrecognized tag"
344+
ref-unit-unit))]
323345
[else
324-
(raise (format "reify-value: unknown tag"))]))
346+
(raise
347+
(make-exn:bug "reify-value: unrecognized value" v))]))
325348

326349
(define (reflect-typelink tl)
327350
(match tl
@@ -355,6 +378,11 @@
355378

356379
(define (reflect-value v)
357380
(match v
381+
[(? boolean?)
382+
(ref-value-data
383+
(reflect-typelink builtin-boolean:typelink)
384+
(if v 1 0) ; boolean pseudo-data tags
385+
empty-chunked-list)]
358386
[(? exact-nonnegative-integer?)
359387
(ref-value-vlit (ref-vlit-pos v))]
360388
[(? exact-integer?)

scheme-libs/racket/unison/primops.ss

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -337,13 +337,16 @@
337337
unison-FOp-Clock.internals.processCPUTime.v1
338338
unison-FOp-Clock.internals.realtime.v1
339339
unison-FOp-Clock.internals.monotonic.v1
340+
builtin-Clock.internals.systemTimeZone.v1
341+
builtin-Clock.internals.systemTimeZone.v1:termlink
340342

341343

342344
; unison-FOp-Value.serialize
343345
unison-FOp-IO.stdHandle
344346
unison-FOp-IO.getArgs.impl.v1
345347

346-
unison-FOp-IO.directoryContents.impl.v3
348+
builtin-IO.directoryContents.impl.v3
349+
builtin-IO.directoryContents.impl.v3:termlink
347350
unison-FOp-IO.systemTimeMicroseconds.v1
348351

349352
unison-FOp-ImmutableArray.copyTo!
@@ -756,6 +759,7 @@
756759
(define-builtin-link IO.getEnv.impl.v1)
757760
(define-builtin-link IO.getChar.impl.v1)
758761
(define-builtin-link IO.getCurrentDirectory.impl.v3)
762+
(define-builtin-link IO.directoryContents.impl.v3)
759763
(define-builtin-link IO.removeDirectory.impl.v3)
760764
(define-builtin-link IO.renameFile.impl.v3)
761765
(define-builtin-link IO.createTempDirectory.impl.v3)
@@ -782,6 +786,7 @@
782786
(define-builtin-link Char.Class.is)
783787
(define-builtin-link Scope.bytearrayOf)
784788
(define-builtin-link unsafe.coerceAbilities)
789+
(define-builtin-link Clock.internals.systemTimeZone.v1)
785790

786791
(begin-encourage-inline
787792
(define-unison (builtin-Value.toBuiltin v) (unison-quote v))
@@ -1121,11 +1126,6 @@
11211126
(define (unison-FOp-IO.getArgs.impl.v1)
11221127
(sum 1 (cdr (command-line))))
11231128

1124-
(define (unison-FOp-IO.directoryContents.impl.v3 path)
1125-
(reify-exn
1126-
(lambda ()
1127-
(sum 1 (directory-contents path)))))
1128-
11291129
(define unison-FOp-IO.systemTimeMicroseconds.v1 current-microseconds)
11301130

11311131
;; TODO should we convert Bytes -> Text directly without the intermediate conversions?
@@ -1155,13 +1155,6 @@
11551155
(close-output-port h))
11561156
(right none))
11571157

1158-
(define (unison-FOp-IO.openFile.impl.v3 fn mode)
1159-
(right (case mode
1160-
[(0) (open-file-input-port (chunked-string->string fn))]
1161-
[(1) (open-file-output-port (chunked-string->string fn))]
1162-
[(2) (open-file-output-port (chunked-string->string fn) 'no-truncate)]
1163-
[else (open-file-input/output-port (chunked-string->string fn))])))
1164-
11651158
(define (unison-FOp-Text.repeat n t)
11661159
(let loop ([cnt 0]
11671160
[acc empty-chunked-string])
@@ -1496,6 +1489,7 @@
14961489
(declare-builtin-link builtin-IO.getArgs.impl.v1)
14971490
(declare-builtin-link builtin-IO.getEnv.impl.v1)
14981491
(declare-builtin-link builtin-IO.getChar.impl.v1)
1492+
(declare-builtin-link builtin-IO.directoryContents.impl.v3)
14991493
(declare-builtin-link builtin-IO.getCurrentDirectory.impl.v3)
15001494
(declare-builtin-link builtin-IO.removeDirectory.impl.v3)
15011495
(declare-builtin-link builtin-IO.renameFile.impl.v3)
@@ -1521,4 +1515,5 @@
15211515
(declare-builtin-link builtin-Char.Class.is)
15221516
(declare-builtin-link builtin-Pattern.many.corrected)
15231517
(declare-builtin-link builtin-unsafe.coerceAbilities)
1518+
(declare-builtin-link builtin-Clock.internals.systemTimeZone.v1)
15241519
)

0 commit comments

Comments
 (0)