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))
212 h = PerlMemShared_malloc(sizeof *h);
213 h->comp = rep_validate_callback(comp);
214 h->exec = rep_validate_callback(exec);
215 #if REP_WORKAROUND_REQUIRE_PROPAGATION
216 h->require_tag = rep_require_tag();
217 #endif /* REP_WORKAROUND_REQUIRE_PROPAGATION */
222 /* We only need for the key to be an unique tag for looking up the value later
223 * Allocated memory provides convenient unique identifiers, so that's why we
224 * use the hint as the key itself. */
225 ptable_store(MY_CXT.tbl, h, h);
227 #endif /* REP_THREADSAFE */
229 return newSViv(PTR2IV(h));
232 STATIC const rep_hint_t *rep_detag(pTHX_ const SV *hint) {
233 #define rep_detag(H) rep_detag(aTHX_ (H))
236 if (!(hint && SvIOK(hint)))
239 h = INT2PTR(rep_hint_t *, SvIVX(hint));
243 h = ptable_fetch(MY_CXT.tbl, h);
245 #endif /* REP_THREADSAFE */
247 #if REP_WORKAROUND_REQUIRE_PROPAGATION
248 if (rep_require_tag() != h->require_tag)
250 #endif /* REP_WORKAROUND_REQUIRE_PROPAGATION */
255 STATIC U32 rep_hash = 0;
257 STATIC const rep_hint_t *rep_hint(pTHX) {
258 #define rep_hint() rep_hint(aTHX)
261 #ifdef cop_hints_fetch_pvn
262 hint = cop_hints_fetch_pvn(PL_curcop,
263 __PACKAGE__, __PACKAGE_LEN__, rep_hash, 0);
265 /* We already require 5.9.5 for the regexp engine API. */
266 hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
268 __PACKAGE__, __PACKAGE_LEN__,
273 return rep_detag(hint);
277 #if PERL_VERSION <= 10
278 Plugin_comp(pTHX_ const SV * const pattern, const U32 flags)
280 Plugin_comp(pTHX_ SV * const pattern, U32 flags)
287 re__engine__Plugin re;
296 if (!h) /* This looks like a pragma leak. Apply the default behaviour */
297 return re_compile(pattern, flags);
299 /* exp/xend version of the pattern & length */
300 pbuf = SvPV((SV*)pattern, plen);
302 /* Our blessed object */
304 SvREFCNT_inc_simple_void_NN(obj);
305 Newxz(re, 1, struct replug);
306 sv_setref_pv(obj, "re::engine::Plugin", (void*)re);
311 re->rx = rx; /* Make the rx accessible from self->rx */
312 rx->intflags = flags; /* Flags for internal use */
313 rx->extflags = flags; /* Flags for perl to use */
314 rx->engine = RE_ENGINE_PLUGIN; /* Compile to use this engine */
316 #if PERL_VERSION <= 10
317 rx->refcnt = 1; /* Refcount so we won't be destroyed */
319 /* Precompiled pattern for pp_regcomp to use */
321 rx->precomp = savepvn(pbuf, rx->prelen);
323 /* Set up qr// stringification to be equivalent to the supplied
324 * pattern, this should be done via overload eventually.
326 rx->wraplen = rx->prelen;
327 Newx(rx->wrapped, rx->wraplen, char);
328 Copy(rx->precomp, rx->wrapped, rx->wraplen, char);
331 /* Store our private object */
334 /* Store the pattern for ->pattern */
335 re->pattern = (SV*)pattern;
336 SvREFCNT_inc_simple_void(re->pattern);
338 /* If there's an exec callback, store it into the private object so
339 * that it will be the one to be called, even if the engine changes
342 re->cb_exec = h->exec;
343 SvREFCNT_inc_simple_void_NN(h->exec);
346 re->cb_num_capture_buff_FETCH = NULL;
347 re->cb_num_capture_buff_STORE = NULL;
348 re->cb_num_capture_buff_LENGTH = NULL;
350 /* Call our callback function if one was defined, if not we've
351 * already set up all the stuff we're going to to need for
352 * subsequent exec and other calls */
361 call_sv(h->comp, G_DISCARD);
367 /* If any of the comp-time accessors were called we'll have to
368 * update the regexp struct with the new info.
371 Newxz(rx->offs, rx->nparens + 1, regexp_paren_pair);
377 Plugin_exec(pTHX_ REGEXP * const RX, char *stringarg, char *strend,
378 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
382 struct regexp *rx = rxREGEXP(RX);
383 GET_SELF_FROM_PPRIVATE(rx->pprivate);
388 /* Store the current str for ->str */
389 SvREFCNT_dec(self->str);
391 SvREFCNT_inc_simple_void(self->str);
397 XPUSHs(rx->pprivate);
401 call_sv(self->cb_exec, G_SCALAR);
422 Plugin_intuit(pTHX_ REGEXP * const RX, SV *sv, char *strpos,
423 char *strend, U32 flags, re_scream_pos_data *data)
427 PERL_UNUSED_ARG(strpos);
428 PERL_UNUSED_ARG(strend);
429 PERL_UNUSED_ARG(flags);
430 PERL_UNUSED_ARG(data);
435 Plugin_checkstr(pTHX_ REGEXP * const RX)
442 Plugin_free(pTHX_ REGEXP * const RX)
445 re__engine__Plugin self;
451 SELF_FROM_PPRIVATE(self, rx->pprivate);
453 SvREFCNT_dec(self->pattern);
454 SvREFCNT_dec(self->str);
456 SvREFCNT_dec(self->cb_exec);
458 SvREFCNT_dec(self->cb_num_capture_buff_FETCH);
459 SvREFCNT_dec(self->cb_num_capture_buff_STORE);
460 SvREFCNT_dec(self->cb_num_capture_buff_LENGTH);
469 callback = self->cb_free;
476 XPUSHs(rx->pprivate);
479 call_sv(callback, G_DISCARD);
490 Plugin_dupe(pTHX_ REGEXP * const RX, CLONE_PARAMS *param)
492 struct regexp *rx = rxREGEXP(RX);
493 Perl_croak(aTHX_ "dupe not supported yet");
499 Plugin_numbered_buff_FETCH(pTHX_ REGEXP * const RX, const I32 paren,
505 struct regexp *rx = rxREGEXP(RX);
506 GET_SELF_FROM_PPRIVATE(rx->pprivate);
508 callback = self->cb_num_capture_buff_FETCH;
515 XPUSHs(rx->pprivate);
516 XPUSHs(sv_2mortal(newSViv(paren)));
519 items = call_sv(callback, G_SCALAR);
528 sv_setsv(sv, &PL_sv_undef);
535 sv_setsv(sv, &PL_sv_undef);
540 Plugin_numbered_buff_STORE(pTHX_ REGEXP * const RX, const I32 paren,
541 SV const * const value)
545 struct regexp *rx = rxREGEXP(RX);
546 GET_SELF_FROM_PPRIVATE(rx->pprivate);
548 callback = self->cb_num_capture_buff_STORE;
555 XPUSHs(rx->pprivate);
556 XPUSHs(sv_2mortal(newSViv(paren)));
557 XPUSHs((SV *) value);
560 call_sv(callback, G_DISCARD);
569 Plugin_numbered_buff_LENGTH(pTHX_ REGEXP * const RX, const SV * const sv,
574 struct regexp *rx = rxREGEXP(RX);
575 GET_SELF_FROM_PPRIVATE(rx->pprivate);
577 callback = self->cb_num_capture_buff_LENGTH;
586 XPUSHs(rx->pprivate);
587 XPUSHs(sv_2mortal(newSViv(paren)));
590 call_sv(callback, G_SCALAR);
602 /* TODO: call FETCH and get the length on that value */
609 Plugin_named_buff (pTHX_ REGEXP * const RX, SV * const key, SV * const value,
616 Plugin_named_buff_iter (pTHX_ REGEXP * const RX, const SV * const lastkey,
623 Plugin_package(pTHX_ REGEXP * const RX)
626 return newSVpvs("re::engine::Plugin");
631 STATIC U32 rep_initialized = 0;
633 STATIC void rep_teardown(pTHX_ void *root) {
634 if (!rep_initialized || aTHX != root)
639 ptable_free(MY_CXT.tbl);
645 STATIC void rep_setup(pTHX) {
646 #define rep_setup() rep_setup(aTHX)
652 MY_CXT.tbl = ptable_new();
656 call_atexit(rep_teardown, aTHX);
661 #else /* REP_THREADSAFE */
665 #endif /* !REP_THREADSAFE */
667 STATIC U32 rep_booted = 0;
669 /* --- XS ------------------------------------------------------------------ */
671 MODULE = re::engine::Plugin PACKAGE = re::engine::Plugin
680 PERL_HASH(rep_hash, __PACKAGE__, __PACKAGE_LEN__);
682 stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
683 newCONSTSUB(stash, "REP_THREADSAFE", newSVuv(REP_THREADSAFE));
684 newCONSTSUB(stash, "REP_FORKSAFE", newSVuv(REP_FORKSAFE));
698 rep_ptable_clone_ud ud;
702 rep_ptable_clone_ud_init(ud, t, MY_CXT.owner);
703 ptable_walk(MY_CXT.tbl, rep_ptable_clone, &ud);
704 rep_ptable_clone_ud_deinit(ud);
711 reap(3, rep_thread_cleanup, NULL);
714 #endif /* REP_THREADSAFE */
717 pattern(re::engine::Plugin self, ...)
719 XPUSHs(self->pattern);
722 str(re::engine::Plugin self, ...)
727 mod(re::engine::Plugin self)
733 flags = self->rx->intflags;
734 if (flags & PMf_FOLD) /* /i */
736 if (flags & PMf_MULTILINE) /* /m */
738 if (flags & PMf_SINGLELINE) /* /s */
740 if (flags & PMf_EXTENDED) /* /x */
742 if (flags & RXf_PMf_KEEPCOPY) /* /p */
746 for (i = 0; i < n; ++i) {
753 stash(re::engine::Plugin self, ...)
756 SvREFCNT_dec(self->stash);
758 SvREFCNT_inc_simple_void(self->stash);
765 minlen(re::engine::Plugin self, ...)
768 self->rx->minlen = (I32)SvIV(ST(1));
771 if (self->rx->minlen) {
772 XPUSHs(sv_2mortal(newSViv(self->rx->minlen)));
774 XPUSHs(sv_2mortal(&PL_sv_undef));
779 gofs(re::engine::Plugin self, ...)
782 self->rx->gofs = (U32)SvIV(ST(1));
785 if (self->rx->gofs) {
786 XPUSHs(sv_2mortal(newSVuv(self->rx->gofs)));
788 XPUSHs(sv_2mortal(&PL_sv_undef));
793 nparens(re::engine::Plugin self, ...)
796 self->rx->nparens = (U32)SvIV(ST(1));
799 if (self->rx->nparens) {
800 XPUSHs(sv_2mortal(newSVuv(self->rx->nparens)));
802 XPUSHs(sv_2mortal(&PL_sv_undef));
807 _exec(re::engine::Plugin self, ...)
810 SvREFCNT_dec(self->cb_exec);
811 self->cb_exec = ST(1);
812 SvREFCNT_inc_simple_void(self->cb_exec);
816 _num_capture_buff_FETCH(re::engine::Plugin self, ...)
819 SvREFCNT_dec(self->cb_num_capture_buff_FETCH);
820 self->cb_num_capture_buff_FETCH = ST(1);
821 SvREFCNT_inc_simple_void(self->cb_num_capture_buff_FETCH);
825 _num_capture_buff_STORE(re::engine::Plugin self, ...)
828 SvREFCNT_dec(self->cb_num_capture_buff_STORE);
829 self->cb_num_capture_buff_STORE = ST(1);
830 SvREFCNT_inc_simple_void(self->cb_num_capture_buff_STORE);
834 _num_capture_buff_LENGTH(re::engine::Plugin self, ...)
837 SvREFCNT_dec(self->cb_num_capture_buff_LENGTH);
838 self->cb_num_capture_buff_LENGTH = ST(1);
839 SvREFCNT_inc_simple_void(self->cb_num_capture_buff_LENGTH);
843 _tag(SV *comp, SV *exec)
845 RETVAL = rep_tag(comp, exec);
852 XPUSHs(sv_2mortal(newSViv(PTR2IV(&engine_plugin))));