all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Eli Zaretskii <eliz@gnu.org>
To: dalanicolai <dalanicolai@gmail.com>
Cc: emacs-devel@gnu.org
Subject: Re: What is the recommended way to find out the number of arguments passed to a module function?
Date: Wed, 10 Jan 2024 21:15:36 +0200	[thread overview]
Message-ID: <83jzohm23r.fsf@gnu.org> (raw)
In-Reply-To: <CACJP=3kYZbwMiOpPtfXExw52080X=_DUJqC_8mbuesftEGGuYQ@mail.gmail.com> (message from dalanicolai on Wed, 10 Jan 2024 20:01:09 +0100)

> 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?



  reply	other threads:[~2024-01-10 19:15 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=83jzohm23r.fsf@gnu.org \
    --to=eliz@gnu.org \
    --cc=dalanicolai@gmail.com \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.