wlambda 0.3.0

WLambda is an embeddable scripting language for Rust
Documentation
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
/*!
Threading API:

```
use wlambda::vval::*;

// Get some random user thread:
let global  = wlambda::prelude::create_wlamba_prelude();
let mut ctx = wlambda::compiler::EvalContext::new(global);

let mut msg_handle = wlambda::threads::MsgHandle::new();
// You may register on multiple threads, but only one thread can use it at a time.
let sender = msg_handle.sender();
sender.register_on_as(&mut ctx, "worker");

let t = std::thread::spawn(move || {
    let quit = std::rc::Rc::new(std::cell::RefCell::new(false));

    let global_t = wlambda::prelude::create_wlamba_prelude();

    let qr = quit.clone();
    global_t.borrow_mut()
        .add_func("thread:quit", move |env: &mut Env, _argc: usize| {
            *qr.borrow_mut() = true;
            Ok(VVal::Nul)
        }, Some(0), Some(0));

    let mut ctx = wlambda::compiler::EvalContext::new(global_t);

    ctx.eval("!:global X = 123");

    // msg_handle.run(&mut ctx);
    // or alternatively:

    loop {
        // Tries to handle one RPC call within 10ms.
        if let None = msg_handle.step(&mut ctx, &std::time::Duration::from_millis(10)) {
            break;
        }

        if *quit.borrow() { break; }

        // do some other work here, that is not blocking the thread indefinitely.
    }
});

// Calls the global `displayln` in the Worker thread with the supplied arguments.
ctx.eval("worker_call :displayln \"hello world from worker thread!\";").unwrap();

ctx.eval("wl:assert_eq (worker_call :wl:eval \"X\") 123;").unwrap();

sender.call("thread:quit", VVal::Nul);

t.join();

```

The alternative async messaging API, that does not provide any return values
from the Thread. However, you could theoretically generate two message handles
for a two way communication.

```
use wlambda::vval::*;

// Get some random user thread:
let global  = wlambda::prelude::create_wlamba_prelude();
let mut ctx = wlambda::compiler::EvalContext::new(global);

let mut msg_handle = wlambda::threads::MsgHandle::new();

let sender = msg_handle.sender();

// You may register on multiple threads, but only one thread can use it at a time.
sender.register_on_as(&mut ctx, "worker");

let t = std::thread::spawn(move || {
    let global_t = wlambda::prelude::create_wlamba_prelude();
    let mut ctx  = wlambda::compiler::EvalContext::new(global_t);

    // This also implicitly defines a thread:quit:
    msg_handle.run(&mut ctx);
});

sender.call("thread:quit", VVal::Nul);

t.join();

```
*/

use crate::compiler::EvalContext;
use crate::vval::*;
use std::collections::VecDeque;
use std::sync::{Arc, Mutex, Condvar};

/// The Sender sends RPC calls to the Receiver thread.
/// Any values passed by WLambda code are serialized into msgpack
/// internally and transmitted to the thread
/// in String form.
/// This means, your values must not be cyclic or contain non serializable
/// data like external handles.
///
/// The Sender also provides a method for storing a sending function
/// in the global variables for the EvalContext.
///
///```
/// let global  = wlambda::prelude::create_wlamba_prelude();
/// let mut ctx = wlambda::compiler::EvalContext::new(global);
///
/// let mut msg_handle = wlambda::threads::MsgHandle::new();
/// let sender = msg_handle.sender();
///
/// // You may register on multiple threads, but only one thread can use it at a time.
/// sender.register_on_as(&mut ctx, "worker");
///
/// // start thread here...
///```
#[derive(Debug, Clone)]
#[cfg(feature="rmp-serde")]
pub struct Sender {
    receiver: Arc<Receiver>,
}

#[cfg(feature="rmp-serde")]
impl Sender {
    fn new(receiver: Arc<Receiver>) -> Self {
        Sender { receiver }
    }

