all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* What is the recommended way to find out the number of arguments passed to a module function?
@ 2024-01-10 19:01 dalanicolai
  2024-01-10 19:15 ` Eli Zaretskii
  2024-01-12  0:52 ` Emanuel Berg
  0 siblings, 2 replies; 8+ messages in thread
From: dalanicolai @ 2024-01-10 19:01 UTC (permalink / raw)
  To: Emacs Devel

[-- Attachment #1: Type: text/plain, Size: 1779 bytes --]

I am trying to write a dynamic module.

In the module API's 'make-function' we should pass a min-arity and a
max-arity.
However, it is unclear to me what is the recommended way to check for the
number of
arguments passed to some module function, as when not passing any argument,
the 'optional' argument does not seem
to be nil, or any emacs-value at all (I have tested if it might be a NULL
pointer). I have tested it using a 'test-module' with the following code:

#include <emacs-module.h>
> int plugin_is_GPL_compatible;
> static emacs_value
> test (emacs_env *env, ptrdiff_t nargs, emacs_value *args, void *data)
> {
>   int integer = env->is_not_nil(env, args[0])? 1 : 0;
>   return env->make_integer(env, integer);
>   /* return args[0]; */
> }
> int
> emacs_module_init (struct emacs_runtime *runtime)
> {
>   emacs_env *env = runtime->get_environment (runtime);
>   emacs_value func = env->make_function (env, 0, 1, test, NULL, NULL);
>   emacs_value symbol = env->intern (env, "test");
>   emacs_value args[] = {symbol, func};
>   env->funcall (env, env->intern (env, "defalias"), 2, args);
>   return 0;
> }


The 'test' function checks if the value of the argument is non-nil, and
'returns' a 1 if it is and a 0 otherwise. It works fine when passing an
argument, e.g. t or nil, but Emacs crashes when I don't pass an argument.
Also, I tried to simply return the value (by replacing the return line with
the line in the comment below it), which returns the value successfully
when I pass an argument, but again Emacs crashes when I don't pass any
argument.

Now, of course, I could 'fix it' by creating a lisp function that always
passes an argument, but I wonder if this is the 'only' solution. Does the
module API provide a way to check for the 'optional' argument?

[-- Attachment #2: Type: text/html, Size: 2214 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: What is the recommended way to find out the number of arguments passed to a module function?
  2024-01-10 19:01 What is the recommended way to find out the number of arguments passed to a module function? dalanicolai
@ 2024-01-10 19:15 ` Eli Zaretskii
  2024-01-10 20:06   ` dalanicolai
       [not found]   ` <CACJP=3mw8tcNXWeH3=Ge5OuDBQ=Jwc9+yYxFejg5weBPXYU4bQ@mail.gmail.com>
  2024-01-12  0:52 ` Emanuel Berg
  1 sibling, 2 replies; 8+ messages in thread
From: Eli Zaretskii @ 2024-01-10 19:15 UTC (permalink / raw)
  To: dalanicolai; +Cc: emacs-devel

> From: dalanicolai <dalanicolai@gmail.com>
> Date: Wed, 10 Jan 2024 20:01:09 +0100
> 
> In the module API's 'make-function' we should pass a min-arity and a max-arity.
> However, it is unclear to me what is the recommended way to check for the number of
> arguments passed to some module function, as when not passing any argument, the 'optional'
> argument does not seem
> to be nil, or any emacs-value at all (I have tested if it might be a NULL pointer). I have tested it using a
> 'test-module' with the following code:
> 
>  #include <emacs-module.h>
>  int plugin_is_GPL_compatible;
>  static emacs_value
>  test (emacs_env *env, ptrdiff_t nargs, emacs_value *args, void *data)
>  {
>    int integer = env->is_not_nil(env, args[0])? 1 : 0;
>    return env->make_integer(env, integer);
>    /* return args[0]; */
>  }
>  int
>  emacs_module_init (struct emacs_runtime *runtime)
>  {
>    emacs_env *env = runtime->get_environment (runtime);
>    emacs_value func = env->make_function (env, 0, 1, test, NULL, NULL);
>    emacs_value symbol = env->intern (env, "test");
>    emacs_value args[] = {symbol, func};
>    env->funcall (env, env->intern (env, "defalias"), 2, args);
>    return 0;
>  }
> 
> The 'test' function checks if the value of the argument is non-nil, and 'returns' a 1 if it is and a 0
> otherwise. It works fine when passing an argument, e.g. t or nil, but Emacs crashes when I don't pass
> an argument. Also, I tried to simply return the value (by replacing the return line with the line in the
> comment below it), which returns the value successfully when I pass an argument, but again Emacs
> crashes when I don't pass any argument.

I guess I'm confused: if you call your function with zero arguments,
then why do you expect to find anything useful in the args[] array, or
even assume that the args[] array can be accessed?

Don't you get nargs = 0 in 'test' in this case?



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: What is the recommended way to find out the number of arguments passed to a module function?
  2024-01-10 19:15 ` Eli Zaretskii
@ 2024-01-10 20:06   ` dalanicolai
       [not found]   ` <CACJP=3mw8tcNXWeH3=Ge5OuDBQ=Jwc9+yYxFejg5weBPXYU4bQ@mail.gmail.com>
  1 sibling, 0 replies; 8+ messages in thread
From: dalanicolai @ 2024-01-10 20:06 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 2876 bytes --]

(I should reply to all :)

Ah okay, well I am completely new to C, so I am still finding out things
(e.g. I have not 'actively' used ptrdiff yet).
But indeed the 'nargs' just give the number of passed args (obviously).

I had checked the 'sizeof' the args array when passing an argument and
without passing an argument,
and it was the same. Then the docs
<https://www.gnu.org/software/emacs/manual/html_node/elisp/Module-Functions.html>
mention that NARGS is the 'required' number of arguments,
I had misunderstood it a little (of course the required number is variable,
but somehow I assumed it
would be 1 in this case). So sometimes I overlook the 'obvious', and
because I had spent some time on testing
things already, I decided to just ask here.

Anyway, as usual, thanks for your quick and helpful answer!

On Wed, 10 Jan 2024 at 20:16, Eli Zaretskii <eliz@gnu.org> wrote:

> > From: dalanicolai <dalanicolai@gmail.com>
> > Date: Wed, 10 Jan 2024 20:01:09 +0100
> >
> > In the module API's 'make-function' we should pass a min-arity and a
> max-arity.
> > However, it is unclear to me what is the recommended way to check for
> the number of
> > arguments passed to some module function, as when not passing any
> argument, the 'optional'
> > argument does not seem
> > to be nil, or any emacs-value at all (I have tested if it might be a
> NULL pointer). I have tested it using a
> > 'test-module' with the following code:
> >
> >  #include <emacs-module.h>
> >  int plugin_is_GPL_compatible;
> >  static emacs_value
> >  test (emacs_env *env, ptrdiff_t nargs, emacs_value *args, void *data)
> >  {
> >    int integer = env->is_not_nil(env, args[0])? 1 : 0;
> >    return env->make_integer(env, integer);
> >    /* return args[0]; */
> >  }
> >  int
> >  emacs_module_init (struct emacs_runtime *runtime)
> >  {
> >    emacs_env *env = runtime->get_environment (runtime);
> >    emacs_value func = env->make_function (env, 0, 1, test, NULL, NULL);
> >    emacs_value symbol = env->intern (env, "test");
> >    emacs_value args[] = {symbol, func};
> >    env->funcall (env, env->intern (env, "defalias"), 2, args);
> >    return 0;
> >  }
> >
> > The 'test' function checks if the value of the argument is non-nil, and
> 'returns' a 1 if it is and a 0
> > otherwise. It works fine when passing an argument, e.g. t or nil, but
> Emacs crashes when I don't pass
> > an argument. Also, I tried to simply return the value (by replacing the
> return line with the line in the
> > comment below it), which returns the value successfully when I pass an
> argument, but again Emacs
> > crashes when I don't pass any argument.
>
> I guess I'm confused: if you call your function with zero arguments,
> then why do you expect to find anything useful in the args[] array, or
> even assume that the args[] array can be accessed?
>
> Don't you get nargs = 0 in 'test' in this case?
>

[-- Attachment #2: Type: text/html, Size: 3776 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: What is the recommended way to find out the number of arguments passed to a module function?
       [not found]   ` <CACJP=3mw8tcNXWeH3=Ge5OuDBQ=Jwc9+yYxFejg5weBPXYU4bQ@mail.gmail.com>
@ 2024-01-10 20:10     ` Eli Zaretskii
  2024-01-10 20:41       ` dalanicolai
  0 siblings, 1 reply; 8+ messages in thread
From: Eli Zaretskii @ 2024-01-10 20:10 UTC (permalink / raw)
  To: dalanicolai; +Cc: emacs-devel

> From: dalanicolai <dalanicolai@gmail.com>
> Date: Wed, 10 Jan 2024 21:05:22 +0100
> 
> Ah okay, well I am completely new to C, so I am still finding out things (e.g. I have not 'actively' used
> ptrdiff yet).
> But indeed the 'nargs' just give the number of passed args (obviously).
> 
> I had checked the 'sizeof' the args array when passing an argument and without passing an
> argument,
> and it was the same. Then the docs mention that NARGS is the 'required' number of arguments,
> I had misunderstood it a little (of course the required number is variable, but somehow I assumed it
> would be 1 in this case). So sometimes I overlook the 'obvious', and because I had spent some time
> on testing
> things already, I decided to just ask here.

The args[] array gets allocated exactly to accommodate nargs
arguments, so you should never access args[] beyond the index nargs-1.

> Anyway, as usual, thanks for your quick and helpful answer!

You're welcome.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: What is the recommended way to find out the number of arguments passed to a module function?
  2024-01-10 20:10     ` Eli Zaretskii
@ 2024-01-10 20:41       ` dalanicolai
  2024-01-10 20:43         ` dalanicolai
  0 siblings, 1 reply; 8+ messages in thread
From: dalanicolai @ 2024-01-10 20:41 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 1265 bytes --]

Thanks Eli.

Also, I realize now, that sizeof works differently on an array and a
pointer (to an array) (AFAIU).
I guess I will slowly or quickly get used to these (initial) pitfalls.


On Wed, 10 Jan 2024 at 21:10, Eli Zaretskii <eliz@gnu.org> wrote:

> > From: dalanicolai <dalanicolai@gmail.com>
> > Date: Wed, 10 Jan 2024 21:05:22 +0100
> >
> > Ah okay, well I am completely new to C, so I am still finding out things
> (e.g. I have not 'actively' used
> > ptrdiff yet).
> > But indeed the 'nargs' just give the number of passed args (obviously).
> >
> > I had checked the 'sizeof' the args array when passing an argument and
> without passing an
> > argument,
> > and it was the same. Then the docs mention that NARGS is the 'required'
> number of arguments,
> > I had misunderstood it a little (of course the required number is
> variable, but somehow I assumed it
> > would be 1 in this case). So sometimes I overlook the 'obvious', and
> because I had spent some time
> > on testing
> > things already, I decided to just ask here.
>
> The args[] array gets allocated exactly to accommodate nargs
> arguments, so you should never access args[] beyond the index nargs-1.
>
> > Anyway, as usual, thanks for your quick and helpful answer!
>
> You're welcome.
>

[-- Attachment #2: Type: text/html, Size: 1810 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: What is the recommended way to find out the number of arguments passed to a module function?
  2024-01-10 20:41       ` dalanicolai
@ 2024-01-10 20:43         ` dalanicolai
  2024-01-11  6:02           ` Eli Zaretskii
  0 siblings, 1 reply; 8+ messages in thread
From: dalanicolai @ 2024-01-10 20:43 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 1469 bytes --]

Well, english is not my first language. Of course, I hope not to get used
to them :))

