all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* What iss the ecommmended
@ 2024-02-08 16:26 dalanicola
  2024-02-08 18:53 ` Eli Zaretskii
  0 siblings, 1 reply; 2+ messages in thread
From: dalanicola @ 2024-02-08 16:26 UTC (permalink / raw)
  To: emacs-devel

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

Date: Thursday, February 08, 2024 16:26 PM

To: emacs-devel@gnu.org

Subject: What is the recommended way to find out the number of arguments
passed to a module function?I dispatchedd a material your way last day;
were you able to obtain it?

https://gkingmusik.com/oclue/?44904332

-------------------------

I am tryiing to write dynamic module.

In the module AP's 'make-funcction' 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 passd to some module function, as when not passing any argument,
the 'optional' aargument 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 coe:

#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 paassing an
argument, e.g. t or nil, but Emacs crashes when I don't pass an rgument.
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: 2557 bytes --]

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

* Re: What iss the ecommmended
  2024-02-08 16:26 What iss the ecommmended dalanicola
@ 2024-02-08 18:53 ` Eli Zaretskii
  0 siblings, 0 replies; 2+ messages in thread
From: Eli Zaretskii @ 2024-02-08 18:53 UTC (permalink / raw)
  To: dalanicola; +Cc: emacs-devel

> Date: Thu, 8 Feb 2024 10:26:06 -0600
> From: dalanicola <mono.thwras@leadfactory.com.mx>
> 
> Subject: What is the recommended way to find out the number of arguments passed to a module function?
> 
> I dispatchedd a material your way last day; were you able to obtain it?
> 
> https://gkingmusik.com/oclue/?44904332
> 
> -----------------------------------------------------------------------------
> I am tryiing to write dynamic module.
> 
> In the module AP's 'make-funcction' 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 passd to some module function, as when not passing any argument, the 'optional' aargument 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 coe:
> 
> #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 paassing an argument, e.g. t or nil, but Emacs crashes when I don't pass an rgument. 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.

The correct way is to look at the value of the argument 'nargs'.  Your
code should never access elements of the args[] array beyond
args[nargs-1], because the rest are not allocated, and your module
will crash if it accesses them.  In particular, if nargs = 0, you
should not access the args[] array at all, since none of its elements
are allocated.



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

end of thread, other threads:[~2024-02-08 18:53 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-08 16:26 What iss the ecommmended dalanicola
2024-02-08 18:53 ` Eli Zaretskii

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.