You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@perl.apache.org by Geoffrey Young <ge...@modperlcookbook.org> on 2003/05/07 21:57:48 UTC

DECLINE_CMD

hi all

   I've been playing around with directive handlers and have found a problem 
with DECLINE_CMD, namely that it doesn't exist within Apache::Const.

   unfortunately, implementing DECLINE_CMD is not as easy as just adding it 
to the constants map - DECLINE_CMD is the only constant in Apache/APR that 
isn't an integer, and all the autogeneration/newCONSTSUB stuff is geared 
toward creating integer constants.

   the below patch works and seems a decent way to do it (now we have a 
routine for character-based constants in case there end up being others), 
but somebody else might have a better idea.

   the good news is that once you have DECLINE_CMD, declining directive 
handlers works the same way it did in mp1 :)

--Geoff

Index: src/modules/perl/modperl_const.c
===================================================================
RCS file: /home/cvspublic/modperl-2.0/src/modules/perl/modperl_const.c,v
retrieving revision 1.8
diff -u -r1.8 modperl_const.c
--- src/modules/perl/modperl_const.c	22 Oct 2001 05:42:23 -0000	1.8
+++ src/modules/perl/modperl_const.c	7 May 2003 19:38:49 -0000
@@ -42,6 +42,42 @@
      return val;
  }

+static char * new_char_constsub(pTHX_ char *lookup,
+                        HV *caller_stash, HV *stash,
+                        const char *name)
+{
+    int name_len = strlen(name);
+    GV **gvp = (GV **)hv_fetch(stash, name, name_len, TRUE);
+
+    /* dont redefine */
+    if (!isGV(*gvp) || !GvCV(*gvp)) {
+
+#if 0
+        fprintf(stderr, "newCONSTSUB(%s, %s, %s)\n",
+                HvNAME(stash), name, lookup);
+#endif
+
+        newCONSTSUB(stash, (char *)name, newSVpv(lookup, 0));
+#ifdef GvSHARED
+        GvSHARED_on(*gvp);
+#endif
+    }
+
+    /* export into callers namespace */
+    if (caller_stash) {
+        GV *alias = *(GV **)hv_fetch(caller_stash,
+                                     (char *)name, name_len, TRUE);
+
+        if (!isGV(alias)) {
+            gv_init(alias, caller_stash, name, name_len, TRUE);
+        }
+
+        GvCV(alias) = GvCV(*gvp);
+    }
+
+    return lookup;
+}
+
  int modperl_const_compile(pTHX_ const char *classname,
                            const char *arg,
                            const char *name)
@@ -54,6 +90,11 @@
      if (strnEQ(classname, "APR", 3)) {
          lookup       = modperl_constants_lookup_apr;
          group_lookup = modperl_constants_group_lookup_apr;
+    }
+    else if (strnEQ(name, "DECLINE_CMD", 11)) {
+      /* DECLINE_CMD is the only constant that's not an integer */
+      new_char_constsub(aTHX_ DECLINE_CMD, caller_stash, stash, name);
+      return 1;
      }
      else {
          lookup       = modperl_constants_lookup_apache;
Index: t/response/TestDirective/perlloadmodule.pm
===================================================================
RCS file: 
/home/cvspublic/modperl-2.0/t/response/TestDirective/perlloadmodule.pm,v
retrieving revision 1.2
diff -u -r1.2 perlloadmodule.pm
--- t/response/TestDirective/perlloadmodule.pm	13 Dec 2002 10:06:55 -0000	1.2
+++ t/response/TestDirective/perlloadmodule.pm	7 May 2003 19:38:49 -0000
@@ -6,7 +6,7 @@
  use Apache::Test;
  use Apache::TestUtil;

-use Apache::Const -compile => qw(OK OR_ALL RSRC_CONF TAKE1 TAKE23);
+use Apache::Const -compile => qw(OK OR_ALL RSRC_CONF TAKE1 TAKE23 
DECLINE_CMD);

  use Apache::CmdParms ();
  use Apache::Module ();
@@ -97,7 +97,7 @@
      my $dir_cfg = $self->get_config($s, $r->per_dir_config);
      my $srv_cfg = $self->get_config($s);

-    plan $r, tests => 7;
+    plan $r, tests => 8;

      t_debug("per-dir config:", $dir_cfg);
      t_debug("per-srv config:", $srv_cfg);
@@ -120,6 +120,8 @@
               'MyTest one two');

      ok t_cmp('per-server', $srv_cfg->{ServerTest});
+
+    ok t_cmp("\x07\x08", Apache::DECLINE_CMD);

      Apache::OK;
  }


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@perl.apache.org
For additional commands, e-mail: dev-help@perl.apache.org


Re: DECLINE_CMD (take 3)

Posted by Geoffrey Young <ge...@modperlcookbook.org>.
> Looks right to me, just can't apply your patch, Mozilla must have ate
> it...

here it is as an attachment.

good to hear from you again :)

--Geoff

Re: DECLINE_CMD (take 3)