On Wed, 10 Jan 2024 at 21:41, dalanicolai <dalanicolai@gmail.com> wrote:

> Thanks Eli.
>
> Also, I realize now, that sizeof works differently on an array and a
> pointer (to an array) (AFAIU).
> I guess I will slowly or quickly get used to these (initial) pitfalls.
>
>
> On Wed, 10 Jan 2024 at 21:10, Eli Zaretskii <eliz@gnu.org> wrote:
>
>> > From: dalanicolai <dalanicolai@gmail.com>
>> > Date: Wed, 10 Jan 2024 21:05:22 +0100
>> >
>> > Ah okay, well I am completely new to C, so I am still finding out
>> things (e.g. I have not 'actively' used
>> > ptrdiff yet).
>> > But indeed the 'nargs' just give the number of passed args (obviously).
>> >
>> > I had checked the 'sizeof' the args array when passing an argument and
>> without passing an
>> > argument,
>> > and it was the same. Then the docs mention that NARGS is the 'required'
>> number of arguments,
>> > I had misunderstood it a little (of course the required number is
>> variable, but somehow I assumed it
>> > would be 1 in this case). So sometimes I overlook the 'obvious', and
>> because I had spent some time
>> > on testing
>> > things already, I decided to just ask here.
>>
>> The args[] array gets allocated exactly to accommodate nargs
>> arguments, so you should never access args[] beyond the index nargs-1.
>>
>> > Anyway, as usual, thanks for your quick and helpful answer!
>>
>> You're welcome.
>>
>

