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->cxreq = h1->cxreq;
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 #if REP_WORKAROUND_REQUIRE_PROPAGATION
170 STATIC IV rep_require_tag(pTHX) {
171 #define rep_require_tag() rep_require_tag(aTHX)
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 if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_REQUIRE)
187 #endif /* REP_WORKAROUND_REQUIRE_PROPAGATION */
189 STATIC SV *rep_tag(pTHX_ SV *comp, SV *exec) {
190 #define rep_tag(C, E) rep_tag(aTHX_ (C), (E))
194 h = PerlMemShared_malloc(sizeof *h);
195 h->comp = rep_validate_callback(comp);
196 h->exec = rep_validate_callback(exec);
197 #if REP_WORKAROUND_REQUIRE_PROPAGATION
198 h->cxreq = rep_require_tag();
199 #endif /* REP_WORKAROUND_REQUIRE_PROPAGATION */
202 /* We only need for the key to be an unique tag for looking up the value later.
203 * Allocated memory provides convenient unique identifiers, so that's why we
204 * use the hint as the key itself. */
205 ptable_store(MY_CXT.tbl, h, h);
206 #endif /* REP_THREADSAFE */
208 return newSViv(PTR2IV(h));
211 STATIC const rep_hint_t *rep_detag(pTHX_ const SV *hint) {
212 #define rep_detag(H) rep_detag(aTHX_ (H))
216 if (!(hint && SvIOK(hint)))
219 h = INT2PTR(rep_hint_t *, SvIVX(hint));
221 h = ptable_fetch(MY_CXT.tbl, h);
222 #endif /* REP_THREADSAFE */
224 #if REP_WORKAROUND_REQUIRE_PROPAGATION
225 if (rep_require_tag() != h->cxreq)
227 #endif /* REP_WORKAROUND_REQUIRE_PROPAGATION */
232 STATIC U32 rep_hash = 0;
234 STATIC const rep_hint_t *rep_hint(pTHX) {
235 #define rep_hint() rep_hint(aTHX)
238 /* We already require 5.9.5 for the regexp engine API. */
239 hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
241 __PACKAGE__, __PACKAGE_LEN__,
245 return rep_detag(hint);
249 #if PERL_VERSION <= 10
250 Plugin_comp(pTHX_ const SV * const pattern, const U32 flags)
252 Plugin_comp(pTHX_ SV * const pattern, U32 flags)
259 re__engine__Plugin re;
263 if (!h) /* This looks like a pragma leak. Apply the default behaviour */
264 return re_compile(pattern, flags);
266 /* exp/xend version of the pattern & length */
268 char* exp = SvPV((SV*)pattern, plen);
270 /* Our blessed object */
273 Newxz(re, 1, struct replug);
274 sv_setref_pv(obj, "re::engine::Plugin", (void*)re);
279 re->rx = rx; /* Make the rx accessible from self->rx */
280 rx->intflags = flags; /* Flags for internal use */
281 rx->extflags = flags; /* Flags for perl to use */
282 rx->engine = RE_ENGINE_PLUGIN; /* Compile to use this engine */
284 #if PERL_VERSION <= 10
285 rx->refcnt = 1; /* Refcount so we won't be destroyed */
287 /* Precompiled pattern for pp_regcomp to use */
289 rx->precomp = savepvn(exp, rx->prelen);
291 /* Set up qr// stringification to be equivalent to the supplied
292 * pattern, this should be done via overload eventually.
294 rx->wraplen = rx->prelen;
295 Newx(rx->wrapped, rx->wraplen, char);
296 Copy(rx->precomp, rx->wrapped, rx->wraplen, char);
299 /* Store our private object */
302 /* Store the pattern for ->pattern */
303 re->pattern = (SV*)pattern;
304 SvREFCNT_inc(re->pattern);
306 /* If there's an exec callback, store it into the private object so
307 * that it will be the one to be called, even if the engine changes
310 re->cb_exec = h->exec;
311 SvREFCNT_inc_simple_void_NN(h->exec);
314 re->cb_num_capture_buff_FETCH = NULL;
315 re->cb_num_capture_buff_STORE = NULL;
316 re->cb_num_capture_buff_LENGTH = NULL;
318 /* Call our callback function if one was defined, if not we've
319 * already set up all the stuff we're going to to need for
320 * subsequent exec and other calls */
329 call_sv(h->comp, G_DISCARD);
335 /* If any of the comp-time accessors were called we'll have to
336 * update the regexp struct with the new info.
339 buffers = rx->nparens;
341 Newxz(rx->offs, buffers + 1, regexp_paren_pair);
347 Plugin_exec(pTHX_ REGEXP * const RX, char *stringarg, char *strend,
348 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
352 struct regexp *rx = rxREGEXP(RX);
353 GET_SELF_FROM_PPRIVATE(rx->pprivate);
356 /* Store the current str for ->str */
358 SvREFCNT_inc(self->str);
364 XPUSHs(rx->pprivate);
368 call_sv(self->cb_exec, G_SCALAR);
390 Plugin_intuit(pTHX_ REGEXP * const RX, SV *sv, char *strpos,
391 char *strend, U32 flags, re_scream_pos_data *data)
395 PERL_UNUSED_ARG(strpos);
396 PERL_UNUSED_ARG(strend);
397 PERL_UNUSED_ARG(flags);
398 PERL_UNUSED_ARG(data);
403 Plugin_checkstr(pTHX_ REGEXP * const RX)
410 Plugin_free(pTHX_ REGEXP * const RX)
412 struct regexp *rx = rxREGEXP(RX);
413 GET_SELF_FROM_PPRIVATE(rx->pprivate);
415 SvREFCNT_dec(self->pattern);
416 SvREFCNT_dec(self->str);
418 SvREFCNT_dec(self->cb_exec);
420 SvREFCNT_dec(self->cb_num_capture_buff_FETCH);
421 SvREFCNT_dec(self->cb_num_capture_buff_STORE);
422 SvREFCNT_dec(self->cb_num_capture_buff_LENGTH);
431 callback = self->cb_free;
438 XPUSHs(rx->pprivate);
441 call_sv(callback, G_DISCARD);
452 Plugin_dupe(pTHX_ REGEXP * const RX, CLONE_PARAMS *param)
454 struct regexp *rx = rxREGEXP(RX);
455 Perl_croak(aTHX_ "dupe not supported yet");
461 Plugin_numbered_buff_FETCH(pTHX_ REGEXP * const RX, const I32 paren,
467 struct regexp *rx = rxREGEXP(RX);
468 GET_SELF_FROM_PPRIVATE(rx->pprivate);
470 callback = self->cb_num_capture_buff_FETCH;
477 XPUSHs(rx->pprivate);
478 XPUSHs(sv_2mortal(newSViv(paren)));
481 items = call_sv(callback, G_SCALAR);
489 sv_setsv(sv, &PL_sv_undef);
496 sv_setsv(sv, &PL_sv_undef);
501 Plugin_numbered_buff_STORE(pTHX_ REGEXP * const RX, const I32 paren,
502 SV const * const value)
506 struct regexp *rx = rxREGEXP(RX);
507 GET_SELF_FROM_PPRIVATE(rx->pprivate);
509 callback = self->cb_num_capture_buff_STORE;
516 XPUSHs(rx->pprivate);
517 XPUSHs(sv_2mortal(newSViv(paren)));
518 XPUSHs(SvREFCNT_inc((SV *) value));
521 call_sv(callback, G_DISCARD);
530 Plugin_numbered_buff_LENGTH(pTHX_ REGEXP * const RX, const SV * const sv,
535 struct regexp *rx = rxREGEXP(RX);
536 GET_SELF_FROM_PPRIVATE(rx->pprivate);
538 callback = self->cb_num_capture_buff_LENGTH;
545 XPUSHs(rx->pprivate);
546 XPUSHs(sv_2mortal(newSViv(paren)));
549 call_sv(callback, G_SCALAR);
561 /* TODO: call FETCH and get the length on that value */
568 Plugin_named_buff (pTHX_ REGEXP * const RX, SV * const key, SV * const value,
575 Plugin_named_buff_iter (pTHX_ REGEXP * const RX, const SV * const lastkey,
582 Plugin_package(pTHX_ REGEXP * const RX)
585 return newSVpvs("re::engine::Plugin");
590 STATIC U32 rep_initialized = 0;
592 STATIC void rep_teardown(pTHX_ void *root) {
595 if (!rep_initialized || aTHX != root)
598 ptable_free(MY_CXT.tbl);
603 STATIC void rep_setup(pTHX) {
604 #define rep_setup() rep_setup(aTHX)
609 MY_CXT.tbl = ptable_new();
612 call_atexit(rep_teardown, aTHX);
617 #else /* REP_THREADSAFE */
621 #endif /* !REP_THREADSAFE */
623 STATIC U32 rep_booted = 0;
625 /* --- XS ------------------------------------------------------------------ */
627 MODULE = re::engine::Plugin PACKAGE = re::engine::Plugin
634 PERL_HASH(rep_hash, __PACKAGE__, __PACKAGE_LEN__);
651 ud.tbl = t = ptable_new();
652 ud.owner = MY_CXT.owner;
653 ptable_walk(MY_CXT.tbl, rep_ptable_clone, &ud);
664 pattern(re::engine::Plugin self, ...)
666 XPUSHs(self->pattern);
669 str(re::engine::Plugin self, ...)
674 mod(re::engine::Plugin self, ...)
677 if (self->rx->intflags & PMf_FOLD) {
678 XPUSHs(sv_2mortal(newSVpvs("i")));
683 if (self->rx->intflags & PMf_MULTILINE) {
684 XPUSHs(sv_2mortal(newSVpvs("m")));
689 if (self->rx->intflags & PMf_SINGLELINE) {
690 XPUSHs(sv_2mortal(newSVpvs("s")));
695 if (self->rx->intflags & PMf_EXTENDED) {
696 XPUSHs(sv_2mortal(newSVpvs("x")));
701 if (self->rx->intflags & RXf_PMf_KEEPCOPY) {
702 XPUSHs(sv_2mortal(newSVpvs("p")));
707 stash(re::engine::Plugin self, ...)
711 SvREFCNT_inc(self->stash);
718 minlen(re::engine::Plugin self, ...)
721 self->rx->minlen = (I32)SvIV(ST(1));
724 if (self->rx->minlen) {
725 XPUSHs(sv_2mortal(newSViv(self->rx->minlen)));
727 XPUSHs(sv_2mortal(&PL_sv_undef));
732 gofs(re::engine::Plugin self, ...)
735 self->rx->gofs = (U32)SvIV(ST(1));
738 if (self->rx->gofs) {
739 XPUSHs(sv_2mortal(newSVuv(self->rx->gofs)));
741 XPUSHs(sv_2mortal(&PL_sv_undef));
746 nparens(re::engine::Plugin self, ...)
749 self->rx->nparens = (U32)SvIV(ST(1));
752 if (self->rx->nparens) {
753 XPUSHs(sv_2mortal(newSVuv(self->rx->nparens)));
755 XPUSHs(sv_2mortal(&PL_sv_undef));
760 _exec(re::engine::Plugin self, ...)
763 SvREFCNT_dec(self->cb_exec);
764 self->cb_exec = ST(1);
765 SvREFCNT_inc(self->cb_exec);
769 _num_capture_buff_FETCH(re::engine::Plugin self, ...)
772 SvREFCNT_dec(self->cb_num_capture_buff_FETCH);
773 self->cb_num_capture_buff_FETCH = ST(1);
774 SvREFCNT_inc(self->cb_num_capture_buff_FETCH);
778 _num_capture_buff_STORE(re::engine::Plugin self, ...)
781 SvREFCNT_dec(self->cb_num_capture_buff_STORE);
782 self->cb_num_capture_buff_STORE = ST(1);
783 SvREFCNT_inc(self->cb_num_capture_buff_STORE);
787 _num_capture_buff_LENGTH(re::engine::Plugin self, ...)
790 SvREFCNT_dec(self->cb_num_capture_buff_LENGTH);
791 self->cb_num_capture_buff_LENGTH = ST(1);
792 SvREFCNT_inc(self->cb_num_capture_buff_LENGTH);
796 _tag(SV *comp, SV *exec)
798 RETVAL = rep_tag(comp, exec);
805 XPUSHs(sv_2mortal(newSViv(PTR2IV(&engine_plugin))));