Posted by "Philippe M. Chiasson" <go...@cpan.org>.
On Fri, 2003-05-09 at 22:28, Geoffrey Young wrote:
> ok, here's take 3.
> 
> re:
> 
> if ($name eq 'DECLINE_CMD' || $name eq 'DIR_MAGIC_TYPE') {
>      print $c_fh <<EOF;
>                return newSVpv($alias{$name}, 0);
> EOF
> 
> I think it's proper to use newSVpv instead of newSVpvn - because the length in question is 
> the length of the constant (not the name), presupposing we know the length of DECLINE_CMD 
> beforehand seems wrong (if we knew the value of the constants then why have a lookup 
> function :).  it also allows us to simplify logic in Code.pm and add to the list of 
> exceptional string cases if they arise in the future.

I think so too.

> anyway, here's the latest version.  I hope I got the threading stuff right :)

Looks right to me, just can't apply your patch, Mozilla must have ate
it...

patching file Changes
1 out of 1 hunk FAILED
[...]
patching file xs/tables/current/ModPerl/FunctionTable.pm
[...]
2 out of 2 hunks FAILED

> --Geoff
> 
> Index: Changes
> ===================================================================
> RCS file: /home/cvspublic/modperl-2.0/Changes,v
> retrieving revision 1.184
> diff -u -r1.184 Changes
> --- Changes	9 May 2003 03:33:02 -0000	1.184
> +++ Changes	9 May 2003 14:26:25 -0000
> @@ -12,6 +12,9 @@
> 
>   =item 1.99_10-dev
> 
> +implement DECLINE_CMD and DIR_MAGIC_TYPE constants
> +[Geoffrey Young]
> +
>   allow init filter handlers to call other methods than just $f->ctx [Stas]
> 
>   Fix Apache::Reload to gracefully handle the case with empty Touchfiles
> Index: lib/Apache/ParseSource.pm
> ===================================================================
> RCS file: /home/cvspublic/modperl-2.0/lib/Apache/ParseSource.pm,v
> retrieving revision 1.42
> diff -u -r1.42 ParseSource.pm
> --- lib/Apache/ParseSource.pm	15 Apr 2003 02:22:41 -0000	1.42
> +++ lib/Apache/ParseSource.pm	9 May 2003 14:26:25 -0000
> @@ -156,8 +156,8 @@
>           satisfy    => [qw{SATISFY_}],
>           remotehost => [qw{REMOTE_}],
>           http       => [qw{HTTP_}],
> -#       config     => [qw{DECLINE_CMD}],
> -#       types      => [qw{DIR_MAGIC_TYPE}],
> +        config     => [qw{DECLINE_CMD}],
> +        types      => [qw{DIR_MAGIC_TYPE}],
>           override   => [qw{OR_ ACCESS_CONF RSRC_CONF}],
>           log        => [qw(APLOG_)],
>       },
> Index: lib/ModPerl/Code.pm
> ===================================================================
> RCS file: /home/cvspublic/modperl-2.0/lib/ModPerl/Code.pm,v
> retrieving revision 1.96
> diff -u -r1.96 Code.pm
> --- lib/ModPerl/Code.pm	24 Apr 2003 01:51:37 -0000	1.96
> +++ lib/ModPerl/Code.pm	9 May 2003 14:26:25 -0000
> @@ -769,7 +769,7 @@
>       my $package_len = length $package;
> 
>       my $func = canon_func(qw(constants lookup), $postfix);
> -    my $proto = "int $func(const char *name)";
> +    my $proto = "SV \*$func(pTHX_ const char *name)";
> 
>       print $h_fh "$proto;\n";
> 
> @@ -804,7 +804,20 @@
>               print $c_fh <<EOF;
>   $ifdef[0]
>             if (strEQ(name, "$name")) {
> -              return $alias{$name};
> +EOF
> +
> +            if ($name eq 'DECLINE_CMD' || $name eq 'DIR_MAGIC_TYPE') {
> +                print $c_fh <<EOF;
> +              return newSVpv($alias{$name}, 0);
> +EOF
> +            }
> +            else {
> +                print $c_fh <<EOF;
> +              return newSViv($alias{$name});
> +EOF
> +            }
> +
> +            print $c_fh <<EOF;
>             }
>   $ifdef[1]
>   EOF
> @@ -815,7 +828,7 @@
>       print $c_fh <<EOF
>       };
>       Perl_croak_nocontext("unknown constant %s", name);
> -    return MP_ENOCONST;
> +    return newSViv(MP_ENOCONST);
>   }
>   EOF
>   }
> Index: src/modules/perl/modperl_const.c
> ===================================================================
> RCS file: /home/cvspublic/modperl-2.0/src/modules/perl/modperl_const.c,v
> retrieving revision 1.8
> diff -u -r1.8 modperl_const.c
> --- src/modules/perl/modperl_const.c	22 Oct 2001 05:42:23 -0000	1.8
> +++ src/modules/perl/modperl_const.c	9 May 2003 14:26:25 -0000
> @@ -1,27 +1,27 @@
>   #include "mod_perl.h"
>   #include "modperl_const.h"
> 
> -typedef int (*constants_lookup)(const char *);
> +typedef SV *(*constants_lookup)(pTHX_ const char *);
>   typedef const char ** (*constants_group_lookup)(const char *);
> 
> -static int new_constsub(pTHX_ constants_lookup lookup,
> +static SV *new_constsub(pTHX_ constants_lookup lookup,
>                           HV *caller_stash, HV *stash,
>                           const char *name)
>   {
>       int name_len = strlen(name);
>       GV **gvp = (GV **)hv_fetch(stash, name, name_len, TRUE);
> -    int val;
> +    SV *val;
> 
>       /* dont redefine */
>       if (!isGV(*gvp) || !GvCV(*gvp)) {
> -        val = (*lookup)(name);
> +        val = (*lookup)(aTHX_ name);
> 
>   #if 0
>           fprintf(stderr, "newCONSTSUB(%s, %s, %d)\n",
>                   HvNAME(stash), name, val);
>   #endif
> 
> -        newCONSTSUB(stash, (char *)name, newSViv(val));
> +        newCONSTSUB(stash, (char *)name, val);
>   #ifdef GvSHARED
>           GvSHARED_on(*gvp);
>   #endif
> Index: src/modules/perl/modperl_module.c
> ===================================================================
> RCS file: /home/cvspublic/modperl-2.0/src/modules/perl/modperl_module.c,v
> retrieving revision 1.12
> diff -u -r1.12 modperl_module.c
> --- src/modules/perl/modperl_module.c	14 Apr 2003 06:38:50 -0000	1.12
> +++ src/modules/perl/modperl_module.c	9 May 2003 14:26:25 -0000
> @@ -669,7 +669,7 @@
>               }
>               else {
>                   cmd->args_how =
> -                    modperl_constants_lookup_apache(SvPV(val, len));
> +                    SvIV(modperl_constants_lookup_apache(aTHX_ SvPV(val, len)));
>               }
>           }
> 
> @@ -695,7 +695,7 @@
>               }
>               else {
>                   cmd->req_override =
> -                    modperl_constants_lookup_apache(SvPV(val, len));
> +                    SvIV(modperl_constants_lookup_apache(aTHX_ SvPV(val, len)));
>               }
>           }
> 
> Index: t/apache/constants.t
> ===================================================================
> RCS file: /home/cvspublic/modperl-2.0/t/apache/constants.t,v
> retrieving revision 1.5
> diff -u -r1.5 constants.t
> --- t/apache/constants.t	18 May 2002 02:29:44 -0000	1.5
> +++ t/apache/constants.t	9 May 2003 14:26:25 -0000
> @@ -5,10 +5,11 @@
>   use Test;
> 
>   use Apache2 ();
> -use Apache::Const -compile => qw(DECLINED :http :common TAKE23 &OPT_EXECCGI);
> +use Apache::Const -compile => qw(DECLINED :http :common TAKE23 &OPT_EXECCGI
> +                                 DECLINE_CMD DIR_MAGIC_TYPE);
>   use Apache::Const; #defaults to :common
> 
> -plan tests => 13;
> +plan tests => 15;
> 
>   ok REDIRECT == 302;
>   ok AUTH_REQUIRED == 401;
> @@ -17,6 +18,8 @@
>   ok Apache::DECLINED == -1;
>   ok Apache::HTTP_GONE == 410;
>   ok Apache::OPT_EXECCGI;
> +ok Apache::DECLINE_CMD eq "\x07\x08";
> +ok Apache::DIR_MAGIC_TYPE eq "httpd/unix-directory";
> 
>   ok ! defined &M_GET;
>   Apache::Const->import('M_GET');
> Index: xs/tables/current/Apache/ConstantsTable.pm
> ===================================================================
> RCS file: /home/cvspublic/modperl-2.0/xs/tables/current/Apache/ConstantsTable.pm,v
> retrieving revision 1.27
> diff -u -r1.27 ConstantsTable.pm
> --- xs/tables/current/Apache/ConstantsTable.pm	24 Aug 2002 17:14:39 -0000	1.27
> +++ xs/tables/current/Apache/ConstantsTable.pm	9 May 2003 14:26:26 -0000
> @@ -143,6 +143,12 @@
>         'HTTP_INSUFFICIENT_STORAGE',
>         'HTTP_NOT_EXTENDED'
>       ],
> +    'config' => [
> +      'DECLINE_CMD'
> +    ],
> +    'types' => [
> +      'DIR_MAGIC_TYPE'
> +    ],
>       'filter_type' => [
>         'AP_FTYPE_RESOURCE',
>         'AP_FTYPE_CONTENT_SET',
> Index: xs/tables/current/ModPerl/FunctionTable.pm
> ===================================================================
> RCS file: /home/cvspublic/modperl-2.0/xs/tables/current/ModPerl/FunctionTable.pm,v
> retrieving revision 1.113
> diff -u -r1.113 FunctionTable.pm
> --- xs/tables/current/ModPerl/FunctionTable.pm	9 May 2003 03:33:02 -0000	1.113
> +++ xs/tables/current/ModPerl/FunctionTable.pm	9 May 2003 14:26:27 -0000
> @@ -1473,7 +1473,7 @@
>       ]
>     },
>     {
> -    'return_type' => 'int',
> +    'return_type' => 'SV *',
>       'name' => 'modperl_constants_lookup_apache',
>       'args' => [
>         {
> @@ -1483,7 +1483,7 @@
>       ]
>     },
>     {
> -    'return_type' => 'int',
> +    'return_type' => 'SV *',
>       'name' => 'modperl_constants_lookup_apr',
>       'args' => [
>         {
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@perl.apache.org
> For additional commands, e-mail: dev-help@perl.apache.org
> 
-- 
Philippe M. Chiasson <go...@cpan.org>

Re: DECLINE_CMD (take 3)

Posted by Stas Bekman <st...@stason.org>.
Geoffrey Young wrote:
> ok, here's take 3.

+1, good work, Geoff!

> if ($name eq 'DECLINE_CMD' || $name eq 'DIR_MAGIC_TYPE') {
>     print $c_fh <<EOF;
>               return newSVpv($alias{$name}, 0);
> EOF
> 
> I think it's proper to use newSVpv instead of newSVpvn - because the 
> length in question is the length of the constant (not the name), 
> presupposing we know the length of DECLINE_CMD beforehand seems wrong 
> (if we knew the value of the constants then why have a lookup function 
> :).  it also allows us to simplify logic in Code.pm and add to the list 
> of exceptional string cases if they arise in the future.

You are correct.

> anyway, here's the latest version.  I hope I got the threading stuff 
> right :)

Yup!

__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@perl.apache.org
For additional commands, e-mail: dev-help@perl.apache.org


Re: DECLINE_CMD (take 3)

Posted by Geoffrey Young <ge...@modperlcookbook.org>.
ok, here's take 3.

re:

if ($name eq 'DECLINE_CMD' || $name eq 'DIR_MAGIC_TYPE') {
     print $c_fh <<EOF;
               return newSVpv($alias{$name}, 0);
EOF

I think it's proper to use newSVpv instead of newSVpvn - because the length in question is 
the length of the constant (not the name), presupposing we know the length of DECLINE_CMD 
beforehand seems wrong (if we knew the value of the constants then why have a lookup 
function :).  it also allows us to simplify logic in Code.pm and add to the list of 
exceptional string cases if they arise in the future.

anyway, here's the latest version.  I hope I got the threading stuff right :)

--Geoff

Index: Changes
===================================================================
RCS file: /home/cvspublic/modperl-2.0/Changes,v
retrieving revision 1.184
diff -u -r1.184 Changes
--- Changes	9 May 2003 03:33:02 -0000	1.184
+++ Changes	9 May 2003 14:26:25 -0000
@@ -12,6 +12,9 @@

  =item 1.99_10-dev

+implement DECLINE_CMD and DIR_MAGIC_TYPE constants
+[Geoffrey Young]
+
  allow init filter handlers to call other methods than just $f->ctx [Stas]

  Fix Apache::Reload to gracefully handle the case with empty Touchfiles
Index: lib/Apache/ParseSource.pm
===================================================================
RCS file: /home/cvspublic/modperl-2.0/lib/Apache/ParseSource.pm,v
retrieving revision 1.42
diff -u -r1.42 ParseSource.pm
--- lib/Apache/ParseSource.pm	15 Apr 2003 02:22:41 -0000	1.42
+++ lib/Apache/ParseSource.pm	9 May 2003 14:26:25 -0000
@@ -156,8 +156,8 @@
          satisfy    => [qw{SATISFY_}],
          remotehost => [qw{REMOTE_}],
          http       => [qw{HTTP_}],
-#       config     => [qw{DECLINE_CMD}],
-#       types      => [qw{DIR_MAGIC_TYPE}],
+        config     => [qw{DECLINE_CMD}],
+        types      => [qw{DIR_MAGIC_TYPE}],
          override   => [qw{OR_ ACCESS_CONF RSRC_CONF}],
          log        => [qw(APLOG_)],
      },
Index: lib/ModPerl/Code.pm
===================================================================
RCS file: /home/cvspublic/modperl-2.0/lib/ModPerl/Code.pm,v
retrieving revision 1.96
diff -u -r1.96 Code.pm
--- lib/ModPerl/Code.pm	24 Apr 2003 01:51:37 -0000	1.96
+++ lib/ModPerl/Code.pm	9 May 2003 14:26:25 -0000
@@ -769,7 +769,7 @@
      my $package_len = length $package;

      my $func = canon_func(qw(constants lookup), $postfix);
-    my $proto = "int $func(const char *name)";
+    my $proto = "SV \*$func(pTHX_ const char *name)";

      print $h_fh "$proto;\n";

@@ -804,7 +804,20 @@
              print $c_fh <<EOF;
  $ifdef[0]
            if (strEQ(name, "$name")) {
-              return $alias{$name};
+EOF
+
+            if ($name eq 'DECLINE_CMD' || $name eq 'DIR_MAGIC_TYPE') {
+                print $c_fh <<EOF;
+              return newSVpv($alias{$name}, 0);
+EOF
+            }
+            else {
+                print $c_fh <<EOF;
+              return newSViv($alias{$name});
+EOF
+            }
+
+            print $c_fh <<EOF;
            }
  $ifdef[1]
  EOF
@@ -815,7 +828,7 @@
      print $c_fh <<EOF
      };
      Perl_croak_nocontext("unknown constant %s", name);
-    return MP_ENOCONST;
+    return newSViv(MP_ENOCONST);
  }
  EOF
  }
Index: src/modules/perl/modperl_const.c
===================================================================
RCS file: /home/cvspublic/modperl-2.0/src/modules/perl/modperl_const.c,v
retrieving revision 1.8
diff -u -r1.8 modperl_const.c
--- src/modules/perl/modperl_const.c	22 Oct 2001 05:42:23 -0000	1.8
+++ src/modules/perl/modperl_const.c	9 May 2003 14:26:25 -0000
@@ -1,27 +1,27 @@
  #include "mod_perl.h"
  #include "modperl_const.h"

-typedef int (*constants_lookup)(const char *);
+typedef SV *(*constants_lookup)(pTHX_ const char *);
  typedef const char ** (*constants_group_lookup)(const char *);

-static int new_constsub(pTHX_ constants_lookup lookup,
+static SV *new_constsub(pTHX_ constants_lookup lookup,
                          HV *caller_stash, HV *stash,
                          const char *name)
  {
      int name_len = strlen(name);
      GV **gvp = (GV **)hv_fetch(stash, name, name_len, TRUE);
-    int val;
+    SV *val;

      /* dont redefine */
      if (!isGV(*gvp) || !GvCV(*gvp)) {
-        val = (*lookup)(name);
+        val = (*lookup)(aTHX_ name);

  #if 0
          fprintf(stderr, "newCONSTSUB(%s, %s, %d)\n",
                  HvNAME(stash), name, val);
  #endif

-        newCONSTSUB(stash, (char *)name, newSViv(val));
+        newCONSTSUB(stash, (char *)name, val);
  #ifdef GvSHARED
          GvSHARED_on(*gvp);
  #endif
Index: src/modules/perl/modperl_module.c
===================================================================
RCS file: /home/cvspublic/modperl-2.0/src/modules/perl/modperl_module.c,v
retrieving revision 1.12
diff -u -r1.12 modperl_module.c
--- src/modules/perl/modperl_module.c	14 Apr 2003 06:38:50 -0000	1.12
+++ src/modules/perl/modperl_module.c	9 May 2003 14:26:25 -0000
@@ -669,7 +669,7 @@
              }
              else {
                  cmd->args_how =
-                    modperl_constants_lookup_apache(SvPV(val, len));
+                    SvIV(modperl_constants_lookup_apache(aTHX_ SvPV(val, len)));
              }
          }

@@ -695,7 +695,7 @@
              }
              else {
                  cmd->req_override =
-                    modperl_constants_lookup_apache(SvPV(val, len));
+                    SvIV(modperl_constants_lookup_apache(aTHX_ SvPV(val, len)));
              }
          }

