-
Notifications
You must be signed in to change notification settings - Fork 14
/
test-state.c
109 lines (88 loc) · 3.2 KB
/
test-state.c
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
/* ----------------------------------------------------------------------------
Copyright (c) 2016, Microsoft Research, Daan Leijen
This is free software; you can redistribute it and/or modify it under the
terms of the Apache License, Version 2.0. A copy of the License can be
found in the file "license.txt" at the root of this distribution.
-----------------------------------------------------------------------------*/
#include "libhandler.h"
#include "tests.h"
/*-----------------------------------------------------------------
Define operations
-----------------------------------------------------------------*/
LH_DEFINE_EFFECT2(state, get, put)
LH_DEFINE_OP0(state, get, int)
LH_DEFINE_VOIDOP1(state, put, int)
/*-----------------------------------------------------------------
Example programs
-----------------------------------------------------------------*/
lh_value state_counter(lh_value arg) {
unreferenced(arg);
int i;
while ((i = state_get()) > 0) {
trace_printf("counter: %i\n", i);
state_put(i-1);
}
return lh_value_int(42);
}
/*-----------------------------------------------------------------
state handler
-----------------------------------------------------------------*/
static lh_value _state_result(lh_value local, lh_value arg) {
unreferenced(local);
//trace_printf("state result: %i, %li\n", *((int*)local), (long)(x));
return arg;
}
static lh_value _state_get(lh_resume rc, lh_value local, lh_value arg) {
unreferenced(arg);
//trace_printf("state get: %i\n", *((int*)local));
return lh_tail_resume(rc, local, local);
}
static lh_value _state_put(lh_resume rc, lh_value local, lh_value arg) {
//trace_printf("state put: %i, %li\n", *((int*)local), (long)(arg));
return lh_tail_resume(rc, arg, lh_value_null);
}
static const lh_operation _state_ops[] = {
{ LH_OP_TAIL_NOOP, LH_OPTAG(state,get), &_state_get },
{ LH_OP_TAIL_NOOP, LH_OPTAG(state,put), &_state_put },
{ LH_OP_NULL, lh_op_null, NULL }
};
static const lh_handlerdef state_def = {
LH_EFFECT(state), NULL, NULL, &_state_result, _state_ops };
lh_value state_handle(lh_value(*action)(lh_value), int state0, lh_value arg) {
return lh_handle(&state_def, lh_value_int(state0), action, arg);
}
//
//void onread(uv_req req) {
// resumecont rc = (resumecont)req.data;
// lh_final_resume(rc, req);
//}
//
//lh_value async_readfilev(resumecont rc, lh_value args) {
// yieldargs* y = lh_yieldargs_value(args);
// uv_req req = malloc(sizeof(uv_req));
// req.data = rc;
// real_async_readfile(lh_ptr_value(y->args[0]), lh_int_value(y->args[1]), onread);
// return lh_value_null;
//}
//
//const char* async_readfile(const char* fname, int mode, int* err) {
// uv_req* req = lh_ptr_value( lh_yieldN(lh_op_async, lh_value_ptr(fname), lh_value_int(mode)) );
// *err = req.error;
// const char* res = req.content;
// free(req);
// return res;
//}
//
/*-----------------------------------------------------------------
testing
-----------------------------------------------------------------*/
static void run() {
lh_value res1 = state_handle(state_counter,2,lh_value_null);
test_printf("final result counter: %i\n", lh_int_value(res1));
}
void test_state()
{
test("state", run,
"final result counter: 42\n"
);
}