[-- Attachment #2: Type: text/html, Size: 2272 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: What is the recommended way to find out the number of arguments passed to a module function?
  2024-01-10 20:43         ` dalanicolai
@ 2024-01-11  6:02           ` Eli Zaretskii
  0 siblings, 0 replies; 8+ messages in thread
From: Eli Zaretskii @ 2024-01-11  6:02 UTC (permalink / raw)
  To: dalanicolai; +Cc: emacs-devel

> From: dalanicolai <dalanicolai@gmail.com>
> Date: Wed, 10 Jan 2024 21:43:36 +0100
> Cc: emacs-devel@gnu.org
> 
> Well, english is not my first language. Of course, I hope not to get used to them :))

There's nothing wrong with your English.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: What is the recommended way to find out the number of arguments passed to a module function?
  2024-01-10 19:01 What is the recommended way to find out the number of arguments passed to a module function? dalanicolai
  2024-01-10 19:15 ` Eli Zaretskii
@ 2024-01-12  0:52 ` Emanuel Berg
  1 sibling, 0 replies; 8+ messages in thread
From: Emanuel Berg @ 2024-01-12  0:52 UTC (permalink / raw)
  To: emacs-devel

dalanicolai wrote:

> I am trying to write a dynamic module.

Good for you, I also did that.

Here is the whole thing, it is very simple so it can be
a starting point for anyone attempting it.

It is complete with a Makefile, a single .c file and its
corresponding .h file. This compiles into a .so file.

  https://dataswamp.org/~incal/emacs-init/random-urandom/

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2024-01-12  0:52 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-10 19:01 What is the recommended way to find out the number of arguments passed to a module function? dalanicolai
2024-01-10 19:15 ` Eli Zaretskii
2024-01-10 20:06   ` dalanicolai
     [not found]   ` <CACJP=3mw8tcNXWeH3=Ge5OuDBQ=Jwc9+yYxFejg5weBPXYU4bQ@mail.gmail.com>
2024-01-10 20:10     ` Eli Zaretskii
2024-01-10 20:41       ` dalanicolai
2024-01-10 20:43         ` dalanicolai
2024-01-11  6:02           ` Eli Zaretskii
2024-01-12  0:52 ` Emanuel Berg

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.