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))))))
17 #if defined(ENTER_with_name) && !REP_HAS_PERL(5, 11, 4)
18 # define ENTERn(N) ENTER_with_name(N)
20 # define ENTERn(N) ENTER
24 #if defined(LEAVE_with_name) && !REP_HAS_PERL(5, 11, 4)
25 # define LEAVEn(N) LEAVE_with_name(N)
27 # define LEAVEn(N) LEAVE
30 #ifndef REP_WORKAROUND_REQUIRE_PROPAGATION
31 # define REP_WORKAROUND_REQUIRE_PROPAGATION !REP_HAS_PERL(5, 10, 1)
34 /* ... Thread safety and multiplicity ...................................... */
36 #ifndef REP_MULTIPLICITY
37 # if defined(MULTIPLICITY) || defined(PERL_IMPLICIT_CONTEXT)
38 # define REP_MULTIPLICITY 1
40 # define REP_MULTIPLICITY 0
43 #if REP_MULTIPLICITY && !defined(tTHX)
44 # define tTHX PerlInterpreter*
47 #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))
48 # define REP_THREADSAFE 1
50 # define MY_CXT_CLONE \
52 my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
53 Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
54 sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
57 # define REP_THREADSAFE 0
59 # define dMY_CXT dNOOP
61 # define MY_CXT rep_globaldata
63 # define START_MY_CXT STATIC my_cxt_t MY_CXT;
65 # define MY_CXT_INIT NOOP
67 # define MY_CXT_CLONE NOOP
70 /* --- Helpers ------------------------------------------------------------- */
72 /* ... Thread-safe hints ................................................... */
77 #if REP_WORKAROUND_REQUIRE_PROPAGATION
84 #define PTABLE_VAL_FREE(V) { \
85 rep_hint_t *h = (V); \
86 SvREFCNT_dec(h->comp); \
87 SvREFCNT_dec(h->exec); \
88 PerlMemShared_free(h); \
98 #define ptable_store(T, K, V) ptable_store(aTHX_ (T), (K), (V))
99 #define ptable_free(T) ptable_free(aTHX_ (T))
101 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
110 STATIC SV *rep_clone(pTHX_ SV *sv, tTHX owner) {
111 #define rep_clone(S, O) rep_clone(aTHX_ (S), (O))
116 if (SvTYPE(sv) == SVt_PVHV && HvNAME_get(sv))
119 param.stashes = stashes;
121 param.proto_perl = owner;
123 dupsv = sv_dup(sv, ¶m);
127 SvREFCNT_dec(stashes);
130 return SvREFCNT_inc(dupsv);
133 STATIC void rep_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
135 rep_hint_t *h1 = ent->val;
138 if (ud->owner == aTHX)
141 h2 = PerlMemShared_malloc(sizeof *h2);
142 h2->comp = rep_clone(h1->comp, ud->owner);
143 SvREFCNT_inc(h2->comp);
144 h2->exec = rep_clone(h1->exec, ud->owner);
145 SvREFCNT_inc(h2->exec);
146 #if REP_WORKAROUND_REQUIRE_PROPAGATION
147 h2->cxreq = h1->cxreq;
150 ptable_store(ud->tbl, ent->key, h2);
155 STATIC void rep_thread_cleanup(pTHX_ void *ud) {
158 ptable_free(MY_CXT.tbl);
161 #endif /* REP_THREADSAFE */
163 STATIC SV *rep_validate_callback(SV *code) {
168 if (SvTYPE(code) < SVt_PVCV)
171 return SvREFCNT_inc_simple_NN(code);
174 #if REP_WORKAROUND_REQUIRE_PROPAGATION
175 STATIC IV rep_require_tag(pTHX) {
176 #define rep_require_tag() rep_require_tag(aTHX)
179 for (si = PL_curstackinfo; si; si = si->si_prev) {
182 for (cxix = si->si_cxix; cxix >= 0; --cxix) {
183 const PERL_CONTEXT *cx = si->si_cxstack + cxix;
185 if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_REQUIRE)
192 #endif /* REP_WORKAROUND_REQUIRE_PROPAGATION */
194 STATIC SV *rep_tag(pTHX_ SV *comp, SV *exec) {
195 #define rep_tag(C, E) rep_tag(aTHX_ (C), (E))
199 h = PerlMemShared_malloc(sizeof *h);
200 h->comp = rep_validate_callback(comp);
201 h->exec = rep_validate_callback(exec);
202 #if REP_WORKAROUND_REQUIRE_PROPAGATION
203 h->cxreq = rep_require_tag();
204 #endif /* REP_WORKAROUND_REQUIRE_PROPAGATION */
207 /* We only need for the key to be an unique tag for looking up the value later.
208 * Allocated memory provides convenient unique identifiers, so that's why we
209 * use the hint as the key itself. */
210 ptable_store(MY_CXT.tbl, h, h);
211 #endif /* REP_THREADSAFE */
213 return newSViv(PTR2IV(h));
216 STATIC const rep_hint_t *rep_detag(pTHX_ const SV *hint) {
217 #define rep_detag(H) rep_detag(aTHX_ (H))
221 if (!(hint && SvIOK(hint)))
224 h = INT2PTR(rep_hint_t *, SvIVX(hint));
226 h = ptable_fetch(MY_CXT.tbl, h);
227 #endif /* REP_THREADSAFE */
229 #if REP_WORKAROUND_REQUIRE_PROPAGATION
230 if (rep_require_tag() != h->cxreq)
232 #endif /* REP_WORKAROUND_REQUIRE_PROPAGATION */
237 STATIC U32 rep_hash = 0;
239 STATIC const rep_hint_t *rep_hint(pTHX) {
240 #define rep_hint() rep_hint(aTHX)
243 #ifdef cop_hints_fetch_pvn
244 hint = cop_hints_fetch_pvn(PL_curcop,
245 __PACKAGE__, __PACKAGE_LEN__, rep_hash, 0);
247 /* We already require 5.9.5 for the regexp engine API. */
248 hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
250 __PACKAGE__, __PACKAGE_LEN__,
255 return rep_detag(hint);
259 #if PERL_VERSION <= 10
260 Plugin_comp(pTHX_ const SV * const pattern, const U32 flags)
262 Plugin_comp(pTHX_ SV * const pattern, U32 flags)
269 re__engine__Plugin re;
273 if (!h) /* This looks like a pragma leak. Apply the default behaviour */
274 return re_compile(pattern, flags);
276 /* exp/xend version of the pattern & length */
278 char* exp = SvPV((SV*)pattern, plen);
280 /* Our blessed object */
283 Newxz(re, 1, struct replug);
284 sv_setref_pv(obj, "re::engine::Plugin", (void*)re);
289 re->rx = rx; /* Make the rx accessible from self->rx */
290 rx->intflags = flags; /* Flags for internal use */
291 rx->extflags = flags; /* Flags for perl to use */
292 rx->engine = RE_ENGINE_PLUGIN; /* Compile to use this engine */
294 #if PERL_VERSION <= 10
295 rx->refcnt = 1; /* Refcount so we won't be destroyed */
297 /* Precompiled pattern for pp_regcomp to use */
299 rx->precomp = savepvn(exp, rx->prelen);
301 /* Set up qr// stringification to be equivalent to the supplied
302 * pattern, this should be done via overload eventually.
304 rx->wraplen = rx->prelen;
305 Newx(rx->wrapped, rx->wraplen, char);
306 Copy(rx->precomp, rx->wrapped, rx->wraplen, char);
309 /* Store our private object */
312 /* Store the pattern for ->pattern */
313 re->pattern = (SV*)pattern;
314 SvREFCNT_inc(re->pattern);
316 /* If there's an exec callback, store it into the private object so
317 * that it will be the one to be called, even if the engine changes
320 re->cb_exec = h->exec;
321 SvREFCNT_inc_simple_void_NN(h->exec);
324 re->cb_num_capture_buff_FETCH = NULL;
325 re->cb_num_capture_buff_STORE = NULL;
326 re->cb_num_capture_buff_LENGTH = NULL;
328 /* Call our callback function if one was defined, if not we've
329 * already set up all the stuff we're going to to need for
330 * subsequent exec and other calls */
339 call_sv(h->comp, G_DISCARD);
345 /* If any of the comp-time accessors were called we'll have to
346 * update the regexp struct with the new info.
349 buffers = rx->nparens;
351 Newxz(rx->offs, buffers + 1, regexp_paren_pair);
357 Plugin_exec(pTHX_ REGEXP * const RX, char *stringarg, char *strend,
358 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
362 struct regexp *rx = rxREGEXP(RX);
363 GET_SELF_FROM_PPRIVATE(rx->pprivate);
366 /* Store the current str for ->str */
368 SvREFCNT_inc(self->str);
374 XPUSHs(rx->pprivate);
378 call_sv(self->cb_exec, G_SCALAR);
400 Plugin_intuit(pTHX_ REGEXP * const RX, SV *sv, char *strpos,
401 char *strend, U32 flags, re_scream_pos_data *data)
405 PERL_UNUSED_ARG(strpos);
406 PERL_UNUSED_ARG(strend);
407 PERL_UNUSED_ARG(flags);
408 PERL_UNUSED_ARG(data);
413 Plugin_checkstr(pTHX_ REGEXP * const RX)
420 Plugin_free(pTHX_ REGEXP * const RX)
422 struct regexp *rx = rxREGEXP(RX);
423 GET_SELF_FROM_PPRIVATE(rx->pprivate);
425 SvREFCNT_dec(self->pattern);
426 SvREFCNT_dec(self->str);
428 SvREFCNT_dec(self->cb_exec);
430 SvREFCNT_dec(self->cb_num_capture_buff_FETCH);
431 SvREFCNT_dec(self->cb_num_capture_buff_STORE);
432 SvREFCNT_dec(self->cb_num_capture_buff_LENGTH);
441 callback = self->cb_free;
448 XPUSHs(rx->pprivate);
451 call_sv(callback, G_DISCARD);
462 Plugin_dupe(pTHX_ REGEXP * const RX, CLONE_PARAMS *param)
464 struct regexp *rx = rxREGEXP(RX);
465 Perl_croak(aTHX_ "dupe not supported yet");
471 Plugin_numbered_buff_FETCH(pTHX_ REGEXP * const RX, const I32 paren,
477 struct regexp *rx = rxREGEXP(RX);
478 GET_SELF_FROM_PPRIVATE(rx->pprivate);
480 callback = self->cb_num_capture_buff_FETCH;
487 XPUSHs(rx->pprivate);
488 XPUSHs(sv_2mortal(newSViv(paren)));
491 items = call_sv(callback, G_SCALAR);
499 sv_setsv(sv, &PL_sv_undef);
506 sv_setsv(sv, &PL_sv_undef);
511 Plugin_numbered_buff_STORE(pTHX_ REGEXP * const RX, const I32 paren,
512 SV const * const value)
516 struct regexp *rx = rxREGEXP(RX);
517 GET_SELF_FROM_PPRIVATE(rx->pprivate);
519 callback = self->cb_num_capture_buff_STORE;
526 XPUSHs(rx->pprivate);
527 XPUSHs(sv_2mortal(newSViv(paren)));
528 XPUSHs(SvREFCNT_inc((SV *) value));
531 call_sv(callback, G_DISCARD);
540 Plugin_numbered_buff_LENGTH(pTHX_ REGEXP * const RX, const SV * const sv,
545 struct regexp *rx = rxREGEXP(RX);
546 GET_SELF_FROM_PPRIVATE(rx->pprivate);
548 callback = self->cb_num_capture_buff_LENGTH;
555 XPUSHs(rx->pprivate);
556 XPUSHs(sv_2mortal(newSViv(paren)));
559 call_sv(callback, G_SCALAR);
571 /* TODO: call FETCH and get the length on that value */
578 Plugin_named_buff (pTHX_ REGEXP * const RX, SV * const key, SV * const value,
585 Plugin_named_buff_iter (pTHX_ REGEXP * const RX, const SV * const lastkey,
592 Plugin_package(pTHX_ REGEXP * const RX)
595 return newSVpvs("re::engine::Plugin");
600 STATIC U32 rep_initialized = 0;
602 STATIC void rep_teardown(pTHX_ void *root) {
605 if (!rep_initialized || aTHX != root)
608 ptable_free(MY_CXT.tbl);
613 STATIC void rep_setup(pTHX) {
614 #define rep_setup() rep_setup(aTHX)
619 MY_CXT.tbl = ptable_new();
622 call_atexit(rep_teardown, aTHX);
627 #else /* REP_THREADSAFE */
631 #endif /* !REP_THREADSAFE */
633 STATIC U32 rep_booted = 0;
635 /* --- XS ------------------------------------------------------------------ */
637 MODULE = re::engine::Plugin PACKAGE = re::engine::Plugin
644 PERL_HASH(rep_hash, __PACKAGE__, __PACKAGE_LEN__);
660 ud.tbl = t = ptable_new();
661 ud.owner = MY_CXT.owner;
662 ptable_walk(MY_CXT.tbl, rep_ptable_clone, &ud);
669 reap(3, rep_thread_cleanup, NULL);
672 #endif /* REP_THREADSAFE */
675 pattern(re::engine::Plugin self, ...)
677 XPUSHs(self->pattern);
680 str(re::engine::Plugin self, ...)
685 mod(re::engine::Plugin self, ...)
688 if (self->rx->intflags & PMf_FOLD) {
689 XPUSHs(sv_2mortal(newSVpvs("i")));
694 if (self->rx->intflags & PMf_MULTILINE) {
695 XPUSHs(sv_2mortal(newSVpvs("m")));
700 if (self->rx->intflags & PMf_SINGLELINE) {
701 XPUSHs(sv_2mortal(newSVpvs("s")));
706 if (self->rx->intflags & PMf_EXTENDED) {
707 XPUSHs(sv_2mortal(newSVpvs("x")));
712 if (self->rx->intflags & RXf_PMf_KEEPCOPY) {
713 XPUSHs(sv_2mortal(newSVpvs("p")));
718 stash(re::engine::Plugin self, ...)
722 SvREFCNT_inc(self->stash);
729 minlen(re::engine::Plugin self, ...)
732 self->rx->minlen = (I32)SvIV(ST(1));
735 if (self->rx->minlen) {
736 XPUSHs(sv_2mortal(newSViv(self->rx->minlen)));
738 XPUSHs(sv_2mortal(&PL_sv_undef));
743 gofs(re::engine::Plugin self, ...)
746 self->rx->gofs = (U32)SvIV(ST(1));
749 if (self->rx->gofs) {
750 XPUSHs(sv_2mortal(newSVuv(self->rx->gofs)));
752 XPUSHs(sv_2mortal(&PL_sv_undef));
757 nparens(re::engine::Plugin self, ...)
760 self->rx->nparens = (U32)SvIV(ST(1));
763 if (self->rx->nparens) {
764 XPUSHs(sv_2mortal(newSVuv(self->rx->nparens)));
766 XPUSHs(sv_2mortal(&PL_sv_undef));
771 _exec(re::engine::Plugin self, ...)
774 SvREFCNT_dec(self->cb_exec);
775 self->cb_exec = ST(1);
776 SvREFCNT_inc(self->cb_exec);
780 _num_capture_buff_FETCH(re::engine::Plugin self, ...)
783 SvREFCNT_dec(self->cb_num_capture_buff_FETCH);
784 self->cb_num_capture_buff_FETCH = ST(1);
785 SvREFCNT_inc(self->cb_num_capture_buff_FETCH);
789 _num_capture_buff_STORE(re::engine::Plugin self, ...)
792 SvREFCNT_dec(self->cb_num_capture_buff_STORE);
793 self->cb_num_capture_buff_STORE = ST(1);
794 SvREFCNT_inc(self->cb_num_capture_buff_STORE);
798 _num_capture_buff_LENGTH(re::engine::Plugin self, ...)
801 SvREFCNT_dec(self->cb_num_capture_buff_LENGTH);
802 self->cb_num_capture_buff_LENGTH = ST(1);
803 SvREFCNT_inc(self->cb_num_capture_buff_LENGTH);
807 _tag(SV *comp, SV *exec)
809 RETVAL = rep_tag(comp, exec);
816 XPUSHs(sv_2mortal(newSViv(PTR2IV(&engine_plugin))));