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 /* re__engine__Plugin self; SELF_FROM_PPRIVATE(self,rx->pprivate) */
79 #define SELF_FROM_PPRIVATE(self, pprivate) \
80 if (sv_isobject(pprivate)) { \
81 SV *ref = SvRV((SV *) pprivate); \
82 IV tmp = SvIV((SV *) ref); \
83 self = INT2PTR(re__engine__Plugin, tmp); \
85 Perl_croak(aTHX_ "Not an object"); \
88 #if XSH_HAS_PERL(5, 19, 4)
89 # define REP_ENG_EXEC_MINEND_TYPE SSize_t
91 # define REP_ENG_EXEC_MINEND_TYPE I32
95 EXTERN_C const regexp_engine engine_plugin;
96 #if XSH_HAS_PERL(5, 11, 0)
97 EXTERN_C REGEXP * Plugin_comp(pTHX_ SV * const, U32);
99 EXTERN_C REGEXP * Plugin_comp(pTHX_ const SV * const, const U32);
101 EXTERN_C I32 Plugin_exec(pTHX_ REGEXP * const, char *, char *,
102 char *, REP_ENG_EXEC_MINEND_TYPE, SV *, void *, U32);
103 #if XSH_HAS_PERL(5, 19, 1)
104 EXTERN_C char * Plugin_intuit(pTHX_ REGEXP * const, SV *, const char * const,
105 char *, char *, U32, re_scream_pos_data *);
107 EXTERN_C char * Plugin_intuit(pTHX_ REGEXP * const, SV *, char *,
108 char *, U32, re_scream_pos_data *);
110 EXTERN_C SV * Plugin_checkstr(pTHX_ REGEXP * const);
111 EXTERN_C void Plugin_free(pTHX_ REGEXP * const);
112 EXTERN_C void * Plugin_dupe(pTHX_ REGEXP * const, CLONE_PARAMS *);
113 EXTERN_C void Plugin_numbered_buff_FETCH(pTHX_ REGEXP * const,
114 const I32, SV * const);
115 EXTERN_C void Plugin_numbered_buff_STORE(pTHX_ REGEXP * const,
116 const I32, SV const * const);
117 EXTERN_C I32 Plugin_numbered_buff_LENGTH(pTHX_ REGEXP * const,
118 const SV * const, const I32);
119 EXTERN_C SV * Plugin_named_buff(pTHX_ REGEXP * const, SV * const,
120 SV * const, const U32);
121 EXTERN_C SV * Plugin_named_buff_iter(pTHX_ REGEXP * const, const SV * const,
123 EXTERN_C SV * Plugin_package(pTHX_ REGEXP * const);
125 EXTERN_C void * Plugin_dupe(pTHX_ REGEXP * const, CLONE_PARAMS *);
128 EXTERN_C const regexp_engine engine_plugin;
131 #define RE_ENGINE_PLUGIN (&engine_plugin)
132 const regexp_engine engine_plugin = {
138 Plugin_numbered_buff_FETCH,
139 Plugin_numbered_buff_STORE,
140 Plugin_numbered_buff_LENGTH,
142 Plugin_named_buff_iter,
144 #if defined(USE_ITHREADS)
147 #if XSH_HAS_PERL(5, 17, 0)
152 typedef struct replug {
153 /* Pointer back to the containing regexp struct so that accessors
154 * can modify nparens, gofs, etc... */
157 /* A copy of the pattern given to comp, for ->pattern */
160 /* A copy of the string being matched against, for ->str */
171 SV *cb_num_capture_buff_FETCH;
172 SV *cb_num_capture_buff_STORE;
173 SV *cb_num_capture_buff_LENGTH;
174 } *re__engine__Plugin;
176 #if XSH_HAS_PERL(5, 11, 0)
177 # define rxREGEXP(RX) (SvANY(RX))
178 # define newREGEXP(RX) ((RX) = ((REGEXP *) newSV_type(SVt_REGEXP)))
180 # define rxREGEXP(RX) (RX)
181 # define newREGEXP(RX) (Newxz((RX), 1, struct regexp))
185 #if XSH_HAS_PERL(5, 11, 0)
186 Plugin_comp(pTHX_ SV * const pattern, U32 flags)
188 Plugin_comp(pTHX_ const SV * const pattern, const U32 flags)
195 re__engine__Plugin re;
196 const xsh_hints_user_t *h;
204 if (!h) /* This looks like a pragma leak. Apply the default behaviour */
205 return re_compile(pattern, flags);
207 /* exp/xend version of the pattern & length */
208 pbuf = SvPV((SV *) pattern, plen);
210 /* Our blessed object */
212 Newxz(re, 1, struct replug);
213 sv_setref_pv(obj, XSH_PACKAGE, (void *) re);
218 re->rx = rx; /* Make the rx accessible from self->rx */
219 rx->intflags = flags; /* Flags for internal use */
220 rx->extflags = flags; /* Flags for perl to use */
221 rx->engine = RE_ENGINE_PLUGIN; /* Compile to use this engine */
223 #if !XSH_HAS_PERL(5, 11, 0)
224 rx->refcnt = 1; /* Refcount so we won't be destroyed */
226 /* Precompiled pattern for pp_regcomp to use */
228 rx->precomp = savepvn(pbuf, rx->prelen);
230 /* Set up qr// stringification to be equivalent to the supplied
231 * pattern, this should be done via overload eventually */
232 rx->wraplen = rx->prelen;
233 Newx(rx->wrapped, rx->wraplen, char);
234 Copy(rx->precomp, rx->wrapped, rx->wraplen, char);
237 /* Store our private object */
240 /* Store the pattern for ->pattern */
241 re->pattern = (SV *) pattern;
242 SvREFCNT_inc_simple_void(re->pattern);
244 /* If there's an exec callback, store it into the private object so that it
245 * will be the one to be called, even if the engine changes in between */
247 re->cb_exec = h->exec;
248 SvREFCNT_inc_simple_void_NN(h->exec);
251 re->cb_num_capture_buff_FETCH = NULL;
252 re->cb_num_capture_buff_STORE = NULL;
253 re->cb_num_capture_buff_LENGTH = NULL;
255 /* Call our callback function if one was defined, if not we've already set up
256 * all the stuff we're going to to need for subsequent exec and other calls */
265 call_sv(h->comp, G_DISCARD);
271 /* If any of the comp-time accessors were called we'll have to
272 * update the regexp struct with the new info */
273 Newxz(rx->offs, rx->nparens + 1, regexp_paren_pair);
279 Plugin_exec(pTHX_ REGEXP * const RX, char *stringarg, char *strend,
280 char *strbeg, REP_ENG_EXEC_MINEND_TYPE minend,
281 SV *sv, void *data, U32 flags)
284 re__engine__Plugin self;
288 SELF_FROM_PPRIVATE(self, rx->pprivate);
294 /* Store the current str for ->str */
295 SvREFCNT_dec(self->str);
297 SvREFCNT_inc_simple_void(self->str);
303 XPUSHs(rx->pprivate);
307 call_sv(self->cb_exec, G_SCALAR);
328 #if XSH_HAS_PERL(5, 19, 1)
329 Plugin_intuit(pTHX_ REGEXP * const RX, SV *sv, const char * const strbeg,
330 char *strpos, char *strend, U32 flags, re_scream_pos_data *data)
332 Plugin_intuit(pTHX_ REGEXP * const RX, SV *sv, char *strpos,
333 char *strend, U32 flags, re_scream_pos_data *data)
338 #if XSH_HAS_PERL(5, 19, 1)
339 PERL_UNUSED_ARG(strbeg);
341 PERL_UNUSED_ARG(strpos);
342 PERL_UNUSED_ARG(strend);
343 PERL_UNUSED_ARG(flags);
344 PERL_UNUSED_ARG(data);
350 Plugin_checkstr(pTHX_ REGEXP * const RX)
358 Plugin_free(pTHX_ REGEXP * const RX)
361 re__engine__Plugin self;
367 SELF_FROM_PPRIVATE(self, rx->pprivate);
369 SvREFCNT_dec(self->pattern);
370 SvREFCNT_dec(self->str);
371 SvREFCNT_dec(self->stash);
373 SvREFCNT_dec(self->cb_exec);
375 SvREFCNT_dec(self->cb_num_capture_buff_FETCH);
376 SvREFCNT_dec(self->cb_num_capture_buff_STORE);
377 SvREFCNT_dec(self->cb_num_capture_buff_LENGTH);
383 SvREFCNT_dec(rx->pprivate);
389 callback = self->cb_free;
396 XPUSHs(rx->pprivate);
399 call_sv(callback, G_DISCARD);
410 Plugin_dupe(pTHX_ REGEXP * const RX, CLONE_PARAMS *param)
412 struct regexp *rx = rxREGEXP(RX);
414 Perl_croak(aTHX_ "dupe not supported yet");
421 Plugin_numbered_buff_FETCH(pTHX_ REGEXP * const RX, const I32 paren,
425 re__engine__Plugin self;
429 SELF_FROM_PPRIVATE(self, rx->pprivate);
431 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)
469 re__engine__Plugin self;
473 SELF_FROM_PPRIVATE(self, rx->pprivate);
475 callback = self->cb_num_capture_buff_STORE;
484 XPUSHs(rx->pprivate);
485 XPUSHs(sv_2mortal(newSViv(paren)));
486 XPUSHs((SV *) value);
489 call_sv(callback, G_DISCARD);
498 Plugin_numbered_buff_LENGTH(pTHX_ REGEXP * const RX, const SV * const sv,
502 re__engine__Plugin self;
506 SELF_FROM_PPRIVATE(self, rx->pprivate);
508 callback = self->cb_num_capture_buff_LENGTH;
518 XPUSHs(rx->pprivate);
519 XPUSHs(sv_2mortal(newSViv(paren)));
522 call_sv(callback, G_SCALAR);
534 /* TODO: call FETCH and get the length on that value */
540 Plugin_named_buff(pTHX_ REGEXP * const RX, SV * const key, SV * const value,
547 Plugin_named_buff_iter(pTHX_ REGEXP * const RX, const SV * const lastkey,
554 Plugin_package(pTHX_ REGEXP * const RX)
558 return newSVpvs(XSH_PACKAGE);
561 static void xsh_user_global_setup(pTHX) {
564 stash = gv_stashpvn(XSH_PACKAGE, XSH_PACKAGE_LEN, 1);
565 newCONSTSUB(stash, "REP_THREADSAFE", newSVuv(XSH_THREADSAFE));
566 newCONSTSUB(stash, "REP_FORKSAFE", newSVuv(XSH_FORKSAFE));
571 /* --- XS ------------------------------------------------------------------ */
573 MODULE = re::engine::Plugin PACKAGE = re::engine::Plugin
590 #endif /* XSH_THREADSAFE */
593 pattern(re::engine::Plugin self, ...)
595 XPUSHs(self->pattern);
599 str(re::engine::Plugin self, ...)
605 mod(re::engine::Plugin self)
611 flags = self->rx->intflags;
612 if (flags & PMf_FOLD) /* /i */
614 if (flags & PMf_MULTILINE) /* /m */
616 if (flags & PMf_SINGLELINE) /* /s */
618 if (flags & PMf_EXTENDED) /* /x */
620 if (flags & RXf_PMf_KEEPCOPY) /* /p */
624 for (i = 0; i < n; ++i) {
631 stash(re::engine::Plugin self, ...)
634 SvREFCNT_dec(self->stash);
636 SvREFCNT_inc_simple_void(self->stash);
644 minlen(re::engine::Plugin self, ...)
647 self->rx->minlen = (I32)SvIV(ST(1));
650 if (self->rx->minlen) {
651 XPUSHs(sv_2mortal(newSViv(self->rx->minlen)));
653 XPUSHs(sv_2mortal(&PL_sv_undef));
659 gofs(re::engine::Plugin self, ...)
662 self->rx->gofs = (U32)SvIV(ST(1));
665 if (self->rx->gofs) {
666 XPUSHs(sv_2mortal(newSVuv(self->rx->gofs)));
668 XPUSHs(sv_2mortal(&PL_sv_undef));
674 nparens(re::engine::Plugin self, ...)
677 self->rx->nparens = (U32)SvIV(ST(1));
680 if (self->rx->nparens) {
681 XPUSHs(sv_2mortal(newSVuv(self->rx->nparens)));
683 XPUSHs(sv_2mortal(&PL_sv_undef));
689 _exec(re::engine::Plugin self, ...)
692 SvREFCNT_dec(self->cb_exec);
693 self->cb_exec = ST(1);
694 SvREFCNT_inc_simple_void(self->cb_exec);
699 _num_capture_buff_FETCH(re::engine::Plugin self, ...)
702 SvREFCNT_dec(self->cb_num_capture_buff_FETCH);
703 self->cb_num_capture_buff_FETCH = ST(1);
704 SvREFCNT_inc_simple_void(self->cb_num_capture_buff_FETCH);
709 _num_capture_buff_STORE(re::engine::Plugin self, ...)
712 SvREFCNT_dec(self->cb_num_capture_buff_STORE);
713 self->cb_num_capture_buff_STORE = ST(1);
714 SvREFCNT_inc_simple_void(self->cb_num_capture_buff_STORE);
719 _num_capture_buff_LENGTH(re::engine::Plugin self, ...)
722 SvREFCNT_dec(self->cb_num_capture_buff_LENGTH);
723 self->cb_num_capture_buff_LENGTH = ST(1);
724 SvREFCNT_inc_simple_void(self->cb_num_capture_buff_LENGTH);
729 _tag(SV *comp, SV *exec)
731 xsh_hints_user_t arg;
735 RETVAL = xsh_hints_tag(&arg);
742 XPUSHs(sv_2mortal(newSViv(PTR2IV(&engine_plugin))));