1 /* This file is part of the re::engine::Plugin Perl module.
2 * See http://search.cpan.org/dist/re-engine-Plugin/ */
4 #define PERL_NO_GET_CONTEXT
11 #define __PACKAGE__ "re::engine::Plugin"
12 #define __PACKAGE_LEN__ (sizeof(__PACKAGE__)-1)
14 #define REP_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
16 #ifndef REP_WORKAROUND_REQUIRE_PROPAGATION
17 # define REP_WORKAROUND_REQUIRE_PROPAGATION !REP_HAS_PERL(5, 10, 1)
20 /* ... Thread safety and multiplicity ...................................... */
22 /* Safe unless stated otherwise in Makefile.PL */
24 # define REP_FORKSAFE 1
27 #ifndef REP_MULTIPLICITY
28 # if defined(MULTIPLICITY) || defined(PERL_IMPLICIT_CONTEXT)
29 # define REP_MULTIPLICITY 1
31 # define REP_MULTIPLICITY 0
34 #if REP_MULTIPLICITY && !defined(tTHX)
35 # define tTHX PerlInterpreter*
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
41 # define MY_CXT_CLONE \
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))
48 # define REP_THREADSAFE 0
50 # define dMY_CXT dNOOP
52 # define MY_CXT rep_globaldata
54 # define START_MY_CXT STATIC my_cxt_t MY_CXT;
56 # define MY_CXT_INIT NOOP
58 # define MY_CXT_CLONE NOOP
61 /* --- Helpers ------------------------------------------------------------- */
63 /* ... Thread-safe hints ................................................... */
68 #if REP_WORKAROUND_REQUIRE_PROPAGATION
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); \
89 #define ptable_store(T, K, V) ptable_store(aTHX_ (T), (K), (V))
90 #define ptable_free(T) ptable_free(aTHX_ (T))
92 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
103 #if REP_HAS_PERL(5, 13, 2)
104 CLONE_PARAMS *params;
108 } rep_ptable_clone_ud;
110 #if REP_HAS_PERL(5, 13, 2)
111 # define rep_ptable_clone_ud_init(U, T, O) \
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))
117 # define rep_ptable_clone_ud_init(U, T, O) \
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)))
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;
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));
138 ptable_store(ud->tbl, ent->key, h2);
143 STATIC void rep_thread_cleanup(pTHX_ void *ud) {
146 ptable_free(MY_CXT.tbl);
149 #endif /* REP_THREADSAFE */
151 STATIC SV *rep_validate_callback(SV *code) {
156 if (SvTYPE(code) < SVt_PVCV)
159 return SvREFCNT_inc_simple_NN(code);
162 #if REP_WORKAROUND_REQUIRE_PROPAGATION
163 STATIC IV rep_require_tag(pTHX) {
164 #define rep_require_tag() rep_require_tag(aTHX)
165 const CV *cv, *outside;
170 /* If for some reason the pragma is operational at run-time, try to discover
171 * the current cv in use. */
174 for (si = PL_curstackinfo; si; si = si->si_prev) {
177 for (cxix = si->si_cxix; cxix >= 0; --cxix) {
178 const PERL_CONTEXT *cx = si->si_cxstack + cxix;
180 switch (CxTYPE(cx)) {
183 /* The propagation workaround is only needed up to 5.10.0 and at that
184 * time format and sub contexts were still identical. And even later the
185 * cv members offsets should have been kept the same. */
187 goto get_enclosing_cv;
189 cv = cx->blk_eval.cv;
190 goto get_enclosing_cv;
201 for (outside = CvOUTSIDE(cv); outside; outside = CvOUTSIDE(cv))
206 #endif /* REP_WORKAROUND_REQUIRE_PROPAGATION */
208 STATIC SV *rep_tag(pTHX_ SV *comp, SV *exec) {
209 #define rep_tag(C, E) rep_tag(aTHX_ (C), (E))
213 h = PerlMemShared_malloc(sizeof *h);
214 h->comp = rep_validate_callback(comp);
215 h->exec = rep_validate_callback(exec);
216 #if REP_WORKAROUND_REQUIRE_PROPAGATION
217 h->require_tag = rep_require_tag();
218 #endif /* REP_WORKAROUND_REQUIRE_PROPAGATION */
223 /* We only need for the key to be an unique tag for looking up the value later
224 * Allocated memory provides convenient unique identifiers, so that's why we
225 * use the hint as the key itself. */
226 ptable_store(MY_CXT.tbl, h, h);
228 #endif /* REP_THREADSAFE */
230 return newSViv(PTR2IV(h));
233 STATIC const rep_hint_t *rep_detag(pTHX_ const SV *hint) {
234 #define rep_detag(H) rep_detag(aTHX_ (H))
237 if (!(hint && SvIOK(hint)))
240 h = INT2PTR(rep_hint_t *, SvIVX(hint));
244 h = ptable_fetch(MY_CXT.tbl, h);
246 #endif /* REP_THREADSAFE */
248 #if REP_WORKAROUND_REQUIRE_PROPAGATION
249 if (rep_require_tag() != h->require_tag)
251 #endif /* REP_WORKAROUND_REQUIRE_PROPAGATION */
256 STATIC U32 rep_hash = 0;
258 STATIC const rep_hint_t *rep_hint(pTHX) {
259 #define rep_hint() rep_hint(aTHX)
262 #ifdef cop_hints_fetch_pvn
263 hint = cop_hints_fetch_pvn(PL_curcop,
264 __PACKAGE__, __PACKAGE_LEN__, rep_hash, 0);
266 /* We already require 5.9.5 for the regexp engine API. */
267 hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
269 __PACKAGE__, __PACKAGE_LEN__,
274 return rep_detag(hint);
278 #if PERL_VERSION <= 10
279 Plugin_comp(pTHX_ const SV * const pattern, const U32 flags)
281 Plugin_comp(pTHX_ SV * const pattern, U32 flags)
288 re__engine__Plugin re;
297 if (!h) /* This looks like a pragma leak. Apply the default behaviour */
298 return re_compile(pattern, flags);
300 /* exp/xend version of the pattern & length */
301 pbuf = SvPV((SV*)pattern, plen);
303 /* Our blessed object */
305 SvREFCNT_inc_simple_void_NN(obj);
306 Newxz(re, 1, struct replug);
307 sv_setref_pv(obj, "re::engine::Plugin", (void*)re);
312 re->rx = rx; /* Make the rx accessible from self->rx */
313 rx->intflags = flags; /* Flags for internal use */
314 rx->extflags = flags; /* Flags for perl to use */
315 rx->engine = RE_ENGINE_PLUGIN; /* Compile to use this engine */
317 #if PERL_VERSION <= 10
318 rx->refcnt = 1; /* Refcount so we won't be destroyed */
320 /* Precompiled pattern for pp_regcomp to use */
322 rx->precomp = savepvn(pbuf, rx->prelen);
324 /* Set up qr// stringification to be equivalent to the supplied
325 * pattern, this should be done via overload eventually.
327 rx->wraplen = rx->prelen;
328 Newx(rx->wrapped, rx->wraplen, char);
329 Copy(rx->precomp, rx->wrapped, rx->wraplen, char);
332 /* Store our private object */
335 /* Store the pattern for ->pattern */
336 re->pattern = (SV*)pattern;
337 SvREFCNT_inc_simple_void(re->pattern);
339 /* If there's an exec callback, store it into the private object so
340 * that it will be the one to be called, even if the engine changes
343 re->cb_exec = h->exec;
344 SvREFCNT_inc_simple_void_NN(h->exec);
347 re->cb_num_capture_buff_FETCH = NULL;
348 re->cb_num_capture_buff_STORE = NULL;
349 re->cb_num_capture_buff_LENGTH = NULL;
351 /* Call our callback function if one was defined, if not we've
352 * already set up all the stuff we're going to to need for
353 * subsequent exec and other calls */
362 call_sv(h->comp, G_DISCARD);
368 /* If any of the comp-time accessors were called we'll have to
369 * update the regexp struct with the new info.
372 Newxz(rx->offs, rx->nparens + 1, regexp_paren_pair);
378 Plugin_exec(pTHX_ REGEXP * const RX, char *stringarg, char *strend,
379 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
383 struct regexp *rx = rxREGEXP(RX);
384 GET_SELF_FROM_PPRIVATE(rx->pprivate);
389 /* Store the current str for ->str */
390 SvREFCNT_dec(self->str);
392 SvREFCNT_inc_simple_void(self->str);
398 XPUSHs(rx->pprivate);
402 call_sv(self->cb_exec, G_SCALAR);
423 Plugin_intuit(pTHX_ REGEXP * const RX, SV *sv, char *strpos,
424 char *strend, U32 flags, re_scream_pos_data *data)
428 PERL_UNUSED_ARG(strpos);
429 PERL_UNUSED_ARG(strend);
430 PERL_UNUSED_ARG(flags);
431 PERL_UNUSED_ARG(data);
436 Plugin_checkstr(pTHX_ REGEXP * const RX)
443 Plugin_free(pTHX_ REGEXP * const RX)
446 re__engine__Plugin self;
452 SELF_FROM_PPRIVATE(self, rx->pprivate);
454 SvREFCNT_dec(self->pattern);
455 SvREFCNT_dec(self->str);
457 SvREFCNT_dec(self->cb_exec);
459 SvREFCNT_dec(self->cb_num_capture_buff_FETCH);
460 SvREFCNT_dec(self->cb_num_capture_buff_STORE);
461 SvREFCNT_dec(self->cb_num_capture_buff_LENGTH);
470 callback = self->cb_free;
477 XPUSHs(rx->pprivate);
480 call_sv(callback, G_DISCARD);
491 Plugin_dupe(pTHX_ REGEXP * const RX, CLONE_PARAMS *param)
493 struct regexp *rx = rxREGEXP(RX);
494 Perl_croak(aTHX_ "dupe not supported yet");
500 Plugin_numbered_buff_FETCH(pTHX_ REGEXP * const RX, const I32 paren,
506 struct regexp *rx = rxREGEXP(RX);
507 GET_SELF_FROM_PPRIVATE(rx->pprivate);
509 callback = self->cb_num_capture_buff_FETCH;
516 XPUSHs(rx->pprivate);
517 XPUSHs(sv_2mortal(newSViv(paren)));
520 items = call_sv(callback, G_SCALAR);
529 sv_setsv(sv, &PL_sv_undef);
536 sv_setsv(sv, &PL_sv_undef);
541 Plugin_numbered_buff_STORE(pTHX_ REGEXP * const RX, const I32 paren,
542 SV const * const value)
546 struct regexp *rx = rxREGEXP(RX);
547 GET_SELF_FROM_PPRIVATE(rx->pprivate);
549 callback = self->cb_num_capture_buff_STORE;
556 XPUSHs(rx->pprivate);
557 XPUSHs(sv_2mortal(newSViv(paren)));
558 XPUSHs((SV *) value);
561 call_sv(callback, G_DISCARD);
570 Plugin_numbered_buff_LENGTH(pTHX_ REGEXP * const RX, const SV * const sv,
575 struct regexp *rx = rxREGEXP(RX);
576 GET_SELF_FROM_PPRIVATE(rx->pprivate);
578 callback = self->cb_num_capture_buff_LENGTH;
587 XPUSHs(rx->pprivate);
588 XPUSHs(sv_2mortal(newSViv(paren)));
591 call_sv(callback, G_SCALAR);
603 /* TODO: call FETCH and get the length on that value */
610 Plugin_named_buff (pTHX_ REGEXP * const RX, SV * const key, SV * const value,
617 Plugin_named_buff_iter (pTHX_ REGEXP * const RX, const SV * const lastkey,
624 Plugin_package(pTHX_ REGEXP * const RX)
627 return newSVpvs("re::engine::Plugin");
632 STATIC U32 rep_initialized = 0;
634 STATIC void rep_teardown(pTHX_ void *root) {
635 if (!rep_initialized || aTHX != root)
640 ptable_free(MY_CXT.tbl);
646 STATIC void rep_setup(pTHX) {
647 #define rep_setup() rep_setup(aTHX)
653 MY_CXT.tbl = ptable_new();
657 call_atexit(rep_teardown, aTHX);
662 #else /* REP_THREADSAFE */
666 #endif /* !REP_THREADSAFE */
668 STATIC U32 rep_booted = 0;
670 /* --- XS ------------------------------------------------------------------ */
672 MODULE = re::engine::Plugin PACKAGE = re::engine::Plugin
681 PERL_HASH(rep_hash, __PACKAGE__, __PACKAGE_LEN__);
683 stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
684 newCONSTSUB(stash, "REP_THREADSAFE", newSVuv(REP_THREADSAFE));
685 newCONSTSUB(stash, "REP_FORKSAFE", newSVuv(REP_FORKSAFE));
699 rep_ptable_clone_ud ud;
703 rep_ptable_clone_ud_init(ud, t, MY_CXT.owner);
704 ptable_walk(MY_CXT.tbl, rep_ptable_clone, &ud);
705 rep_ptable_clone_ud_deinit(ud);
712 reap(3, rep_thread_cleanup, NULL);
715 #endif /* REP_THREADSAFE */
718 pattern(re::engine::Plugin self, ...)
720 XPUSHs(self->pattern);
723 str(re::engine::Plugin self, ...)
728 mod(re::engine::Plugin self)
734 flags = self->rx->intflags;
735 if (flags & PMf_FOLD) /* /i */
737 if (flags & PMf_MULTILINE) /* /m */
739 if (flags & PMf_SINGLELINE) /* /s */
741 if (flags & PMf_EXTENDED) /* /x */
743 if (flags & RXf_PMf_KEEPCOPY) /* /p */
747 for (i = 0; i < n; ++i) {
754 stash(re::engine::Plugin self, ...)
757 SvREFCNT_dec(self->stash);
759 SvREFCNT_inc_simple_void(self->stash);
766 minlen(re::engine::Plugin self, ...)
769 self->rx->minlen = (I32)SvIV(ST(1));
772 if (self->rx->minlen) {
773 XPUSHs(sv_2mortal(newSViv(self->rx->minlen)));
775 XPUSHs(sv_2mortal(&PL_sv_undef));
780 gofs(re::engine::Plugin self, ...)
783 self->rx->gofs = (U32)SvIV(ST(1));
786 if (self->rx->gofs) {
787 XPUSHs(sv_2mortal(newSVuv(self->rx->gofs)));
789 XPUSHs(sv_2mortal(&PL_sv_undef));
794 nparens(re::engine::Plugin self, ...)
797 self->rx->nparens = (U32)SvIV(ST(1));
800 if (self->rx->nparens) {
801 XPUSHs(sv_2mortal(newSVuv(self->rx->nparens)));
803 XPUSHs(sv_2mortal(&PL_sv_undef));
808 _exec(re::engine::Plugin self, ...)
811 SvREFCNT_dec(self->cb_exec);
812 self->cb_exec = ST(1);
813 SvREFCNT_inc_simple_void(self->cb_exec);
817 _num_capture_buff_FETCH(re::engine::Plugin self, ...)
820 SvREFCNT_dec(self->cb_num_capture_buff_FETCH);
821 self->cb_num_capture_buff_FETCH = ST(1);
822 SvREFCNT_inc_simple_void(self->cb_num_capture_buff_FETCH);
826 _num_capture_buff_STORE(re::engine::Plugin self, ...)
829 SvREFCNT_dec(self->cb_num_capture_buff_STORE);
830 self->cb_num_capture_buff_STORE = ST(1);
831 SvREFCNT_inc_simple_void(self->cb_num_capture_buff_STORE);
835 _num_capture_buff_LENGTH(re::engine::Plugin self, ...)
838 SvREFCNT_dec(self->cb_num_capture_buff_LENGTH);
839 self->cb_num_capture_buff_LENGTH = ST(1);
840 SvREFCNT_inc_simple_void(self->cb_num_capture_buff_LENGTH);
844 _tag(SV *comp, SV *exec)
846 RETVAL = rep_tag(comp, exec);
853 XPUSHs(sv_2mortal(newSViv(PTR2IV(&engine_plugin))));