Index: t/apache/constants.t
===================================================================
RCS file: /home/cvspublic/modperl-2.0/t/apache/constants.t,v
retrieving revision 1.5
diff -u -r1.5 constants.t
--- t/apache/constants.t	18 May 2002 02:29:44 -0000	1.5
+++ t/apache/constants.t	9 May 2003 14:26:25 -0000
@@ -5,10 +5,11 @@
  use Test;

  use Apache2 ();
-use Apache::Const -compile => qw(DECLINED :http :common TAKE23 &OPT_EXECCGI);
+use Apache::Const -compile => qw(DECLINED :http :common TAKE23 &OPT_EXECCGI
+                                 DECLINE_CMD DIR_MAGIC_TYPE);
  use Apache::Const; #defaults to :common

-plan tests => 13;
+plan tests => 15;

  ok REDIRECT == 302;
  ok AUTH_REQUIRED == 401;
@@ -17,6 +18,8 @@
  ok Apache::DECLINED == -1;
  ok Apache::HTTP_GONE == 410;
  ok Apache::OPT_EXECCGI;
+ok Apache::DECLINE_CMD eq "\x07\x08";
+ok Apache::DIR_MAGIC_TYPE eq "httpd/unix-directory";

  ok ! defined &M_GET;
  Apache::Const->import('M_GET');