    /// Registers a call and message sending function on the supplied EvalContext.
    /// You can call this function with the global variable name
    /// of the thread function you want to call and additional
    /// arguments to that function.
    ///
    /// ```no_run
    /// let global  = wlambda::prelude::create_wlamba_prelude();
    /// let mut ctx = wlambda::compiler::EvalContext::new(global);
    ///
    /// let mut msg_handle = wlambda::threads::MsgHandle::new();
    /// let sender = msg_handle.sender();
    ///
    /// sender.register_on_as(&mut ctx, "worker");
    ///
    /// ctx.eval("worker_call :displayln \"hello world from worker thread!\";").unwrap();
    ///
    /// // _send will not wait for the call to be finished or even started.
    /// ctx.eval("worker_send :displayln \"hello world from worker thread!\";").unwrap();
    /// ```
    pub fn register_on_as(&self, ctx: &mut EvalContext, variable_name: &str) {
        let sender = self.clone();
        ctx.set_global_var(&format!("{}_call", variable_name),
            &VValFun::new_fun(
                move |env: &mut Env, argc: usize| {
                    let args =
                        if argc == 1 { VVal::Nul }
                        else {
                            let a = VVal::vec();
                            for i in 1..argc {
                                a.push(env.arg(i).clone());
                            }
                            a
                        };
                    Ok(sender.call(&env.arg(0).s_raw(), args))
                }, Some(1), None));

        let sender = self.clone();
        ctx.set_global_var(&format!("{}_send", variable_name),
            &VValFun::new_fun(
                move |env: &mut Env, argc: usize| {
                    let args =
                        if argc == 1 { VVal::Nul }
                        else {
                            let a = VVal::vec();
                            for i in 1..argc {
                                a.push(env.arg(i).clone());
                            }
                            a
                        };
                    sender.send(&env.arg(0).s_raw(), args);
                    Ok(VVal::Nul)
                }, Some(1), None));
    }

    /// Calls the global variable in the receiver thread
    /// with the given argument vector. You can pass `none` value
    /// as args too.
    pub fn call(&self, var_name: &str, args: VVal) -> VVal {
        let r = &*self.receiver;

        let mut mx = r.mx.lock().unwrap();
        {
            while mx.0 != RecvState::Open {
                mx = r.cv.wait(mx).unwrap();
            }

            mx.0 = RecvState::Call;
            mx.1 = var_name.to_string();
            mx.2 = args.to_msgpack().unwrap();
            mx.3 = false;

            r.cv.notify_all();
        }

        while mx.0 != RecvState::Return {
            mx = r.cv.wait(mx).unwrap();
        }

        let ret =
            if mx.3 {
                VVal::err_msg(&String::from_utf8(mx.2.clone()).unwrap())
            } else {
                VVal::from_msgpack(&mx.2).unwrap()
            };

        mx.0 = RecvState::Open;
        mx.1 = String::from("");
        mx.2 = String::from("").into_bytes();
        mx.3 = false;

        r.cv.notify_all();

        ret
    }

    /// With send you can asynchronously send messages in form of
    /// method calls to the receiver. The Receiver will process all
    /// received messages per `step()`. Multiple senders can send
    /// messages, while multiple Receivers can process them.
    pub fn send(&self, var_name: &str, args: VVal) {
        let r = &*self.receiver;
        let mut mx = r.mx.lock().unwrap();
        while !(mx.0 == RecvState::Open || mx.0 == RecvState::Msg) {
            mx = r.cv.wait(mx).unwrap();
        }
        mx.0 = RecvState::Msg;
        mx.4.push_back((
            var_name.to_string(),
            args.to_msgpack().unwrap()));
    }
}

#[cfg(feature="rmp-serde")]
#[derive(Debug, Copy, Clone, PartialEq)]
enum RecvState {
    Open,
    Msg,
    Call,
    Return,
}

type RecvData = (RecvState, String, Vec<u8>, bool, VecDeque<(String, Vec<u8>)>);
type RecvMutex = Mutex<RecvData>;

#[derive(Debug)]
#[cfg(feature="rmp-serde")]
pub struct Receiver {
    mx: RecvMutex,
    cv: Condvar,
}

