mirror of
https://github.com/janet-lang/janet
synced 2024-11-19 23:24:49 +00:00
Add ev/call.
This is a common operation, and making fibers manually can be tedious.
This commit is contained in:
parent
51cf6465ff
commit
cd197e8be3
@ -1,27 +1,17 @@
|
||||
(defn handler
|
||||
"Simple handler for connections."
|
||||
[stream]
|
||||
(def id (gensym))
|
||||
(def b @"")
|
||||
(print "Connection " id "!")
|
||||
(while (:read stream 1024 b)
|
||||
(repeat 10 (print "work for " id " ...") (ev/sleep 1))
|
||||
(:write stream b)
|
||||
(buffer/clear b))
|
||||
(printf "Done %v!" id))
|
||||
|
||||
(defn spawn-kid
|
||||
"Run handler in a new fiber"
|
||||
[conn]
|
||||
(def f (fiber/new handler))
|
||||
(ev/go f conn))
|
||||
|
||||
(print "Starting echo server on 127.0.0.1:8000")
|
||||
|
||||
(def server (net/server "127.0.0.1" "8000"))
|
||||
(defer (:close stream)
|
||||
(def id (gensym))
|
||||
(def b @"")
|
||||
(print "Connection " id "!")
|
||||
(while (:read stream 1024 b)
|
||||
(repeat 10 (print "work for " id " ...") (ev/sleep 1))
|
||||
(:write stream b)
|
||||
(buffer/clear b))
|
||||
(printf "Done %v!" id)))
|
||||
|
||||
# Run server.
|
||||
(while true
|
||||
(with [conn (:accept server)]
|
||||
(spawn-kid conn)
|
||||
(ev/sleep 0.1)))
|
||||
(let [server (net/server "127.0.0.1" "8000")]
|
||||
(print "Starting echo server on 127.0.0.1:8000")
|
||||
(while true (ev/call handler (:accept server))))
|
||||
|
@ -516,6 +516,11 @@
|
||||
(with-syms [iter]
|
||||
~(do (var ,iter ,n) (while (> ,iter 0) ,;body (-- ,iter)))))
|
||||
|
||||
(defmacro forever
|
||||
"Evaluate body repeatedly forever, or until a break statement at the top level."
|
||||
[& body]
|
||||
~(while true ,;body))
|
||||
|
||||
(defmacro each
|
||||
"Loop over each value in ds. Returns nil."
|
||||
[x ds & body]
|
||||
|
@ -517,7 +517,7 @@ void janet_ev_deinit(void) {
|
||||
|
||||
/* C functions */
|
||||
|
||||
static Janet cfun_ev_spawn(int32_t argc, Janet *argv) {
|
||||
static Janet cfun_ev_go(int32_t argc, Janet *argv) {
|
||||
janet_arity(argc, 1, 2);
|
||||
JanetFiber *fiber = janet_getfiber(argv, 0);
|
||||
Janet value = argc == 2 ? argv[1] : janet_wrap_nil();
|
||||
@ -525,6 +525,14 @@ static Janet cfun_ev_spawn(int32_t argc, Janet *argv) {
|
||||
return argv[0];
|
||||
}
|
||||
|
||||
static Janet cfun_ev_call(int32_t argc, Janet *argv) {
|
||||
janet_arity(argc, 1, -1);
|
||||
JanetFunction *fn = janet_getfunction(argv, 0);
|
||||
JanetFiber *fiber = janet_fiber(fn, 64, argc - 1, argv + 1);
|
||||
janet_schedule(fiber, janet_wrap_nil());
|
||||
return janet_wrap_fiber(fiber);
|
||||
}
|
||||
|
||||
static Janet cfun_ev_sleep(int32_t argc, Janet *argv) {
|
||||
janet_fixarity(argc, 1);
|
||||
double sec = janet_getnumber(argv, 0);
|
||||
@ -537,7 +545,13 @@ static Janet cfun_ev_sleep(int32_t argc, Janet *argv) {
|
||||
|
||||
static const JanetReg ev_cfuns[] = {
|
||||
{
|
||||
"ev/go", cfun_ev_spawn,
|
||||
"ev/call", cfun_ev_call,
|
||||
JDOC("(ev/call fn & args)\n\n"
|
||||
"Call a function asynchronously. Returns a fiber that is scheduled to "
|
||||
"run the function.")
|
||||
},
|
||||
{
|
||||
"ev/go", cfun_ev_go,
|
||||
JDOC("(ev/go fiber &opt value)\n\n"
|
||||
"Put a fiber on the event loop to be resumed later. Optionally pass "
|
||||
"a value to resume with, otherwise resumes with nil.")
|
||||
|
@ -664,7 +664,7 @@ static void check_stream_flag(JanetStream *stream, int flag) {
|
||||
if (flag == JANET_STREAM_WRITABLE) msg = "writable";
|
||||
if (flag == JANET_STREAM_ACCEPTABLE) msg = "server";
|
||||
if (flag == JANET_STREAM_UDPSERVER) msg = "datagram server";
|
||||
janet_panicf("bad stream - expected %s stream", msg);
|
||||
janet_panicf("bad stream, expected %s stream", msg);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user