Index: xs/tables/current/Apache/ConstantsTable.pm
===================================================================
RCS file: /home/cvspublic/modperl-2.0/xs/tables/current/Apache/ConstantsTable.pm,v
retrieving revision 1.27
diff -u -r1.27 ConstantsTable.pm
--- xs/tables/current/Apache/ConstantsTable.pm	24 Aug 2002 17:14:39 -0000	1.27
+++ xs/tables/current/Apache/ConstantsTable.pm	9 May 2003 14:26:26 -0000
@@ -143,6 +143,12 @@
        'HTTP_INSUFFICIENT_STORAGE',
        'HTTP_NOT_EXTENDED'
      ],
+    'config' => [
+      'DECLINE_CMD'
+    ],
+    'types' => [
+      'DIR_MAGIC_TYPE'
+    ],
      'filter_type' => [
        'AP_FTYPE_RESOURCE',
        'AP_FTYPE_CONTENT_SET',
Index: xs/tables/current/ModPerl/FunctionTable.pm
===================================================================
RCS file: /home/cvspublic/modperl-2.0/xs/tables/current/ModPerl/FunctionTable.pm,v
retrieving revision 1.113
diff -u -r1.113 FunctionTable.pm
--- xs/tables/current/ModPerl/FunctionTable.pm	9 May 2003 03:33:02 -0000	1.113
+++ xs/tables/current/ModPerl/FunctionTable.pm	9 May 2003 14:26:27 -0000
@@ -1473,7 +1473,7 @@
      ]
    },
    {
-    'return_type' => 'int',
+    'return_type' => 'SV *',
      'name' => 'modperl_constants_lookup_apache',
      'args' => [
        {
@@ -1483,7 +1483,7 @@
      ]
    },
    {
-    'return_type' => 'int',
+    'return_type' => 'SV *',
      'name' => 'modperl_constants_lookup_apr',
      'args' => [
        {


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@perl.apache.org
For additional commands, e-mail: dev-help@perl.apache.org


Re: DECLINE_CMD

Posted by Geoffrey Young <ge...@modperlcookbook.org>.
> Hmm, I haven't realized that it's used in _module.c as well. So my idea 
> introduces a bit of an overhead to allocate the SV, when it only wants 
> an int.
> But since this happens only during the server startup, I suppose it's 
> not a problem.

that was my thought.

[snip]
> 
> A few more comments below:
> 
>> Index: lib/ModPerl/Code.pm
> 
> [...]
> 
>>  $proto
>>  {
>> +    dTHX;
> 
> 
> It's going to be much more efficient to pass pTHX_ to the function.

ok, I'll try that.  I really have no idea how threads work, I just figured 
out that I needed something to get it to compile :)

> 
>> +          if (strEQ(name, "$name")) {
>> +              return newSVpv($alias{$name},0);
> 
> 
> Use newSVpvn, you know the length already (faster).

well, then I need two if statements - one for DECLINE_CMD and one for 
DIR_MAGIC_TYPE.  I'm not sure that the added logic is worth the (one time 
and slight) hit.

> 
>> +          }
>> +$ifdef[1]
>> +EOF
>> +            }
>> +            else {
>> +                print $c_fh <<EOF;
>>  $ifdef[0]
>>            if (strEQ(name, "$name")) {
>> -              return $alias{$name};
>> +              return newSViv($alias{$name});
> 
> 
> Use strnEQ (faster), like the orig int functions do. 

the original functions use strEQ - I didn't change that for the int ones and 
only carried it over for the string ones.

> You don't really 
> have to make special cases for these two constants. Just use the old 
> generation code and put 'return SVpvn($alias{$name});' if perl sees 
> these two constants, otherwise put 'return SViv($alias{$name});'

yeah :)

as per SViv vs newSViv, if we remove the newSV stuff then is it still save 
to remove the newSV from val here:

 > I think it should be:
 >
 > -        newCONSTSUB(stash, (char *)name, newSViv(val));
 > +        newCONSTSUB(stash, (char *)name, val);

?

> 
>> -typedef int (*constants_lookup)(const char *);
>> +typedef SV *(*constants_lookup)(const char *);
> 
> 
> So pTHX_ goes here:
> 
> -typedef int (*constants_lookup)(const char *);
> +typedef SV *(*constants_lookup)(pTHX_ const char *);
> 
> and aTHX_ in the caller...
> 
>> +                    SvIV(modperl_constants_lookup_apache(SvPV(val, 
>> len)));
>>              }
>>          }
>>
>> @@ -695,7 +695,7 @@
>>              }
>>              else {
>>                  cmd->req_override =
>> -                    modperl_constants_lookup_apache(SvPV(val, len));
>> +                    SvIV(modperl_constants_lookup_apache(SvPV(val, 
>> len)));
> 
> 
> it's probably safe to use SvIVX here, but since this code is not 
> run-time critical SvIV is just fine.

cool, thanks for the comments.  I'll have (another) patch soon.

--Geoff


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@perl.apache.org
For additional commands, e-mail: dev-help@perl.apache.org


Re: DECLINE_CMD

Posted by Stas Bekman <st...@stason.org>.
Geoffrey Young wrote:
> 
>> I think there is a much cleaner solution. Fix ModPerl::Code to make
>> modperl_constants_lookup_apr and modperl_constants_lookup_apache 
>> return SV. 
> 
> 
> good idea - I thought it would be harder than it was to change the 
> return type of everything. 

Hmm, I haven't realized that it's used in _module.c as well. So my idea 
introduces a bit of an overhead to allocate the SV, when it only wants an int.
But since this happens only during the server startup, I suppose it's not a 
problem.

> here is take 2.  the only thing that I'm not 
> sure about is
> 
> -        newCONSTSUB(stash, (char *)name, newSViv(val));
> +        newCONSTSUB(stash, (char *)name, newSVsv(val));
> 
> and whether we need to copy val with newSVsv or can just pass val along 
> without the copy.

I think it should be:

-        newCONSTSUB(stash, (char *)name, newSViv(val));
+        newCONSTSUB(stash, (char *)name, val);


>> BTW, why do we have the same generated files in two places?
>>
>> grep -Ilr modperl_constants_lookup_apr src xs 
>> src/modules/perl/modperl_const.c | grep '\.c$'
>> src/modules/perl/modperl_const.c
>> src/modules/perl/modperl_constants.c
>> xs/ModPerl/Const/modperl_const.c
>> xs/ModPerl/Const/modperl_constants.c
> 
> 
> I have no idea :)

