]> git.vpit.fr Git - perl/modules/re-engine-Plugin.git/blobdiff - Plugin.xs
Get rid of smart matches
[perl/modules/re-engine-Plugin.git] / Plugin.xs
index 2229d48f3882b3ea8acb2ab1b4a64f83767b52aa..c16dfba3ed652bc61b82192036b858183fc49692 100644 (file)
--- a/Plugin.xs
+++ b/Plugin.xs
@@ -18,6 +18,7 @@
 typedef struct {
  SV *comp;
  SV *exec;
+ SV *free;
 } xsh_hints_user_t;
 
 static SV *rep_validate_callback(SV *code) {
@@ -34,6 +35,7 @@ static SV *rep_validate_callback(SV *code) {
 static void xsh_hints_user_init(pTHX_ xsh_hints_user_t *hv, xsh_hints_user_t *v) {
  hv->comp = rep_validate_callback(v->comp);
  hv->exec = rep_validate_callback(v->exec);
+ hv->free = rep_validate_callback(v->free);
 
  return;
 }
@@ -43,6 +45,7 @@ static void xsh_hints_user_init(pTHX_ xsh_hints_user_t *hv, xsh_hints_user_t *v)
 static void xsh_hints_user_clone(pTHX_ xsh_hints_user_t *nv, xsh_hints_user_t *ov, CLONE_PARAMS *params) {
  nv->comp = xsh_dup_inc(ov->comp, params);
  nv->exec = xsh_dup_inc(ov->exec, params);
+ nv->free = xsh_dup_inc(ov->free, params);
 
  return;
 }
@@ -52,6 +55,7 @@ static void xsh_hints_user_clone(pTHX_ xsh_hints_user_t *nv, xsh_hints_user_t *o
 static void xsh_hints_user_deinit(pTHX_ xsh_hints_user_t *hv) {
  SvREFCNT_dec(hv->comp);
  SvREFCNT_dec(hv->exec);
+ SvREFCNT_dec(hv->free);
 
  return;
 }
@@ -75,10 +79,6 @@ static void xsh_hints_user_deinit(pTHX_ xsh_hints_user_t *hv) {
 
 /* --- Custom regexp engine ------------------------------------------------ */
 
-#define GET_SELF_FROM_PPRIVATE(pprivate) \
- re__engine__Plugin self;                \
- SELF_FROM_PPRIVATE(self,pprivate);
-
 /* re__engine__Plugin self; SELF_FROM_PPRIVATE(self,rx->pprivate) */
 #define SELF_FROM_PPRIVATE(self, pprivate) \
  if (sv_isobject(pprivate)) {              \
@@ -155,7 +155,7 @@ const regexp_engine engine_plugin = {
 
 typedef struct replug {
  /* Pointer back to the containing regexp struct so that accessors
-  * can modify nparens, gofs etc. */
+  * can modify nparens, gofs, etc... */
  struct regexp *rx;
 
  /* A copy of the pattern given to comp, for ->pattern */
@@ -213,9 +213,8 @@ Plugin_comp(pTHX_ const SV * const pattern, const U32 flags)
 
  /* Our blessed object */
  obj = newSV(0);
- SvREFCNT_inc_simple_void_NN(obj);
  Newxz(re, 1, struct replug);
- sv_setref_pv(obj, "re::engine::Plugin", (void *) re);
+ sv_setref_pv(obj, XSH_PACKAGE, (void *) re);
 
  newREGEXP(RX);
  rx = rxREGEXP(RX);
@@ -233,8 +232,7 @@ Plugin_comp(pTHX_ const SV * const pattern, const U32 flags)
  rx->precomp  = savepvn(pbuf, rx->prelen);
 
  /* Set up qr// stringification to be equivalent to the supplied
-  * pattern, this should be done via overload eventually.
-  */ 
+  * pattern, this should be done via overload eventually */
  rx->wraplen  = rx->prelen;
  Newx(rx->wrapped, rx->wraplen, char);
  Copy(rx->precomp, rx->wrapped, rx->wraplen, char);
@@ -247,21 +245,25 @@ Plugin_comp(pTHX_ const SV * const pattern, const U32 flags)
  re->pattern  = (SV *) pattern;
  SvREFCNT_inc_simple_void(re->pattern);
 
- /* If there's an exec callback, store it into the private object so
-  * that it will be the one to be called, even if the engine changes
-  * in between */
+ /* If there's an exec callback, store it into the private object so that it
+  * will be the one to be called, even if the engine changes in between */
  if (h->exec) {
   re->cb_exec = h->exec;
   SvREFCNT_inc_simple_void_NN(h->exec);
  }
 
+ /* Same goes for the free callback, if there's one. */
+ if (h->free) {
+  re->cb_free = h->free;
+  SvREFCNT_inc_simple_void_NN(h->free);
+ }
+
  re->cb_num_capture_buff_FETCH  = NULL;
  re->cb_num_capture_buff_STORE  = NULL;
  re->cb_num_capture_buff_LENGTH = NULL;
 
- /* Call our callback function if one was defined, if not we've
-  * already set up all the stuff we're going to to need for
-  * subsequent exec and other calls */
+ /* Call our callback function if one was defined, if not we've already set up
+  * all the stuff we're going to to need for subsequent exec and other calls */
  if (h->comp) {
   ENTER;
   SAVETMPS;
@@ -277,8 +279,7 @@ Plugin_comp(pTHX_ const SV * const pattern, const U32 flags)
  }
 
  /* If any of the comp-time accessors were called we'll have to
-  * update the regexp struct with the new info.
-  */
+  * update the regexp struct with the new info */
  Newxz(rx->offs, rx->nparens + 1, regexp_paren_pair);
 
  return RX;
@@ -289,14 +290,16 @@ Plugin_exec(pTHX_ REGEXP * const RX, char *stringarg, char *strend,
             char *strbeg, REP_ENG_EXEC_MINEND_TYPE minend,
             SV *sv, void *data, U32 flags)
 {
- dSP;
+ struct regexp *rx;
+ re__engine__Plugin self;
  I32 matched;
- struct regexp *rx = rxREGEXP(RX);
 
- GET_SELF_FROM_PPRIVATE(rx->pprivate);
+ rx = rxREGEXP(RX);
+ SELF_FROM_PPRIVATE(self, rx->pprivate);
 
  if (self->cb_exec) {
   SV *ret;
+  dSP;
 
   /* Store the current str for ->str */
   SvREFCNT_dec(self->str);
@@ -366,6 +369,8 @@ Plugin_free(pTHX_ REGEXP * const RX)
 {
  struct regexp *rx;
  re__engine__Plugin self;
+ SV *callback;
+ dSP;
 
  if (PL_dirty)
   return;
@@ -373,22 +378,6 @@ Plugin_free(pTHX_ REGEXP * const RX)
  rx = rxREGEXP(RX);
  SELF_FROM_PPRIVATE(self, rx->pprivate);
 
- SvREFCNT_dec(self->pattern);
- SvREFCNT_dec(self->str);
-
- SvREFCNT_dec(self->cb_exec);
-
- SvREFCNT_dec(self->cb_num_capture_buff_FETCH);
- SvREFCNT_dec(self->cb_num_capture_buff_STORE);
- SvREFCNT_dec(self->cb_num_capture_buff_LENGTH);
-
- self->rx = NULL;
- Safefree(self);
-
-/*
- dSP;
- SV *callback;
-
  callback = self->cb_free;
 
  if (callback) {
@@ -405,8 +394,24 @@ Plugin_free(pTHX_ REGEXP * const RX)
   FREETMPS;
   LEAVE;
  }
+
+ SvREFCNT_dec(self->pattern);
+ SvREFCNT_dec(self->str);
+ SvREFCNT_dec(self->stash);
+
+ SvREFCNT_dec(self->cb_exec);
+
+ SvREFCNT_dec(self->cb_num_capture_buff_FETCH);
+ SvREFCNT_dec(self->cb_num_capture_buff_STORE);
+ SvREFCNT_dec(self->cb_num_capture_buff_LENGTH);
+
+ self->rx = NULL;
+
+ Safefree(self);
+
+ SvREFCNT_dec(rx->pprivate);
+
  return;
-*/
 }
 
 void *
@@ -424,16 +429,19 @@ void
 Plugin_numbered_buff_FETCH(pTHX_ REGEXP * const RX, const I32 paren,
                            SV * const sv)
 {
dSP;
I32 items;
struct regexp *rx;
re__engine__Plugin self;
  SV *callback;
- struct regexp *rx = rxREGEXP(RX);
 
- GET_SELF_FROM_PPRIVATE(rx->pprivate);
+ rx = rxREGEXP(RX);
+ SELF_FROM_PPRIVATE(self, rx->pprivate);
 
  callback = self->cb_num_capture_buff_FETCH;
 
  if (callback) {
+  I32 items;
+  dSP;
+
   ENTER;
   SAVETMPS;
 
@@ -465,15 +473,18 @@ void
 Plugin_numbered_buff_STORE(pTHX_ REGEXP * const RX, const I32 paren,
                            SV const * const value)
 {
- dSP;
+ struct regexp *rx;
+ re__engine__Plugin self;
  SV *callback;
- struct regexp *rx = rxREGEXP(RX);
 
- GET_SELF_FROM_PPRIVATE(rx->pprivate);
+ rx = rxREGEXP(RX);
+ SELF_FROM_PPRIVATE(self, rx->pprivate);
 
  callback = self->cb_num_capture_buff_STORE;
 
  if (callback) {
+  dSP;
+
   ENTER;
   SAVETMPS;
 
@@ -495,16 +506,18 @@ I32
 Plugin_numbered_buff_LENGTH(pTHX_ REGEXP * const RX, const SV * const sv,
                             const I32 paren)
 {
- dSP;
+ struct regexp *rx;
+ re__engine__Plugin self;
  SV *callback;
- struct regexp *rx = rxREGEXP(RX);
 
- GET_SELF_FROM_PPRIVATE(rx->pprivate);
+ rx = rxREGEXP(RX);
+ SELF_FROM_PPRIVATE(self, rx->pprivate);
 
  callback = self->cb_num_capture_buff_LENGTH;
 
  if (callback) {
   IV ret;
+  dSP;
 
   ENTER;
   SAVETMPS;
@@ -550,7 +563,7 @@ Plugin_package(pTHX_ REGEXP * const RX)
 {
  PERL_UNUSED_ARG(RX);
 
- return newSVpvs("re::engine::Plugin");
+ return newSVpvs(XSH_PACKAGE);
 }
 
 static void xsh_user_global_setup(pTHX) {
@@ -588,11 +601,13 @@ void
 pattern(re::engine::Plugin self, ...)
 PPCODE:
  XPUSHs(self->pattern);
+ XSRETURN(1);
 
 void
 str(re::engine::Plugin self, ...)
 PPCODE:
  XPUSHs(self->str);
+ XSRETURN(1);
 
 void
 mod(re::engine::Plugin self)
@@ -630,6 +645,7 @@ PPCODE:
   XSRETURN_EMPTY;
  } else {
   XPUSHs(self->stash);
+  XSRETURN(1);
  }
 
 void
@@ -644,6 +660,7 @@ PPCODE:
   } else {
    XPUSHs(sv_2mortal(&PL_sv_undef));
   }
+  XSRETURN(1);
  }
 
 void
@@ -658,6 +675,7 @@ PPCODE:
   } else {
    XPUSHs(sv_2mortal(&PL_sv_undef));
   }
+  XSRETURN(1);
  }
 
 void
@@ -672,6 +690,7 @@ PPCODE:
   } else {
    XPUSHs(sv_2mortal(&PL_sv_undef));
   }
+  XSRETURN(1);
  }
 
 void
@@ -682,6 +701,17 @@ PPCODE:
   self->cb_exec = ST(1);
   SvREFCNT_inc_simple_void(self->cb_exec);
  }
+ XSRETURN(0);
+
+void
+_free(re::engine::Plugin self, ...)
+PPCODE:
+ if (items > 1) {
+  SvREFCNT_dec(self->cb_free);
+  self->cb_free = ST(1);
+  SvREFCNT_inc_simple_void(self->cb_free);
+ }
+ XSRETURN(0);
 
 void
 _num_capture_buff_FETCH(re::engine::Plugin self, ...)
@@ -691,6 +721,7 @@ PPCODE:
   self->cb_num_capture_buff_FETCH = ST(1);
   SvREFCNT_inc_simple_void(self->cb_num_capture_buff_FETCH);
  }
+ XSRETURN(0);
 
 void
 _num_capture_buff_STORE(re::engine::Plugin self, ...)
@@ -700,6 +731,7 @@ PPCODE:
   self->cb_num_capture_buff_STORE = ST(1);
   SvREFCNT_inc_simple_void(self->cb_num_capture_buff_STORE);
  }
+ XSRETURN(0);
 
 void
 _num_capture_buff_LENGTH(re::engine::Plugin self, ...)
@@ -709,14 +741,16 @@ PPCODE:
   self->cb_num_capture_buff_LENGTH = ST(1);
   SvREFCNT_inc_simple_void(self->cb_num_capture_buff_LENGTH);
  }
+ XSRETURN(0);
 
 SV *
-_tag(SV *comp, SV *exec)
+_tag(SV *comp, SV *exec, SV *free)
 PREINIT:
  xsh_hints_user_t arg;
 CODE:
  arg.comp = comp;
  arg.exec = exec;
+ arg.free = free;
  RETVAL = xsh_hints_tag(&arg);
 OUTPUT:
  RETVAL
@@ -725,3 +759,4 @@ void
 ENGINE()
 PPCODE:
  XPUSHs(sv_2mortal(newSViv(PTR2IV(&engine_plugin))));
+ XSRETURN(1);