emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

What is the recommended way to find out the number of arguments passed t


From: dalanicolai
Subject: What is the recommended way to find out the number of arguments passed to a module function?
Date: Wed, 10 Jan 2024 20:01:09 +0100

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?

reply via email to

[Prev in Thread] Current Thread [Next in Thread]