Hmm...

A few more comments below:

> Index: lib/ModPerl/Code.pm
[...]
>  $proto
>  {
> +    dTHX;

It's going to be much more efficient to pass pTHX_ to the function.

> +          if (strEQ(name, "$name")) {
> +              return newSVpv($alias{$name},0);

Use newSVpvn, you know the length already (faster).

> +          }
> +$ifdef[1]
> +EOF
> +            }
> +            else {
> +                print $c_fh <<EOF;
>  $ifdef[0]
>            if (strEQ(name, "$name")) {
> -              return $alias{$name};
> +              return newSViv($alias{$name});

Use strnEQ (faster), like the orig int functions do. You don't really have to 
make special cases for these two constants. Just use the old generation code 
and put 'return SVpvn($alias{$name});' if perl sees these two constants, 
otherwise put 'return SViv($alias{$name});'

> -typedef int (*constants_lookup)(const char *);
> +typedef SV *(*constants_lookup)(const char *);

So pTHX_ goes here:

-typedef int (*constants_lookup)(const char *);
+typedef SV *(*constants_lookup)(pTHX_ const char *);

and aTHX_ in the caller...

> +                    SvIV(modperl_constants_lookup_apache(SvPV(val, len)));
>              }
>          }
> 
> @@ -695,7 +695,7 @@
>              }
>              else {
>                  cmd->req_override =
> -                    modperl_constants_lookup_apache(SvPV(val, len));
> +                    SvIV(modperl_constants_lookup_apache(SvPV(val, len)));

it's probably safe to use SvIVX here, but since this code is not run-time 
critical SvIV is just fine.

__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@perl.apache.org
For additional commands, e-mail: dev-help@perl.apache.org


Re: DECLINE_CMD

Posted by Geoffrey Young <ge...@modperlcookbook.org>.
> I think there is a much cleaner solution. Fix ModPerl::Code to make
> modperl_constants_lookup_apr and modperl_constants_lookup_apache return 
> SV. 

good idea - I thought it would be harder than it was to change the return type of 
everything.  here is take 2.  the only thing that I'm not sure about is

-        newCONSTSUB(stash, (char *)name, newSViv(val));
+        newCONSTSUB(stash, (char *)name, newSVsv(val));

and whether we need to copy val with newSVsv or can just pass val along without the copy.

> 
> BTW, why do we have the same generated files in two places?
> 
> grep -Ilr modperl_constants_lookup_apr src xs 
> src/modules/perl/modperl_const.c | grep '\.c$'
> src/modules/perl/modperl_const.c
> src/modules/perl/modperl_constants.c
> xs/ModPerl/Const/modperl_const.c
> xs/ModPerl/Const/modperl_constants.c

I have no idea :)

