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/29 16:16:23 UTC

$parms->info(), take 2

ok, here is an updated patch, with the XS cleaned up a bit.

the important change between this version and the last is that $parms->info() is now 
read-only.  I couldn't think of a case where you would want to write to the info slot, and 
doing so would just cause erroneous results - since directive handlers typically rely on 
that data to make decisions about how directives are to be handled, changing that value in 
the middle of the config process would almost certainly be bad.

also included is adding the -u flag to part of getdiff.pl.  I know that's not platform 
independent, but -u was used elsewhere in getdiff.pl and I'm sure you don't want people 
sending in non-unified diffs :)

--Geoff

Index: Changes
===================================================================
RCS file: /home/cvspublic/modperl-2.0/Changes,v
retrieving revision 1.189
diff -u -r1.189 Changes
--- Changes     20 May 2003 06:53:47 -0000      1.189
+++ Changes     29 May 2003 13:53:56 -0000
@@ -12,6 +12,9 @@

  =item 1.99_10-dev

+implement $parms->info.  directive handlers should now be complete.
+[Geoffrey Young]
+
  MP_GTOP now works with modern GCC
  [Philippe M. Chiasson <gozer@cpan.org]

Index: src/modules/perl/modperl_module.c
===================================================================
RCS file: /home/cvspublic/modperl-2.0/src/modules/perl/modperl_module.c,v
retrieving revision 1.13
diff -u -r1.13 modperl_module.c
--- src/modules/perl/modperl_module.c   12 May 2003 13:00:15 -0000      1.13
+++ src/modules/perl/modperl_module.c   29 May 2003 13:53:56 -0000
@@ -13,12 +13,6 @@
      modperl_module_info_t *minfo;
  } modperl_module_cfg_t;

-typedef struct {
-    module *modp;
-    const char *cmd_data;
-    const char *func_name;
-} modperl_module_cmd_data_t;
-
  #define MP_MODULE_INFO(modp) \
      (modperl_module_info_t *)modp->dynamic_load_handle

@@ -711,7 +705,7 @@
          cmd->cmd_data = info;

          /* no default if undefined */
-        if (!(errmsg = modperl_module_cmd_fetch(aTHX_ obj, "data", &val))) {
+        if (!(errmsg = modperl_module_cmd_fetch(aTHX_ obj, "cmd_data", &val))) {
              info->cmd_data = apr_pstrdup(p, SvPV(val, len));
          }

Index: src/modules/perl/modperl_types.h
===================================================================
RCS file: /home/cvspublic/modperl-2.0/src/modules/perl/modperl_types.h,v
retrieving revision 1.66
diff -u -r1.66 modperl_types.h
--- src/modules/perl/modperl_types.h    17 Apr 2003 08:04:47 -0000      1.66
+++ src/modules/perl/modperl_types.h    29 May 2003 13:53:56 -0000
@@ -242,4 +242,10 @@
      char *path_info;
  } modperl_uri_t;

