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 /* --- Helpers ------------------------------------------------------------- */
11 #define XSH_PACKAGE "re::engine::Plugin"
16 /* ... Lexical hints ....................................................... */
23 static SV *rep_validate_callback(SV *code) {
28 if (SvTYPE(code) < SVt_PVCV)
31 return SvREFCNT_inc_simple_NN(code);
34 static void xsh_hints_user_init(pTHX_ xsh_hints_user_t *hv, xsh_hints_user_t *v) {
35 hv->comp = rep_validate_callback(v->comp);
36 hv->exec = rep_validate_callback(v->exec);
43 static void xsh_hints_user_clone(pTHX_ xsh_hints_user_t *nv, xsh_hints_user_t *ov, CLONE_PARAMS *params) {
44 nv->comp = xsh_dup_inc(ov->comp, params);
45 nv->exec = xsh_dup_inc(ov->exec, params);
50 #endif /* XSH_THREADSAFE */
52 static void xsh_hints_user_deinit(pTHX_ xsh_hints_user_t *hv) {
53 SvREFCNT_dec(hv->comp);
54 SvREFCNT_dec(hv->exec);
59 #define rep_hint() xsh_hints_detag(xsh_hints_fetch())
61 #define XSH_HINTS_TYPE_USER 1
62 #define XSH_HINTS_ONLY_COMPILE_TIME 0
64 #include "xsh/hints.h"
66 /* ... Thread-local storage ................................................ */
68 #define XSH_THREADS_USER_CONTEXT 0
69 #define XSH_THREADS_USER_LOCAL_SETUP 0
70 #define XSH_THREADS_USER_LOCAL_TEARDOWN 0
71 #define XSH_THREADS_USER_GLOBAL_TEARDOWN 0
72 #define XSH_THREADS_COMPILE_TIME_PROTECTION 0
74 #include "xsh/threads.h"
76 /* --- Custom regexp engine ------------------------------------------------ */
78 #define GET_SELF_FROM_PPRIVATE(pprivate) \
79 re__engine__Plugin self; \
80 SELF_FROM_PPRIVATE(self,pprivate);
82 /* re__engine__Plugin self; SELF_FROM_PPRIVATE(self,rx->pprivate) */
83 #define SELF_FROM_PPRIVATE(self, pprivate) \
84 if (sv_isobject(pprivate)) { \
85 SV *ref = SvRV((SV *) pprivate); \
86 IV tmp = SvIV((SV *) ref); \
87 self = INT2PTR(re__engine__Plugin, tmp); \
89 Perl_croak(aTHX_ "Not an object"); \
92 #if XSH_HAS_PERL(5, 19, 4)
93 # define REP_ENG_EXEC_MINEND_TYPE SSize_t
95 # define REP_ENG_EXEC_MINEND_TYPE I32
99 EXTERN_C const regexp_engine engine_plugin;
100 #if XSH_HAS_PERL(5, 11, 0)
101 EXTERN_C REGEXP * Plugin_comp(pTHX_ SV * const, U32);
103 EXTERN_C REGEXP * Plugin_comp(pTHX_ const SV * const, const U32);
105 EXTERN_C I32 Plugin_exec(pTHX_ REGEXP * const, char *, char *,
106 char *, REP_ENG_EXEC_MINEND_TYPE, SV *, void *, U32);
107 #if XSH_HAS_PERL(5, 19, 1)
108 EXTERN_C char * Plugin_intuit(pTHX_ REGEXP * const, SV *, const char * const,
109 char *, char *, U32, re_scream_pos_data *);
111 EXTERN_C char * Plugin_intuit(pTHX_ REGEXP * const, SV *, char *,
112 char *, U32, re_scream_pos_data *);
114 EXTERN_C SV * Plugin_checkstr(pTHX_ REGEXP * const);
115 EXTERN_C void Plugin_free(pTHX_ REGEXP * const);
116 EXTERN_C void * Plugin_dupe(pTHX_ REGEXP * const, CLONE_PARAMS *);
117 EXTERN_C void Plugin_numbered_buff_FETCH(pTHX_ REGEXP * const,
118 const I32, SV * const);
119 EXTERN_C void Plugin_numbered_buff_STORE(pTHX_ REGEXP * const,
120 const I32, SV const * const);
121 EXTERN_C I32 Plugin_numbered_buff_LENGTH(pTHX_ REGEXP * const,
122 const SV * const, const I32);
123 EXTERN_C SV * Plugin_named_buff(pTHX_ REGEXP * const, SV * const,
124 SV * const, const U32);
125 EXTERN_C SV * Plugin_named_buff_iter(pTHX_ REGEXP * const, const SV * const,
127 EXTERN_C SV * Plugin_package(pTHX_ REGEXP * const);
129 EXTERN_C void * Plugin_dupe(pTHX_ REGEXP * const, CLONE_PARAMS *);
132 EXTERN_C const regexp_engine engine_plugin;
135 #define RE_ENGINE_PLUGIN (&engine_plugin)
136 const regexp_engine engine_plugin = {
142 Plugin_numbered_buff_FETCH,
143 Plugin_numbered_buff_STORE,
144 Plugin_numbered_buff_LENGTH,
146 Plugin_named_buff_iter,
148 #if defined(USE_ITHREADS)
151 #if XSH_HAS_PERL(5, 17, 0)
156 typedef struct replug {
157 /* Pointer back to the containing regexp struct so that accessors
158 * can modify nparens, gofs etc. */
161 /* A copy of the pattern given to comp, for ->pattern */
164 /* A copy of the string being matched against, for ->str */
175 SV *cb_num_capture_buff_FETCH;
176 SV *cb_num_capture_buff_STORE;
177 SV *cb_num_capture_buff_LENGTH;
178 } *re__engine__Plugin;
180 #if XSH_HAS_PERL(5, 11, 0)
181 # define rxREGEXP(RX) (SvANY(RX))
182 # define newREGEXP(RX) ((RX) = ((REGEXP *) newSV_type(SVt_REGEXP)))
184 # define rxREGEXP(RX) (RX)
185 # define newREGEXP(RX) (Newxz((RX), 1, struct regexp))
189 #if XSH_HAS_PERL(5, 11, 0)
190 Plugin_comp(pTHX_ SV * const pattern, U32 flags)
192 Plugin_comp(pTHX_ const SV * const pattern, const U32 flags)
199 re__engine__Plugin re;
200 const xsh_hints_user_t *h;
208 if (!h) /* This looks like a pragma leak. Apply the default behaviour */
209 return re_compile(pattern, flags);
211 /* exp/xend version of the pattern & length */
212 pbuf = SvPV((SV *) pattern, plen);
214 /* Our blessed object */
216 SvREFCNT_inc_simple_void_NN(obj);
217 Newxz(re, 1, struct replug);
218 sv_setref_pv(obj, "re::engine::Plugin", (void *) re);
223 re->rx = rx; /* Make the rx accessible from self->rx */
224 rx->intflags = flags; /* Flags for internal use */
225 rx->extflags = flags; /* Flags for perl to use */
226 rx->engine = RE_ENGINE_PLUGIN; /* Compile to use this engine */
228 #if !XSH_HAS_PERL(5, 11, 0)
229 rx->refcnt = 1; /* Refcount so we won't be destroyed */
231 /* Precompiled pattern for pp_regcomp to use */
233 rx->precomp = savepvn(pbuf, rx->prelen);
235 /* Set up qr// stringification to be equivalent to the supplied
236 * pattern, this should be done via overload eventually.
238 rx->wraplen = rx->prelen;
239 Newx(rx->wrapped, rx->wraplen, char);
240 Copy(rx->precomp, rx->wrapped, rx->wraplen, char);
243 /* Store our private object */
246 /* Store the pattern for ->pattern */
247 re->pattern = (SV *) pattern;
248 SvREFCNT_inc_simple_void(re->pattern);
250 /* If there's an exec callback, store it into the private object so
251 * that it will be the one to be called, even if the engine changes
254 re->cb_exec = h->exec;
255 SvREFCNT_inc_simple_void_NN(h->exec);
258 re->cb_num_capture_buff_FETCH = NULL;
259 re->cb_num_capture_buff_STORE = NULL;
260 re->cb_num_capture_buff_LENGTH = NULL;
262 /* Call our callback function if one was defined, if not we've
263 * already set up all the stuff we're going to to need for
264 * subsequent exec and other calls */
273 call_sv(h->comp, G_DISCARD);
279 /* If any of the comp-time accessors were called we'll have to
280 * update the regexp struct with the new info.
282 Newxz(rx->offs, rx->nparens + 1, regexp_paren_pair);
288 Plugin_exec(pTHX_ REGEXP * const RX, char *stringarg, char *strend,
289 char *strbeg, REP_ENG_EXEC_MINEND_TYPE minend,
290 SV *sv, void *data, U32 flags)
294 struct regexp *rx = rxREGEXP(RX);
296 GET_SELF_FROM_PPRIVATE(rx->pprivate);
301 /* Store the current str for ->str */
302 SvREFCNT_dec(self->str);
304 SvREFCNT_inc_simple_void(self->str);
310 XPUSHs(rx->pprivate);
314 call_sv(self->cb_exec, G_SCALAR);
335 #if XSH_HAS_PERL(5, 19, 1)
336 Plugin_intuit(pTHX_ REGEXP * const RX, SV *sv, const char * const strbeg,
337 char *strpos, char *strend, U32 flags, re_scream_pos_data *data)
339 Plugin_intuit(pTHX_ REGEXP * const RX, SV *sv, char *strpos,
340 char *strend, U32 flags, re_scream_pos_data *data)
345 #if XSH_HAS_PERL(5, 19, 1)
346 PERL_UNUSED_ARG(strbeg);
348 PERL_UNUSED_ARG(strpos);
349 PERL_UNUSED_ARG(strend);
350 PERL_UNUSED_ARG(flags);
351 PERL_UNUSED_ARG(data);
357 Plugin_checkstr(pTHX_ REGEXP * const RX)
365 Plugin_free(pTHX_ REGEXP * const RX)
368 re__engine__Plugin self;
374 SELF_FROM_PPRIVATE(self, rx->pprivate);
376 SvREFCNT_dec(self->pattern);
377 SvREFCNT_dec(self->str);
379 SvREFCNT_dec(self->cb_exec);
381 SvREFCNT_dec(self->cb_num_capture_buff_FETCH);
382 SvREFCNT_dec(self->cb_num_capture_buff_STORE);
383 SvREFCNT_dec(self->cb_num_capture_buff_LENGTH);
392 callback = self->cb_free;
399 XPUSHs(rx->pprivate);
402 call_sv(callback, G_DISCARD);
413 Plugin_dupe(pTHX_ REGEXP * const RX, CLONE_PARAMS *param)
415 struct regexp *rx = rxREGEXP(RX);
417 Perl_croak(aTHX_ "dupe not supported yet");
424 Plugin_numbered_buff_FETCH(pTHX_ REGEXP * const RX, const I32 paren,
430 struct regexp *rx = rxREGEXP(RX);
432 GET_SELF_FROM_PPRIVATE(rx->pprivate);
434 callback = self->cb_num_capture_buff_FETCH;
441 XPUSHs(rx->pprivate);
442 XPUSHs(sv_2mortal(newSViv(paren)));
445 items = call_sv(callback, G_SCALAR);
453 sv_setsv(sv, &PL_sv_undef);
460 sv_setsv(sv, &PL_sv_undef);
465 Plugin_numbered_buff_STORE(pTHX_ REGEXP * const RX, const I32 paren,
466 SV const * const value)
470 struct regexp *rx = rxREGEXP(RX);
472 GET_SELF_FROM_PPRIVATE(rx->pprivate);
474 callback = self->cb_num_capture_buff_STORE;
481 XPUSHs(rx->pprivate);
482 XPUSHs(sv_2mortal(newSViv(paren)));
483 XPUSHs((SV *) value);
486 call_sv(callback, G_DISCARD);
495 Plugin_numbered_buff_LENGTH(pTHX_ REGEXP * const RX, const SV * const sv,
500 struct regexp *rx = rxREGEXP(RX);
502 GET_SELF_FROM_PPRIVATE(rx->pprivate);
504 callback = self->cb_num_capture_buff_LENGTH;
513 XPUSHs(rx->pprivate);
514 XPUSHs(sv_2mortal(newSViv(paren)));
517 call_sv(callback, G_SCALAR);
529 /* TODO: call FETCH and get the length on that value */
535 Plugin_named_buff(pTHX_ REGEXP * const RX, SV * const key, SV * const value,
542 Plugin_named_buff_iter(pTHX_ REGEXP * const RX, const SV * const lastkey,
549 Plugin_package(pTHX_ REGEXP * const RX)
553 return newSVpvs("re::engine::Plugin");
556 static void xsh_user_global_setup(pTHX) {
559 stash = gv_stashpvn(XSH_PACKAGE, XSH_PACKAGE_LEN, 1);
560 newCONSTSUB(stash, "REP_THREADSAFE", newSVuv(XSH_THREADSAFE));
561 newCONSTSUB(stash, "REP_FORKSAFE", newSVuv(XSH_FORKSAFE));
566 /* --- XS ------------------------------------------------------------------ */
568 MODULE = re::engine::Plugin PACKAGE = re::engine::Plugin
585 #endif /* XSH_THREADSAFE */
588 pattern(re::engine::Plugin self, ...)
590 XPUSHs(self->pattern);
593 str(re::engine::Plugin self, ...)
598 mod(re::engine::Plugin self)
604 flags = self->rx->intflags;
605 if (flags & PMf_FOLD) /* /i */
607 if (flags & PMf_MULTILINE) /* /m */
609 if (flags & PMf_SINGLELINE) /* /s */
611 if (flags & PMf_EXTENDED) /* /x */
613 if (flags & RXf_PMf_KEEPCOPY) /* /p */
617 for (i = 0; i < n; ++i) {
624 stash(re::engine::Plugin self, ...)
627 SvREFCNT_dec(self->stash);
629 SvREFCNT_inc_simple_void(self->stash);
636 minlen(re::engine::Plugin self, ...)
639 self->rx->minlen = (I32)SvIV(ST(1));
642 if (self->rx->minlen) {
643 XPUSHs(sv_2mortal(newSViv(self->rx->minlen)));
645 XPUSHs(sv_2mortal(&PL_sv_undef));
650 gofs(re::engine::Plugin self, ...)
653 self->rx->gofs = (U32)SvIV(ST(1));
656 if (self->rx->gofs) {
657 XPUSHs(sv_2mortal(newSVuv(self->rx->gofs)));
659 XPUSHs(sv_2mortal(&PL_sv_undef));
664 nparens(re::engine::Plugin self, ...)
667 self->rx->nparens = (U32)SvIV(ST(1));
670 if (self->rx->nparens) {
671 XPUSHs(sv_2mortal(newSVuv(self->rx->nparens)));
673 XPUSHs(sv_2mortal(&PL_sv_undef));
678 _exec(re::engine::Plugin self, ...)
681 SvREFCNT_dec(self->cb_exec);
682 self->cb_exec = ST(1);
683 SvREFCNT_inc_simple_void(self->cb_exec);
687 _num_capture_buff_FETCH(re::engine::Plugin self, ...)
690 SvREFCNT_dec(self->cb_num_capture_buff_FETCH);
691 self->cb_num_capture_buff_FETCH = ST(1);
692 SvREFCNT_inc_simple_void(self->cb_num_capture_buff_FETCH);
696 _num_capture_buff_STORE(re::engine::Plugin self, ...)
699 SvREFCNT_dec(self->cb_num_capture_buff_STORE);
700 self->cb_num_capture_buff_STORE = ST(1);
701 SvREFCNT_inc_simple_void(self->cb_num_capture_buff_STORE);
705 _num_capture_buff_LENGTH(re::engine::Plugin self, ...)
708 SvREFCNT_dec(self->cb_num_capture_buff_LENGTH);
709 self->cb_num_capture_buff_LENGTH = ST(1);
710 SvREFCNT_inc_simple_void(self->cb_num_capture_buff_LENGTH);
714 _tag(SV *comp, SV *exec)
716 xsh_hints_user_t arg;
720 RETVAL = xsh_hints_tag(&arg);
727 XPUSHs(sv_2mortal(newSViv(PTR2IV(&engine_plugin))));