]> git.vpit.fr Git - perl/modules/re-engine-Plugin.git/blob - Plugin.xs
Don't cleanup thread local storage before END blocks are executed
[perl/modules/re-engine-Plugin.git] / Plugin.xs
1 /* This file is part of the re::engine::Plugin Perl module.
2  * See http://search.cpan.org/dist/re-engine-Plugin/ */
3
4 #define PERL_NO_GET_CONTEXT
5 #include "EXTERN.h"
6 #include "perl.h"
7 #include "XSUB.h"
8
9 #include "Plugin.h"
10
11 #define __PACKAGE__     "re::engine::Plugin"
12 #define __PACKAGE_LEN__ (sizeof(__PACKAGE__)-1)
13
14 #define REP_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
15
16 #ifndef REP_WORKAROUND_REQUIRE_PROPAGATION
17 # define REP_WORKAROUND_REQUIRE_PROPAGATION !REP_HAS_PERL(5, 10, 1)
18 #endif
19
20 /* ... Thread safety and multiplicity ...................................... */
21
22 /* Safe unless stated otherwise in Makefile.PL */
23 #ifndef REP_FORKSAFE
24 # define REP_FORKSAFE 1
25 #endif
26
27 #ifndef REP_MULTIPLICITY
28 # if defined(MULTIPLICITY) || defined(PERL_IMPLICIT_CONTEXT)
29 #  define REP_MULTIPLICITY 1
30 # else
31 #  define REP_MULTIPLICITY 0
32 # endif
33 #endif
34 #if REP_MULTIPLICITY && !defined(tTHX)
35 # define tTHX PerlInterpreter*
36 #endif
37
38 #if REP_MULTIPLICITY && defined(USE_ITHREADS) && defined(dMY_CXT) && defined(MY_CXT) && defined(START_MY_CXT) && defined(MY_CXT_INIT) && (defined(MY_CXT_CLONE) || defined(dMY_CXT_SV))
39 # define REP_THREADSAFE 1
40 # ifndef MY_CXT_CLONE
41 #  define MY_CXT_CLONE \
42     dMY_CXT_SV;                                                      \
43     my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
44     Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
45     sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
46 # endif
47 #else
48 # define REP_THREADSAFE 0
49 # undef  dMY_CXT
50 # define dMY_CXT      dNOOP
51 # undef  MY_CXT
52 # define MY_CXT       rep_globaldata
53 # undef  START_MY_CXT
54 # define START_MY_CXT STATIC my_cxt_t MY_CXT;
55 # undef  MY_CXT_INIT
56 # define MY_CXT_INIT  NOOP
57 # undef  MY_CXT_CLONE
58 # define MY_CXT_CLONE NOOP
59 #endif
60
61 /* --- Helpers ------------------------------------------------------------- */
62
63 /* ... Thread-safe hints ................................................... */
64
65 typedef struct {
66  SV *comp;
67  SV *exec;
68 #if REP_WORKAROUND_REQUIRE_PROPAGATION
69  IV  require_tag;
70 #endif
71 } rep_hint_t;
72
73 #if REP_THREADSAFE
74
75 #define PTABLE_VAL_FREE(V) { \
76  rep_hint_t *h = (V);        \
77  SvREFCNT_dec(h->comp);      \
78  SvREFCNT_dec(h->exec);      \
79  PerlMemShared_free(h);      \
80 }
81
82 #define pPTBL  pTHX
83 #define pPTBL_ pTHX_
84 #define aPTBL  aTHX
85 #define aPTBL_ aTHX_
86
87 #include "ptable.h"
88
89 #define ptable_store(T, K, V) ptable_store(aTHX_ (T), (K), (V))
90 #define ptable_free(T)        ptable_free(aTHX_ (T))
91
92 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
93
94 typedef struct {
95  ptable *tbl;
96  tTHX    owner;
97 } my_cxt_t;
98
99 START_MY_CXT
100
101 typedef struct {
102  ptable *tbl;
103 #if REP_HAS_PERL(5, 13, 2)
104  CLONE_PARAMS *params;
105 #else
106  CLONE_PARAMS params;
107 #endif
108 } rep_ptable_clone_ud;
109
110 #if REP_HAS_PERL(5, 13, 2)
111 # define rep_ptable_clone_ud_init(U, T, O) \
112    (U).tbl    = (T); \
113    (U).params = Perl_clone_params_new((O), aTHX)
114 # define rep_ptable_clone_ud_deinit(U) Perl_clone_params_del((U).params)
115 # define rep_dup_inc(S, U)             SvREFCNT_inc(sv_dup((S), (U)->params))
116 #else
117 # define rep_ptable_clone_ud_init(U, T, O) \
118    (U).tbl               = (T);     \
119    (U).params.stashes    = newAV(); \
120    (U).params.flags      = 0;       \
121    (U).params.proto_perl = (O)
122 # define rep_ptable_clone_ud_deinit(U) SvREFCNT_dec((U).params.stashes)
123 # define rep_dup_inc(S, U)             SvREFCNT_inc(sv_dup((S), &((U)->params)))
124 #endif
125
126 STATIC void rep_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
127  rep_ptable_clone_ud *ud = ud_;
128  rep_hint_t *h1 = ent->val;
129  rep_hint_t *h2;
130
131  h2              = PerlMemShared_malloc(sizeof *h2);
132  h2->comp        = rep_dup_inc(h1->comp, ud);
133  h2->exec        = rep_dup_inc(h1->exec, ud);
134 #if REP_WORKAROUND_REQUIRE_PROPAGATION
135  h2->require_tag = PTR2IV(rep_dup_inc(INT2PTR(SV *, h1->require_tag), ud));
136 #endif
137
138  ptable_store(ud->tbl, ent->key, h2);
139 }
140
141 STATIC void rep_thread_cleanup(pTHX_ void *ud) {
142  dMY_CXT;
143
144  ptable_free(MY_CXT.tbl);
145 }
146
147 STATIC int rep_endav_free(pTHX_ SV *sv, MAGIC *mg) {
148  SAVEDESTRUCTOR_X(rep_thread_cleanup, NULL);
149
150  return 0;
151 }
152
153 STATIC MGVTBL rep_endav_vtbl = {
154  0,
155  0,
156  0,
157  0,
158  rep_endav_free
159 #if MGf_COPY
160  , 0
161 #endif
162 #if MGf_DUP
163  , 0
164 #endif
165 #if MGf_LOCAL
166  , 0
167 #endif
168 };
169
170 #endif /* REP_THREADSAFE */
171
172 STATIC SV *rep_validate_callback(SV *code) {
173  if (!SvROK(code))
174   return NULL;
175
176  code = SvRV(code);
177  if (SvTYPE(code) < SVt_PVCV)
178   return NULL;
179
180  return SvREFCNT_inc_simple_NN(code);
181 }
182
183 #if REP_WORKAROUND_REQUIRE_PROPAGATION
184 STATIC IV rep_require_tag(pTHX) {
185 #define rep_require_tag() rep_require_tag(aTHX)
186  const CV *cv, *outside;
187
188  cv = PL_compcv;
189
190  if (!cv) {
191   /* If for some reason the pragma is operational at run-time, try to discover
192    * the current cv in use. */
193   const PERL_SI *si;
194
195   for (si = PL_curstackinfo; si; si = si->si_prev) {
196    I32 cxix;
197
198    for (cxix = si->si_cxix; cxix >= 0; --cxix) {
199     const PERL_CONTEXT *cx = si->si_cxstack + cxix;
200
201     switch (CxTYPE(cx)) {
202      case CXt_SUB:
203      case CXt_FORMAT:
204       /* The propagation workaround is only needed up to 5.10.0 and at that
205        * time format and sub contexts were still identical. And even later the
206        * cv members offsets should have been kept the same. */
207       cv = cx->blk_sub.cv;
208       goto get_enclosing_cv;
209      case CXt_EVAL:
210       cv = cx->blk_eval.cv;
211       goto get_enclosing_cv;
212      default:
213       break;
214     }
215    }
216   }
217
218   cv = PL_main_cv;
219  }
220
221 get_enclosing_cv:
222  for (outside = CvOUTSIDE(cv); outside; outside = CvOUTSIDE(cv))
223   cv = outside;
224
225  return PTR2IV(cv);
226 }
227 #endif /* REP_WORKAROUND_REQUIRE_PROPAGATION */
228
229 STATIC SV *rep_tag(pTHX_ SV *comp, SV *exec) {
230 #define rep_tag(C, E) rep_tag(aTHX_ (C), (E))
231  rep_hint_t *h;
232
233  h              = PerlMemShared_malloc(sizeof *h);
234  h->comp        = rep_validate_callback(comp);
235  h->exec        = rep_validate_callback(exec);
236 #if REP_WORKAROUND_REQUIRE_PROPAGATION
237  h->require_tag = rep_require_tag();
238 #endif /* REP_WORKAROUND_REQUIRE_PROPAGATION */
239
240 #if REP_THREADSAFE
241  {
242   dMY_CXT;
243   /* We only need for the key to be an unique tag for looking up the value later
244    * Allocated memory provides convenient unique identifiers, so that's why we
245    * use the hint as the key itself. */
246   ptable_store(MY_CXT.tbl, h, h);
247  }
248 #endif /* REP_THREADSAFE */
249
250  return newSViv(PTR2IV(h));
251 }
252
253 STATIC const rep_hint_t *rep_detag(pTHX_ const SV *hint) {
254 #define rep_detag(H) rep_detag(aTHX_ (H))
255  rep_hint_t *h;
256
257  if (!(hint && SvIOK(hint)))
258   return NULL;
259
260  h = INT2PTR(rep_hint_t *, SvIVX(hint));
261 #if REP_THREADSAFE
262  {
263   dMY_CXT;
264   h = ptable_fetch(MY_CXT.tbl, h);
265  }
266 #endif /* REP_THREADSAFE */
267
268 #if REP_WORKAROUND_REQUIRE_PROPAGATION
269  if (rep_require_tag() != h->require_tag)
270   return NULL;
271 #endif /* REP_WORKAROUND_REQUIRE_PROPAGATION */
272
273  return h;
274 }
275
276 STATIC U32 rep_hash = 0;
277
278 STATIC const rep_hint_t *rep_hint(pTHX) {
279 #define rep_hint() rep_hint(aTHX)
280  SV *hint;
281
282 #ifdef cop_hints_fetch_pvn
283  hint = cop_hints_fetch_pvn(PL_curcop,
284                             __PACKAGE__, __PACKAGE_LEN__, rep_hash, 0);
285 #else
286  /* We already require 5.9.5 for the regexp engine API. */
287  hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
288                                        NULL,
289                                        __PACKAGE__, __PACKAGE_LEN__,
290                                        0,
291                                        rep_hash);
292 #endif
293
294  return rep_detag(hint);
295 }
296
297 REGEXP *
298 #if PERL_VERSION <= 10
299 Plugin_comp(pTHX_ const SV * const pattern, const U32 flags)
300 #else
301 Plugin_comp(pTHX_ SV * const pattern, U32 flags)
302 #endif
303 {
304     dSP;
305     struct regexp * rx;
306     REGEXP *RX;
307
308     re__engine__Plugin re;
309     const rep_hint_t *h;
310
311     STRLEN plen;
312     char *pbuf;
313
314     SV *obj;
315
316     h = rep_hint();
317     if (!h) /* This looks like a pragma leak. Apply the default behaviour */
318         return re_compile(pattern, flags);
319
320     /* exp/xend version of the pattern & length */
321     pbuf = SvPV((SV*)pattern, plen);
322
323     /* Our blessed object */
324     obj = newSV(0);
325     SvREFCNT_inc_simple_void_NN(obj);
326     Newxz(re, 1, struct replug);
327     sv_setref_pv(obj, "re::engine::Plugin", (void*)re);
328
329     newREGEXP(RX);
330     rx = rxREGEXP(RX);
331
332     re->rx = rx;                   /* Make the rx accessible from self->rx */
333     rx->intflags = flags;          /* Flags for internal use */
334     rx->extflags = flags;          /* Flags for perl to use */
335     rx->engine = RE_ENGINE_PLUGIN; /* Compile to use this engine */
336
337 #if PERL_VERSION <= 10
338     rx->refcnt = 1;                /* Refcount so we won't be destroyed */
339
340     /* Precompiled pattern for pp_regcomp to use */
341     rx->prelen = plen;
342     rx->precomp = savepvn(pbuf, rx->prelen);
343
344     /* Set up qr// stringification to be equivalent to the supplied
345      * pattern, this should be done via overload eventually.
346      */
347     rx->wraplen = rx->prelen;
348     Newx(rx->wrapped, rx->wraplen, char);
349     Copy(rx->precomp, rx->wrapped, rx->wraplen, char);
350 #endif
351
352     /* Store our private object */
353     rx->pprivate = obj;
354
355     /* Store the pattern for ->pattern */
356     re->pattern = (SV*)pattern;
357     SvREFCNT_inc_simple_void(re->pattern);
358
359     /* If there's an exec callback, store it into the private object so
360      * that it will be the one to be called, even if the engine changes
361      * in between */
362     if (h->exec) {
363         re->cb_exec = h->exec;
364         SvREFCNT_inc_simple_void_NN(h->exec);
365     }
366
367     re->cb_num_capture_buff_FETCH  = NULL;
368     re->cb_num_capture_buff_STORE  = NULL;
369     re->cb_num_capture_buff_LENGTH = NULL;
370
371     /* Call our callback function if one was defined, if not we've
372      * already set up all the stuff we're going to to need for
373      * subsequent exec and other calls */
374     if (h->comp) {
375         ENTER;
376         SAVETMPS;
377
378         PUSHMARK(SP);
379         XPUSHs(obj);
380         PUTBACK;
381
382         call_sv(h->comp, G_DISCARD);
383
384         FREETMPS;
385         LEAVE;
386     }
387
388     /* If any of the comp-time accessors were called we'll have to
389      * update the regexp struct with the new info.
390      */
391
392     Newxz(rx->offs, rx->nparens + 1, regexp_paren_pair);
393
394     return RX;
395 }
396
397 I32
398 Plugin_exec(pTHX_ REGEXP * const RX, char *stringarg, char *strend,
399             char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
400 {
401     dSP;
402     I32 matched;
403     struct regexp *rx = rxREGEXP(RX);
404     GET_SELF_FROM_PPRIVATE(rx->pprivate);
405
406     if (self->cb_exec) {
407         SV *ret;
408
409         /* Store the current str for ->str */
410         SvREFCNT_dec(self->str);
411         self->str = sv;
412         SvREFCNT_inc_simple_void(self->str);
413
414         ENTER;
415         SAVETMPS;
416
417         PUSHMARK(SP);
418         XPUSHs(rx->pprivate);
419         XPUSHs(sv);
420         PUTBACK;
421
422         call_sv(self->cb_exec, G_SCALAR);
423
424         SPAGAIN;
425
426         ret = POPs;
427         if (SvTRUE(ret))
428             matched = 1;
429         else
430             matched = 0;
431
432         PUTBACK;
433         FREETMPS;
434         LEAVE;
435     } else {
436         matched = 0;
437     }
438
439     return matched;
440 }
441
442 char *
443 Plugin_intuit(pTHX_ REGEXP * const RX, SV *sv, char *strpos,
444                      char *strend, U32 flags, re_scream_pos_data *data)
445 {
446     PERL_UNUSED_ARG(RX);
447     PERL_UNUSED_ARG(sv);
448     PERL_UNUSED_ARG(strpos);
449     PERL_UNUSED_ARG(strend);
450     PERL_UNUSED_ARG(flags);
451     PERL_UNUSED_ARG(data);
452     return NULL;
453 }
454
455 SV *
456 Plugin_checkstr(pTHX_ REGEXP * const RX)
457 {
458     PERL_UNUSED_ARG(RX);
459     return NULL;
460 }
461
462 void
463 Plugin_free(pTHX_ REGEXP * const RX)
464 {
465     struct regexp *rx;
466     re__engine__Plugin self;
467
468     if (PL_dirty)
469         return;
470
471     rx = rxREGEXP(RX);
472     SELF_FROM_PPRIVATE(self, rx->pprivate);
473
474     SvREFCNT_dec(self->pattern);
475     SvREFCNT_dec(self->str);
476
477     SvREFCNT_dec(self->cb_exec);
478
479     SvREFCNT_dec(self->cb_num_capture_buff_FETCH);
480     SvREFCNT_dec(self->cb_num_capture_buff_STORE);
481     SvREFCNT_dec(self->cb_num_capture_buff_LENGTH);
482
483     self->rx = NULL;
484     Safefree(self);
485
486 /*
487     dSP;
488     SV * callback;
489
490     callback = self->cb_free;
491
492     if (callback) {
493         ENTER;
494         SAVETMPS;
495
496         PUSHMARK(SP);
497         XPUSHs(rx->pprivate);
498         PUTBACK;
499
500         call_sv(callback, G_DISCARD);
501
502         PUTBACK;
503         FREETMPS;
504         LEAVE;
505     }
506     return;
507 */
508 }
509
510 void *
511 Plugin_dupe(pTHX_ REGEXP * const RX, CLONE_PARAMS *param)
512 {
513     struct regexp *rx = rxREGEXP(RX);
514     Perl_croak(aTHX_ "dupe not supported yet");
515     return rx->pprivate;
516 }
517
518
519 void
520 Plugin_numbered_buff_FETCH(pTHX_ REGEXP * const RX, const I32 paren,
521                            SV * const sv)
522 {
523     dSP;
524     I32 items;
525     SV * callback;
526     struct regexp *rx = rxREGEXP(RX);
527     GET_SELF_FROM_PPRIVATE(rx->pprivate);
528
529     callback = self->cb_num_capture_buff_FETCH;
530
531     if (callback) {
532         ENTER;
533         SAVETMPS;
534
535         PUSHMARK(SP);
536         XPUSHs(rx->pprivate);
537         XPUSHs(sv_2mortal(newSViv(paren)));
538         PUTBACK;
539
540         items = call_sv(callback, G_SCALAR);
541
542         if (items == 1) {
543             SV *ret;
544
545             SPAGAIN;
546             ret = POPs;
547             sv_setsv(sv, ret);
548         } else {
549             sv_setsv(sv, &PL_sv_undef);
550         }
551
552         PUTBACK;
553         FREETMPS;
554         LEAVE;
555     } else {
556         sv_setsv(sv, &PL_sv_undef);
557     }
558 }
559
560 void
561 Plugin_numbered_buff_STORE(pTHX_ REGEXP * const RX, const I32 paren,
562                            SV const * const value)
563 {
564     dSP;
565     SV * callback;
566     struct regexp *rx = rxREGEXP(RX);
567     GET_SELF_FROM_PPRIVATE(rx->pprivate);
568
569     callback = self->cb_num_capture_buff_STORE;
570
571     if (callback) {
572         ENTER;
573         SAVETMPS;
574
575         PUSHMARK(SP);
576         XPUSHs(rx->pprivate);
577         XPUSHs(sv_2mortal(newSViv(paren)));
578         XPUSHs((SV *) value);
579         PUTBACK;
580
581         call_sv(callback, G_DISCARD);
582
583         PUTBACK;
584         FREETMPS;
585         LEAVE;
586     }
587 }
588
589 I32
590 Plugin_numbered_buff_LENGTH(pTHX_ REGEXP * const RX, const SV * const sv,
591                               const I32 paren)
592 {
593     dSP;
594     SV * callback;
595     struct regexp *rx = rxREGEXP(RX);
596     GET_SELF_FROM_PPRIVATE(rx->pprivate);
597
598     callback = self->cb_num_capture_buff_LENGTH;
599
600     if (callback) {
601         IV ret;
602
603         ENTER;
604         SAVETMPS;
605
606         PUSHMARK(SP);
607         XPUSHs(rx->pprivate);
608         XPUSHs(sv_2mortal(newSViv(paren)));
609         PUTBACK;
610
611         call_sv(callback, G_SCALAR);
612
613         SPAGAIN;
614
615         ret = POPi;
616
617         PUTBACK;
618         FREETMPS;
619         LEAVE;
620
621         return (I32)ret;
622     } else {
623         /* TODO: call FETCH and get the length on that value */
624         return 0;
625     }
626 }
627
628
629 SV*
630 Plugin_named_buff (pTHX_ REGEXP * const RX, SV * const key, SV * const value,
631                    const U32 flags)
632 {
633     return NULL;
634 }
635
636 SV*
637 Plugin_named_buff_iter (pTHX_ REGEXP * const RX, const SV * const lastkey,
638                         const U32 flags)
639 {
640     return NULL;
641 }
642
643 SV*
644 Plugin_package(pTHX_ REGEXP * const RX)
645 {
646     PERL_UNUSED_ARG(RX);
647     return newSVpvs("re::engine::Plugin");
648 }
649
650 #if REP_THREADSAFE
651
652 STATIC U32 rep_initialized = 0;
653
654 STATIC void rep_teardown(pTHX_ void *root) {
655  if (!rep_initialized || aTHX != root)
656   return;
657
658  {
659   dMY_CXT;
660   ptable_free(MY_CXT.tbl);
661  }
662
663  rep_initialized = 0;
664 }
665
666 STATIC void rep_setup(pTHX) {
667 #define rep_setup() rep_setup(aTHX)
668  if (rep_initialized)
669   return;
670
671  {
672   MY_CXT_INIT;
673   MY_CXT.tbl   = ptable_new();
674   MY_CXT.owner = aTHX;
675  }
676
677  call_atexit(rep_teardown, aTHX);
678
679  rep_initialized = 1;
680 }
681
682 #else  /*  REP_THREADSAFE */
683
684 #define rep_setup()
685
686 #endif /* !REP_THREADSAFE */
687
688 STATIC U32 rep_booted = 0;
689
690 /* --- XS ------------------------------------------------------------------ */
691
692 MODULE = re::engine::Plugin       PACKAGE = re::engine::Plugin
693
694 PROTOTYPES: DISABLE
695
696 BOOT:
697 {
698     if (!rep_booted++) {
699         HV *stash;
700
701         PERL_HASH(rep_hash, __PACKAGE__, __PACKAGE_LEN__);
702
703         stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
704         newCONSTSUB(stash, "REP_THREADSAFE",  newSVuv(REP_THREADSAFE));
705         newCONSTSUB(stash, "REP_FORKSAFE",    newSVuv(REP_FORKSAFE));
706     }
707
708     rep_setup();
709 }
710
711 #if REP_THREADSAFE
712
713 void
714 CLONE(...)
715 PREINIT:
716     ptable *t;
717     GV     *gv;
718 PPCODE:
719     {
720         rep_ptable_clone_ud ud;
721         dMY_CXT;
722
723         t = ptable_new();
724         rep_ptable_clone_ud_init(ud, t, MY_CXT.owner);
725         ptable_walk(MY_CXT.tbl, rep_ptable_clone, &ud);
726         rep_ptable_clone_ud_deinit(ud);
727     }
728     {
729         MY_CXT_CLONE;
730         MY_CXT.tbl   = t;
731         MY_CXT.owner = aTHX;
732     }
733     gv = gv_fetchpv(__PACKAGE__ "::_THREAD_CLEANUP", 0, SVt_PVCV);
734     if (gv) {
735         CV *cv = GvCV(gv);
736         if (!PL_endav)
737             PL_endav = newAV();
738         SvREFCNT_inc(cv);
739         if (!av_store(PL_endav, av_len(PL_endav) + 1, (SV *) cv))
740             SvREFCNT_dec(cv);
741         sv_magicext((SV *) PL_endav, NULL, PERL_MAGIC_ext, &rep_endav_vtbl, NULL, 0);
742     }
743     XSRETURN(0);
744
745 void
746 _THREAD_CLEANUP(...)
747 PROTOTYPE: DISABLE
748 PPCODE:
749     rep_thread_cleanup(aTHX_ NULL);
750     XSRETURN(0);
751
752 #endif /* REP_THREADSAFE */
753
754 void
755 pattern(re::engine::Plugin self, ...)
756 PPCODE:
757     XPUSHs(self->pattern);
758
759 void
760 str(re::engine::Plugin self, ...)
761 PPCODE:
762     XPUSHs(self->str);
763
764 void
765 mod(re::engine::Plugin self)
766 PREINIT:
767     U32 flags;
768     char mods[5 + 1];
769     int n = 0, i;
770 PPCODE:
771     flags = self->rx->intflags;
772     if (flags & PMf_FOLD)         /* /i */
773         mods[n++] = 'i';
774     if (flags & PMf_MULTILINE)    /* /m */
775         mods[n++] = 'm';
776     if (flags & PMf_SINGLELINE)   /* /s */
777         mods[n++] = 's';
778     if (flags & PMf_EXTENDED)     /* /x */
779         mods[n++] = 'x';
780     if (flags & RXf_PMf_KEEPCOPY) /* /p */
781         mods[n++] = 'p';
782     mods[n] = '\0';
783     EXTEND(SP, 2 * n);
784     for (i = 0; i < n; ++i) {
785         mPUSHp(mods + i, 1);
786         PUSHs(&PL_sv_yes);
787     }
788     XSRETURN(2 * n);
789
790 void
791 stash(re::engine::Plugin self, ...)
792 PPCODE:
793     if (items > 1) {
794         SvREFCNT_dec(self->stash);
795         self->stash = ST(1);
796         SvREFCNT_inc_simple_void(self->stash);
797         XSRETURN_EMPTY;
798     } else {
799         XPUSHs(self->stash);
800     }
801
802 void
803 minlen(re::engine::Plugin self, ...)
804 PPCODE:
805     if (items > 1) {
806         self->rx->minlen = (I32)SvIV(ST(1));
807         XSRETURN_EMPTY;
808     } else {
809         if (self->rx->minlen) {
810             XPUSHs(sv_2mortal(newSViv(self->rx->minlen)));
811         } else {
812             XPUSHs(sv_2mortal(&PL_sv_undef));
813         }
814     }
815
816 void
817 gofs(re::engine::Plugin self, ...)
818 PPCODE:
819     if (items > 1) {
820         self->rx->gofs = (U32)SvIV(ST(1));
821         XSRETURN_EMPTY;
822     } else {
823         if (self->rx->gofs) {
824             XPUSHs(sv_2mortal(newSVuv(self->rx->gofs)));
825         } else {
826             XPUSHs(sv_2mortal(&PL_sv_undef));
827         }
828     }
829
830 void
831 nparens(re::engine::Plugin self, ...)
832 PPCODE:
833     if (items > 1) {
834         self->rx->nparens = (U32)SvIV(ST(1));
835         XSRETURN_EMPTY;
836     } else {
837         if (self->rx->nparens) {
838             XPUSHs(sv_2mortal(newSVuv(self->rx->nparens)));
839         } else {
840             XPUSHs(sv_2mortal(&PL_sv_undef));
841         }
842     }
843
844 void
845 _exec(re::engine::Plugin self, ...)
846 PPCODE:
847     if (items > 1) {
848         SvREFCNT_dec(self->cb_exec);
849         self->cb_exec = ST(1);
850         SvREFCNT_inc_simple_void(self->cb_exec);
851     }
852
853 void
854 _num_capture_buff_FETCH(re::engine::Plugin self, ...)
855 PPCODE:
856     if (items > 1) {
857         SvREFCNT_dec(self->cb_num_capture_buff_FETCH);
858         self->cb_num_capture_buff_FETCH = ST(1);
859         SvREFCNT_inc_simple_void(self->cb_num_capture_buff_FETCH);
860     }
861
862 void
863 _num_capture_buff_STORE(re::engine::Plugin self, ...)
864 PPCODE:
865     if (items > 1) {
866         SvREFCNT_dec(self->cb_num_capture_buff_STORE);
867         self->cb_num_capture_buff_STORE = ST(1);
868         SvREFCNT_inc_simple_void(self->cb_num_capture_buff_STORE);
869     }
870
871 void
872 _num_capture_buff_LENGTH(re::engine::Plugin self, ...)
873 PPCODE:
874     if (items > 1) {
875         SvREFCNT_dec(self->cb_num_capture_buff_LENGTH);
876         self->cb_num_capture_buff_LENGTH = ST(1);
877         SvREFCNT_inc_simple_void(self->cb_num_capture_buff_LENGTH);
878     }
879
880 SV *
881 _tag(SV *comp, SV *exec)
882 CODE:
883     RETVAL = rep_tag(comp, exec);
884 OUTPUT:
885     RETVAL
886
887 void
888 ENGINE()
889 PPCODE:
890     XPUSHs(sv_2mortal(newSViv(PTR2IV(&engine_plugin))));