#[cfg(feature="rmp-serde")]
impl Receiver {
    fn new() -> Arc<Self> {
        Arc::new(Receiver {
            mx: Mutex::new((
                    RecvState::Open,
                    String::from(""),
                    vec![],
                    false,
                    VecDeque::new())),
            cv: Condvar::new(),
        })
    }
}

#[cfg(feature="rmp-serde")]
fn mx_recv_error(mx: &mut RecvData, s: &str) {
    mx.0 = RecvState::Return;
    mx.2 =
        VVal::err_msg(&format!("return value serialization error: {}", s))
        .to_msgpack()
        .unwrap();
    mx.3 = true;
}

#[cfg(feature="rmp-serde")]
fn mx_return(mx: &mut RecvData, v: &VVal) {
    mx.0 = RecvState::Return;
    match v.to_msgpack() {
        Ok(s) => {
            mx.2 = s;
            mx.3 = false;
        },
        Err(s) => mx_recv_error(mx, &s),
    }
}

#[cfg(feature="rmp-serde")]
#[derive(Clone)]
pub struct MsgHandle {
    receiver: Arc<Receiver>,
}

/// This a messaging handle for providing receiver and sender handles
/// for the inter thread communication of WLambda instances.
///
/// The communication is internally done either by RPC or by async
/// message passing. The VVal or WLambda values are serialized as msgpack
/// internally for transmission to the
/// other thread. This is not a high speed interface, as serialization
/// and allocations are done.
///
/// The communication between WLambda thread instances can be done by
/// multiple senders and multiple receivers, but bear in mind, that in
/// case of RPC one RPC call will lock out all other sender and
/// receiver thread, as RPC calls are synchronous.
///
/// Pass this MsgHandle to the receiver thread or multiple threads.
/// You can clone this handle if you want to use it in multiple threads.
#[cfg(feature="rmp-serde")]
impl MsgHandle {
    pub fn new() -> Self {
        MsgHandle {
            receiver: Receiver::new(),
        }
    }

    /// Returns a Sender handle.
    pub fn sender(&self) -> Sender {
        Sender::new(self.receiver.clone())
    }

    /// Starts executing Sender requests infinitely long.
    /// A global `thread:quit` function is defined, which can be used
    /// to stop this infinitely long loop.
    pub fn run(&mut self, ctx: &mut EvalContext) {
        let quit = std::rc::Rc::new(std::cell::RefCell::new(false));

        let qr = quit.clone();
        ctx.set_global_var(
            "thread:quit",
            &VValFun::new_fun(move |_env: &mut Env, _argc: usize| {
                *qr.borrow_mut() = true;
                Ok(VVal::Nul)
            }, Some(0), Some(0)));

        loop {
            self.step(ctx, &std::time::Duration::from_secs(1));
            if *quit.borrow() { break; }
        }
    }

    /// Tries to execute a RPC call or received message if the request is
    /// received within _timeout_ duration. Returns `None` if something
    /// went wrong. Otherwise `Some(())` is returned.
    pub fn step(&mut self, ctx: &mut EvalContext, timeout: &std::time::Duration) -> Option<()> {
        let r = &*self.receiver;

        let mut mx = r.mx.lock().unwrap();
        loop {
            let state = mx.0;
            match state {
                RecvState::Call => {
                    if let Some(v) = ctx.get_global_var(&mx.1) {
                        match VVal::from_msgpack(&mx.2) {
                            Ok(args) => {
                                let arg =
                                    if args.is_none() { vec![] }
                                    else { args.to_vec() };
                                match ctx.call(&v, &arg) {
                                    Ok(vret) => {
                                        mx_return(&mut *mx, &vret);
                                    },
                                    Err(sa) => {
                                        mx_recv_error(&mut *mx,
                                            &format!("uncaught stack action: {:?}",
                                                     sa));
                                    }
                                }
                            },
                            Err(s) => {
                                mx_recv_error(&mut *mx,
                                    &format!("deserialization error: {}", s));
                            }
                        }

                    } else {
                        let gvar = mx.1.clone();
                        mx_recv_error(&mut *mx,
                            &format!("no such global variable: {}", gvar));
                    }

                    r.cv.notify_all();
                    break;
                },
                RecvState::Msg => {
                    std::mem::drop(mx);
                    loop {
                        let mut mx = r.mx.lock().unwrap();
                        if mx.4.is_empty() {
                            mx.0 = RecvState::Open;
                            break;
                        }
                        let (name, ser_val) = mx.4.pop_front().unwrap();
                        std::mem::drop(mx);

                        if let Some(v) = ctx.get_global_var(&name) {
                            if let Ok(args) = VVal::from_msgpack(&ser_val) {
                                let arg =
                                    if args.is_none() { vec![] }
                                    else { args.to_vec() };
                                ctx.call(&v, &arg).unwrap_or(VVal::Nul);
                            }
                        }
                    }
                    r.cv.notify_all();
                    break;
                },
                _ => {
                    // FIXME: As soon as Rust is not nightly anymore,
                    //        use wait_timeout_until for more precise timeout.
                    let res = r.cv.wait_timeout(mx, *timeout).unwrap();
                    mx = res.0;
                    if res.1.timed_out() {
                        break;
                    }
                }
            }
        }

        Some(())
    }
}

