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);
153 STATIC void rep_thread_cleanup(pTHX_ void *);
155 STATIC void rep_thread_cleanup(pTHX_ void *ud) {
161 SAVEDESTRUCTOR_X(rep_thread_cleanup, level);
165 PerlMemShared_free(level);
166 ptable_free(MY_CXT.tbl);
170 #endif /* REP_THREADSAFE */
172 STATIC SV *rep_validate_callback(SV *code) {
177 if (SvTYPE(code) < SVt_PVCV)
180 return SvREFCNT_inc_simple_NN(code);
183 #if REP_WORKAROUND_REQUIRE_PROPAGATION
184 STATIC IV rep_require_tag(pTHX) {
185 #define rep_require_tag() rep_require_tag(aTHX)
188 for (si = PL_curstackinfo; si; si = si->si_prev) {
191 for (cxix = si->si_cxix; cxix >= 0; --cxix) {
192 const PERL_CONTEXT *cx = si->si_cxstack + cxix;
194 if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_REQUIRE)
201 #endif /* REP_WORKAROUND_REQUIRE_PROPAGATION */
203 STATIC SV *rep_tag(pTHX_ SV *comp, SV *exec) {
204 #define rep_tag(C, E) rep_tag(aTHX_ (C), (E))
208 h = PerlMemShared_malloc(sizeof *h);
209 h->comp = rep_validate_callback(comp);
210 h->exec = rep_validate_callback(exec);
211 #if REP_WORKAROUND_REQUIRE_PROPAGATION
212 h->cxreq = rep_require_tag();
213 #endif /* REP_WORKAROUND_REQUIRE_PROPAGATION */
216 /* We only need for the key to be an unique tag for looking up the value later.
217 * Allocated memory provides convenient unique identifiers, so that's why we
218 * use the hint as the key itself. */
219 ptable_store(MY_CXT.tbl, h, h);
220 #endif /* REP_THREADSAFE */
222 return newSViv(PTR2IV(h));
225 STATIC const rep_hint_t *rep_detag(pTHX_ const SV *hint) {
226 #define rep_detag(H) rep_detag(aTHX_ (H))
230 if (!(hint && SvIOK(hint)))
233 h = INT2PTR(rep_hint_t *, SvIVX(hint));
235 h = ptable_fetch(MY_CXT.tbl, h);
236 #endif /* REP_THREADSAFE */
238 #if REP_WORKAROUND_REQUIRE_PROPAGATION
239 if (rep_require_tag() != h->cxreq)
241 #endif /* REP_WORKAROUND_REQUIRE_PROPAGATION */
246 STATIC U32 rep_hash = 0;
248 STATIC const rep_hint_t *rep_hint(pTHX) {
249 #define rep_hint() rep_hint(aTHX)
252 #ifdef cop_hints_fetch_pvn
253 hint = cop_hints_fetch_pvn(PL_curcop,
254 __PACKAGE__, __PACKAGE_LEN__, rep_hash, 0);
256 /* We already require 5.9.5 for the regexp engine API. */
257 hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
259 __PACKAGE__, __PACKAGE_LEN__,
264 return rep_detag(hint);
268 #if PERL_VERSION <= 10
269 Plugin_comp(pTHX_ const SV * const pattern, const U32 flags)
271 Plugin_comp(pTHX_ SV * const pattern, U32 flags)
278 re__engine__Plugin re;
282 if (!h) /* This looks like a pragma leak. Apply the default behaviour */
283 return re_compile(pattern, flags);
285 /* exp/xend version of the pattern & length */
287 char* exp = SvPV((SV*)pattern, plen);
289 /* Our blessed object */
292 Newxz(re, 1, struct replug);
293 sv_setref_pv(obj, "re::engine::Plugin", (void*)re);
298 re->rx = rx; /* Make the rx accessible from self->rx */
299 rx->intflags = flags; /* Flags for internal use */
300 rx->extflags = flags; /* Flags for perl to use */
301 rx->engine = RE_ENGINE_PLUGIN; /* Compile to use this engine */
303 #if PERL_VERSION <= 10
304 rx->refcnt = 1; /* Refcount so we won't be destroyed */
306 /* Precompiled pattern for pp_regcomp to use */
308 rx->precomp = savepvn(exp, rx->prelen);
310 /* Set up qr// stringification to be equivalent to the supplied
311 * pattern, this should be done via overload eventually.
313 rx->wraplen = rx->prelen;
314 Newx(rx->wrapped, rx->wraplen, char);
315 Copy(rx->precomp, rx->wrapped, rx->wraplen, char);
318 /* Store our private object */
321 /* Store the pattern for ->pattern */
322 re->pattern = (SV*)pattern;
323 SvREFCNT_inc(re->pattern);
325 /* If there's an exec callback, store it into the private object so
326 * that it will be the one to be called, even if the engine changes
329 re->cb_exec = h->exec;
330 SvREFCNT_inc_simple_void_NN(h->exec);
333 re->cb_num_capture_buff_FETCH = NULL;
334 re->cb_num_capture_buff_STORE = NULL;
335 re->cb_num_capture_buff_LENGTH = NULL;
337 /* Call our callback function if one was defined, if not we've
338 * already set up all the stuff we're going to to need for
339 * subsequent exec and other calls */
348 call_sv(h->comp, G_DISCARD);
354 /* If any of the comp-time accessors were called we'll have to
355 * update the regexp struct with the new info.
358 buffers = rx->nparens;
360 Newxz(rx->offs, buffers + 1, regexp_paren_pair);
366 Plugin_exec(pTHX_ REGEXP * const RX, char *stringarg, char *strend,
367 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
371 struct regexp *rx = rxREGEXP(RX);
372 GET_SELF_FROM_PPRIVATE(rx->pprivate);
375 /* Store the current str for ->str */
377 SvREFCNT_inc(self->str);
383 XPUSHs(rx->pprivate);
387 call_sv(self->cb_exec, G_SCALAR);
409 Plugin_intuit(pTHX_ REGEXP * const RX, SV *sv, char *strpos,
410 char *strend, U32 flags, re_scream_pos_data *data)
414 PERL_UNUSED_ARG(strpos);
415 PERL_UNUSED_ARG(strend);
416 PERL_UNUSED_ARG(flags);
417 PERL_UNUSED_ARG(data);
422 Plugin_checkstr(pTHX_ REGEXP * const RX)
429 Plugin_free(pTHX_ REGEXP * const RX)
431 struct regexp *rx = rxREGEXP(RX);
432 GET_SELF_FROM_PPRIVATE(rx->pprivate);
434 SvREFCNT_dec(self->pattern);
435 SvREFCNT_dec(self->str);
437 SvREFCNT_dec(self->cb_exec);
439 SvREFCNT_dec(self->cb_num_capture_buff_FETCH);
440 SvREFCNT_dec(self->cb_num_capture_buff_STORE);
441 SvREFCNT_dec(self->cb_num_capture_buff_LENGTH);
450 callback = self->cb_free;
457 XPUSHs(rx->pprivate);
460 call_sv(callback, G_DISCARD);
471 Plugin_dupe(pTHX_ REGEXP * const RX, CLONE_PARAMS *param)
473 struct regexp *rx = rxREGEXP(RX);
474 Perl_croak(aTHX_ "dupe not supported yet");
480 Plugin_numbered_buff_FETCH(pTHX_ REGEXP * const RX, const I32 paren,
486 struct regexp *rx = rxREGEXP(RX);
487 GET_SELF_FROM_PPRIVATE(rx->pprivate);
489 callback = self->cb_num_capture_buff_FETCH;
496 XPUSHs(rx->pprivate);
497 XPUSHs(sv_2mortal(newSViv(paren)));
500 items = call_sv(callback, G_SCALAR);
508 sv_setsv(sv, &PL_sv_undef);
515 sv_setsv(sv, &PL_sv_undef);
520 Plugin_numbered_buff_STORE(pTHX_ REGEXP * const RX, const I32 paren,
521 SV const * const value)
525 struct regexp *rx = rxREGEXP(RX);
526 GET_SELF_FROM_PPRIVATE(rx->pprivate);
528 callback = self->cb_num_capture_buff_STORE;
535 XPUSHs(rx->pprivate);
536 XPUSHs(sv_2mortal(newSViv(paren)));
537 XPUSHs(SvREFCNT_inc((SV *) value));
540 call_sv(callback, G_DISCARD);
549 Plugin_numbered_buff_LENGTH(pTHX_ REGEXP * const RX, const SV * const sv,
554 struct regexp *rx = rxREGEXP(RX);
555 GET_SELF_FROM_PPRIVATE(rx->pprivate);
557 callback = self->cb_num_capture_buff_LENGTH;
564 XPUSHs(rx->pprivate);
565 XPUSHs(sv_2mortal(newSViv(paren)));
568 call_sv(callback, G_SCALAR);
580 /* TODO: call FETCH and get the length on that value */
587 Plugin_named_buff (pTHX_ REGEXP * const RX, SV * const key, SV * const value,
594 Plugin_named_buff_iter (pTHX_ REGEXP * const RX, const SV * const lastkey,
601 Plugin_package(pTHX_ REGEXP * const RX)
604 return newSVpvs("re::engine::Plugin");
609 STATIC U32 rep_initialized = 0;
611 STATIC void rep_teardown(pTHX_ void *root) {
614 if (!rep_initialized || aTHX != root)
617 ptable_free(MY_CXT.tbl);
622 STATIC void rep_setup(pTHX) {
623 #define rep_setup() rep_setup(aTHX)
628 MY_CXT.tbl = ptable_new();
631 call_atexit(rep_teardown, aTHX);
636 #else /* REP_THREADSAFE */
640 #endif /* !REP_THREADSAFE */
642 STATIC U32 rep_booted = 0;
644 /* --- XS ------------------------------------------------------------------ */
646 MODULE = re::engine::Plugin PACKAGE = re::engine::Plugin
653 PERL_HASH(rep_hash, __PACKAGE__, __PACKAGE_LEN__);
670 ud.tbl = t = ptable_new();
671 ud.owner = MY_CXT.owner;
672 ptable_walk(MY_CXT.tbl, rep_ptable_clone, &ud);
680 level = PerlMemShared_malloc(sizeof *level);
683 SAVEDESTRUCTOR_X(rep_thread_cleanup, level);
690 pattern(re::engine::Plugin self, ...)
692 XPUSHs(self->pattern);
695 str(re::engine::Plugin self, ...)
700 mod(re::engine::Plugin self, ...)
703 if (self->rx->intflags & PMf_FOLD) {
704 XPUSHs(sv_2mortal(newSVpvs("i")));
709 if (self->rx->intflags & PMf_MULTILINE) {
710 XPUSHs(sv_2mortal(newSVpvs("m")));
715 if (self->rx->intflags & PMf_SINGLELINE) {
716 XPUSHs(sv_2mortal(newSVpvs("s")));
721 if (self->rx->intflags & PMf_EXTENDED) {
722 XPUSHs(sv_2mortal(newSVpvs("x")));
727 if (self->rx->intflags & RXf_PMf_KEEPCOPY) {
728 XPUSHs(sv_2mortal(newSVpvs("p")));
733 stash(re::engine::Plugin self, ...)
737 SvREFCNT_inc(self->stash);
744 minlen(re::engine::Plugin self, ...)
747 self->rx->minlen = (I32)SvIV(ST(1));
750 if (self->rx->minlen) {
751 XPUSHs(sv_2mortal(newSViv(self->rx->minlen)));
753 XPUSHs(sv_2mortal(&PL_sv_undef));
758 gofs(re::engine::Plugin self, ...)
761 self->rx->gofs = (U32)SvIV(ST(1));
764 if (self->rx->gofs) {
765 XPUSHs(sv_2mortal(newSVuv(self->rx->gofs)));
767 XPUSHs(sv_2mortal(&PL_sv_undef));
772 nparens(re::engine::Plugin self, ...)
775 self->rx->nparens = (U32)SvIV(ST(1));
778 if (self->rx->nparens) {
779 XPUSHs(sv_2mortal(newSVuv(self->rx->nparens)));
781 XPUSHs(sv_2mortal(&PL_sv_undef));
786 _exec(re::engine::Plugin self, ...)
789 SvREFCNT_dec(self->cb_exec);
790 self->cb_exec = ST(1);
791 SvREFCNT_inc(self->cb_exec);
795 _num_capture_buff_FETCH(re::engine::Plugin self, ...)
798 SvREFCNT_dec(self->cb_num_capture_buff_FETCH);
799 self->cb_num_capture_buff_FETCH = ST(1);
800 SvREFCNT_inc(self->cb_num_capture_buff_FETCH);
804 _num_capture_buff_STORE(re::engine::Plugin self, ...)
807 SvREFCNT_dec(self->cb_num_capture_buff_STORE);
808 self->cb_num_capture_buff_STORE = ST(1);
809 SvREFCNT_inc(self->cb_num_capture_buff_STORE);
813 _num_capture_buff_LENGTH(re::engine::Plugin self, ...)
816 SvREFCNT_dec(self->cb_num_capture_buff_LENGTH);
817 self->cb_num_capture_buff_LENGTH = ST(1);
818 SvREFCNT_inc(self->cb_num_capture_buff_LENGTH);
822 _tag(SV *comp, SV *exec)
824 RETVAL = rep_tag(comp, exec);
831 XPUSHs(sv_2mortal(newSViv(PTR2IV(&engine_plugin))));