| 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 |
2237 |
| size_t limit = 0; |
| 17 |
2237 |
| int limited = 0; |
| 18 |
2237 |
| SV *validator = NULL, *initial; |
| 19 |
|
| CODE: |
| 20 |
2237 |
▶ | if (items > 2 && (items & 1)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (items > 2 && (items & 1)) |
56 |
2181 |
2237 |
| if (items > 2 && (items & 1)) |
2 |
54 |
56 |
|
| 21 |
2 |
| croak("State::Store::new: odd number of options"); |
| 22 |
2278 |
▶ | for (i = 2; i < items; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 2; i < items; i += 2) { |
54 |
2224 |
2278 |
|
| 23 |
54 |
| const char *name = SvPV_nolen(ST(i)); |
| 24 |
54 |
▶ | if (strEQ(name, "history")) |
Branch: 1 branch
| Branch | True | False | Total |
| if (strEQ(name, "history")) |
38 |
16 |
54 |
|
| 25 |
38 |
| store_limit_arg(aTHX_ ST(i + 1), &limit, &limited, "State::Store::new"); |
| 26 |
16 |
▶ | else if (strEQ(name, "validator")) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strEQ(name, "validator")) |
15 |
1 |
16 |
|
| 27 |
15 |
| validator = store_validator_arg(aTHX_ ST(i + 1), "State::Store::new"); |
| 28 |
|
| else |
| 29 |
1 |
| croak("State::Store::new: unknown option \"%" SVf "\"", SVfARG(ST(i))); |
| 30 |
|
| } |
| 31 |
|
| /* The first value is checked, with undef for the one before it, |
| 32 |
|
| * before there is a store to refuse it. */ |
| 33 |
2224 |
▶ | initial = sv_2mortal(store_copy_in(aTHX_ items > 1 ? ST(1) : &PL_sv_undef)); |
Branch: 1 branch
| Branch | True | False | Total |
| initial = sv_2mortal(store_copy_in(aTHX_ items > 1 ? ST(1) : &PL_sv_undef)); |
2210 |
14 |
2224 |
|
| 34 |
2224 |
▶ | if (validator && !store_validate(aTHX_ validator, NULL, NULL, initial, &PL_sv_undef, &PL_sv_undef, NULL)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (validator && !store_validate(aTHX_ validator, NULL, NULL, initial, &PL_sv_undef, &PL_sv_undef, NULL)) |
10 |
2214 |
2224 |
| if (validator && !store_validate(aTHX_ validator, NULL, NULL, initial, &PL_sv_undef, &PL_sv_undef, NULL)) |
1 |
9 |
10 |
|
| 35 |
1 |
| croak("State::Store::new: the validator refused the value"); |
| 36 |
2223 |
| obj = store_new(aTHX_ store_class_stash(aTHX_ klass), initial, &s); |
| 37 |
2223 |
| s->limit = limit; |
| 38 |
2223 |
| s->limited = limited; |
| 39 |
2223 |
▶ | s->validator = validator ? SvREFCNT_inc_simple_NN(validator) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| s->validator = validator ? SvREFCNT_inc_simple_NN(validator) : NULL; |
9 |
2214 |
2223 |
|
| 40 |
2223 |
| ST(0) = obj; |
| 41 |
2223 |
| XSRETURN(1); |
| 42 |
|
| |
| 43 |
|
| void |
| 44 |
|
| current(self) |
| 45 |
|
| SV *self |
| 46 |
|
| CODE: |
| 47 |
92469 |
| ST(0) = store_get(aTHX_ store_handle(aTHX_ self, "State::Store::current")); |
| 48 |
92459 |
| XSRETURN(1); |
| 49 |
|
| |
| 50 |
|
| # The store is returned, so calls chain. It is held for the length of the |
| 51 |
|
| # call: the value displaced may have a DESTROY that drops the last other |
| 52 |
|
| # reference to it. |
| 53 |
|
| void |
| 54 |
|
| set(self, value) |
| 55 |
|
| SV *self |
| 56 |
|
| SV *value |
| 57 |
|
| PREINIT: |
| 58 |
|
| store *s; |
| 59 |
|
| SV *hold; |
| 60 |
|
| CODE: |
| 61 |
96950 |
| s = store_handle(aTHX_ self, "State::Store::set"); |
| 62 |
96942 |
| hold = sv_2mortal(newSVsv(self)); |
| 63 |
96942 |
▶ | if (store_put(aTHX_ s, value, hold, 1, "State::Store::set") < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (store_put(aTHX_ s, value, hold, 1, "State::Store::set") < 0) |
3 |
96932 |
96935 |
|
| 64 |
3 |
| croak("State::Store::set: the validator refused the value"); |
| 65 |
96932 |
| store_deliver(aTHX_ s, hold); |
| 66 |
96925 |
| ST(0) = self; |
| 67 |
96925 |
| XSRETURN(1); |
| 68 |
|
| |
| 69 |
|
| # swap($code, @args): the value becomes what $code returns when called with |
| 70 |
|
| # the current value and @args. Called once, in scalar context. A die leaves |
| 71 |
|
| # the store as it was and goes on up. |
| 72 |
|
| void |
| 73 |
|
| swap(self, code, ...) |
| 74 |
|
| SV *self |
| 75 |
|
| SV *code |
| 76 |
|
| PREINIT: |
| 77 |
|
| store *s; |
| 78 |
|
| SV *current, *result, *hold, *err, **args; |
| 79 |
|
| I32 i; |
| 80 |
|
| CODE: |
| 81 |
110797 |
| s = store_handle(aTHX_ self, "State::Store::swap"); |
| 82 |
110789 |
| SvGETMAGIC(code); |
| 83 |
110789 |
▶ | if (!SvROK(code) || SvTYPE(SvRV(code)) != SVt_PVCV) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK(code) || SvTYPE(SvRV(code)) != SVt_PVCV) |
110786 |
3 |
110789 |
| if (!SvROK(code) || SvTYPE(SvRV(code)) != SVt_PVCV) |
3 |
110783 |
110786 |
|
| 84 |
6 |
| croak("State::Store::swap: not a code reference"); |
| 85 |
110783 |
| store_check_mutable(aTHX_ s, "State::Store::swap"); |
| 86 |
110781 |
| hold = sv_2mortal(newSVsv(self)); |
| 87 |
|
| /* The callback may set the store it is swapping, which would free |
| 88 |
|
| * the value it was handed. */ |
| 89 |
110781 |
| current = sv_2mortal(SvREFCNT_inc_simple_NN(s->value)); |
| 90 |
|
| |
| 91 |
110781 |
| ENTER; |
| 92 |
110781 |
| SAVETMPS; |
| 93 |
110781 |
| Newx(args, items - 1, SV *); |
| 94 |
110781 |
| SAVEFREEPV(args); |
| 95 |
110781 |
| args[0] = current; |
| 96 |
110783 |
▶ | for (i = 2; i < items; i++) args[i - 1] = ST(i); |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 2; i < items; i++) args[i - 1] = ST(i); |
2 |
110781 |
110783 |
|
| 97 |
110781 |
| err = store_call(aTHX_ code, args, items - 1, G_SCALAR, &result, NULL); |
| 98 |
110781 |
▶ | if (err) store_rethrow(aTHX_ err); |
Branch: 1 branch
| Branch | True | False | Total |
| if (err) store_rethrow(aTHX_ err); |
5 |
110776 |
110781 |
|
| 99 |
110776 |
▶ | if (store_put(aTHX_ s, result, hold, 1, "State::Store::swap") < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (store_put(aTHX_ s, result, hold, 1, "State::Store::swap") < 0) |
3 |
110773 |
110776 |
|
| 100 |
3 |
| croak("State::Store::swap: the validator refused the value"); |
| 101 |
110773 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
110773 |
0 |
110773 |
|
| 102 |
110773 |
| LEAVE; |
| 103 |
110773 |
| store_deliver(aTHX_ s, hold); |
| 104 |
|
| |
| 105 |
110773 |
| ST(0) = self; |
| 106 |
110773 |
| XSRETURN(1); |
| 107 |
|
| |
| 108 |
|
| void |
| 109 |
|
| CLONE_SKIP(...) |
| 110 |
|
| CODE: |
| 111 |
0 |
| ST(0) = &PL_sv_yes; |
| 112 |
0 |
| XSRETURN(1); |