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 #ifndef ENTER_with_name
15 # define ENTER_with_name(N) ENTER
18 #ifndef LEAVE_with_name
19 # define LEAVE_with_name(N) LEAVE
22 #define REP_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
24 #ifndef REP_WORKAROUND_REQUIRE_PROPAGATION
25 # define REP_WORKAROUND_REQUIRE_PROPAGATION !REP_HAS_PERL(5, 10, 1)
28 /* ... Thread safety and multiplicity ...................................... */
30 #ifndef REP_MULTIPLICITY
31 # if defined(MULTIPLICITY) || defined(PERL_IMPLICIT_CONTEXT)
32 # define REP_MULTIPLICITY 1
34 # define REP_MULTIPLICITY 0
37 #if REP_MULTIPLICITY && !defined(tTHX)
38 # define tTHX PerlInterpreter*
41 #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))
42 # define REP_THREADSAFE 1
44 # define MY_CXT_CLONE \
46 my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
47 Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
48 sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
51 # define REP_THREADSAFE 0
53 # define dMY_CXT dNOOP
55 # define MY_CXT rep_globaldata
57 # define START_MY_CXT STATIC my_cxt_t MY_CXT;
59 # define MY_CXT_INIT NOOP
61 # define MY_CXT_CLONE NOOP
64 /* --- Helpers ------------------------------------------------------------- */
66 /* ... Thread-safe hints ................................................... */
71 #if REP_WORKAROUND_REQUIRE_PROPAGATION
78 #define PTABLE_VAL_FREE(V) { \
79 rep_hint_t *h = (V); \
80 SvREFCNT_dec(h->comp); \
81 SvREFCNT_dec(h->exec); \
82 PerlMemShared_free(h); \
92 #define ptable_store(T, K, V) ptable_store(aTHX_ (T), (K), (V))
93 #define ptable_free(T) ptable_free(aTHX_ (T))
95 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
104 STATIC SV *rep_clone(pTHX_ SV *sv, tTHX owner) {
105 #define rep_clone(S, O) rep_clone(aTHX_ (S), (O))
110 if (SvTYPE(sv) == SVt_PVHV && HvNAME_get(sv))
113 param.stashes = stashes;
115 param.proto_perl = owner;
117 dupsv = sv_dup(sv, ¶m);
121 SvREFCNT_dec(stashes);
124 return SvREFCNT_inc(dupsv);
127 STATIC void rep_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
129 rep_hint_t *h1 = ent->val;
132 if (ud->owner == aTHX)
135 h2 = PerlMemShared_malloc(sizeof *h2);
136 h2->comp = rep_clone(h1->comp, ud->owner);
137 SvREFCNT_inc(h2->comp);
138 h2->exec = rep_clone(h1->exec, ud->owner);
139 SvREFCNT_inc(h2->exec);
140 #if REP_WORKAROUND_REQUIRE_PROPAGATION
141 h2->cxreq = h1->cxreq;
144 ptable_store(ud->tbl, ent->key, h2);
147 STATIC void rep_thread_cleanup(pTHX_ void *);
149 STATIC void rep_thread_cleanup(pTHX_ void *ud) {
155 SAVEDESTRUCTOR_X(rep_thread_cleanup, level);
159 PerlMemShared_free(level);
160 ptable_free(MY_CXT.tbl);
164 #endif /* REP_THREADSAFE */
166 STATIC SV *rep_validate_callback(SV *code) {
171 if (SvTYPE(code) < SVt_PVCV)
174 return SvREFCNT_inc_simple_NN(code);
177 #if REP_WORKAROUND_REQUIRE_PROPAGATION
178 STATIC IV rep_require_tag(pTHX) {
179 #define rep_require_tag() rep_require_tag(aTHX)
182 for (si = PL_curstackinfo; si; si = si->si_prev) {
185 for (cxix = si->si_cxix; cxix >= 0; --cxix) {
186 const PERL_CONTEXT *cx = si->si_cxstack + cxix;
188 if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_REQUIRE)
195 #endif /* REP_WORKAROUND_REQUIRE_PROPAGATION */
197 STATIC SV *rep_tag(pTHX_ SV *comp, SV *exec) {
198 #define rep_tag(C, E) rep_tag(aTHX_ (C), (E))
202 h = PerlMemShared_malloc(sizeof *h);
203 h->comp = rep_validate_callback(comp);
204 h->exec = rep_validate_callback(exec);
205 #if REP_WORKAROUND_REQUIRE_PROPAGATION
206 h->cxreq = rep_require_tag();
207 #endif /* REP_WORKAROUND_REQUIRE_PROPAGATION */
210 /* We only need for the key to be an unique tag for looking up the value later.
211 * Allocated memory provides convenient unique identifiers, so that's why we
212 * use the hint as the key itself. */
213 ptable_store(MY_CXT.tbl, h, h);
214 #endif /* REP_THREADSAFE */
216 return newSViv(PTR2IV(h));
219 STATIC const rep_hint_t *rep_detag(pTHX_ const SV *hint) {
220 #define rep_detag(H) rep_detag(aTHX_ (H))
224 if (!(hint && SvIOK(hint)))
227 h = INT2PTR(rep_hint_t *, SvIVX(hint));
229 h = ptable_fetch(MY_CXT.tbl, h);
230 #endif /* REP_THREADSAFE */
232 #if REP_WORKAROUND_REQUIRE_PROPAGATION
233 if (rep_require_tag() != h->cxreq)
235 #endif /* REP_WORKAROUND_REQUIRE_PROPAGATION */
240 STATIC U32 rep_hash = 0;
242 STATIC const rep_hint_t *rep_hint(pTHX) {
243 #define rep_hint() rep_hint(aTHX)
246 /* We already require 5.9.5 for the regexp engine API. */
247 hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
249 __PACKAGE__, __PACKAGE_LEN__,
253 return rep_detag(hint);
257 #if PERL_VERSION <= 10
258 Plugin_comp(pTHX_ const SV * const pattern, const U32 flags)
260 Plugin_comp(pTHX_ SV * const pattern, U32 flags)
267 re__engine__Plugin re;
271 if (!h) /* This looks like a pragma leak. Apply the default behaviour */
272 return re_compile(pattern, flags);
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);
314 /* If there's an exec callback, store it into the private object so
315 * that it will be the one to be called, even if the engine changes
318 re->cb_exec = h->exec;
319 SvREFCNT_inc_simple_void_NN(h->exec);
322 re->cb_num_capture_buff_FETCH = NULL;
323 re->cb_num_capture_buff_STORE = NULL;
324 re->cb_num_capture_buff_LENGTH = NULL;
326 /* Call our callback function if one was defined, if not we've
327 * already set up all the stuff we're going to to need for
328 * subsequent exec and other calls */
337 call_sv(h->comp, G_DISCARD);
343 /* If any of the comp-time accessors were called we'll have to
344 * update the regexp struct with the new info.
347 buffers = rx->nparens;
349 Newxz(rx->offs, buffers + 1, regexp_paren_pair);
355 Plugin_exec(pTHX_ REGEXP * const RX, char *stringarg, char *strend,
356 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
360 struct regexp *rx = rxREGEXP(RX);
361 GET_SELF_FROM_PPRIVATE(rx->pprivate);
364 /* Store the current str for ->str */
366 SvREFCNT_inc(self->str);
372 XPUSHs(rx->pprivate);
376 call_sv(self->cb_exec, G_SCALAR);
398 Plugin_intuit(pTHX_ REGEXP * const RX, SV *sv, char *strpos,
399 char *strend, U32 flags, re_scream_pos_data *data)
403 PERL_UNUSED_ARG(strpos);
404 PERL_UNUSED_ARG(strend);
405 PERL_UNUSED_ARG(flags);
406 PERL_UNUSED_ARG(data);
411 Plugin_checkstr(pTHX_ REGEXP * const RX)
418 Plugin_free(pTHX_ REGEXP * const RX)
420 struct regexp *rx = rxREGEXP(RX);
421 GET_SELF_FROM_PPRIVATE(rx->pprivate);
423 SvREFCNT_dec(self->pattern);
424 SvREFCNT_dec(self->str);
426 SvREFCNT_dec(self->cb_exec);
428 SvREFCNT_dec(self->cb_num_capture_buff_FETCH);
429 SvREFCNT_dec(self->cb_num_capture_buff_STORE);
430 SvREFCNT_dec(self->cb_num_capture_buff_LENGTH);
439 callback = self->cb_free;
446 XPUSHs(rx->pprivate);
449 call_sv(callback, G_DISCARD);
460 Plugin_dupe(pTHX_ REGEXP * const RX, CLONE_PARAMS *param)
462 struct regexp *rx = rxREGEXP(RX);
463 Perl_croak(aTHX_ "dupe not supported yet");
469 Plugin_numbered_buff_FETCH(pTHX_ REGEXP * const RX, const I32 paren,
475 struct regexp *rx = rxREGEXP(RX);
476 GET_SELF_FROM_PPRIVATE(rx->pprivate);
478 callback = self->cb_num_capture_buff_FETCH;
485 XPUSHs(rx->pprivate);
486 XPUSHs(sv_2mortal(newSViv(paren)));
489 items = call_sv(callback, G_SCALAR);
497 sv_setsv(sv, &PL_sv_undef);
504 sv_setsv(sv, &PL_sv_undef);
509 Plugin_numbered_buff_STORE(pTHX_ REGEXP * const RX, const I32 paren,
510 SV const * const value)
514 struct regexp *rx = rxREGEXP(RX);
515 GET_SELF_FROM_PPRIVATE(rx->pprivate);
517 callback = self->cb_num_capture_buff_STORE;
524 XPUSHs(rx->pprivate);
525 XPUSHs(sv_2mortal(newSViv(paren)));
526 XPUSHs(SvREFCNT_inc((SV *) value));
529 call_sv(callback, G_DISCARD);
538 Plugin_numbered_buff_LENGTH(pTHX_ REGEXP * const RX, const SV * const sv,
543 struct regexp *rx = rxREGEXP(RX);
544 GET_SELF_FROM_PPRIVATE(rx->pprivate);
546 callback = self->cb_num_capture_buff_LENGTH;
553 XPUSHs(rx->pprivate);
554 XPUSHs(sv_2mortal(newSViv(paren)));
557 call_sv(callback, G_SCALAR);
569 /* TODO: call FETCH and get the length on that value */
576 Plugin_named_buff (pTHX_ REGEXP * const RX, SV * const key, SV * const value,
583 Plugin_named_buff_iter (pTHX_ REGEXP * const RX, const SV * const lastkey,
590 Plugin_package(pTHX_ REGEXP * const RX)
593 return newSVpvs("re::engine::Plugin");
598 STATIC U32 rep_initialized = 0;
600 STATIC void rep_teardown(pTHX_ void *root) {
603 if (!rep_initialized || aTHX != root)
606 ptable_free(MY_CXT.tbl);
611 STATIC void rep_setup(pTHX) {
612 #define rep_setup() rep_setup(aTHX)
617 MY_CXT.tbl = ptable_new();
620 call_atexit(rep_teardown, aTHX);
625 #else /* REP_THREADSAFE */
629 #endif /* !REP_THREADSAFE */
631 STATIC U32 rep_booted = 0;
633 /* --- XS ------------------------------------------------------------------ */
635 MODULE = re::engine::Plugin PACKAGE = re::engine::Plugin
642 PERL_HASH(rep_hash, __PACKAGE__, __PACKAGE_LEN__);
659 ud.tbl = t = ptable_new();
660 ud.owner = MY_CXT.owner;
661 ptable_walk(MY_CXT.tbl, rep_ptable_clone, &ud);
669 level = PerlMemShared_malloc(sizeof *level);
671 LEAVE_with_name("sub");
672 SAVEDESTRUCTOR_X(rep_thread_cleanup, level);
673 ENTER_with_name("sub");
679 pattern(re::engine::Plugin self, ...)
681 XPUSHs(self->pattern);
684 str(re::engine::Plugin self, ...)
689 mod(re::engine::Plugin self, ...)
692 if (self->rx->intflags & PMf_FOLD) {
693 XPUSHs(sv_2mortal(newSVpvs("i")));
698 if (self->rx->intflags & PMf_MULTILINE) {
699 XPUSHs(sv_2mortal(newSVpvs("m")));
704 if (self->rx->intflags & PMf_SINGLELINE) {
705 XPUSHs(sv_2mortal(newSVpvs("s")));
710 if (self->rx->intflags & PMf_EXTENDED) {
711 XPUSHs(sv_2mortal(newSVpvs("x")));
716 if (self->rx->intflags & RXf_PMf_KEEPCOPY) {
717 XPUSHs(sv_2mortal(newSVpvs("p")));
722 stash(re::engine::Plugin self, ...)
726 SvREFCNT_inc(self->stash);
733 minlen(re::engine::Plugin self, ...)
736 self->rx->minlen = (I32)SvIV(ST(1));
739 if (self->rx->minlen) {
740 XPUSHs(sv_2mortal(newSViv(self->rx->minlen)));
742 XPUSHs(sv_2mortal(&PL_sv_undef));
747 gofs(re::engine::Plugin self, ...)
750 self->rx->gofs = (U32)SvIV(ST(1));
753 if (self->rx->gofs) {
754 XPUSHs(sv_2mortal(newSVuv(self->rx->gofs)));
756 XPUSHs(sv_2mortal(&PL_sv_undef));
761 nparens(re::engine::Plugin self, ...)
764 self->rx->nparens = (U32)SvIV(ST(1));
767 if (self->rx->nparens) {
768 XPUSHs(sv_2mortal(newSVuv(self->rx->nparens)));
770 XPUSHs(sv_2mortal(&PL_sv_undef));
775 _exec(re::engine::Plugin self, ...)
778 SvREFCNT_dec(self->cb_exec);
779 self->cb_exec = ST(1);
780 SvREFCNT_inc(self->cb_exec);
784 _num_capture_buff_FETCH(re::engine::Plugin self, ...)
787 SvREFCNT_dec(self->cb_num_capture_buff_FETCH);
788 self->cb_num_capture_buff_FETCH = ST(1);
789 SvREFCNT_inc(self->cb_num_capture_buff_FETCH);
793 _num_capture_buff_STORE(re::engine::Plugin self, ...)
796 SvREFCNT_dec(self->cb_num_capture_buff_STORE);
797 self->cb_num_capture_buff_STORE = ST(1);
798 SvREFCNT_inc(self->cb_num_capture_buff_STORE);
802 _num_capture_buff_LENGTH(re::engine::Plugin self, ...)
805 SvREFCNT_dec(self->cb_num_capture_buff_LENGTH);
806 self->cb_num_capture_buff_LENGTH = ST(1);
807 SvREFCNT_inc(self->cb_num_capture_buff_LENGTH);
811 _tag(SV *comp, SV *exec)
813 RETVAL = rep_tag(comp, exec);
820 XPUSHs(sv_2mortal(newSViv(PTR2IV(&engine_plugin))));