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 #ifndef REP_MULTIPLICITY
23 # if defined(MULTIPLICITY) || defined(PERL_IMPLICIT_CONTEXT)
24 # define REP_MULTIPLICITY 1
26 # define REP_MULTIPLICITY 0
29 #if REP_MULTIPLICITY && !defined(tTHX)
30 # define tTHX PerlInterpreter*
33 #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))
34 # define REP_THREADSAFE 1
36 # define MY_CXT_CLONE \
38 my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
39 Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
40 sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
43 # define REP_THREADSAFE 0
45 # define dMY_CXT dNOOP
47 # define MY_CXT rep_globaldata
49 # define START_MY_CXT STATIC my_cxt_t MY_CXT;
51 # define MY_CXT_INIT NOOP
53 # define MY_CXT_CLONE NOOP
56 /* --- Helpers ------------------------------------------------------------- */
58 /* ... Thread-safe hints ................................................... */
63 #if REP_WORKAROUND_REQUIRE_PROPAGATION
70 #define PTABLE_VAL_FREE(V) { \
71 rep_hint_t *h = (V); \
72 SvREFCNT_dec(h->comp); \
73 SvREFCNT_dec(h->exec); \
74 PerlMemShared_free(h); \
84 #define ptable_store(T, K, V) ptable_store(aTHX_ (T), (K), (V))
85 #define ptable_free(T) ptable_free(aTHX_ (T))
87 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
96 STATIC SV *rep_clone(pTHX_ SV *sv, tTHX owner) {
97 #define rep_clone(S, O) rep_clone(aTHX_ (S), (O))
102 if (SvTYPE(sv) == SVt_PVHV && HvNAME_get(sv))
105 param.stashes = stashes;
107 param.proto_perl = owner;
109 dupsv = sv_dup(sv, ¶m);
113 SvREFCNT_dec(stashes);
116 return SvREFCNT_inc(dupsv);
119 STATIC void rep_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
121 rep_hint_t *h1 = ent->val;
124 if (ud->owner == aTHX)
127 h2 = PerlMemShared_malloc(sizeof *h2);
128 h2->comp = rep_clone(h1->comp, ud->owner);
129 SvREFCNT_inc(h2->comp);
130 h2->exec = rep_clone(h1->exec, ud->owner);
131 SvREFCNT_inc(h2->exec);
132 #if REP_WORKAROUND_REQUIRE_PROPAGATION
133 h2->requires = h1->requires;
136 ptable_store(ud->tbl, ent->key, h2);
139 STATIC void rep_thread_cleanup(pTHX_ void *);
141 STATIC void rep_thread_cleanup(pTHX_ void *ud) {
147 SAVEDESTRUCTOR_X(rep_thread_cleanup, level);
151 PerlMemShared_free(level);
152 ptable_free(MY_CXT.tbl);
156 #endif /* REP_THREADSAFE */
158 STATIC SV *rep_validate_callback(SV *code) {
163 if (SvTYPE(code) < SVt_PVCV)
166 return SvREFCNT_inc_simple_NN(code);
169 STATIC SV *rep_tag(pTHX_ SV *comp, SV *exec) {
170 #define rep_tag(C, E) rep_tag(aTHX_ (C), (E))
174 h = PerlMemShared_malloc(sizeof *h);
176 h->comp = rep_validate_callback(comp);
177 h->exec = rep_validate_callback(exec);
179 #if REP_WORKAROUND_REQUIRE_PROPAGATION
184 for (si = PL_curstackinfo; si; si = si->si_prev) {
187 for (cxix = si->si_cxix; cxix >= 0; --cxix) {
188 const PERL_CONTEXT *cx = si->si_cxstack + cxix;
190 if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_REQUIRE)
195 h->requires = requires;
200 /* We only need for the key to be an unique tag for looking up the value later.
201 * Allocated memory provides convenient unique identifiers, so that's why we
202 * use the hint as the key itself. */
203 ptable_store(MY_CXT.tbl, h, h);
204 #endif /* REP_THREADSAFE */
206 return newSViv(PTR2IV(h));
209 STATIC const rep_hint_t *rep_detag(pTHX_ const SV *hint) {
210 #define rep_detag(H) rep_detag(aTHX_ (H))
214 if (!(hint && SvIOK(hint)))
217 h = INT2PTR(rep_hint_t *, SvIVX(hint));
219 h = ptable_fetch(MY_CXT.tbl, h);
220 #endif /* REP_THREADSAFE */
222 #if REP_WORKAROUND_REQUIRE_PROPAGATION
227 for (si = PL_curstackinfo; si; si = si->si_prev) {
230 for (cxix = si->si_cxix; cxix >= 0; --cxix) {
231 const PERL_CONTEXT *cx = si->si_cxstack + cxix;
233 if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_REQUIRE
234 && ++requires > h->requires)
244 STATIC U32 rep_hash = 0;
246 STATIC const rep_hint_t *rep_hint(pTHX) {
247 #define rep_hint() rep_hint(aTHX)
250 /* We already require 5.9.5 for the regexp engine API. */
251 hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
253 __PACKAGE__, __PACKAGE_LEN__,
257 return rep_detag(hint);
261 #if PERL_VERSION <= 10
262 Plugin_comp(pTHX_ const SV * const pattern, const U32 flags)
264 Plugin_comp(pTHX_ SV * const pattern, U32 flags)
271 re__engine__Plugin re;
274 /* exp/xend version of the pattern & length */
276 char* exp = SvPV((SV*)pattern, plen);
278 /* Our blessed object */
281 Newxz(re, 1, struct replug);
282 sv_setref_pv(obj, "re::engine::Plugin", (void*)re);
287 re->rx = rx; /* Make the rx accessible from self->rx */
288 rx->intflags = flags; /* Flags for internal use */
289 rx->extflags = flags; /* Flags for perl to use */
290 rx->engine = RE_ENGINE_PLUGIN; /* Compile to use this engine */
292 #if PERL_VERSION <= 10
293 rx->refcnt = 1; /* Refcount so we won't be destroyed */
295 /* Precompiled pattern for pp_regcomp to use */
297 rx->precomp = savepvn(exp, rx->prelen);
299 /* Set up qr// stringification to be equivalent to the supplied
300 * pattern, this should be done via overload eventually.
302 rx->wraplen = rx->prelen;
303 Newx(rx->wrapped, rx->wraplen, char);
304 Copy(rx->precomp, rx->wrapped, rx->wraplen, char);
307 /* Store our private object */
310 /* Store the pattern for ->pattern */
311 re->pattern = (SV*)pattern;
312 SvREFCNT_inc(re->pattern);
315 * Call our callback function if one was defined, if not we've
316 * already set up all the stuff we're going to to need for
317 * subsequent exec and other calls
328 call_sv(h->comp, G_DISCARD);
334 /* If any of the comp-time accessors were called we'll have to
335 * update the regexp struct with the new info.
338 buffers = rx->nparens;
340 Newxz(rx->offs, buffers + 1, regexp_paren_pair);
346 Plugin_exec(pTHX_ REGEXP * const RX, char *stringarg, char *strend,
347 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
351 struct regexp *rx = rxREGEXP(RX);
353 GET_SELF_FROM_PPRIVATE(rx->pprivate);
357 /* Store the current str for ->str */
359 SvREFCNT_inc(self->str);
365 XPUSHs(rx->pprivate);
369 call_sv(h->exec, G_SCALAR);
391 Plugin_intuit(pTHX_ REGEXP * const RX, SV *sv, char *strpos,
392 char *strend, U32 flags, re_scream_pos_data *data)
396 PERL_UNUSED_ARG(strpos);
397 PERL_UNUSED_ARG(strend);
398 PERL_UNUSED_ARG(flags);
399 PERL_UNUSED_ARG(data);
404 Plugin_checkstr(pTHX_ REGEXP * const RX)
411 Plugin_free(pTHX_ REGEXP * const RX)
417 GET_SELF_FROM_PPRIVATE(rx->pprivate);
419 callback = self->cb_free;
426 XPUSHs(rx->pprivate);
429 call_sv(callback, G_DISCARD);
440 Plugin_dupe(pTHX_ REGEXP * const RX, CLONE_PARAMS *param)
442 struct regexp *rx = rxREGEXP(RX);
443 Perl_croak(aTHX_ "dupe not supported yet");
449 Plugin_numbered_buff_FETCH(pTHX_ REGEXP * const RX, const I32 paren,
455 struct regexp *rx = rxREGEXP(RX);
456 GET_SELF_FROM_PPRIVATE(rx->pprivate);
458 callback = self->cb_num_capture_buff_FETCH;
465 XPUSHs(rx->pprivate);
466 XPUSHs(sv_2mortal(newSViv(paren)));
469 items = call_sv(callback, G_SCALAR);
477 sv_setsv(sv, &PL_sv_undef);
484 sv_setsv(sv, &PL_sv_undef);
489 Plugin_numbered_buff_STORE(pTHX_ REGEXP * const RX, const I32 paren,
490 SV const * const value)
494 struct regexp *rx = rxREGEXP(RX);
495 GET_SELF_FROM_PPRIVATE(rx->pprivate);
497 callback = self->cb_num_capture_buff_STORE;
504 XPUSHs(rx->pprivate);
505 XPUSHs(sv_2mortal(newSViv(paren)));
506 XPUSHs(SvREFCNT_inc((SV *) value));
509 call_sv(callback, G_DISCARD);
518 Plugin_numbered_buff_LENGTH(pTHX_ REGEXP * const RX, const SV * const sv,
523 struct regexp *rx = rxREGEXP(RX);
524 GET_SELF_FROM_PPRIVATE(rx->pprivate);
526 callback = self->cb_num_capture_buff_LENGTH;
533 XPUSHs(rx->pprivate);
534 XPUSHs(sv_2mortal(newSViv(paren)));
537 call_sv(callback, G_SCALAR);
549 /* TODO: call FETCH and get the length on that value */
556 Plugin_named_buff (pTHX_ REGEXP * const RX, SV * const key, SV * const value,
563 Plugin_named_buff_iter (pTHX_ REGEXP * const RX, const SV * const lastkey,
570 Plugin_package(pTHX_ REGEXP * const RX)
573 return newSVpvs("re::engine::Plugin");
578 STATIC U32 rep_initialized = 0;
580 STATIC void rep_teardown(pTHX_ void *root) {
583 if (!rep_initialized || aTHX != root)
586 ptable_free(MY_CXT.tbl);
591 STATIC void rep_setup(pTHX) {
592 #define rep_setup() rep_setup(aTHX)
597 MY_CXT.tbl = ptable_new();
600 call_atexit(rep_teardown, aTHX);
605 #else /* REP_THREADSAFE */
609 #endif /* !REP_THREADSAFE */
611 STATIC U32 rep_booted = 0;
613 /* --- XS ------------------------------------------------------------------ */
615 MODULE = re::engine::Plugin PACKAGE = re::engine::Plugin
622 PERL_HASH(rep_hash, __PACKAGE__, __PACKAGE_LEN__);
639 ud.tbl = t = ptable_new();
640 ud.owner = MY_CXT.owner;
641 ptable_walk(MY_CXT.tbl, rep_ptable_clone, &ud);
652 pattern(re::engine::Plugin self, ...)
654 XPUSHs(self->pattern);
657 str(re::engine::Plugin self, ...)
662 mod(re::engine::Plugin self, ...)
665 if (self->rx->intflags & PMf_FOLD) {
666 XPUSHs(sv_2mortal(newSVpvs("i")));
671 if (self->rx->intflags & PMf_MULTILINE) {
672 XPUSHs(sv_2mortal(newSVpvs("m")));
677 if (self->rx->intflags & PMf_SINGLELINE) {
678 XPUSHs(sv_2mortal(newSVpvs("s")));
683 if (self->rx->intflags & PMf_EXTENDED) {
684 XPUSHs(sv_2mortal(newSVpvs("x")));
689 if (self->rx->intflags & RXf_PMf_KEEPCOPY) {
690 XPUSHs(sv_2mortal(newSVpvs("p")));
695 stash(re::engine::Plugin self, ...)
699 SvREFCNT_inc(self->stash);
706 minlen(re::engine::Plugin self, ...)
709 self->rx->minlen = (I32)SvIV(ST(1));
712 if (self->rx->minlen) {
713 XPUSHs(sv_2mortal(newSViv(self->rx->minlen)));
715 XPUSHs(sv_2mortal(&PL_sv_undef));
720 gofs(re::engine::Plugin self, ...)
723 self->rx->gofs = (U32)SvIV(ST(1));
726 if (self->rx->gofs) {
727 XPUSHs(sv_2mortal(newSVuv(self->rx->gofs)));
729 XPUSHs(sv_2mortal(&PL_sv_undef));
734 nparens(re::engine::Plugin self, ...)
737 self->rx->nparens = (U32)SvIV(ST(1));
740 if (self->rx->nparens) {
741 XPUSHs(sv_2mortal(newSVuv(self->rx->nparens)));
743 XPUSHs(sv_2mortal(&PL_sv_undef));
748 _num_capture_buff_FETCH(re::engine::Plugin self, ...)
751 self->cb_num_capture_buff_FETCH = ST(1);
752 SvREFCNT_inc(self->cb_num_capture_buff_FETCH);
756 _num_capture_buff_STORE(re::engine::Plugin self, ...)
759 self->cb_num_capture_buff_STORE = ST(1);
760 SvREFCNT_inc(self->cb_num_capture_buff_STORE);
764 _num_capture_buff_LENGTH(re::engine::Plugin self, ...)
767 self->cb_num_capture_buff_LENGTH = ST(1);
768 SvREFCNT_inc(self->cb_num_capture_buff_LENGTH);
772 _tag(SV *comp, SV *exec)
774 RETVAL = rep_tag(comp, exec);
781 XPUSHs(sv_2mortal(newSViv(PTR2IV(&engine_plugin))));