impl Default for MsgHandle {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    #[cfg(feature="rmp-serde")]
    #[test]
    fn check_rpc() {
        use crate::vval::*;

        // Get some random user thread:
        let global  = crate::prelude::create_wlamba_prelude();
        let mut ctx = crate::compiler::EvalContext::new(global);

        let mut msg_handle = crate::threads::MsgHandle::new();
        let sender = msg_handle.sender();
        sender.register_on_as(&mut ctx, "worker");

        let t = std::thread::spawn(move || {
            let quit = std::rc::Rc::new(std::cell::RefCell::new(false));

            let global_t = crate::prelude::create_wlamba_prelude();

            let qr = quit.clone();
            global_t.borrow_mut().add_func("thread:quit", move |_env: &mut Env, _argc: usize| {
                *qr.borrow_mut() = true;
                Ok(VVal::Nul)
            }, Some(0), Some(0));

            let mut ctx = crate::compiler::EvalContext::new(global_t);

            ctx.eval("!:global X = 123").unwrap();

            loop {
                msg_handle.step(&mut ctx, &std::time::Duration::from_secs(1));
                if *quit.borrow() { break; }
            }
        });

        ctx.eval("worker_call :displayln \"hello world from worker thread!\";").unwrap();
        ctx.eval("wl:assert_eq (worker_call :wl:eval \"X\") 123;").unwrap();

        sender.call("thread:quit", VVal::Nul);

        t.join().unwrap();
    }

    #[cfg(feature="rmp-serde")]
    #[test]
    fn check_rpc_quit() {
        use crate::vval::*;

        // Get some random user thread:
        let global  = crate::prelude::create_wlamba_prelude();
        let mut ctx = crate::compiler::EvalContext::new(global);

        let mut msg_handle = crate::threads::MsgHandle::new();
        let sender = msg_handle.sender();
        sender.register_on_as(&mut ctx, "worker");

        let t = std::thread::spawn(move || {
            let global_t = crate::prelude::create_wlamba_prelude();
            let mut ctx = crate::compiler::EvalContext::new(global_t);

            ctx.eval("!:global X = 123").unwrap();
            msg_handle.run(&mut ctx);
        });

        ctx.eval("wl:assert_eq (worker_call :wl:eval \"X\") 123;").unwrap();

        sender.call("thread:quit", VVal::Nul);

        t.join().unwrap();
    }

