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
9 #define __PACKAGE__ "re::engine::Plugin"
10 #define __PACKAGE_LEN__ (sizeof(__PACKAGE__)-1)
12 #define REP_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
14 #ifndef REP_WORKAROUND_REQUIRE_PROPAGATION
15 # define REP_WORKAROUND_REQUIRE_PROPAGATION !REP_HAS_PERL(5, 10, 1)
18 /* ... Thread safety and multiplicity ...................................... */
20 /* Safe unless stated otherwise in Makefile.PL */
22 # define REP_FORKSAFE 1
25 #ifndef REP_MULTIPLICITY
26 # if defined(MULTIPLICITY) || defined(PERL_IMPLICIT_CONTEXT)
27 # define REP_MULTIPLICITY 1
29 # define REP_MULTIPLICITY 0
32 #if REP_MULTIPLICITY && !defined(tTHX)
33 # define tTHX PerlInterpreter*
36 #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))
37 # define REP_THREADSAFE 1
39 # define MY_CXT_CLONE \
41 my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
42 Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
43 sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
46 # define REP_THREADSAFE 0
48 # define dMY_CXT dNOOP
50 # define MY_CXT rep_globaldata
52 # define START_MY_CXT STATIC my_cxt_t MY_CXT;
54 # define MY_CXT_INIT NOOP
56 # define MY_CXT_CLONE NOOP
59 /* --- Helpers ------------------------------------------------------------- */
61 /* ... Thread-safe hints ................................................... */
66 #if REP_WORKAROUND_REQUIRE_PROPAGATION
73 #define PTABLE_VAL_FREE(V) { \
74 rep_hint_t *h = (V); \
75 SvREFCNT_dec(h->comp); \
76 SvREFCNT_dec(h->exec); \
77 PerlMemShared_free(h); \
87 #define ptable_store(T, K, V) ptable_store(aTHX_ (T), (K), (V))
88 #define ptable_free(T) ptable_free(aTHX_ (T))
90 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
101 #if REP_HAS_PERL(5, 13, 2)
102 CLONE_PARAMS *params;
106 } rep_ptable_clone_ud;
108 #if REP_HAS_PERL(5, 13, 2)
109 # define rep_ptable_clone_ud_init(U, T, O) \
111 (U).params = Perl_clone_params_new((O), aTHX)
112 # define rep_ptable_clone_ud_deinit(U) Perl_clone_params_del((U).params)
113 # define rep_dup_inc(S, U) SvREFCNT_inc(sv_dup((S), (U)->params))
115 # define rep_ptable_clone_ud_init(U, T, O) \
117 (U).params.stashes = newAV(); \
118 (U).params.flags = 0; \
119 (U).params.proto_perl = (O)
120 # define rep_ptable_clone_ud_deinit(U) SvREFCNT_dec((U).params.stashes)
121 # define rep_dup_inc(S, U) SvREFCNT_inc(sv_dup((S), &((U)->params)))
124 STATIC void rep_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
125 rep_ptable_clone_ud *ud = ud_;
126 rep_hint_t *h1 = ent->val;
129 h2 = PerlMemShared_malloc(sizeof *h2);
130 h2->comp = rep_dup_inc(h1->comp, ud);
131 h2->exec = rep_dup_inc(h1->exec, ud);
132 #if REP_WORKAROUND_REQUIRE_PROPAGATION
133 h2->require_tag = PTR2IV(rep_dup_inc(INT2PTR(SV *, h1->require_tag), ud));
136 ptable_store(ud->tbl, ent->key, h2);
139 STATIC void rep_thread_cleanup(pTHX_ void *ud) {
142 ptable_free(MY_CXT.tbl);
145 STATIC int rep_endav_free(pTHX_ SV *sv, MAGIC *mg) {
146 SAVEDESTRUCTOR_X(rep_thread_cleanup, NULL);
151 STATIC MGVTBL rep_endav_vtbl = {
168 #endif /* REP_THREADSAFE */
170 STATIC SV *rep_validate_callback(SV *code) {
175 if (SvTYPE(code) < SVt_PVCV)
178 return SvREFCNT_inc_simple_NN(code);
181 #if REP_WORKAROUND_REQUIRE_PROPAGATION
182 STATIC IV rep_require_tag(pTHX) {
183 #define rep_require_tag() rep_require_tag(aTHX)
184 const CV *cv, *outside;
189 /* If for some reason the pragma is operational at run-time, try to discover
190 * the current cv in use. */
193 for (si = PL_curstackinfo; si; si = si->si_prev) {
196 for (cxix = si->si_cxix; cxix >= 0; --cxix) {
197 const PERL_CONTEXT *cx = si->si_cxstack + cxix;
199 switch (CxTYPE(cx)) {
202 /* The propagation workaround is only needed up to 5.10.0 and at that
203 * time format and sub contexts were still identical. And even later the
204 * cv members offsets should have been kept the same. */
206 goto get_enclosing_cv;
208 cv = cx->blk_eval.cv;
209 goto get_enclosing_cv;
220 for (outside = CvOUTSIDE(cv); outside; outside = CvOUTSIDE(cv))
225 #endif /* REP_WORKAROUND_REQUIRE_PROPAGATION */
227 STATIC SV *rep_tag(pTHX_ SV *comp, SV *exec) {
228 #define rep_tag(C, E) rep_tag(aTHX_ (C), (E))
231 h = PerlMemShared_malloc(sizeof *h);
232 h->comp = rep_validate_callback(comp);
233 h->exec = rep_validate_callback(exec);
234 #if REP_WORKAROUND_REQUIRE_PROPAGATION
235 h->require_tag = rep_require_tag();
236 #endif /* REP_WORKAROUND_REQUIRE_PROPAGATION */
241 /* We only need for the key to be an unique tag for looking up the value later
242 * Allocated memory provides convenient unique identifiers, so that's why we
243 * use the hint as the key itself. */
244 ptable_store(MY_CXT.tbl, h, h);
246 #endif /* REP_THREADSAFE */
248 return newSViv(PTR2IV(h));
251 STATIC const rep_hint_t *rep_detag(pTHX_ const SV *hint) {
252 #define rep_detag(H) rep_detag(aTHX_ (H))
255 if (!(hint && SvIOK(hint)))
258 h = INT2PTR(rep_hint_t *, SvIVX(hint));
262 h = ptable_fetch(MY_CXT.tbl, h);
264 #endif /* REP_THREADSAFE */
266 #if REP_WORKAROUND_REQUIRE_PROPAGATION
267 if (rep_require_tag() != h->require_tag)
269 #endif /* REP_WORKAROUND_REQUIRE_PROPAGATION */
274 STATIC U32 rep_hash = 0;
276 STATIC const rep_hint_t *rep_hint(pTHX) {
277 #define rep_hint() rep_hint(aTHX)
280 #ifdef cop_hints_fetch_pvn
281 hint = cop_hints_fetch_pvn(PL_curcop,
282 __PACKAGE__, __PACKAGE_LEN__, rep_hash, 0);
284 /* We already require 5.9.5 for the regexp engine API. */
285 hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
287 __PACKAGE__, __PACKAGE_LEN__,
292 return rep_detag(hint);
295 /* --- Custom regexp engine ------------------------------------------------ */
297 #define GET_SELF_FROM_PPRIVATE(pprivate) \
298 re__engine__Plugin self; \
299 SELF_FROM_PPRIVATE(self,pprivate);
301 /* re__engine__Plugin self; SELF_FROM_PPRIVATE(self,rx->pprivate) */
302 #define SELF_FROM_PPRIVATE(self, pprivate) \
303 if (sv_isobject(pprivate)) { \
304 SV * ref = SvRV((SV*)pprivate); \
305 IV tmp = SvIV((SV*)ref); \
306 self = INT2PTR(re__engine__Plugin,tmp); \
308 Perl_croak(aTHX_ "Not an object"); \
312 EXTERN_C const regexp_engine engine_plugin;
313 #if REP_HAS_PERL(5, 11, 0)
314 EXTERN_C REGEXP * Plugin_comp(pTHX_ SV * const, U32);
316 EXTERN_C REGEXP * Plugin_comp(pTHX_ const SV * const, const U32);
318 EXTERN_C I32 Plugin_exec(pTHX_ REGEXP * const, char *, char *,
319 char *, I32, SV *, void *, U32);
320 EXTERN_C char * Plugin_intuit(pTHX_ REGEXP * const, SV *, char *,
321 char *, U32, re_scream_pos_data *);
322 EXTERN_C SV * Plugin_checkstr(pTHX_ REGEXP * const);
323 EXTERN_C void Plugin_free(pTHX_ REGEXP * const);
324 EXTERN_C void * Plugin_dupe(pTHX_ REGEXP * const, CLONE_PARAMS *);
325 EXTERN_C void Plugin_numbered_buff_FETCH(pTHX_ REGEXP * const,
326 const I32, SV * const);
327 EXTERN_C void Plugin_numbered_buff_STORE(pTHX_ REGEXP * const,
328 const I32, SV const * const);
329 EXTERN_C I32 Plugin_numbered_buff_LENGTH(pTHX_ REGEXP * const,
330 const SV * const, const I32);
331 EXTERN_C SV * Plugin_named_buff (pTHX_ REGEXP * const, SV * const,
332 SV * const, const U32);
333 EXTERN_C SV * Plugin_named_buff_iter (pTHX_ REGEXP * const, const SV * const,
335 EXTERN_C SV * Plugin_package(pTHX_ REGEXP * const);
337 EXTERN_C void * Plugin_dupe(pTHX_ REGEXP * const, CLONE_PARAMS *);
340 EXTERN_C const regexp_engine engine_plugin;
343 #define RE_ENGINE_PLUGIN (&engine_plugin)
344 const regexp_engine engine_plugin = {
350 Plugin_numbered_buff_FETCH,
351 Plugin_numbered_buff_STORE,
352 Plugin_numbered_buff_LENGTH,
354 Plugin_named_buff_iter,
356 #if defined(USE_ITHREADS)
361 typedef struct replug {
362 /* Pointer back to the containing regexp struct so that accessors
363 * can modify nparens, gofs etc. */
366 /* A copy of the pattern given to comp, for ->pattern */
369 /* A copy of the string being matched against, for ->str */
380 SV * cb_num_capture_buff_FETCH;
381 SV * cb_num_capture_buff_STORE;
382 SV * cb_num_capture_buff_LENGTH;
383 } *re__engine__Plugin;
385 #if REP_HAS_PERL(5, 11, 0)
386 # define rxREGEXP(RX) (SvANY(RX))
387 # define newREGEXP(RX) ((RX) = ((REGEXP*) newSV_type(SVt_REGEXP)))
389 # define rxREGEXP(RX) (RX)
390 # define newREGEXP(RX) (Newxz((RX), 1, struct regexp))
394 #if REP_HAS_PERL(5, 11, 0)
395 Plugin_comp(pTHX_ SV * const pattern, U32 flags)
397 Plugin_comp(pTHX_ const SV * const pattern, const U32 flags)
404 re__engine__Plugin re;
413 if (!h) /* This looks like a pragma leak. Apply the default behaviour */
414 return re_compile(pattern, flags);
416 /* exp/xend version of the pattern & length */
417 pbuf = SvPV((SV*)pattern, plen);
419 /* Our blessed object */
421 SvREFCNT_inc_simple_void_NN(obj);
422 Newxz(re, 1, struct replug);
423 sv_setref_pv(obj, "re::engine::Plugin", (void*)re);
428 re->rx = rx; /* Make the rx accessible from self->rx */
429 rx->intflags = flags; /* Flags for internal use */
430 rx->extflags = flags; /* Flags for perl to use */
431 rx->engine = RE_ENGINE_PLUGIN; /* Compile to use this engine */
433 #if !REP_HAS_PERL(5, 11, 0)
434 rx->refcnt = 1; /* Refcount so we won't be destroyed */
436 /* Precompiled pattern for pp_regcomp to use */
438 rx->precomp = savepvn(pbuf, rx->prelen);
440 /* Set up qr// stringification to be equivalent to the supplied
441 * pattern, this should be done via overload eventually.
443 rx->wraplen = rx->prelen;
444 Newx(rx->wrapped, rx->wraplen, char);
445 Copy(rx->precomp, rx->wrapped, rx->wraplen, char);
448 /* Store our private object */
451 /* Store the pattern for ->pattern */
452 re->pattern = (SV*)pattern;
453 SvREFCNT_inc_simple_void(re->pattern);
455 /* If there's an exec callback, store it into the private object so
456 * that it will be the one to be called, even if the engine changes
459 re->cb_exec = h->exec;
460 SvREFCNT_inc_simple_void_NN(h->exec);
463 re->cb_num_capture_buff_FETCH = NULL;
464 re->cb_num_capture_buff_STORE = NULL;
465 re->cb_num_capture_buff_LENGTH = NULL;
467 /* Call our callback function if one was defined, if not we've
468 * already set up all the stuff we're going to to need for
469 * subsequent exec and other calls */
478 call_sv(h->comp, G_DISCARD);
484 /* If any of the comp-time accessors were called we'll have to
485 * update the regexp struct with the new info.
488 Newxz(rx->offs, rx->nparens + 1, regexp_paren_pair);
494 Plugin_exec(pTHX_ REGEXP * const RX, char *stringarg, char *strend,
495 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
499 struct regexp *rx = rxREGEXP(RX);
500 GET_SELF_FROM_PPRIVATE(rx->pprivate);
505 /* Store the current str for ->str */
506 SvREFCNT_dec(self->str);
508 SvREFCNT_inc_simple_void(self->str);
514 XPUSHs(rx->pprivate);
518 call_sv(self->cb_exec, G_SCALAR);
539 Plugin_intuit(pTHX_ REGEXP * const RX, SV *sv, char *strpos,
540 char *strend, U32 flags, re_scream_pos_data *data)
544 PERL_UNUSED_ARG(strpos);
545 PERL_UNUSED_ARG(strend);
546 PERL_UNUSED_ARG(flags);
547 PERL_UNUSED_ARG(data);
552 Plugin_checkstr(pTHX_ REGEXP * const RX)
559 Plugin_free(pTHX_ REGEXP * const RX)
562 re__engine__Plugin self;
568 SELF_FROM_PPRIVATE(self, rx->pprivate);
570 SvREFCNT_dec(self->pattern);
571 SvREFCNT_dec(self->str);
573 SvREFCNT_dec(self->cb_exec);
575 SvREFCNT_dec(self->cb_num_capture_buff_FETCH);
576 SvREFCNT_dec(self->cb_num_capture_buff_STORE);
577 SvREFCNT_dec(self->cb_num_capture_buff_LENGTH);
586 callback = self->cb_free;
593 XPUSHs(rx->pprivate);
596 call_sv(callback, G_DISCARD);
607 Plugin_dupe(pTHX_ REGEXP * const RX, CLONE_PARAMS *param)
609 struct regexp *rx = rxREGEXP(RX);
610 Perl_croak(aTHX_ "dupe not supported yet");
616 Plugin_numbered_buff_FETCH(pTHX_ REGEXP * const RX, const I32 paren,
622 struct regexp *rx = rxREGEXP(RX);
623 GET_SELF_FROM_PPRIVATE(rx->pprivate);
625 callback = self->cb_num_capture_buff_FETCH;
632 XPUSHs(rx->pprivate);
633 XPUSHs(sv_2mortal(newSViv(paren)));
636 items = call_sv(callback, G_SCALAR);
645 sv_setsv(sv, &PL_sv_undef);
652 sv_setsv(sv, &PL_sv_undef);
657 Plugin_numbered_buff_STORE(pTHX_ REGEXP * const RX, const I32 paren,
658 SV const * const value)
662 struct regexp *rx = rxREGEXP(RX);
663 GET_SELF_FROM_PPRIVATE(rx->pprivate);
665 callback = self->cb_num_capture_buff_STORE;
672 XPUSHs(rx->pprivate);
673 XPUSHs(sv_2mortal(newSViv(paren)));
674 XPUSHs((SV *) value);
677 call_sv(callback, G_DISCARD);
686 Plugin_numbered_buff_LENGTH(pTHX_ REGEXP * const RX, const SV * const sv,
691 struct regexp *rx = rxREGEXP(RX);
692 GET_SELF_FROM_PPRIVATE(rx->pprivate);
694 callback = self->cb_num_capture_buff_LENGTH;
703 XPUSHs(rx->pprivate);
704 XPUSHs(sv_2mortal(newSViv(paren)));
707 call_sv(callback, G_SCALAR);
719 /* TODO: call FETCH and get the length on that value */
726 Plugin_named_buff (pTHX_ REGEXP * const RX, SV * const key, SV * const value,
733 Plugin_named_buff_iter (pTHX_ REGEXP * const RX, const SV * const lastkey,
740 Plugin_package(pTHX_ REGEXP * const RX)
743 return newSVpvs("re::engine::Plugin");
748 STATIC U32 rep_initialized = 0;
750 STATIC void rep_teardown(pTHX_ void *root) {
751 if (!rep_initialized || aTHX != root)
756 ptable_free(MY_CXT.tbl);
762 STATIC void rep_setup(pTHX) {
763 #define rep_setup() rep_setup(aTHX)
769 MY_CXT.tbl = ptable_new();
773 call_atexit(rep_teardown, aTHX);
778 #else /* REP_THREADSAFE */
782 #endif /* !REP_THREADSAFE */
784 STATIC U32 rep_booted = 0;
786 /* --- XS ------------------------------------------------------------------ */
788 MODULE = re::engine::Plugin PACKAGE = re::engine::Plugin
797 PERL_HASH(rep_hash, __PACKAGE__, __PACKAGE_LEN__);
799 stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
800 newCONSTSUB(stash, "REP_THREADSAFE", newSVuv(REP_THREADSAFE));
801 newCONSTSUB(stash, "REP_FORKSAFE", newSVuv(REP_FORKSAFE));
816 rep_ptable_clone_ud ud;
820 rep_ptable_clone_ud_init(ud, t, MY_CXT.owner);
821 ptable_walk(MY_CXT.tbl, rep_ptable_clone, &ud);
822 rep_ptable_clone_ud_deinit(ud);
829 gv = gv_fetchpv(__PACKAGE__ "::_THREAD_CLEANUP", 0, SVt_PVCV);
835 if (!av_store(PL_endav, av_len(PL_endav) + 1, (SV *) cv))
837 sv_magicext((SV *) PL_endav, NULL, PERL_MAGIC_ext, &rep_endav_vtbl, NULL, 0);
845 rep_thread_cleanup(aTHX_ NULL);
848 #endif /* REP_THREADSAFE */
851 pattern(re::engine::Plugin self, ...)
853 XPUSHs(self->pattern);
856 str(re::engine::Plugin self, ...)
861 mod(re::engine::Plugin self)
867 flags = self->rx->intflags;
868 if (flags & PMf_FOLD) /* /i */
870 if (flags & PMf_MULTILINE) /* /m */
872 if (flags & PMf_SINGLELINE) /* /s */
874 if (flags & PMf_EXTENDED) /* /x */
876 if (flags & RXf_PMf_KEEPCOPY) /* /p */
880 for (i = 0; i < n; ++i) {
887 stash(re::engine::Plugin self, ...)
890 SvREFCNT_dec(self->stash);
892 SvREFCNT_inc_simple_void(self->stash);
899 minlen(re::engine::Plugin self, ...)
902 self->rx->minlen = (I32)SvIV(ST(1));
905 if (self->rx->minlen) {
906 XPUSHs(sv_2mortal(newSViv(self->rx->minlen)));
908 XPUSHs(sv_2mortal(&PL_sv_undef));
913 gofs(re::engine::Plugin self, ...)
916 self->rx->gofs = (U32)SvIV(ST(1));
919 if (self->rx->gofs) {
920 XPUSHs(sv_2mortal(newSVuv(self->rx->gofs)));
922 XPUSHs(sv_2mortal(&PL_sv_undef));
927 nparens(re::engine::Plugin self, ...)
930 self->rx->nparens = (U32)SvIV(ST(1));
933 if (self->rx->nparens) {
934 XPUSHs(sv_2mortal(newSVuv(self->rx->nparens)));
936 XPUSHs(sv_2mortal(&PL_sv_undef));
941 _exec(re::engine::Plugin self, ...)
944 SvREFCNT_dec(self->cb_exec);
945 self->cb_exec = ST(1);
946 SvREFCNT_inc_simple_void(self->cb_exec);
950 _num_capture_buff_FETCH(re::engine::Plugin self, ...)
953 SvREFCNT_dec(self->cb_num_capture_buff_FETCH);
954 self->cb_num_capture_buff_FETCH = ST(1);
955 SvREFCNT_inc_simple_void(self->cb_num_capture_buff_FETCH);
959 _num_capture_buff_STORE(re::engine::Plugin self, ...)
962 SvREFCNT_dec(self->cb_num_capture_buff_STORE);
963 self->cb_num_capture_buff_STORE = ST(1);
964 SvREFCNT_inc_simple_void(self->cb_num_capture_buff_STORE);
968 _num_capture_buff_LENGTH(re::engine::Plugin self, ...)
971 SvREFCNT_dec(self->cb_num_capture_buff_LENGTH);
972 self->cb_num_capture_buff_LENGTH = ST(1);
973 SvREFCNT_inc_simple_void(self->cb_num_capture_buff_LENGTH);
977 _tag(SV *comp, SV *exec)
979 RETVAL = rep_tag(comp, exec);
986 XPUSHs(sv_2mortal(newSViv(PTR2IV(&engine_plugin))));