--Geoff

Index: Changes
===================================================================
RCS file: /home/cvspublic/modperl-2.0/Changes,v
retrieving revision 1.183
diff -u -r1.183 Changes
--- Changes	8 May 2003 00:31:27 -0000	1.183
+++ Changes	8 May 2003 13:53:54 -0000
@@ -12,6 +12,9 @@

  =item 1.99_10-dev

+implement DECLINE_CMD and DIR_MAGIC_TYPE constants
+[Geoffrey Young]
+
  Fix Apache::Reload to gracefully handle the case with empty Touchfiles
  [Dmitri Tikhonov <dm...@netilla.com>]

Index: lib/Apache/ParseSource.pm
===================================================================
RCS file: /home/cvspublic/modperl-2.0/lib/Apache/ParseSource.pm,v
retrieving revision 1.42
diff -u -r1.42 ParseSource.pm
--- lib/Apache/ParseSource.pm	15 Apr 2003 02:22:41 -0000	1.42
+++ lib/Apache/ParseSource.pm	8 May 2003 13:53:54 -0000
@@ -156,8 +156,8 @@
          satisfy    => [qw{SATISFY_}],
          remotehost => [qw{REMOTE_}],
          http       => [qw{HTTP_}],
-#       config     => [qw{DECLINE_CMD}],
-#       types      => [qw{DIR_MAGIC_TYPE}],
+        config     => [qw{DECLINE_CMD}],
+        types      => [qw{DIR_MAGIC_TYPE}],
          override   => [qw{OR_ ACCESS_CONF RSRC_CONF}],
          log        => [qw(APLOG_)],
      },