+typedef struct {
+    module *modp;
+    const char *cmd_data;
+    const char *func_name;
+} modperl_module_cmd_data_t;
+
  #endif /* MODPERL_TYPES_H */
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  29 May 2003 13:53:56 -0000
@@ -23,6 +23,7 @@
      },
      {
       name => 'MyOtherTest',
+     cmd_data => 'some info',
      },
      {
       name => 'ServerTest',
@@ -72,11 +73,13 @@
  sub MyTest {
      my($self, $parms, @args) = @_;
      $self->{MyTest} = \@args;
+    $self->{MyTestInfo} = $parms->info;
  }

  sub MyOtherTest {
      my($self, $parms, $arg) = @_;
      $self->{MyOtherTest} = $arg;
+    $self->{MyOtherTestInfo} = $parms->info;
  }

  sub ServerTest {
@@ -97,7 +100,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 => 9;

      t_debug("per-dir config:", $dir_cfg);
      t_debug("per-srv config:", $srv_cfg);
@@ -116,8 +119,13 @@
      ok t_cmp('value', $dir_cfg->{MyOtherTest},
               'MyOtherTest value');

+    ok t_cmp('some info', $dir_cfg->{MyOtherTestInfo},
+             'MyOtherTest cmd_data');
+
      ok t_cmp(['one', 'two'], $dir_cfg->{MyTest},
               'MyTest one two');
+
+    ok ! $dir_cfg->{MyTestInfo};

      ok t_cmp('per-server', $srv_cfg->{ServerTest});

Index: util/getdiff.pl
===================================================================
RCS file: /home/cvspublic/modperl-2.0/util/getdiff.pl,v
retrieving revision 1.1
diff -u -r1.1 getdiff.pl
--- util/getdiff.pl     13 Aug 2001 02:18:38 -0000      1.1
+++ util/getdiff.pl     29 May 2003 13:53:57 -0000
@@ -12,7 +12,7 @@
  # patch -p0 < newtest.patch

  # cvs diff
-my $o = `cvs diff`;
+my $o = `cvs diff -u`;

  # strip '? filename' cvs lines for unknown files
  $o =~ s/^\?.*\n//gm;
Index: xs/maps/modperl_functions.map
===================================================================
RCS file: /home/cvspublic/modperl-2.0/xs/maps/modperl_functions.map,v
retrieving revision 1.56
diff -u -r1.56 modperl_functions.map
--- xs/maps/modperl_functions.map       1 Apr 2003 05:20:50 -0000       1.56
+++ xs/maps/modperl_functions.map       29 May 2003 13:53:57 -0000
@@ -130,3 +130,5 @@
   mpxs_Apache__Directive_as_hash
   Apache__Directive_lookup | MPXS_ | ...

+MODULE=Apache::CmdParms
+ Apache__CmdParms_info | MPXS_ | ...
Index: xs/tables/current/Apache/StructureTable.pm
===================================================================
RCS file: /home/cvspublic/modperl-2.0/xs/tables/current/Apache/StructureTable.pm,v
retrieving revision 1.35
diff -u -r1.35 StructureTable.pm
--- xs/tables/current/Apache/StructureTable.pm  24 Aug 2002 17:14:40 -0000      1.35
+++ xs/tables/current/Apache/StructureTable.pm  29 May 2003 13:53:57 -0000
@@ -2499,10 +2499,6 @@
      'type' => 'cmd_parms',
      'elts' => [
        {
-        'type' => 'void *',
-        'name' => 'info'
-      },
-      {
          'type' => 'int',
          'name' => 'override'
        },
Index: xs/tables/current/ModPerl/FunctionTable.pm
===================================================================
RCS file: /home/cvspublic/modperl-2.0/xs/tables/current/ModPerl/FunctionTable.pm,v
retrieving revision 1.114
diff -u -r1.114 FunctionTable.pm
--- xs/tables/current/ModPerl/FunctionTable.pm  12 May 2003 13:00:15 -0000      1.114
+++ xs/tables/current/ModPerl/FunctionTable.pm  29 May 2003 13:53:58 -0000
@@ -6641,6 +6641,19 @@
        }
      ]
    },
+  {
+    'return_type' => 'char *',
+    'name' => 'Apache__CmdParms_info',
+    'attr' => [
+      'static'
+    ],
+    'args' => [
+      {
+        'type' => 'PerlInterpreter *',
+        'name' => 'my_perl'
+      },
+    ]
+  },
  ];



--- /dev/null   Tue May  5 16:32:27 1998
+++ xs/Apache/CmdParms/Apache__CmdParms.h       Thu May 29 09:47:03 2003
@@ -0,0 +1,24 @@
+#include "modperl_module.h"
+
+static XS(MPXS_Apache__CmdParms_info)
+{
+    dXSARGS;
+
+    mpxs_usage_items_1("cmd_parms");
+
+    mpxs_PPCODE({
+        cmd_parms *obj;
+        const char *data;
+
+        obj = mp_xs_sv2_Apache__CmdParms(ST(0));
+
+        data = ((modperl_module_cmd_data_t *)obj->info)->cmd_data;
+
+        if (data) {
+            XPUSHs(sv_2mortal(newSVpv(data, 0)));
+        }
+        else {
+            XPUSHs(&PL_sv_undef);
+        }
+    });
+}


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


Re: $parms->info(), take 2

