lib/Object/PadX/Enum.xs

filter: Statementstmt 95.7% 89 / 93 Branchbran 62.5% 20 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 87.2% 109 / 125 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Object/PadX/Enum.pm ↑ Summary
1 /* Object::PadX::Enum
2 *
3 * Thin XS layer registering two keywords (`enum`, `item`) via XS::Parse::Keyword.
4 * All non-trivial work happens in Object::PadX::Enum (the .pm) via the
5 * documented Object::Pad::MOP::Class API.
6 *
7 * Pattern reference: Object-Pad-0.825/lib/Object/Pad.xs:406-625 (build_classlike)
8 */
9  
10 #define PERL_NO_GET_CONTEXT
11 #include "EXTERN.h"
12 #include "perl.h"
13 #include "XSUB.h"
14  
15 #include "XSParseKeyword.h"
16  
17 /* lex_consume_unichar is not part of the stable lexer-API export surface in
18 * all perls; provide a tiny local equivalent. Same shim as
19 * Object-Pad-0.825/hax/perl-additions.c.inc.
20 */
21 44 static bool S_lex_consume_unichar(pTHX_ U32 c)
22 {
23 44 ▶ if (lex_peek_unichar(0) != c)
Branch: 1 branch
BranchTrueFalseTotal
if (lex_peek_unichar(0) != c) 0 44 44
24 0 return FALSE;
25 44 lex_read_unichar(0);
26 44 return TRUE;
27 }
28 #define lex_consume_unichar(c) S_lex_consume_unichar(aTHX_ (c))
29  
30 /* Single-keyword compile-time state. We do not support nested enums; a saved
31 * snapshot at entry makes accidental nesting visible as a parse-time error
32 * via `item`'s check hook rather than as silent corruption.
33 */
34 static int inside_enum_depth = 0;
35 static SV *current_enum_classname = NULL;
36  
37 /* Construct an OP tree that, at runtime, calls a named fully-qualified Perl
38 * sub with the given pre-built list of argument OPs.
39 *
40 * `args_list` must be an OP_LIST op (newLISTOP(OP_LIST, ...)). Ownership of
41 * `args_list` is transferred to the returned op.
42 */
43 57 static OP *S_make_call_op(pTHX_ const char *subname, OP *args_list)
44 {
45 57 GV *gv = gv_fetchpv(subname, GV_ADD, SVt_PVCV);
46 57 OP *cv_ref = newCVREF(0, newGVOP(OP_GV, 0, gv));
47  
48 57 args_list = op_append_elem(OP_LIST, args_list, cv_ref);
49 57 return newUNOP(OP_ENTERSUB, OPf_STACKED, args_list);
50 }
51 #define make_call_op(name, list) S_make_call_op(aTHX_ (name), (list))
52  
53 /* --------------------------------------------------------------------- */
54 /* `enum NAME { BODY }` */
55 /* --------------------------------------------------------------------- */
56  
57 29 static int build_enum(pTHX_ OP **out, XSParseKeywordPiece *args[], size_t nargs, void *hookdata)
58 {
59 PERL_UNUSED_ARG(hookdata);
60 PERL_UNUSED_ARG(nargs);
61  
62 29 SV *packagename = args[0]->sv;
63 29 int nattrs = args[1]->i;
64  
65 /* Snapshot prior context so a stray nested `enum` won't corrupt state. */
66 29 SV *saved_classname = current_enum_classname;
67 29 int saved_depth = inside_enum_depth;
68  
69 29 current_enum_classname = packagename;
70 29 inside_enum_depth = 1;
71  
72 /* Marshal `[name, value_or_undef]` pairs into a Perl AV ref for the helper. */
73 29 AV *attrs_av = newAV();
74 48 ▶ for (int i = 0; i < nattrs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < nattrs; i++) { 19 29 48
75 19 AV *pair = newAV();
76 19 av_push(pair, SvREFCNT_inc(args[2 + i]->attr.name));
77 19 SV *value = args[2 + i]->attr.value;
78 19 ▶ av_push(pair, value ? SvREFCNT_inc(value) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
av_push(pair, value ? SvREFCNT_inc(value) : newSV(0)); 19 0 19
79 19 av_push(attrs_av, newRV_noinc((SV *)pair));
80 }
81 29 SV *attrs_ref = sv_2mortal(newRV_noinc((SV *)attrs_av));
82  
83 /* Drive Object::Pad::MOP::Class->begin_class via the Perl helper. This
84 * sets compclassmeta, registers UNITCHECK auto-seal, and adds $ordinal.
85 */
86 {
87 29 dSP;
88 29 ENTER;
89 29 SAVETMPS;
90 29 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 29 29
91 29 ▶ XPUSHs(packagename);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(packagename); 0 29 29
92 29 ▶ XPUSHs(attrs_ref);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(attrs_ref); 0 29 29
93 29 PUTBACK;
94 29 call_pv("Object::PadX::Enum::_begin_enum", G_VOID | G_DISCARD);
95 22 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 22 22
96 22 LEAVE;
97 }
98  
99 22 lex_read_space(0);
100 22 ▶ if (!lex_consume_unichar('{'))
Branch: 1 branch
BranchTrueFalseTotal
if (!lex_consume_unichar('{')) 0 22 22
101 0 croak("Expected '{' after 'enum %" SVf "'", SVfARG(packagename));
102  
103 22 ENTER;
104  
105 /* Object::Pad's `field`/`method` keywords assert PL_curstname matches
106 * compclassmeta's name. begin_class sets compclassmeta but not the
107 * package; do that here, mirroring Pad.xs:546-555. SAVE machinery
108 * ensures restoration at the matching LEAVE.
109 */
110 22 SAVEGENERICSV(PL_curstash);
111 22 save_item(PL_curstname);
112 22 PL_curstash = (HV *)SvREFCNT_inc(gv_stashsv(packagename, GV_ADD));
113 22 sv_setsv(PL_curstname, packagename);
114  
115 /* The entire enum body is compiled into an anonymous CV and executed
116 * immediately below, BEGIN-block style, so items exist as soon as the
117 * closing brace has been parsed. start_subparse must precede the body
118 * parse so block lexicals land in the new CV's pad; the same pattern
119 * Object::Pad uses for ADJUST blocks.
120 */
121 22 I32 floor_ix = start_subparse(FALSE, 0);
122 22 I32 save_ix = block_start(TRUE);
123  
124 22 OP *body = parse_stmtseq(0);
125 22 body = block_end(save_ix, body);
126  
127 22 ▶ if (!lex_consume_unichar('}'))
Branch: 1 branch
BranchTrueFalseTotal
if (!lex_consume_unichar('}')) 0 22 22
128 0 croak("Expected '}' at end of 'enum %" SVf "' body", SVfARG(packagename));
129  
130 22 inside_enum_depth = saved_depth;
131 22 current_enum_classname = saved_classname;
132  
133 /* Trailing call: Object::PadX::Enum::_finalize_enum("NAME"); */
134 22 OP *finalize_args = newLISTOP(OP_LIST, 0, NULL, NULL);
135 22 finalize_args = op_append_elem(OP_LIST, finalize_args,
136 newSVOP(OP_CONST, 0, SvREFCNT_inc(packagename)));
137  
138 22 OP *finalize_call = make_call_op("Object::PadX::Enum::_finalize_enum", finalize_args);
139 22 OP *finalize_stmt = newSTATEOP(0, NULL, finalize_call);
140  
141 /* body may be NULL for an empty enum block. */
142 22 OP *combined = body
143 22 ? op_append_elem(OP_LINESEQ, body, finalize_stmt)
144 22 ▶ : finalize_stmt;
Branch: 1 branch
BranchTrueFalseTotal
: finalize_stmt; 22 0 22
145  
146 22 CV *bodycv = newATTRSUB(floor_ix, NULL, NULL, NULL, combined);
147  
148 22 LEAVE;
149  
150 /* Execute the body now, at compile time. Item args are evaluated here;
151 * _finalize_enum seals the class, constructs the singletons and installs
152 * the constant accessors. Any croak becomes a compile-time error.
153 */
154 {
155 22 dSP;
156 22 ENTER;
157 22 SAVETMPS;
158 22 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 22 22
159 22 PUTBACK;
160 22 call_sv((SV *)bodycv, G_VOID | G_DISCARD | G_NOARGS);
161 19 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 19 19
162 19 LEAVE;
163 }
164 19 SvREFCNT_dec(bodycv);
165  
166 /* Everything already happened; emit a no-op statement. */
167 19 *out = newOP(OP_NULL, 0);
168  
169 19 return KEYWORD_PLUGIN_STMT;
170 }
171  
172 static const struct XSParseKeywordPieceType pieces_enum[] = {
173 XPK_PACKAGENAME,
174 XPK_ATTRIBUTES,
175 {0}
176 };
177  
178 static const struct XSParseKeywordHooks hooks_enum = {
179 .permit_hintkey = "Object::PadX::Enum/enum",
180 .pieces = pieces_enum,
181 .build = &build_enum,
182 };
183  
184 /* --------------------------------------------------------------------- */
185 /* `item NAME ( args, ... );` */
186 /* --------------------------------------------------------------------- */
187  
188 37 static void check_item(pTHX_ void *hookdata)
189 {
190 PERL_UNUSED_ARG(hookdata);
191  
192 37 ▶ if (!inside_enum_depth)
Branch: 1 branch
BranchTrueFalseTotal
if (!inside_enum_depth) 2 35 37
193 2 croak("'item' is only valid inside an 'enum { ... }' block");
194 35 }
195  
196 35 static int build_item(pTHX_ OP **out, XSParseKeywordPiece *args[], size_t nargs, void *hookdata)
197 {
198 PERL_UNUSED_ARG(hookdata);
199 PERL_UNUSED_ARG(nargs);
200  
201 35 SV *itemname = args[0]->sv;
202 35 int has_parens = args[1]->i;
203 35 ▶ OP *listexpr = has_parens ? args[2]->op : NULL;
Branch: 1 branch
BranchTrueFalseTotal
OP *listexpr = has_parens ? args[2]->op : NULL; 8 27 35
204 35 int line = args[0]->line;
205  
206 35 ▶ if (!current_enum_classname)
Branch: 1 branch
BranchTrueFalseTotal
if (!current_enum_classname) 0 35 35
207 0 croak("Internal error: 'item %" SVf "' has no enclosing enum class", SVfARG(itemname));
208  
209 /* Runtime call: _register_item(CLASSNAME, NAME, LINE, ARGS...); */
210 35 OP *call_args = newLISTOP(OP_LIST, 0, NULL, NULL);
211 35 call_args = op_append_elem(OP_LIST, call_args,
212 newSVOP(OP_CONST, 0, SvREFCNT_inc(current_enum_classname)));
213 35 call_args = op_append_elem(OP_LIST, call_args,
214 newSVOP(OP_CONST, 0, SvREFCNT_inc(itemname)));
215 35 call_args = op_append_elem(OP_LIST, call_args,
216 newSVOP(OP_CONST, 0, newSViv(line)));
217  
218 35 ▶ if (listexpr)
Branch: 1 branch
BranchTrueFalseTotal
if (listexpr) 8 27 35
219 8 call_args = op_append_elem(OP_LIST, call_args, listexpr);
220  
221 35 *out = make_call_op("Object::PadX::Enum::_register_item", call_args);
222 35 return KEYWORD_PLUGIN_STMT;
223 }
224  
225 static const struct XSParseKeywordPieceType pieces_item[] = {
226 XPK_IDENT,
227 XPK_PARENS_OPT(XPK_LISTEXPR),
228 XPK_AUTOSEMI,
229 {0}
230 };
231  
232 static const struct XSParseKeywordHooks hooks_item = {
233 .permit_hintkey = "Object::PadX::Enum/item",
234 .pieces = pieces_item,
235 .check = &check_item,
236 .build = &build_item,
237 };
238  
239 /* --------------------------------------------------------------------- */
240  
241 MODULE = Object::PadX::Enum PACKAGE = Object::PadX::Enum
242  
243 PROTOTYPES: DISABLE
244  
245 BOOT:
246 9 boot_xs_parse_keyword(0.48);
247 9 register_xs_parse_keyword("enum", &hooks_enum, NULL);
248 9 register_xs_parse_keyword("item", &hooks_item, NULL);
« blib/lib/Object/PadX/Enum.pm ↑ Summary