-
Notifications
You must be signed in to change notification settings - Fork 0
/
emscripten.scm
218 lines (160 loc) · 6.09 KB
/
emscripten.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
;;;============================================================================
;;; File: "emscripten.scm"
;;; Copyright (c) 2020 by Marc Feeley, All Rights Reserved.
;;;============================================================================
(##c-declare #<<end-of-c-declare
#include <stdio.h>
#include "emscripten.h"
end-of-c-declare
)
(##c-declare #<<end-of-c-declare
#include <stdio.h>
#include "emscripten.h"
#define SCHEME_LIBRARY_LINKER ___LNK___app__
___BEGIN_C_LINKAGE
extern ___mod_or_lnk SCHEME_LIBRARY_LINKER (___global_state_struct*);
___END_C_LINKAGE
extern void setup() {
/*
* Setup the Scheme library by calling "___setup" with appropriate
* parameters. The call to "___setup_params_reset" sets all parameters
* to their default setting.
*/
___setup_params_struct setup_params;
___setup_params_reset(&setup_params);
setup_params.version = ___VERSION;
setup_params.linker = SCHEME_LIBRARY_LINKER;
___ON_THROW(___setup(&setup_params);,);
}
extern void cleanup() {
/* Cleanup the Scheme library */
___cleanup();
}
extern void user_interrupt() {
___raise_interrupt(___INTR_USER);
}
extern void heartbeat_interrupt() {
___raise_interrupt(___INTR_HEARTBEAT);
}
extern ___SCMOBJ scheme_idle() {
#define ___NARGS 0
___BEGIN_SFUN_SCMOBJ(___GLO__23__23_idle)
___BEGIN_SFUN_BODY
___SFUN_CALL_SCMOBJ
___SFUN_SET_RESULT_SCMOBJ
___END_SFUN_BODY
___SFUN_ERROR_SCMOBJ
___SFUN_SET_RESULT_SCMOBJ
___END_SFUN_SCMOBJ
return ___result;
#undef ___NARGS
}
extern double idle() {
___processor_state ___ps = ___PSTATE;
___SCMOBJ result = ___FIX(0);
___ON_THROW(result = scheme_idle();,);
if (___FIXNUMP(result))
return -1.0; /* signal program termination */
else
return ___FLONUM_VAL(result);
}
end-of-c-declare
)
(define (##idle)
(##declare (not interrupts-enabled))
(##thread-heartbeat!)
(let ((current-processor
(macro-current-processor)))
;; check if there are runnable threads
(if (##not (##eq? (macro-btq-singleton? current-processor)
(macro-current-thread)))
;; there are runnable threads
interval-runnable
;; there are no runnable threads, so check if there are threads
;; waiting for a timeout or for a device to become ready
(let* ((next-sleeper
(macro-toq-leftmost current-processor))
(interval-sleep
(if (##eq? next-sleeper current-processor)
+inf.0
(begin
;; There is a sleeping thread, so figure out in
;; how much time it needs to wake up.
(##flmax
(##fl- (macro-thread-timeout next-sleeper)
(##current-time-point))
interval-min-wait))))
(next-condvar
(macro-btq-deq-next current-processor))
(interval-io
(if (##eq? next-condvar current-processor)
interval-no-io-pending ;; I/O is not pending, just relax
interval-io-pending))) ;; I/O is pending, so come back soon
(##flmin interval-sleep interval-io)))))
(define interval-runnable 0.0)
(set! interval-runnable 0.0)
(define interval-io-pending 0.0)
(set! interval-io-pending 0.010)
(define interval-no-io-pending 0.0)
(set! interval-no-io-pending 1.0)
(define interval-min-wait 0.0)
(set! interval-min-wait 0.0001)
;;;----------------------------------------------------------------------------
;; Remember REPL history from one run of Gambit to the next.
(define (##load-repl-history tty)
(##tty-history-set! tty (##local-storage-get "gambit-repl-history")))
(define (##save-repl-history tty)
(##local-storage-set "gambit-repl-history" (##tty-history tty)))
(define (##setup-repl-history)
(let* ((ct (macro-current-thread))
(channel (##thread-repl-channel-get! ct))
(tty (macro-repl-channel-input-port channel)))
(if (##tty? tty)
(let ((orig-read-expr
(macro-repl-channel-ports-read-expr channel)))
(##load-repl-history tty)
(macro-repl-channel-ports-read-expr-set!
channel
(lambda (channel)
(let ((result (orig-read-expr channel)))
(##save-repl-history tty)
result)))))))
;;;----------------------------------------------------------------------------
(current-directory "~")
(##module-search-order-set! '("~~lib" "~~userlib" "~"))
;;;----------------------------------------------------------------------------
;; Start the main REPL in the primordial thread, and create a service
;; thread which executes the rest of the program (returning back from
;; the C call to ___setup).
(define (##main-program-set! thunk)
(##main-set!
(lambda ()
(##continuation-capture
(lambda (cont)
(##setup-repl-history)
(##thread-start!
(make-root-thread
(lambda ()
(let ((orig-denv (macro-thread-denv (macro-current-thread))))
(macro-denv-interrupt-mask-set! orig-denv 8) ;; (expt 2 ___INTR_USER)
;; Need to take a copy of the dynamic environment because
;; ##continuation-return-no-winding will change the
;; thread's dynamic environment
(##continuation-return-no-winding
cont
(lambda (orig-denv)
;; Must disable user interrupts in service thread so
;; that the idle function won't start a REPL of its own
;; on a ctrl-c. The new interrupt mask will take
;; effect at the thread-restore! operation.
(macro-thread-save!
(lambda (thread orig-denv)
(macro-thread-denv-set! thread orig-denv)
(macro-thread-restore!
thread
(lambda ()
#f)))
orig-denv)))))))
(##thread-yield!)
(##continuation-graft cont thunk))))))
;;;============================================================================