Index: lib/ModPerl/Code.pm
===================================================================
RCS file: /home/cvspublic/modperl-2.0/lib/ModPerl/Code.pm,v
retrieving revision 1.96
diff -u -r1.96 Code.pm
--- lib/ModPerl/Code.pm	24 Apr 2003 01:51:37 -0000	1.96
+++ lib/ModPerl/Code.pm	8 May 2003 13:53:55 -0000
@@ -769,7 +769,7 @@
      my $package_len = length $package;

      my $func = canon_func(qw(constants lookup), $postfix);
-    my $proto = "int $func(const char *name)";
+    my $proto = "SV \*$func(const char *name)";

      print $h_fh "$proto;\n";

@@ -777,6 +777,8 @@

  $proto
  {
+    dTHX;
+
      if (*name == 'A' && strnEQ(name, "$package", $package_len)) {
          name += $package_len;
      }
@@ -801,13 +803,25 @@

          for my $name (@$names) {
              my @ifdef = constants_ifdef($alias{$name});
-            print $c_fh <<EOF;
+            if ($name eq 'DECLINE_CMD' ||
+                $name eq 'DIR_MAGIC_TYPE') {
+                print $c_fh <<EOF;
+$ifdef[0]
+          if (strEQ(name, "$name")) {
+              return newSVpv($alias{$name},0);
+          }
+$ifdef[1]
+EOF
+            }
+            else {
+                print $c_fh <<EOF;
  $ifdef[0]
            if (strEQ(name, "$name")) {
-              return $alias{$name};
+              return newSViv($alias{$name});
            }
  $ifdef[1]
  EOF
+           }
          }
          print $c_fh "      break;\n";
      }
@@ -815,7 +829,7 @@
      print $c_fh <<EOF
      };
      Perl_croak_nocontext("unknown constant %s", name);
-    return MP_ENOCONST;
+    return newSViv(MP_ENOCONST);
  }
  EOF
  }
Index: src/modules/perl/modperl_const.c
===================================================================
RCS file: /home/cvspublic/modperl-2.0/src/modules/perl/modperl_const.c,v
retrieving revision 1.8
diff -u -r1.8 modperl_const.c
--- src/modules/perl/modperl_const.c	22 Oct 2001 05:42:23 -0000	1.8
+++ src/modules/perl/modperl_const.c	8 May 2003 13:53:55 -0000
@@ -1,16 +1,16 @@
  #include "mod_perl.h"
  #include "modperl_const.h"

-typedef int (*constants_lookup)(const char *);
+typedef SV *(*constants_lookup)(const char *);
  typedef const char ** (*constants_group_lookup)(const char *);

-static int new_constsub(pTHX_ constants_lookup lookup,
+static SV *new_constsub(pTHX_ constants_lookup lookup,
                          HV *caller_stash, HV *stash,
                          const char *name)
  {
      int name_len = strlen(name);
      GV **gvp = (GV **)hv_fetch(stash, name, name_len, TRUE);
-    int val;
+    SV *val;

      /* dont redefine */
      if (!isGV(*gvp) || !GvCV(*gvp)) {
@@ -21,7 +21,7 @@
                  HvNAME(stash), name, val);
  #endif

-        newCONSTSUB(stash, (char *)name, newSViv(val));
+        newCONSTSUB(stash, (char *)name, newSVsv(val));
  #ifdef GvSHARED
          GvSHARED_on(*gvp);
  #endif
Index: src/modules/perl/modperl_module.c
===================================================================
RCS file: /home/cvspublic/modperl-2.0/src/modules/perl/modperl_module.c,v
retrieving revision 1.12
diff -u -r1.12 modperl_module.c
--- src/modules/perl/modperl_module.c	14 Apr 2003 06:38:50 -0000	1.12
+++ src/modules/perl/modperl_module.c	8 May 2003 13:53:56 -0000
@@ -669,7 +669,7 @@
              }
              else {
                  cmd->args_how =
-                    modperl_constants_lookup_apache(SvPV(val, len));
+                    SvIV(modperl_constants_lookup_apache(SvPV(val, len)));
              }
          }

@@ -695,7 +695,7 @@
              }
              else {
                  cmd->req_override =
-                    modperl_constants_lookup_apache(SvPV(val, len));
+                    SvIV(modperl_constants_lookup_apache(SvPV(val, len)));
              }
          }