Posted by Stas Bekman <st...@stason.org>.
Geoffrey Young wrote:
> 
>>> all those *.h files are full of both mpxs_ functions and static XS 
>>> functions, with functionaity implemented in one or the other 
>>> seemingly at random.  I have no idea how you would choose one format 
>>> over the other, but if the mpxs_ implementation makes you more 
>>> comfortable, that's fine - I'll commit that approach tomorrow.
>>
>>
>>
>> Even though they look random, they aren't. I have tried to document 
>> some of these here:
>> http://perl.apache.org/docs/2.0/devel/core/explained.html#Adding_Wrappers_for_existing_APIs_and_Creating_New_APIs 
>>
> 
> 
> wow.  here I am raking through the code trying to figure it all out and 
> there it is in print.
> 
>> Admittedly it's incomplete. I wrote that document when I was coping 
>> with this puzzle myself. As you are now coping with it, it'd help if 
>> you could improve that document.
>>
>> I believe Doug's idea was to have to write (and maintain) the minimal 
>> amount of code. So if you have a simple function where the args and 
>> return values can be converted from perl to C and back automatically, 
>> you use just that. If you have an optional number of args you use 
>> (...) and manipulate the stack. If you have a variable number/type of 
>> return args you use XS(). If you read the above section, things might 
>> get more clear.
> 
> 
> yeah, that actually helps lots.  now I know what to do with the others 
> that will come :)  thanks stas.

Glad to hear that you have found it useful. If you look at your committed 
function and the first patch that you have made you will see how much simpler 
the committed one was.

My "offer" still stands: while you write new wrappers, please help to improve 
that document, explaining all the logic, since it's not complete.

> anyway, I made the change and committed it this morning - all tests pass 
> and I can use it in real life just fine.

geoff++

> more stuff to come... :)

Looking forward to it!

__________________________________________________________________
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: $parms->info(), take 2

Posted by Geoffrey Young <ge...@modperlcookbook.org>.
>> all those *.h files are full of both mpxs_ functions and static XS 
>> functions, with functionaity implemented in one or the other seemingly 
>> at random.  I have no idea how you would choose one format over the 
>> other, but if the mpxs_ implementation makes you more comfortable, 
>> that's fine - I'll commit that approach tomorrow.
> 
> 
> Even though they look random, they aren't. I have tried to document some 
> of these here:
> http://perl.apache.org/docs/2.0/devel/core/explained.html#Adding_Wrappers_for_existing_APIs_and_Creating_New_APIs 
> 

wow.  here I am raking through the code trying to figure it all out and 
there it is in print.

> Admittedly it's incomplete. I wrote that document when I was coping with 
> this puzzle myself. As you are now coping with it, it'd help if you 
> could improve that document.
> 
> I believe Doug's idea was to have to write (and maintain) the minimal 
> amount of code. So if you have a simple function where the args and 
> return values can be converted from perl to C and back automatically, 
> you use just that. If you have an optional number of args you use (...) 
> and manipulate the stack. If you have a variable number/type of return 
> args you use XS(). If you read the above section, things might get more 
> clear.

yeah, that actually helps lots.  now I know what to do with the others that 
will come :)  thanks stas.

anyway, I made the change and committed it this morning - all tests pass and 
I can use it in real life just fine.

more stuff to come... :)

--Geoff


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


Re: $parms->info(), take 2

Posted by Stas Bekman <st...@stason.org>.
Geoffrey Young wrote:
> 
>>> +static XS(MPXS_Apache__CmdParms_info)
>>
> 
>> All you need is:
>>
>> SV *mpxs_Apache__CmdParms_info(pTHX_ cmd_params* cmd_parms)
> 
> 
>>
>> this is untested, but should give you an idea. of course you need to 
>> adjust the map file and Functions.pm.
>>
>> Doesn't it look much simpler?
> 
> 
> all those *.h files are full of both mpxs_ functions and static XS 
> functions, with functionaity implemented in one or the other seemingly 
> at random.  I have no idea how you would choose one format over the 
> other, but if the mpxs_ implementation makes you more comfortable, 
> that's fine - I'll commit that approach tomorrow.