    #[cfg(feature="rmp-serde")]
    #[test]
    fn check_rpc_msgs() {
        use crate::vval::*;

        let r = std::sync::Arc::new(std::sync::Mutex::new(String::from("")));
        let ri = r.clone();

        // Get some random user thread:
        let global  = crate::prelude::create_wlamba_prelude();
        let mut ctx = crate::compiler::EvalContext::new(global);

        let mut msg_handle = crate::threads::MsgHandle::new();
        let sender = msg_handle.sender();
        sender.register_on_as(&mut ctx, "worker");

        let t = std::thread::spawn(move || {
            let global_t = crate::prelude::create_wlamba_prelude();
            let mut ctx = crate::compiler::EvalContext::new(global_t);

            ctx.eval(r#"
                !:global X = $[1,2,3,4];
                !:global Y = { pop X };
                !:global G = { push X (str $[_, _1]); };
                !:global H = { push X (_ + _1); };
            "#).unwrap();
            msg_handle.run(&mut ctx);

            {
                let mut i = ri.lock().unwrap();
                std::mem::replace(
                    &mut *i,
                    ctx.eval("$[pop X, pop X]").unwrap().s());
            }
        });

        sender.send("Y",           VVal::Nul);
        sender.send("Y",           VVal::Nul);
        sender.send("Y",           VVal::Nul);
        ctx.eval("worker_call :G 45 44").unwrap();
        ctx.eval("worker_send :H 11 13").unwrap();
        sender.send("thread:quit", VVal::Nul);

        std::thread::sleep(std::time::Duration::from_secs(2));

        let i = r.lock().unwrap();
        assert_eq!(*i, "$[24,\"$[45,44]\"]", "popping works");

        t.join().unwrap();
    }

    #[cfg(feature="rmp-serde")]
    #[test]
    fn check_rpc_msgs_from_eval() {
        let r = std::sync::Arc::new(std::sync::Mutex::new(0));
        let ri = r.clone();

        // Get some random user thread:
        let global  = crate::prelude::create_wlamba_prelude();
        let mut ctx = crate::compiler::EvalContext::new(global);

        let mut msg_handle = crate::threads::MsgHandle::new();
        let sender = msg_handle.sender();
        sender.register_on_as(&mut ctx, "worker");

        let t = std::thread::spawn(move || {
            let global_t = crate::prelude::create_wlamba_prelude();
            let mut ctx = crate::compiler::EvalContext::new(global_t);

            ctx.eval(r#"
                !:global X = $[13,2,3,4];
                !:global Y = { pop X };
            "#).unwrap();
            msg_handle.run(&mut ctx);

            {
                let mut i = ri.lock().unwrap();
                std::mem::replace(
                    &mut *i,
                    ctx.eval("pop X").unwrap().i());
            }
        });


        ctx.eval(r#"
            worker_send :Y;
            worker_send :Y;
            worker_send :Y;
            worker_send :thread:quit;
        "#).unwrap();

        std::thread::sleep(std::time::Duration::from_secs(2));

        let i = r.lock().unwrap();
        assert_eq!(*i, 13, "popping works");

        t.join().unwrap();
    }


    #[cfg(feature="rmp-serde")]
    #[test]
    fn check_rpc_msgs_bytes() {
        let r = std::sync::Arc::new(std::sync::Mutex::new(String::from("")));
        let ri = r.clone();

        // Get some random user thread:
        let global  = crate::prelude::create_wlamba_prelude();
        let mut ctx = crate::compiler::EvalContext::new(global);

        let mut msg_handle = crate::threads::MsgHandle::new();
        let sender = msg_handle.sender();
        sender.register_on_as(&mut ctx, "worker");

        let t = std::thread::spawn(move || {
            let global_t = crate::prelude::create_wlamba_prelude();
            let mut ctx = crate::compiler::EvalContext::new(global_t);

            ctx.eval(r#"
                !:global X = $[13,2,3,4];
                !:global Y = { .X = _; };
            "#).unwrap();
            msg_handle.run(&mut ctx);

            {
                let mut i = ri.lock().unwrap();
                std::mem::replace(
                    &mut *i, ctx.eval("X").unwrap().s());
            }
        });


        ctx.eval(r#"
            worker_send :Y $b"ABC";
            worker_send :thread:quit;
        "#).unwrap();

        std::thread::sleep(std::time::Duration::from_secs(2));

        let i = r.lock().unwrap();
        assert_eq!(*i, "$b\"ABC\"", "transmitting bytes works");

        t.join().unwrap();
    }
}