Index: t/apache/constants.t
===================================================================
RCS file: /home/cvspublic/modperl-2.0/t/apache/constants.t,v
retrieving revision 1.5
diff -u -r1.5 constants.t
--- t/apache/constants.t	18 May 2002 02:29:44 -0000	1.5
+++ t/apache/constants.t	8 May 2003 13:53:56 -0000
@@ -5,10 +5,11 @@
  use Test;

  use Apache2 ();
-use Apache::Const -compile => qw(DECLINED :http :common TAKE23 &OPT_EXECCGI);
+use Apache::Const -compile => qw(DECLINED :http :common TAKE23 &OPT_EXECCGI
+                                 DECLINE_CMD DIR_MAGIC_TYPE);
  use Apache::Const; #defaults to :common

-plan tests => 13;
+plan tests => 15;

  ok REDIRECT == 302;
  ok AUTH_REQUIRED == 401;
@@ -17,6 +18,8 @@
  ok Apache::DECLINED == -1;
  ok Apache::HTTP_GONE == 410;
  ok Apache::OPT_EXECCGI;
+ok Apache::DECLINE_CMD eq "\x07\x08";
+ok Apache::DIR_MAGIC_TYPE eq "httpd/unix-directory";

  ok ! defined &M_GET;
  Apache::Const->import('M_GET');
Index: xs/tables/current/Apache/ConstantsTable.pm
===================================================================
RCS file: /home/cvspublic/modperl-2.0/xs/tables/current/Apache/ConstantsTable.pm,v
retrieving revision 1.27
diff -u -r1.27 ConstantsTable.pm
--- xs/tables/current/Apache/ConstantsTable.pm	24 Aug 2002 17:14:39 -0000	1.27
+++ xs/tables/current/Apache/ConstantsTable.pm	8 May 2003 13:53:57 -0000
@@ -143,6 +143,12 @@
        'HTTP_INSUFFICIENT_STORAGE',
        'HTTP_NOT_EXTENDED'
      ],
+    'config' => [
+      'DECLINE_CMD'
+    ],
+    'types' => [
+      'DIR_MAGIC_TYPE'
+    ],
      'filter_type' => [
        'AP_FTYPE_RESOURCE',
        'AP_FTYPE_CONTENT_SET',
Index: xs/tables/current/ModPerl/FunctionTable.pm
===================================================================
RCS file: /home/cvspublic/modperl-2.0/xs/tables/current/ModPerl/FunctionTable.pm,v
retrieving revision 1.112
diff -u -r1.112 FunctionTable.pm
--- xs/tables/current/ModPerl/FunctionTable.pm	16 Apr 2003 03:03:35 -0000	1.112
+++ xs/tables/current/ModPerl/FunctionTable.pm	8 May 2003 13:54:00 -0000
@@ -1469,7 +1469,7 @@
      ]
    },
    {
-    'return_type' => 'int',
+    'return_type' => 'SV *',
      'name' => 'modperl_constants_lookup_apache',
      'args' => [
        {
@@ -1479,7 +1479,7 @@
      ]
    },
    {
-    'return_type' => 'int',
+    'return_type' => 'SV *',
      'name' => 'modperl_constants_lookup_apr',
      'args' => [
        {



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@perl.apache.org
For additional commands, e-mail: dev-help@perl.apache.org


Re: DECLINE_CMD

Posted by Stas Bekman <st...@stason.org>.
Geoffrey Young wrote:
> hi all
> 
>   I've been playing around with directive handlers and have found a 
> problem with DECLINE_CMD, namely that it doesn't exist within 
> Apache::Const.
> 
>   unfortunately, implementing DECLINE_CMD is not as easy as just adding 
> it to the constants map - DECLINE_CMD is the only constant in Apache/APR 
> that isn't an integer, and all the autogeneration/newCONSTSUB stuff is 
> geared toward creating integer constants.
> 
>   the below patch works and seems a decent way to do it (now we have a 
> routine for character-based constants in case there end up being 
> others), but somebody else might have a better idea.

I think I do have such idea ;)

I think your implementation is fine, however this:

  > +    else if (strnEQ(name, "DECLINE_CMD", 11)) {

adds an unnecessary string comparison in more than 50% of times.

I think there is a much cleaner solution. Fix ModPerl::Code to make
modperl_constants_lookup_apr and modperl_constants_lookup_apache return SV. 
Voila, now replace all current returns:

   return Foo;

with:

   return newSViv(Foo);

and add a special case for "DECLINE_CMD", which will do:

   return newSVpvn(DECLINE_CMD, 11);

Does it sound good?

BTW, why do we have the same generated files in two places?

grep -Ilr modperl_constants_lookup_apr src xs src/modules/perl/modperl_const.c 
| grep '\.c$'
src/modules/perl/modperl_const.c
src/modules/perl/modperl_constants.c
xs/ModPerl/Const/modperl_const.c
xs/ModPerl/Const/modperl_constants.c

Also I'd suggest to put this sub-test in t/apache/constants.t? since you just 
compare the string, without testing the functionality. Let's keep this 
particular test focused on perlmodule testing.

However a new test verifying the actual use of Apache::DECLINE_CMD would be 
useful.

[...]

> Index: t/response/TestDirective/perlloadmodule.pm
> ===================================================================
> RCS file: 
> /home/cvspublic/modperl-2.0/t/response/TestDirective/perlloadmodule.pm,v

[...]

> +
> +    ok t_cmp("\x07\x08", Apache::DECLINE_CMD);

Finally check: lib/Apache/ParseSource.pm (uncomment DECLINE_CMD there?)

And here is another missing constant, which is a string as well, (which will 
make your code even less efficient)

#define DIR_MAGIC_TYPE "httpd/unix-directory"

it's now commented out in lib/Apache/ParseSource.pm

__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@perl.apache.org
For additional commands, e-mail: dev-help@perl.apache.org