Even though they look random, they aren't. I have tried to document some of 
these here:
http://perl.apache.org/docs/2.0/devel/core/explained.html#Adding_Wrappers_for_existing_APIs_and_Creating_New_APIs
Admittedly it's incomplete. I wrote that document when I was coping with this 
puzzle myself. As you are now coping with it, it'd help if you could improve 
that document.

I believe Doug's idea was to have to write (and maintain) the minimal amount 
of code. So if you have a simple function where the args and return values can 
be converted from perl to C and back automatically, you use just that. If you 
have an optional number of args you use (...) and manipulate the stack. If you 
have a variable number/type of return args you use XS(). If you read the above 
section, things might get more clear.

The ways you coded the function the first and second time weren't wrong, they 
were just unnecessary verbose. All we want is to keep the code as simple as 
possible, which simplifies the maintence and reduces the number of potential 
bugs. Hope this "noble" goal is understandable.

__________________________________________________________________
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: $parms->info(), take 2

Posted by Geoffrey Young <ge...@modperlcookbook.org>.
>> +static XS(MPXS_Apache__CmdParms_info)

> All you need is:
> 
> SV *mpxs_Apache__CmdParms_info(pTHX_ cmd_params* cmd_parms)

> 
> this is untested, but should give you an idea. of course you need to 
> adjust the map file and Functions.pm.
> 
> Doesn't it look much simpler?

all those *.h files are full of both mpxs_ functions and static XS 
functions, with functionaity implemented in one or the other seemingly at 
random.  I have no idea how you would choose one format over the other, but 
if the mpxs_ implementation makes you more comfortable, that's fine - I'll 
commit that approach tomorrow.

--Geoff


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


Re: $parms->info(), take 2

Posted by Stas Bekman <st...@stason.org>.
 > also included is adding the -u flag to part of getdiff.pl.  I know
 > that's not platform independent, but -u was used elsewhere in getdiff.pl
 > and I'm sure you don't want people sending in non-unified diffs :)

+1, but commit it separately. We all have ~/.cvsrc with diff -u so we didn't 
notice that ;)

Geoffrey Young wrote:
> ok, here is an updated patch, with the XS cleaned up a bit.
> 
> the important change between this version and the last is that 
> $parms->info() is now read-only.  I couldn't think of a case where you 
> would want to write to the info slot, and doing so would just cause 
> erroneous results - since directive handlers typically rely on that data 
> to make decisions about how directives are to be handled, changing that 
> value in the middle of the config process would almost certainly be bad.
[..]
> --- /dev/null   Tue May  5 16:32:27 1998
> +++ xs/Apache/CmdParms/Apache__CmdParms.h       Thu May 29 09:47:03 2003
> @@ -0,0 +1,24 @@
> +#include "modperl_module.h"
> +
> +static XS(MPXS_Apache__CmdParms_info)
> +{
> +    dXSARGS;
> +
> +    mpxs_usage_items_1("cmd_parms");
> +
> +    mpxs_PPCODE({
> +        cmd_parms *obj;
> +        const char *data;
> +
> +        obj = mp_xs_sv2_Apache__CmdParms(ST(0));
> +
> +        data = ((modperl_module_cmd_data_t *)obj->info)->cmd_data;
> +
> +        if (data) {
> +            XPUSHs(sv_2mortal(newSVpv(data, 0)));
> +        }
> +        else {
> +            XPUSHs(&PL_sv_undef);
> +        }
> +    });
> +}

All you need is:

SV *mpxs_Apache__CmdParms_info(pTHX_ cmd_params* cmd_parms)
{
    const char *data =
        ((modperl_module_cmd_data_t *)cmd_parms->info)->cmd_data;
    if (data) {
        return newSVpv(data, 0);
    }

    return &PL_sv_undef;
}

or if you want to use "usage":

SV *mpxs_Apache__CmdParms_info(pTHX_ I32 items, SV **MARK, SV **SP)
{
    const char *data;

    mpxs_usage_items_1("cmd_parms");

    if ((data = ((modperl_module_cmd_data_t *)cmd_parms->info)->cmd_data));
        return newSVpv(data, 0);
    }

    return &PL_sv_undef;

}

this is untested, but should give you an idea. of course you need to adjust 
the map file and Functions.pm.

Doesn't it look much simpler?

__________________________________________________________________
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