xs/store.xs

filter: Statementstmt 97.3% 73 / 75 Branchbran 98.3% 59 / 60 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 97.7% 132 / 135 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/store/store_watch.h ↑ Summary Store.xs »
1 # State::Store: the cell. CODE blocks that set ST(0) and return by count:
2 # a swap runs Perl, which can move the stack, and ST() reads its base afresh
3 # each time where a saved SP would not.
4  
5 MODULE = State::Store PACKAGE = State::Store
6  
7 # new($initial, %options). The options are read before the store is made,
8 # so a bad one leaves nothing behind.
9 void
10 new(klass, ...)
11 SV *klass
12 PREINIT:
13 store *s;
14 SV *obj;
15 I32 i;
16 4766 size_t limit = 0, k;
17 4766 int limited = 0;
18 4766 SV *validator = NULL, *initial;
19 4766 AV *past = NULL, *future = NULL;
20 CODE:
21 4766 ▶ if (items > 2 && (items & 1))
Branch: 2 branches
BranchTrueFalseTotal
if (items > 2 && (items & 1)) 2578 2188 4766
if (items > 2 && (items & 1)) 3 2575 2578
22 3 croak("State::Store::new: odd number of options");
23 11525 ▶ for (i = 2; i < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 2; i < items; i += 2) { 6979 4546 11525
24 6979 const char *name = SvPV_nolen(ST(i));
25 6979 ▶ if (strEQ(name, "history"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name, "history")) 2136 4843 6979
26 2136 store_limit_arg(aTHX_ ST(i + 1), &limit, &limited, "State::Store::new");
27 4843 ▶ else if (strEQ(name, "validator"))
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(name, "validator")) 221 4622 4843
28 221 validator = store_validator_arg(aTHX_ ST(i + 1), "State::Store::new");
29 4622 ▶ else if (strEQ(name, "past"))
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(name, "past")) 2516 2106 4622
30 2516 past = store_history_arg(aTHX_ ST(i + 1), "past", "State::Store::new");
31 2106 ▶ else if (strEQ(name, "future"))
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(name, "future")) 2105 1 2106
32 2105 future = store_history_arg(aTHX_ ST(i + 1), "future", "State::Store::new");
33 else
34 1 croak("State::Store::new: unknown option \"%" SVf "\"", SVfARG(ST(i)));
35 }
36 /* Every value the store will hold is checked before there is a
37 * store to refuse it: in the order of time, the past, the value,
38 * then the future, each with the one before it, and undef before
39 * the first. A value in the history is one undo or redo can make
40 * current without asking, so it is asked now or never. */
41 4546 ▶ initial = sv_2mortal(store_copy_in(aTHX_ items > 1 ? ST(1) : &PL_sv_undef));
Branch: 1 branch
BranchTrueFalseTotal
initial = sv_2mortal(store_copy_in(aTHX_ items > 1 ? ST(1) : &PL_sv_undef)); 4532 14 4546
42 4546 ▶ if (validator) {
Branch: 1 branch
BranchTrueFalseTotal
if (validator) { 216 4330 4546
43 216 SV *before = &PL_sv_undef;
44 621 ▶ for (k = 0; k < STORE_AV_COUNT(past); k++) {
Branch: 2 branches
BranchTrueFalseTotal
for (k = 0; k < STORE_AV_COUNT(past); k++) { 610 11 621
for (k = 0; k < STORE_AV_COUNT(past); k++) { 607 14 621
45 607 SV *v = AvARRAY(past)[k];
46 607 ▶ if (!store_validate(aTHX_ validator, NULL, NULL, v, before, &PL_sv_undef, NULL))
Branch: 1 branch
BranchTrueFalseTotal
if (!store_validate(aTHX_ validator, NULL, NULL, v, before, &PL_sv_undef, NULL)) 201 405 606
47 201 croak("State::Store::new: the validator refused a value of the past");
48 405 before = v;
49 }
50 14 ▶ if (!store_validate(aTHX_ validator, NULL, NULL, initial, before, &PL_sv_undef, NULL))
Branch: 1 branch
BranchTrueFalseTotal
if (!store_validate(aTHX_ validator, NULL, NULL, initial, before, &PL_sv_undef, NULL)) 2 12 14
51 2 croak("State::Store::new: the validator refused the value");
52 12 before = initial;
53 16 ▶ for (k = 0; k < STORE_AV_COUNT(future); k++) {
Branch: 2 branches
BranchTrueFalseTotal
for (k = 0; k < STORE_AV_COUNT(future); k++) { 7 9 16
for (k = 0; k < STORE_AV_COUNT(future); k++) { 5 11 16
54 5 SV *v = AvARRAY(future)[k];
55 5 ▶ if (!store_validate(aTHX_ validator, NULL, NULL, v, before, &PL_sv_undef, NULL))
Branch: 1 branch
BranchTrueFalseTotal
if (!store_validate(aTHX_ validator, NULL, NULL, v, before, &PL_sv_undef, NULL)) 1 4 5
56 1 croak("State::Store::new: the validator refused a value of the future");
57 4 before = v;
58 }
59 }
60 4341 obj = store_new(aTHX_ store_class_stash(aTHX_ klass), initial, &s);
61 4341 s->limit = limit;
62 4341 s->limited = limited;
63 4341 ▶ if (past || future)
Branch: 2 branches
BranchTrueFalseTotal
if (past || future) 2234 2107 4341
if (past || future) 1 2233 2234
64 4209 ▶ store_fill_history(aTHX_ s, STORE_AV_VALUES(past), STORE_AV_COUNT(past),
Branch: 4 branches
BranchTrueFalseTotal
store_fill_history(aTHX_ s, STORE_AV_VALUES(past), STORE_AV_COUNT(past), 2101 7 2108
store_fill_history(aTHX_ s, STORE_AV_VALUES(past), STORE_AV_COUNT(past), 2101 7 2108
store_fill_history(aTHX_ s, STORE_AV_VALUES(past), STORE_AV_COUNT(past), 2107 1 2108
store_fill_history(aTHX_ s, STORE_AV_VALUES(past), STORE_AV_COUNT(past), 2107 1 2108
65 2101 STORE_AV_VALUES(future), STORE_AV_COUNT(future),
66 "State::Store::new");
67 4341 ▶ s->validator = validator ? SvREFCNT_inc_simple_NN(validator) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
s->validator = validator ? SvREFCNT_inc_simple_NN(validator) : NULL; 11 4330 4341
68 4341 ST(0) = obj;
69 4341 XSRETURN(1);
70  
71 void
72 current(self)
73 SV *self
74 CODE:
75 114380 ST(0) = store_get(aTHX_ store_handle(aTHX_ self, "State::Store::current"));
76 114370 XSRETURN(1);
77  
78 # The store is returned, so calls chain. It is held for the length of the
79 # call: the value displaced may have a DESTROY that drops the last other
80 # reference to it.
81 void
82 set(self, value)
83 SV *self
84 SV *value
85 PREINIT:
86 store *s;
87 SV *hold;
88 CODE:
89 105184 s = store_handle(aTHX_ self, "State::Store::set");
90 105176 hold = sv_2mortal(newSVsv(self));
91 105176 ▶ if (store_put(aTHX_ s, value, hold, 1, "State::Store::set") < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (store_put(aTHX_ s, value, hold, 1, "State::Store::set") < 0) 3 105166 105169
92 3 croak("State::Store::set: the validator refused the value");
93 105166 store_deliver(aTHX_ s, hold);
94 105159 ST(0) = self;
95 105159 XSRETURN(1);
96  
97 # swap($code, @args): the value becomes what $code returns when called with
98 # the current value and @args. Called once, in scalar context. A die leaves
99 # the store as it was and goes on up.
100 void
101 swap(self, code, ...)
102 SV *self
103 SV *code
104 PREINIT:
105 store *s;
106 SV *current, *result, *hold, *err, **args;
107 I32 i;
108 CODE:
109 110797 s = store_handle(aTHX_ self, "State::Store::swap");
110 110789 SvGETMAGIC(code);
111 110789 ▶ if (!SvROK(code) || SvTYPE(SvRV(code)) != SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(code) || SvTYPE(SvRV(code)) != SVt_PVCV) 110786 3 110789
if (!SvROK(code) || SvTYPE(SvRV(code)) != SVt_PVCV) 3 110783 110786
112 6 croak("State::Store::swap: not a code reference");
113 110783 store_check_mutable(aTHX_ s, "State::Store::swap");
114 110781 hold = sv_2mortal(newSVsv(self));
115 /* The callback may set the store it is swapping, which would free
116 * the value it was handed. */
117 110781 current = sv_2mortal(SvREFCNT_inc_simple_NN(s->value));
118  
119 110781 ENTER;
120 110781 SAVETMPS;
121 110781 Newx(args, items - 1, SV *);
122 110781 SAVEFREEPV(args);
123 110781 args[0] = current;
124 110783 ▶ for (i = 2; i < items; i++) args[i - 1] = ST(i);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 2; i < items; i++) args[i - 1] = ST(i); 2 110781 110783
125 110781 err = store_call(aTHX_ code, args, items - 1, G_SCALAR, &result, NULL);
126 110781 ▶ if (err) store_rethrow(aTHX_ err);
Branch: 1 branch
BranchTrueFalseTotal
if (err) store_rethrow(aTHX_ err); 5 110776 110781
127 110776 ▶ if (store_put(aTHX_ s, result, hold, 1, "State::Store::swap") < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (store_put(aTHX_ s, result, hold, 1, "State::Store::swap") < 0) 3 110773 110776
128 3 croak("State::Store::swap: the validator refused the value");
129 110773 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 110773 0 110773
130 110773 LEAVE;
131 110773 store_deliver(aTHX_ s, hold);
132  
133 110773 ST(0) = self;
134 110773 XSRETURN(1);
135  
136 void
137 CLONE_SKIP(...)
138 CODE:
139 0 ST(0) = &PL_sv_yes;
140 0 XSRETURN(1);
« include/store/store_watch.h ↑ Summary Store.xs »